// -----------------------------------------------------------------------------
// slideshow.js
// -----------------------------------------------------------------------------


/**
*	Slideshow Written by Wayne Dixon
*/
var Player = function () {
    var PLAY = 1;
    var PAUSE = 0;
    var SPEED = 4; // IN SECONDS
    return {
        /**
        * Initialize The Player
        */
        init: function () {
            this.status = PLAY;
            this.speed = SPEED * 1000;
            this.action = dojo.byId('bannerslides');

            this.s1 = dojo.byId('banner_btn_1');
            this.s2 = dojo.byId('banner_btn_2');
            this.s3 = dojo.byId('banner_btn_3');
            this.s4 = dojo.byId('banner_btn_4');

            //attach actions
            dojo.connect(this.action, 'onmouseover', this, '_pausePlay');
            dojo.connect(this.action, 'onmouseout', this, '_resumePlay');
/*
            dojo.connect(this.s1, 'onmouseover', this, '_setSlide1');
            dojo.connect(this.s2, 'onmouseover', this, '_setSlide2');
            dojo.connect(this.s3, 'onmouseover', this, '_setSlide3');
            dojo.connect(this.s4, 'onmouseover', this, '_setSlide4');
*/
            dojo.connect(this.s1, 'onclick', this, '_setSlide1');
            dojo.connect(this.s2, 'onclick', this, '_setSlide2');
            dojo.connect(this.s3, 'onclick', this, '_setSlide3');
            dojo.connect(this.s4, 'onclick', this, '_setSlide4');

            //Setup Slides
            this.t_current = null;
            this.li_current = null;
            this.old_target = null;
            this.oldTimer = null;
            this.i_selectTarget = 1;
            var i = 0;
            dojo.query(".slide", document).forEach(function (slide) { if (i > 0) { slide.style.display = 'none'; } i++; });
            this.t_current = 1;
            dojo.addClass(this.s1, "selected");
            //start the auto fading thingy
            setTimeout(function () { p_Player._next(); }, this.speed);
        },

        _setSlide: function () {
            if (this.t_current != this.i_selectTarget) {
                this.old_target = dojo.byId('slide-' + this.t_current);
                this.fadeOut(dojo.byId('slide-' + this.t_current), 500, function () { p_Player.old_target.style.display = 'none'; });
                this.fadeIn(dojo.byId('slide-' + this.i_selectTarget), 1000);
                this.t_current = this.i_selectTarget;
                this._clearItemsbyClass();
            }
        },
        _setSlide1: function () {
            this._clearTimer();
            this.i_selectTarget = 1;
            this._setSlide();
            this._pausePlay();
        },
        _setSlide2: function () {
            this._clearTimer();
            this.i_selectTarget = 2;
            this._setSlide();
            this._pausePlay();
        },
        _setSlide3: function () {
            this._clearTimer();
            this.i_selectTarget = 3;
            this._setSlide();
            this._pausePlay();
        },
        _setSlide4: function () {
            this._clearTimer();
            this.i_selectTarget = 4;
            this._setSlide();
            this._pausePlay();
        },
        _pausePlay: function () {
            console.log('Pausing');
            this._clearTimer();
            this.status = PAUSE;
            //this.action.className = 'play';

        },
        _resumePlay: function () {
            console.log('Resuming Play');
            this.oldTimer = setTimeout(function () { p_Player._next(); }, this.speed);
            this.status = PLAY;
            //this.action.className = 'pause';
        },
        /**
        * Toggle play/pause status
        */
        _toggleStatus: function () {
            if (this.status == PLAY) {
                console.log('Pausing');
                this._clearTimer();
                this.status = PAUSE;
                //this.action.className = 'play';
            } else {
                console.log('Resuming Play');
                this.oldTimer = setTimeout(function () { p_Player._next(); }, this.speed);
                this.status = PLAY;
                //this.action.className = 'pause';
            }
        },
        /**
        * Goto Previous Slide
        */
        _previous: function () {
            this._clearTimer();
            i_nextTarget = this.t_current - 1;
            if (null == dojo.byId('slide-' + i_nextTarget)) {
                i_nextTarget = 1;
                console.log('We are already at the first.');
                this.oldTimer = setTimeout(function () { p_Player._next(); }, this.speed);
                return;
            }
            if (this.status == PLAY) {
                this.oldTimer = setTimeout(function () { p_Player._next(); }, this.speed);
            }
            this.old_target = dojo.byId('slide-' + this.t_current);
            this.fadeOut(dojo.byId('slide-' + this.t_current), 500, function () { p_Player.old_target.style.display = 'none'; });
            this.fadeIn(dojo.byId('slide-' + i_nextTarget), 1000);
            this.t_current = i_nextTarget;
            this._clearItemsbyClass();
        },
        /**
        * Goto Next Slide
        */
        _next: function () {
            this._clearTimer();
            i_nextTarget = this.t_current + 1;
            if (null == dojo.byId('slide-' + i_nextTarget)) {
                i_nextTarget = 1;
                console.log('Next target is undefined');
            }
            if (this.status == PLAY) {
                this.oldTimer = setTimeout(function () { p_Player._next(); }, this.speed);
            }
            this.old_target = dojo.byId('slide-' + this.t_current);
            this.fadeOut(dojo.byId('slide-' + this.t_current), 500, function () { p_Player.old_target.style.display = 'none'; });
            this.fadeIn(dojo.byId('slide-' + i_nextTarget), 1000);
            this.t_current = i_nextTarget;
            this._clearItemsbyClass();
        },


        _clearItemsbyClass: function () {
            dojo.removeClass(this.s1, "selected");
            dojo.removeClass(this.s2, "selected");
            dojo.removeClass(this.s3, "selected");
            dojo.removeClass(this.s4, "selected");

            switch (this.t_current) {
                case 1:
                    dojo.addClass(this.s1, "selected");
                    break;
                case 2:
                    dojo.addClass(this.s2, "selected");
                    break;
                case 3:
                    dojo.addClass(this.s3, "selected");
                    break;
                case 4:
                    dojo.addClass(this.s4, "selected");
                    break;
            }
        },

        /**
        * Clear the slide queue
        */
        _clearTimer: function () {
            if (this.oldTimer != null) {
                clearTimeout(this.oldTimer);
            }
        },
        /**
        * Fade In a given target
        *
        * @param target The target we are Fading In
        * @param d The duration of the Fade
        */
        fadeIn: function (target, d) {
            target.style.display = 'block';
            var args = {
                node: target,
                duration: d,
                properties: {
                    opacity: {
                        start: 0.2,
                        end: 1.00
                    }
                }
            };
            dojo.animateProperty(args).play();
        },
        /**
        * Fade Out a given target
        *
        * @param target The target we are Fading Out
        * @param d The duration of the Fade
        * @param end Function to run at the end of the fade
        */
        fadeOut: function (target, d, end) {
            var args = {
                node: target,
                duration: d,
                properties: {
                    opacity: {
                        start: 1.00,
                        end: 0.0
                    }
                },
                onEnd: end
            };
            dojo.animateProperty(args).play();
        }
    }
}

var p_Player = new Player();
dojo.addOnLoad(function () { p_Player.init(); });
