YAHOO.namespace("SW.local.PromoArea");

(function() {

    var yuiDom = YAHOO.util.Dom;
    var yuiEvent = YAHOO.util.Event;

    var promos = null;
    var sifrNxtTxt = null;
    var sifrPrevTxt = null;
    var currentPromo = null;
    var currentPromoIndex = 0;
    var autoPlayInterval = null;
	var isPaused = false;
	var nextTxtWidth = 105;
	var prevTxtWidth = 145;
	var showPrevConf = {width: { to: prevTxtWidth }};
	var showNxtConf = {width: { to: nextTxtWidth }};
	var hideLabelConf = {width: { to: 0 }};
    SW.local.PromoArea = {
        init: function() {
            promos = yuiDom.getElementsByClassName("promo", "div", "promoPlacement"); 
               if (promos.length > 1) {
                document.getElementById("promoNavigation").style.visibility = "visible";
                yuiEvent.addListener(yuiDom.get("back"), "click", SW.local.PromoArea.goToPrevPromo);
                yuiEvent.addListener(yuiDom.get("back"), "mouseover", SW.local.PromoArea.showPreviousLable);
                yuiEvent.addListener(yuiDom.get("back"), "mouseout", SW.local.PromoArea.hidePreviousLable);
                
                yuiEvent.addListener(yuiDom.get("play"), "click", SW.local.PromoArea.resumeRotation);
                yuiEvent.addListener(yuiDom.get("pause"), "click", SW.local.PromoArea.pauseRotation);
                
                yuiEvent.addListener(yuiDom.get("next"), "click", SW.local.PromoArea.goToNextPromo);
                yuiEvent.addListener(yuiDom.get("next"), "mouseover", SW.local.PromoArea.showNextLable);
                yuiEvent.addListener(yuiDom.get("next"), "mouseout", SW.local.PromoArea.hideNextLable);
                
                nextTxtWidth = yuiDom.get("nextHiddenTxt") != null ? yuiDom.get("nextHiddenTxt").clientWidth + 8 : nextTxtWidth;
                prevTxtWidth = yuiDom.get("prevHiddenTxt") != null ? yuiDom.get("prevHiddenTxt").clientWidth + 8 : prevTxtWidth;                
                 
                showPrevConf = {width: { to: prevTxtWidth }};
            	showNxtConf = {width: { to: nextTxtWidth }};
            }
            
            promos.forEach(function(promo) {
                promo.toggler = SW.widget.displayToggler({
                    element: promo,
                    showOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "2");
						/*yuiDom.setStyle(config.element, "display", "block");*/
                    },
                    hideOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "1");
						/*yuiDom.setStyle(config.element, "display", "none");*/
                    },
                    showDuration:.800,
                    hideDuration:.500
                });
            });
            SW.local.PromoArea.startRotation();
        },
        startRotation: function() {
            SW.local.PromoArea.togglePromo(promos[currentPromoIndex]);
            if (promos.length == 1) autoPlayInterval = null;
            else autoPlayInterval = setInterval(SW.local.PromoArea.autoPlay, 7000);
        },
        resumeRotation: function() {
            yuiDom.get("pause").style.display = "block";
            yuiDom.get("play").style.display = "none";
            autoPlayInterval = setInterval(SW.local.PromoArea.autoPlay, 7000);
        },
        pauseRotation: function() {
            yuiDom.get("pause").style.display = "none";
            yuiDom.get("play").style.display = "block";
            clearInterval(autoPlayInterval);
            autoPlayInterval = null;
			isPaused = true;
        },
        autoPlay: function() {
            currentPromoIndex = (currentPromoIndex+1)%promos.length;
            SW.local.PromoArea.togglePromo(promos[currentPromoIndex]);
			isPaused = false;
        },
        togglePromo: function(promo) {
            if(currentPromo !== null) {
                currentPromo.toggler.hide();
            }
            currentPromo = promo;
            currentPromo.toggler.show();
        },
        goToPrevPromo: function(e) {
            clearInterval(autoPlayInterval);
            autoPlayInterval = null;
            if (currentPromoIndex == 0) currentPromoIndex = promos.length - 1;
            else currentPromoIndex = (currentPromoIndex-1)%promos.length;
            SW.local.PromoArea.togglePromo(promos[currentPromoIndex]);
            if(!isPaused)
				autoPlayInterval = setInterval(SW.local.PromoArea.autoPlay, 7000);
        },
        goToNextPromo: function(e) {
            clearInterval(autoPlayInterval);
            autoPlayInterval = null;
            currentPromoIndex = (currentPromoIndex+1)%promos.length;
            SW.local.PromoArea.togglePromo(promos[currentPromoIndex]);
			if(!isPaused)
				autoPlayInterval = setInterval(SW.local.PromoArea.autoPlay, 7000);
        },
        showPreviousLable: function(e) {
            sifrPrevTxt = yuiDom.getElementsByClassName("sIFR-flash");
            if(sifrPrevTxt != null) {
	            for(var prev=0; prev< sifrPrevTxt.length; prev++) {
		            if(sifrPrevTxt[prev].parentNode.parentNode.id == "prev_txt") {
		            	sifrPrevTxt[prev].style.width = prevTxtWidth+"px";
		            }
	            }
            }
        	var showPrev = new yuiAnim("prev_txt", showPrevConf, 0.6, YAHOO.util.Easing.easeBoth);
            showPrev.animate();
        },
        hidePreviousLable: function(e) {
        	var hidePrev = new yuiAnim("prev_txt", hideLabelConf, 0.6, YAHOO.util.Easing.easeBoth);
        	hidePrev.animate();
        },
        showNextLable: function(e) {
        	sifrNxtTxt = yuiDom.getElementsByClassName("sIFR-flash");
            if(sifrNxtTxt != null) {
	            for(var nxt=0; nxt< sifrNxtTxt.length; nxt++) {
		            if(sifrNxtTxt[nxt].parentNode.parentNode.id == "next_txt") { 
		            	sifrNxtTxt[nxt].style.width = nextTxtWidth+"px";
		            }
	            }
            }
            var showNxt = new YAHOO.util.Anim("next_txt", showNxtConf, 0.6, YAHOO.util.Easing.easeBoth);
        	showNxt.animate();
        },
        hideNextLable: function(e) {
        	var hideNxt = new YAHOO.util.Anim("next_txt", hideLabelConf, 0.6, YAHOO.util.Easing.easeBoth);
        	hideNxt.animate();
        }
    };

    yuiEvent.onDOMReady(function() {
        SW.local.PromoArea.init();
    });

})();

