/**
 * @author Thanh
 */
var Carousel = {
	intNbSlides: 0,
	arrSlides: null,
	intCurrentSlide: 0,
	intCurrentImage: 0,
	
	intSlideWidth: 800,
	intSlidesWidth: 0,
	intCurePos: 0,
	init: function() {
		jQuery('#carousel_slides').css('width', jQuery('#carousel_next_slide').attr('rel') * 800+'px');
		Carousel.intSlidesWidth = jQuery('#carousel_next_slide').attr('rel') * 800 - 800;
		Carousel.intCurrentImage = 0;

		//jQuery(".carousel_slide").hide();
		Carousel.arrSlides = new Array();
		// Parcours des slides et alimentation du tableau
		jQuery(".carousel_slide").each(function(i){
			Carousel.intNbSlides += 1;
			Carousel.arrSlides[i] = new Array();
			Carousel.arrSlides[i]['name'] = jQuery(this).attr('id');
			Carousel.arrSlides[i]['images'] = jQuery("#"+Carousel.arrSlides[i]['name'] + ' img').length;
		});

		jQuery("a#carousel_previous_slide").click(function(){
			return Carousel.previous();
		});		
		jQuery("a#carousel_next_slide").click(function(){
			return Carousel.next();
		});

		jQuery("a#carousel_previous_image").click(function(){
			return Carousel.previousImage();
		});		
		jQuery("a#carousel_next_image").click(function(){
			return Carousel.nextImage();
		});
		
	},
	next: function() {
		/*Carousel.intCurrentSlide++;
		if (Carousel.intCurrentSlide > Carousel.intNbSlides) {
			Carousel.intCurrentSlide = 0;
		}*/
		// Vérification que l'on n'a pas fait défilé tout les slides
		if (Carousel.intCurePos <= -Carousel.intSlidesWidth) {
			Carousel.intCurePos = 0;
			Carousel.intCurrentSlide = 0;
		}
		else {
			// Calcul de la position suivante
			Carousel.intCurePos -= Carousel.intSlideWidth;	
			Carousel.intCurrentSlide++				
		}

		// Défilement
			jQuery("#carousel_slides").animate({opacity: 0.1}, 400).animate({ 
			opacity: 1.0,
	        marginLeft: Carousel.intCurePos+"px"
      	}, 1000 );
		
		Carousel.intCurrentImage = 0;
		Carousel.showImage();
		
		return false;		
	},
	previous: function() {
		/*Carousel.intCurrentSlide--;
		if (Carousel.intCurrentSlide < 0) {
			Carousel.intCurrentSlide = Carousel.intNbSlides-1;
		}	*/
		
		// Vérification que l'on n'a pas fait défilé tout les slides
		if (Carousel.intCurePos >= 0) {
			Carousel.intCurePos = -Carousel.intSlidesWidth;
			Carousel.intCurrentSlide = Carousel.intNbSlides-1
		}
		else {
			// Calcul de la position suivante
			Carousel.intCurePos += Carousel.intSlideWidth;
			Carousel.intCurrentSlide--					
		}

		// Défilement
			jQuery("#carousel_slides").animate({opacity: 0.1}, 400).animate({ 
			opacity: 1.0,
	        marginLeft: Carousel.intCurePos+"px"
      	}, 1000 );

		Carousel.intCurrentImage = 0;
		Carousel.showImage();

		return false;			
	},
	showImage: function() {
		jQuery("#"+Carousel.arrSlides[Carousel.intCurrentSlide]['name'] + ' img').hide();
		jQuery("#"+Carousel.arrSlides[Carousel.intCurrentSlide]['name'] + ' img').eq(Carousel.intCurrentImage).fadeIn();
		
		return false;
	},
	nextImage: function() {
		Carousel.intCurrentImage++;
		if (Carousel.intCurrentImage >= Carousel.arrSlides[Carousel.intCurrentSlide]['images']) {
			Carousel.intCurrentImage = 0;
		}
		return Carousel.showImage();
	},
	previousImage: function() {
		Carousel.intCurrentImage--;
		if (Carousel.intCurrentImage < 0) {
			Carousel.intCurrentImage = Carousel.arrSlides[Carousel.intCurrentSlide]['images'] - 1;
		}		

		return Carousel.showImage();		
	}
}

 
jQuery(function(){

	// Champ de recherche
	jQuery("#s").focus(function(){
		jQuery("#s").val('');
	});				
				
	jQuery("#s").blur(function(){
		if (jQuery("#s").val() == '') {
			jQuery("#s").val('Rechercher...');
		}
	});		
	
	if (document.getElementById('carousel')) {
		// Carousel
		Carousel.init();
		Carousel.showImage();		
	}

});
