/*
    This class provides all the functionality of a content slider or rotator. You can write your
    own controller or use AfkSliderControl.js to provide buttons for the user to press to
    switch between pieces of content.

    Note: the pieces of content can be ANY html element (a picture, a div with a picture, ...).

    You can change the animation by overriding the "this._animate" function. Right now,
    this._animate = this._animateFade
*/


/*
    //Naming conventions
    this.someVarJ = jQuery object
    this._something = a private variable/function

*/

function AfkSlider(contentDivID)
{
    this.uniqueWindowID = window.afkWindowObjectMap.addObject(this);
    this.objectEvalString = "window.afkWindowObjectMap.getObjectByID(" + this.uniqueWindowID + ")";

    this.contentDivID = contentDivID;
    this.callBackObjects = [];

    this.contentDivJ = null;
    this.contentItemsJ = null;
    this.curItemN = -1;
    this.contentCount = 0;
    this.timerID = null;
  
    this.zIndexContentBottom = 4; //these are swapped between content items during fade from one to the other.
    this.zIndexContentMiddle = 5;
    this.zIndexContentTop = 6;
  
    this.contentTimeDefault = 3000;
    this.contentTimes = [];
    this.autoStart = true;
    this.stopped = false;

    //-----------------------------------------------------------------------------------------------------------------
    //---------------------- does the setup... only call this once!   ------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    this.initialize = function()
    {
        this.validate();     //TODO more
        this.contentDivJ = $(contentDivID).eq(0);
        this.contentItemsJ = this.contentDivJ.find(".slider_item");
        this.contentCount = this.contentItemsJ.size();
  
        //add reference to this object to each content item so event handlers can access this object
        for(i =0; i<this.contentCount; i++ ){
            this.contentItemsJ[i].sliderLink = this;
        }

        if(this.contentTimes.length == 0){
            for(i=0; i<this.contentCount; i++){
                this.contentTimes[i] = this.contentTimeDefault;
            }
        }
        this._animationInit();
    };


    //-----------------------------------------------------------------------------------------------------------------
    //---------------------- simply validates setup and prints nice error messages ------------------------------------
    //-----------------------------------------------------------------------------------------------------------------
    this.validate = function(){
        if($(this.contentDivID).size() != 1)
            throw "contentDivID[" + this.contentDivID + "] matches " + $(this.contentDivID).size() + " elements. contentDivID must match only a single object. Example: '#slider_div'.";
    };
  
    //-----------------------------------------------------------------------------------------
    // call this to wait for all the content items to be loaded before showing slider
    //-----------------------------------------------------------------------------------------
    this.showNow = function(){
        this.appear();
        if(this.autoStart == true){
            this.restartTimer();
        }
    }


    //-----------------------------------------------------------------------------------------
    // starts the timer after waiting for one timeout to show the slider
    this.startNow = function(){
        var evalString = this.objectEvalString+".appear();";
        evalString +=    this.objectEvalString+".restartTimer();";
        setTimeout(evalString, this.contentTimeDefault);
    }

    //------------------------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------------------------
    this.appear = function(){
        if(this.curItemN == -1){
            this.selectItem(0);
        }
        this._animateAppearance();
    }
    

    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this.selectNext = function(){
        var nextItemN = (this.curItemN + 1) % this.contentCount;
        this.selectItem(nextItemN);
    };



    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this.getContentCount = function(){
        return this.contentCount;
    };
  
    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this.selectItem = function(nextItemN){
        if(nextItemN != this.curItemN){ //don't fade in / out to show current N
            this._animate(this.curItemN, nextItemN);
            this.curItemN = nextItemN;

            //notify callbacks that it has changed
            for (i=0; i<this.callBackObjects.length; i++) {
                this.callBackObjects[i].itemChanged(nextItemN);
            }

        }
        if(this.stopped == false)
            this.restartTimer();
    };
    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this.getCurrentN = function(){
        return this.curItemN;
    };



    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this.stopTimer = function(){
        this._clearPendingTimeout();
        this.stopped = true;
    };
    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this.restartTimer = function(){
        this._clearPendingTimeout();
        this.stopped = false;

        //setup timer interval to call this.selectNext() after X seconds
        this.timerID = setTimeout(this.objectEvalString+".selectNext()", this.contentTimes[this.curItemN]); 
        //this.timerID = window.setTimeout($(this).selectNext, this.contentTimes[this.curItemN], null);

    };

    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this._clearPendingTimeout = function(){
        if(this.timerID != null){
            window.clearTimeout(this.timerID);
            this.timerID = null;
        }
    }


    this.addCallBackObject = function(Object){
        this.callBackObjects.push(Object);
    }





    this._animationInit = function(){
        this.contentItemsJ.hide();
        this.contentItemsJ.css('zIndex', this.zIndexContentBottom);
    };
    this._animateAppearance = function(){
        this.contentItemsJ.eq(this.curItemN).fadeIn(); //show the first slider item
    };

  
    //-------------------------------------------------------------
    //
    //-------------------------------------------------------------
    this._animateFade = function(curN, nextN){
        var curItemJ = this.contentItemsJ.eq(curN);
        var nextItemJ = this.contentItemsJ.eq(nextN);
    
        nextItemJ.hide();
        this.contentItemsJ.not(curItemJ).css('zIndex', this.zIndexContentBottom);
        this.contentItemsJ.not(curItemJ).css('opacity', 1);
        nextItemJ.css('zIndex', this.zIndexContentTop);
        curItemJ.css('zIndex', this.zIndexContentMiddle);

        //needed in case user is switching between items quickly
        nextItemJ.stop(true);
        curItemJ.stop(true);
        nextItemJ.fadeIn("slow");
    }

    //this is where you can set the animation function here
    this._animate = this._animateFade;
  


}



