/**
 * Insight
 *
 * @namespace Yahoo.application
 * @class Test
 * @constructor
 */

YAHOO.application.Insight = function()
{    
    launchCarousel = function()
    {	
        if ( ! $D.get( 'author_rotation' )  ) return;
	    //Set Globals
	    currentAuthorDisplayed = 0;
	    authorListItems = $S.query('div#author_rotation ol li');
	    maxNumberOfAuthors = authorListItems.length - 1; 
	    
	    var i = 1;	    
	    $D.batch(authorListItems, function(el) {
	        //Hide all other li excpet first
	        if (i != 1) {
	            el.style.display = 'none';
	        }
	        i++;
	    });
	   
       
    }

    carouselListeners = function()
    {
        $E.on($D.get('next'), 'click', function(ev) {
            $E.preventDefault(ev);
            var nextAuthorNumber = currentAuthorDisplayed + 1;
            if (nextAuthorNumber <= maxNumberOfAuthors) {
                authorListItems[currentAuthorDisplayed].style.display = 'none';
                authorListItems[nextAuthorNumber].style.display = 'block';
                //reset current author
                currentAuthorDisplayed = nextAuthorNumber;
            } else {
                //Go back to 1st author
                authorListItems[currentAuthorDisplayed].style.display = 'none';
                authorListItems[0].style.display = 'block';
                //reset current author
                currentAuthorDisplayed = 0;
            }
        });
        
        $E.on($D.get('previous'), 'click', function(ev) {
            $E.preventDefault(ev);
            var prevAuthorNumber = currentAuthorDisplayed - 1;
            if (prevAuthorNumber >= 0) {
                authorListItems[currentAuthorDisplayed].style.display = 'none';
                authorListItems[prevAuthorNumber].style.display = 'block';
                //reset current author
                currentAuthorDisplayed = prevAuthorNumber;
            } else {
                //Go to top last author
                authorListItems[currentAuthorDisplayed].style.display = 'none';
                authorListItems[maxNumberOfAuthors].style.display = 'block';
                //reset current author
                currentAuthorDisplayed = maxNumberOfAuthors;
            }
            
        });
    }
	
	setAutoPlay = function()
	{
	    authorAutoPlayIbterval = setInterval(function(){
	        var nextAuthorNumber = currentAuthorDisplayed + 1;
	        if (nextAuthorNumber <= maxNumberOfAuthors) {
                authorListItems[currentAuthorDisplayed].style.display = 'none';
                authorListItems[nextAuthorNumber].style.display = 'block';
                //reset current author
                currentAuthorDisplayed = nextAuthorNumber;
            } else {
                //Go back to 1st author
                authorListItems[currentAuthorDisplayed].style.display = 'none';
                authorListItems[0].style.display = 'block';
                //reset current author
                currentAuthorDisplayed = 0;
            }
	    }, 10000)
	    
	}
	
	disableSpeakersMainPage = function()
	{
	    
	    $E.on($S.query('ul#nav li a'), 'click', function(ev) {
	        var el = $E.getTarget(ev);
	        if (el.href === 'http://insight.paulthepriceisright.com/speakers/') {
	            $E.preventDefault(ev);
	        }
	    });
	    
	}
	
	searchBoxInputText = function()
	{
	    $E.on($D.get('s'), 'focus', function(ev){
	        if ($D.get('s').value == 'Search') {
	            $D.get('s').value = '';
	        }
	    });
	    
	    $E.on($D.get('s'), 'blur', function(ev){
	        if ($D.get('s').value == '') {
	            $D.get('s').value = 'Search';
	        }
	    });
	    
	}
	    
    // Public Methods
    return {
        /**
         *sets event listener
         * 
         */
        init: function()
        {
            $E.onDOMReady(function() {
                launchCarousel();
                carouselListeners();
                //setAutoPlay();
                disableSpeakersMainPage();
                searchBoxInputText();
            });
        }
    };
}().init();