// Creating Global Namespace for Athleticum
// requires jQuery 1.3.2.

// Creating namespaces
var athleticum = {}

/*$.browser.msie6 = $.browser.msie && ($.browser.version && ($.browser.version < 7) || /6.0/.test(navigator.userAgent)); 
$.browser.msie7 = $.browser.msie && ($.browser.version && ($.browser.version < 8 && $.browser.version > 6) || /7.0/.test(navigator.userAgent)); */

// Starting point
$(document).ready( function() {
	athleticum.promo.init();
	athleticum.listHelper();
});

athleticum.listHelper = function() {
	$('#quickaccess ul li:last-child, #quickaccess ul li:eq(3), #products ul li:last-child, #products ul li:eq(3)').addClass('last');
}

// Promotion Slider
athleticum.promo = {
	props: {
		intervalTime: 8000,
		promoInt: 0,
		nbrArray: ['zero','one','two','three','four','five','six','seven','eight','nine']
	},
	init: function() {
		// Price Text-Backgorund replace
		$('#promotion div.price strong').each(function() {
			var price = $(this).text();
			var priceHtml = '';
			for(var i = 0;i<price.length;i++) {
				var nbr = price.charAt(i);
				var nChar;
				if(nbr=='.') {
					nChar = 'dot';
				} else if(nbr=='-') {
					nChar = 'dash';
				} else {
					nChar = athleticum.promo.props.nbrArray[parseInt(nbr)]
				}
				priceHtml += '<span class="number ' + nChar + '">' + nbr + '</span>';
			}
			$(this).html(priceHtml);
		});
		
		// Link auf Li setzen
		$('ul#ratationList li').each(function() {
			if($(this).find('a.promo-button').attr('href') != ''){
				$(this).hover(
					function(){$(this).addClass('active');},
					function(){$(this).removeClass('active');}
				);
				$(this).click(function() {
					window.location =  $(this).find('a.promo-button').attr('href');
				});
			}
		});
		
		// Create PromoNavi
		var promoNavHtml = '<ul id="promoNav">';
		$('div#promotion li.slide').each(function(i) {
			promoNavHtml += '<li><a href="#" idx="'+i+'">'+(i+1)+'</a></li>';
		});
		promoNavHtml += '</ul>';
		$('div#quickaccess').append(promoNavHtml);
		
		$('ul#promoNav a:eq(0)').addClass('active');
		
		$('ul#promoNav a').each(function(i) {
			$(this).click(function() {
				athleticum.promo.slide($(this).attr('idx'));
				return false;
			});
		});
		
		// Create PromoSwitch
		var promoSwitchHtml = '<ul id="promoSwitch">';
		promoSwitchHtml += '<li class="prev"><a href="#"><strong>&laquo;</strong></a></li>';
		promoSwitchHtml += '<li class="next"><a href="#"><strong>&raquo;</strong></a></li>';
		promoSwitchHtml += '</ul>';
		$('div#promotion').append(promoSwitchHtml);
		
		$('ul#promoSwitch a:eq(0)').click(function() {
			athleticum.promo.slide(-1);
			return false;
		});
		$('ul#promoSwitch a:eq(1)').click(function() {
			athleticum.promo.slide();
			return false;
		});
		//$("ul#promoSwitch li").fadeOut(150);
		
		$('div#promotion').hover(
			/*function(){ $("ul#promoSwitch li").fadeIn(150)},
			function(){ $("ul#promoSwitch li").fadeOut(150)}*/
			function(){
				$("ul#promoSwitch li a").addClass('active');
				/*window.clearInterval(athleticum.promo.promoInterval);*/
			},
			function(){
				$("ul#promoSwitch li a").removeClass('active');
				/*athleticum.promo.promoInterval = window.setInterval('athleticum.promo.slide()', athleticum.promo.props.intervalTime);*/
			}
		);
		
		
		// Start Interval for Slider 
		athleticum.promo.promoInterval = window.setInterval('athleticum.promo.slide()', athleticum.promo.props.intervalTime);
		
	},
	slide: function(dir) {
		window.clearInterval(athleticum.promo.promoInterval);
		if(dir == -1) {
			athleticum.promo.props.promoInt = athleticum.promo.props.promoInt==0?($('div#promotion li.slide').size()-1):athleticum.promo.props.promoInt-1;
		} else if(dir >= -1) {
			athleticum.promo.props.promoInt = parseInt(dir);
		}else {
			athleticum.promo.props.promoInt = athleticum.promo.props.promoInt==($('div#promotion li.slide').size()-1)?0:athleticum.promo.props.promoInt+1;
		}
		var newLeft = (athleticum.promo.props.promoInt*941)*-1 + 'px'
		$('div#promotion ul#ratationList').animate({left: newLeft}, 500);
		
		$('ul#promoNav a').each(function(i) {
			$(this).removeClass('active');
		});
		$('ul#promoNav a:eq('+athleticum.promo.props.promoInt+')').addClass('active');
		athleticum.promo.promoInterval = window.setInterval('athleticum.promo.slide()', athleticum.promo.props.intervalTime);
	}
}

