/******************************************************************************
 * Automatic Content Slider
 * @author Pedro Henrique de M. Rodrigues
 * @version 1.0
 * @date 2008/12/16
 */

var ACS = function()
{
	/*
	 * Number of contents
	 */
	this.cont_number = 0;
	
	/*
	 * Current selected content (CSC)
	 */
	this.csc = 0;
	
	/*
	 * Automatic Slider Interval (ASI) (in seconds)
	 */
	this.asi = 5;
	
	/*
	 * setInterval Reference
	 */
	this.si_ref = null;
	
	/*
     * setTimeOut Reference
     */
    this.sto_ref = null;
	
	
	/*
	 * Class Initialization
	 */
	this.init = function ()
	{
		// Update the Number of contents
		this.cont_number = $('.acs .menu li').length;
	
		// Select the first content
		acs.changeContent(0);
		
		// Init slider
		acs.initSlider(acs.asi);
		
		// Set Events to the menu
		$('.acs .menu li').hover(
			function()
			{
				acs.stopSlider();	
				acs.triggerChangeContent($('.acs .menu li').index(this));
			},
			function()
			{
				clearTimeout(acs.sto_ref);
				acs.initSlider();
			}
		);

		// Set Events to the content
		$('.acs .content').hover( function(){ acs.stopSlider() }, function(){ acs.initSlider() } );	
		
	}; // end init()
	
	
	/*
	 * Trigger the function ChangeContent
	 */
	this.triggerChangeContent = function (id)
	{ 
		var fun = function(){
			acs.changeContent(id);		
		}
		acs.sto_ref = setTimeout(fun, 300);
	};
	
	
	/*
	 * Change the content
	 */
	this.changeContent = function (id)
	{
		// Slider
		if(id == undefined) id = (acs.csc + 1) % acs.cont_number;

		// Is the id been showed?
		else if($('.acs .content li:eq(' + id + ')').css('display') != 'none') return;

		// Verify id
		if( (id < 0) || (id > acs.cont_number - 1) ) id = 0;

		// Change the selected in menu
		$('.acs .menu li:eq(' + acs.csc + ')').removeClass('selected');
		$('.acs .menu li:eq(' + id + ')').addClass('selected');
	
		// Change the selected content
		acs.hideElement($('.acs .content li:eq(' + acs.csc + ')'), function(){ acs.showElement($('.acs .content li:eq(' + id + ')')) });	

		// Update this.csc
		acs.csc = id;
	
	}; // end changeContent()
	
	
	/*
	 * Initialize the slider
	 */
	this.initSlider = function ()
	{
		acs.si_ref = setInterval('acs.changeContent()', this.asi * 1000);

	}; // end initSlider
	
	
	/*
	 * Stop the slider
	 */
	this.stopSlider = function ()
	{
		clearInterval(acs.si_ref);

	}; // end stopSlider
	
	
	/*
	 * Show an element
	 * Params:
	 *  - elem: element
	 */
	this.showElement = function (elem)
	{
		$(elem).fadeIn("normal");
	};
	
	
	/*
	 * Hide an element
	 * Params:
	 *  - elem    : element
	 *  - callback: function to execute after this
	 */
	this.hideElement = function (elem, callback)
	{
		(callback) ? $(elem).fadeOut("normal", function(){ callback() }) : $(elem).fadeOut("normal");
	};

}; /* End ACS */

var acs = new ACS();

