// this will be taken care of in the namespace method in BODOG.js
if (!BODOG) { var BODOG = {}; }
if (!BODOG.components) { BODOG.components = {}; }


	
/**
* Random Display of items
*/
BODOG.components.toggleFact = {
	
	/** Settings can be overwritten by passing in an options object to this.init(), example at bottom */
	settings: {
		
        randomLinkID: "#next", //ID for whatever you want the click event to be applied to
        factsID: "#did-you-know p", //Queries for whatever items you want to randomly cycle through
        randomOrder: true  //switch to false for ordered toggling
	},

    init: function(options) {
		$.extend(this.settings, options || {});

		this.showTip();
        
        // add the click event to the select ID
		this.addLinkEvent();
	},
    
    tipCount: function(){ return $(this.settings.factsID).length; },
    currentFact : 0,
    lastFact : null,
    
    showTip: function() {

        if ( this.settings.randomOrder ){

            this.randomDisplay();
        
        } else {

            this.orderedDisplay();
        
        }
 
    },
    
    randomDisplay: function() {
       
        this.currentFact = Math.floor( Math.random() * this.tipCount() );
        var nexttip = eval('"'+this.settings.factsID+':eq(' + this.currentFact + ')"');

        if ( (this.lastFact != this.currentFact) ){
        
            $(this.settings.factsID).hide();  
                          
            $(nexttip).show();
            
        }else{
            if ( this.tipCount() > 1 ){
                this.showTip();
            }
        }
        
        this.lastFact = this.currentFact;
        
    },
    
    orderedDisplay: function() {
    
        var nexttip = eval('"'+this.settings.factsID+':eq(' + this.currentFact + ')"');

        $(this.settings.factsID).hide();  

        $(nexttip).show();
        
        //incrementing it first to offset index of this.currentFact which starts at 0
        this.currentFact++;
        
        if ( this.tipCount() == this.currentFact ){
            this.currentFact = 0;
        } 
               
        
    },
    
    addLinkEvent: function() {
    
		var that = this;

		$(this.settings.randomLinkID).click(function(){
            that.showTip();
			return false;
		});
		
	}
};
        



/** Initialize the component when the DOM is ready */
$(function() {
	BODOG.components.toggleFact.init();
});

/*
Example of extending settings
*/
/*
    BODOG.components.toggleFact.init(
        $.extend.settings = { 
		    randomLinkID: "#next", //ID for whatever you want the click event to be applied to
            factsID: "#did-you-know p", //Queries for whatever items you want to randomly cycle through
            randomOrder: true  //switch to false for ordered toggling
        }
        
    );
*/