YAHOO.namespace("SW.local.SlideShowPlayer");

(function() {
    var yuiDom = YAHOO.util.Dom;
    var yuiEvent = YAHOO.util.Event;
    var yuiLang = YAHOO.lang;

    SW.local.SlideShowPlayer = {
        rotateCollection : null,
        currentItem : null,
        currentItemIndex : 0,
        autoPlayInterval : null,
        isPaused : false,
        config:{
            items:null, /*must be provided by invoker*/
            showDuration:.800,
            hideDuration:.500,
            rotateInterval: 7000,
            autoPlay: true
        },

        init: function(userConfig) {
            this.config = yuiLang.merge(this.config, userConfig);

            this.rotateCollection = this.config.items;

            if (this.rotateCollection.length > 1) {
                yuiEvent.addListener(yuiDom.get("back"), "click", SW.local.SlideShowPlayer.goToPrevItem, this,true);
                yuiEvent.addListener(yuiDom.get("play"), "click", SW.local.SlideShowPlayer.resumeRotation, this,true);
                yuiEvent.addListener(yuiDom.get("pause"), "click", SW.local.SlideShowPlayer.pauseRotation, this,true);
                yuiEvent.addListener(yuiDom.get("next"), "click", SW.local.SlideShowPlayer.goToNextItem, this,true);
            }

            this.rotateCollection.forEach(function(item) {
                var slideShowPlayer = this;
                item.toggler = SW.widget.displayToggler({
                    element: item,
                    showOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "2");
                    },
                    hideOnComplete: function(config) {
                        yuiDom.setStyle(config.element, "zIndex", "1");
                    },
                    showDuration: slideShowPlayer.config.showDuration,
                    hideDuration: slideShowPlayer.config.hideDuration
                });
            }, this);
            SW.local.SlideShowPlayer.startRotation();
        },
        startRotation: function() {
            SW.local.SlideShowPlayer.toggleItem(this.rotateCollection[this.currentItemIndex]);
            if (this.rotateCollection.length == 1 || !this.config.autoPlay) this.autoPlayInterval = null;
            else this.autoPlayInterval = setInterval(function(){SW.local.SlideShowPlayer.autoPlay()}, this.config.rotateInterval);
        },
        resumeRotation: function() {
            yuiDom.get("pause").style.display = "block";
            yuiDom.get("play").style.display = "none";
            if(this.config.autoPlay){
                this.autoPlayInterval = setInterval(function(){SW.local.SlideShowPlayer.autoPlay()}, this.config.rotateInterval);
            }
        },
        pauseRotation: function() {
            yuiDom.get("pause").style.display = "none";
            yuiDom.get("play").style.display = "block";
            clearInterval(this.autoPlayInterval);
            this.autoPlayInterval = null;
			this.isPaused = true;
        },
        autoPlay: function() {
            this.currentItemIndex = (this.currentItemIndex+1)%this.rotateCollection.length;
            SW.local.SlideShowPlayer.toggleItem(this.rotateCollection[this.currentItemIndex]);
			this.isPaused = false;
        },
        toggleItem: function(promo) {
            if(this.currentItem) {
                this.currentItem.toggler.hide();                
            }
            this.currentItem = promo;
            if(this.currentItem) {
                this.currentItem.toggler.show();
            }
        },
        goToPrevItem: function(e) {
            clearInterval(this.autoPlayInterval);
            this.autoPlayInterval = null;
            if (this.currentItemIndex == 0) this.currentItemIndex = this.rotateCollection.length - 1;
            else this.currentItemIndex = (this.currentItemIndex-1)%this.rotateCollection.length;
            SW.local.SlideShowPlayer.toggleItem(this.rotateCollection[this.currentItemIndex]);
            if(!this.isPaused && this.config.autoPlay)
				this.autoPlayInterval = setInterval(function(){SW.local.SlideShowPlayer.autoPlay()}, this.config.rotateInterval);

            // omniture tracking
            if (typeof s != 'undefined') {
                s.linkTrackVars = 'events';
                s.linkTrackEvents='event45';
                s.events = 'event45';
                s.tl(this, 'o', 'Previous Featured Item');
            }
        },
        goToNextItem: function(e) {
            clearInterval(this.autoPlayInterval);
            this.autoPlayInterval = null;
            this.currentItemIndex = (this.currentItemIndex+1)%this.rotateCollection.length;
            SW.local.SlideShowPlayer.toggleItem(this.rotateCollection[this.currentItemIndex]);
			if(!this.isPaused && this.config.autoPlay)
				this.autoPlayInterval = setInterval(function(){SW.local.SlideShowPlayer.autoPlay()}, this.config.rotateInterval);
            // omniture tracking
            if (typeof s != 'undefined') {
                s.linkTrackVars = 'events';
                s.linkTrackEvents='event44';
                s.events = 'event44';
                s.tl(this, 'o', 'Next Featured Item');
            }
        }
    };
})();
