/******************************************************************************
 * 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;

		// 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');
		
		// Selected element
		var sel = '.acs .menu li:eq(' + id + ')';
		
		// Change the class
		$(sel).addClass('selected');

		// Change the selected content
	    acs.highlight($(".acs .content div.acs-highlight"), 0, 10, function(){
	        $(".acs .content div.acs-image").css("background-image", 'url(' + $(sel + ' img').attr('src') + ')');
	        acs.highlight($(".acs .content div.acs-highlight"), 10, 0);
	    });
	    
		acs.hideElement($('.acs .content div .acs-title'), function(){
			$('.acs .content div .acs-title').text($(sel + ' img').attr('alt'));
			acs.showElement($('.acs .content div .acs-title'));
		});
		$('.acs .content div a').attr('href', $(sel + ' img').attr('rel'));	

		// 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");
	};
	

	/*
	 * http://stackoverflow.com/questions/663568/how-do-i-animate-a-background-color-to-transparent-in-jquery/3874040#3874040
	 */
	this.highlight = function (ele, from, to, callback)
	{
		var callback = (callback != undefined) ? callback : false;
	    from += from > to ? -1 : 1;
	    if(!$.support.opacity){/*
	        if(from != to){
	            var opStr = (Math.round(from * 28.3)).toString(16);
	            ele.css({background:'transparent',filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr=#" + opStr + "000000, endColorstr=#" + opStr + "000000)"});   
	        } else if(to == 0){
	            ele.css({background:'transparent',filter:"none"});   
	        }
	        var delay = 10;*/
	    } else {
	        ele.css("backgroundColor", "rgba(0, 0, 0, " + (from) / 10 + ")");
	        var delay = 40;
	    }
	    if(from != to)
	        setTimeout(function() { acs.highlight(ele, from, to, callback) }, delay);
	    else if(typeof(callback) == "function")
	    	callback();
	}

}; /* End ACS */

var acs = new ACS();

