/*------------------------------------------------------------------------------
    JS Document (https://developer.mozilla.org/en/JavaScript)

    project:    STPo
    created:    2010-09-27
    author:     Christophe ANDRIEU
                chris@stpo.fr
                http://www.stpo.fr

                CopyLeft 2010
                Bisous aux pompeurs !

    summary:    CONSTANTES
                UTILITIES
                WINDOW.ONLOAD
                PORTFOLIO CARROUSEL
----------------------------------------------------------------------------- */
/*  =CONSTANTES
----------------------------------------------------------------------------- */
jQuery.noConflict();
var d = document;
var w = window;
stpo = {};


/*  =UTILITIES
----------------------------------------------------------------------------- */
var log = function(x) {
    if (typeof console != 'undefined') {
        console.log(x);
    }
};


/*  =WINDOW.ONLOAD
----------------------------------------------------------------------------- */
jQuery(document).ready(function(){

    // Call Functions
    stpo.carrousel();									// portfolio carrousel
    
    if (jQuery.browser.msie && jQuery.browser.version == 6) {
        // IE 6 FUNCTIONS ONLY
        
    }

});

/*  =PORTFOLIO CARROUSEL THANKS TO STPO
	ATTENTION, le premier élément du Carousel doit impérativement être doté 
	de dimensions (via HTML ou CSS)
----------------------------------------------------------------------------- */
stpo.carrousel = function(){
	jQuery('.imgCarousel div').each(function(i){
		
		var myId = 'carousel_'+i;
		jQuery(this).attr('id',myId);
		
		var myUlLength = jQuery('#'+myId+' ul li').length;
		
		if (myUlLength > 1){
						
			var myLiWidth = jQuery('#'+myId+' ul li')[0].offsetWidth + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-left')) + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-right'));
			
			jQuery('#'+myId+' ul').css('width', (myLiWidth * myUlLength));
			
			var myLiHeight = jQuery('#'+myId+' ul li')[0].offsetHeight + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-top')) + parseInt(jQuery('#'+myId+' ul li:eq(1)').css('margin-bottom'));
			
			jQuery(this).append('<a href="#" class="prevLink" title="image précédente"></a><a href="#" class="nextLink" title="image suivante"></a>');
					
			jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').css('top', (myLiHeight / 2 - 46)+'px');
			
			var clickPermitted = true;
			
			jQuery('#'+myId+' .prevLink').addClass('linkToOff');
	
			jQuery(this).hover(
				function () {
					jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').fadeIn('fast');
				}, 
				function () {
					jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').fadeOut('fast');
				}
			);
			
			jQuery('#'+myId+' .prevLink, #'+myId+' .nextLink').click(function(){
				
				if(clickPermitted){
					
					var myLeft = parseInt(jQuery('#'+myId+' ul').css('left'));
					
					if (jQuery(this).attr('class') == 'prevLink') {
						
						if (myLeft != 0) {
							
							var myNewLeft = myLeft + myLiWidth;
							
							if (myNewLeft == 0) jQuery(this).addClass('linkToOff');
							jQuery('#'+myId+' .nextLink').removeClass('linkToOff');
							
						}
						
					}
					else if (jQuery(this).attr('class') == 'nextLink') {
						if (myLeft != -(myLiWidth * (myUlLength - 1))) {
							
							var myNewLeft = myLeft - myLiWidth;
							
							if (myNewLeft == -(myLiWidth * (myUlLength - 1))) jQuery(this).addClass('linkToOff');
							jQuery('#'+myId+' .prevLink').removeClass('linkToOff');
							
						}
					}
					
					if (myNewLeft != undefined){
						
						clickPermitted = false;
						
						jQuery('#'+myId+' ul').animate({ 
							left: myNewLeft+'px'
						}, 600, function(){ clickPermitted = true; });
					}
			
				}
				
				jQuery(this).blur();
				return false;
				
			});
			
		}
	});
};

