salmon.namespace.addNamespace("promotions");
promotions = {
	init:function(){
		var themedPromos = $("div.themedPromotion");
		var themedCarousels = $("div.themedPromotionCarousel");
		for(var i = themedPromos.length-1; i>=0;i--) {
			promotions.themedPromo = new promotions.ThemedPromo(themedPromos[i]);
		}
		for(var i = themedCarousels.length-1; i>=0;i--) {
			promotions.themedCarousel = new promotions.ThemedCarousel(themedCarousels[i]);
		}
	},
	ThemedCarousel:function(ThemedCarousel){
		var isAnimating = false;
		var currentItem = 1;
		var maxItems = 8;
		var animateTimer = 3000;
		var highestZindex = 0;
		
		$(ThemedCarousel).find(".back a").click(goBack);
		$(ThemedCarousel).find(".forward a").click(goForward);
		var count = 1;
		$(ThemedCarousel).find(".themedPromotionHolder ul li").each(function(){
			if(count<=maxItems){
				$(this).css({
					 position:"absolute",
					 top:0,
					 left:0,
					 display:((count==1)?"block":"none"),
					 zIndex:count++
				});
			} else {
				$(this).remove();
			}
		});
		
		$(document).pngFix();
		
		function goBack(){
			if(!isAnimating){
				currentItem = (currentItem==1) ? maxItems : currentItem-1;
				fadeNewItem();
			}
			return false;
		}
		function goForward(){
			if(!isAnimating){
				currentItem = (currentItem==maxItems) ? 1 : currentItem+1;
				fadeNewItem();
			}
			return false;
		}
		function fadeNewItem(){
			if(!isAnimating){
				isAnimating = true;
				$(ThemedCarousel).find(".themedPromotionHolder ul li").each(function(){
					highestZindex = ($(this).css("zIndex")>highestZindex) ? $(this).css("zIndex") : highestZindex;
				});
				var count = 1;
				$(ThemedCarousel).find(".themedPromotionHolder ul li").each(function(){
					if(count++ == currentItem){
						$(this).css({
							zIndex:++highestZindex
						}).fadeIn(animateTimer,function(){
							$(ThemedCarousel).find(".themedPromotionHolder ul li").each(function(){
								if($(this).css("zIndex") != highestZindex){
									$(this).css({display:"none"});
								}
							});						
							isAnimating = false;
						});
					}
				});
			}
			$(ThemedCarousel).find("a").blur();
		}
	},
	ThemedPromo:function(ThemedPromo){
		var isPlaying = false;
		var isAnimating = false;
		var isDirect = false;
		var currentItem = 1;
		var nextItem = 2;
		var maxItems = 8;
		var pauseTimer = 3000;
		var animateTimer = 3000;
		var highestZindex = 0;
		var inicatorPositions =["0","0px","0px","0px"];
		var init = (function(){
			var promotionHolder = ThemedPromo;
			var promotionHolders = $(ThemedPromo).find(".themedPromotionHolder ul li");
			if((promotionHolder == null) || (promotionHolders.length <= 1)){
				return;
			}
			var count = 1;
			$(promotionHolders).each(function(){
				if(count<=maxItems){
					$(ThemedPromo).find(".themedPromotionControls ul").append("<li><a href=\"#"+count+"\""
						+((count==1)?" class=\"current\"":"")
						+">"+count+"</a></li>");
					$(this).css({
						 position:"absolute",
						 top:0,
						 left:0,
						 display:((count==1)?"block":"none"),
						 zIndex:count++
					});
				} else {
					$(this).remove();
				}
			});
			$(ThemedPromo).find(".themedPromotionControls").append("<div class=\"pausePlay Pause\"><strong>Pause</strong><span>&#160;</span></div>");
			$(ThemedPromo).find(".pausePlay").click(pausePlay);
			if(jQuery.browser.opera){
				$(ThemedPromo).find(".pausePlay").css({top:"-18px"});
			}
			$(ThemedPromo).find(".themedPromotionControls li a").click(tp_gothere);
			isPlaying = true;
			wait();
		})();
		function pausePlay(){
			var whatAction = $(this).find("strong").text();
			if(whatAction == "Pause"){
				$(this).removeClass("Pause").addClass("Play");
				$(this).find("strong").text("Play");
				pause();
			} else {
				$(this).removeClass("Play").addClass("Pause");
				$(this).find("strong").text("Pause");
				play();
			}
			$(this).blur();
		}
		function play(){
			isPlaying = true;
			animate();
		}
		function pause(){
			isPlaying = false;
		}
		function tp_gothere(){
			var goWhere = $(this).text();
			$(ThemedPromo).find(".pausePlay").removeClass("Pause").addClass("Play");
			$(ThemedPromo).find(".pausePlay").find("strong").text("Play");
			isPlaying = false;
			window.clearTimeout();
			$(ThemedPromo).find(".themedPromotionHolder ul li").stop();
			$(ThemedPromo).find(".themedPromotionHolder ul li").css({opacity:""});
			$(ThemedPromo).find(".indicator").stop();
			nextItem = $(this).text();
			isDirect = true;
			animate();
			$(this).blur();
			return false;
		}
		function animate(){
			if(!isDirect ){
				if(isAnimating || !isPlaying){
					return;
				}
			}
			isDirect = false;
			isAnimating = true;
			$(ThemedPromo).find(".themedPromotionControls ul li a").removeClass("current");
			$(ThemedPromo).find(".themedPromotionHolder ul li").each(function(){
				highestZindex = ($(this).css("zIndex")>highestZindex) ? $(this).css("zIndex") : highestZindex;
			});
			var count = 1;
			$(ThemedPromo).find(".themedPromotionHolder ul li").each(function(){
				if(count++ == nextItem){
					$(this).css({
						zIndex:++highestZindex
					}).fadeIn(animateTimer,function(){
						$(ThemedPromo).find(".themedPromotionHolder ul li").each(function(){
							if($(this).css("zIndex") != highestZindex){
								$(this).css({display:"none"});
							}
						});						
						$(ThemedPromo).find(".themedPromotionControls ul li a").each(function(){
							if($(this).text() == nextItem){
								$(this).addClass("current");
							}
						});
						nextItem = ((nextItem+1)>maxItems)?1:++nextItem;
						isAnimating = false;
						if(isPlaying){
							wait();
						}
					});
					$(ThemedPromo).find(".indicator").animate({left:inicatorPositions[nextItem]},animateTimer);
				}
			});
		}
		function wait(){
			window.setTimeout(animate, pauseTimer);
		}
	}
};
$(document).ready(function(){
	promotions.init();
});
