/*------------------------------------------------------------------------------
    JS Document (https://developer.mozilla.org/en/JavaScript)

    project:    Lucien Barriere
    created:    2010-04-22
    author:     Christophe ANDRIEU

    summary:    CONSTANTES
                UTILITIES
				FUNCTIONS
                WINDOW.ONLOAD

----------------------------------------------------------------------------- */
/*  =CONSTANTES
----------------------------------------------------------------------------- */
var d = document;
var w = window;
var debug = null;
window.config = {}


/*  =UTILITIES
----------------------------------------------------------------------------- */
var log = function(x) {
    if (typeof console != 'undefined')
        console.log(x);
    else if (debug) {
        debug.value += x + '\n';
        debug.scrollTop = debug.scrollHeight;
    }
};

/*  =FUNCTIONS
----------------------------------------------------------------------------- */
var carouselScript = {
		
	myTiming: 5000,		// intervale
	myTimer: null,		// objet Timer
	myImgs: [],			// tableau des items
	currentItem: null,	// item courant
	
	onClass: 'on',
	prevId: 'prev',
	nextId: 'next',
	carouselItems: jQuery('#carouselContent li'),
	carouselNav: jQuery('#carouselNav'),
	carouselNavItems: jQuery('#carouselNav li'),
	itemId: "#item",
	
	_showImage: function(myRank)
	{
		if (!myRank) myRank = 1;
		this.currentItem = myRank;
		
		// affichage du bon item
		this.carouselItems.hide();
		jQuery(this.itemId + myRank).show();
		
		// affectation de la classe ON
		this.carouselNavItems.removeClass(this.onClass);
		jQuery(this.carouselNav).find('li:eq('+(myRank)+')').addClass(this.onClass);
		
	},
	
	_stop: function()
	{
		clearTimeout(this.myTimer);
	},
	
	_autorun: function(myRank)
	{
		if (!myRank) myRank = 1;
		
		this._showImage(myRank);
		myRank++;
		if (myRank == carouselScript.myImgs.length + 1) myRank = 1;	// bouclage
		
		this.myTimer = setTimeout(function(){
			carouselScript._stop(); 
			carouselScript._autorun(myRank); 
		},this.myTiming);
		
	},
	
	_init: function()
	{

		// stockage des items
		jQuery.each(this.carouselItems, function(i, n){
			carouselScript.myImgs[i] = n.id;
		});
		
		if (carouselScript.myImgs.length > 1){
			
			var myRank = 1;
			
			// on click
			this.carouselNavItems.bind('click', function(){

				if (jQuery(this).attr('id') == carouselScript.prevId){
					
					myRank = parseInt(carouselScript.currentItem) - 1;
					if (myRank == 0) myRank = carouselScript.myImgs.length;	// bouclage
					
				}
				else if (jQuery(this).attr('id') == carouselScript.nextId){
					
					myRank = parseInt(carouselScript.currentItem) + 1;
					if (myRank == carouselScript.myImgs.length + 1) myRank = 1;	// bouclage
					
				}
				else {
					
					myRank = parseInt(jQuery(this).find('a').attr('href').split('item')[1]);
					
				}
				
				carouselScript._stop();
				carouselScript._autorun(myRank);
				
				jQuery(this).children('a').blur();
				return false;
				
			});
			
			carouselScript._stop();
			carouselScript._autorun(myRank);

		}
	}
}

/*  =WINDOW.ONLOAD
----------------------------------------------------------------------------- */
jQuery(document).ready(function(){
    
    // Functions
	carouselScript._init();
	
});