var carroussel = {
	itemWidth : 575,
	currentPosition: 2,
	maxItem: 0,
	onMove: false,
	
	init: function(mosaic){
		var me = this;
		var $items = $('.carroussel_item');
		var $conteneur = $('#carroussel_content');
		me.maxItem = $items.length;
		me.currentPosition = 2;
		
		if(me.maxItem > 0){
			// on initialise la largeur du conteneur general
			$conteneur.width(me.itemWidth * me.maxItem);
			// on positionne le diaporama sur le premier element
			$conteneur.css('right', me.itemWidth);
			
			// on applique une classe "portrait' ou 'paysage' en fonction de la taille de l'image
			$items.each(function(){
				var $image = $(this).find('.image_portfolio');
				// var src = $image.attr('src');
				// var temp = new Image;
				// temp.src = src;

				//alert( jQuery.contains($(this), $('.logo')) );
				if ( $('.logo').size() == 0 ){
					$image.width() > $image.height() ? $(this).addClass('paysage_nologo') : $(this).addClass('portrait_nologo');
				} else {
					$image.width() > $image.height() ? $(this).addClass('paysage') : $(this).addClass('portrait');
				}
				
				// cas des images quasiment carré
				/* - */
				if($image.height() != 0){
					var ratio = $image.width() / $image.height();
					if(ratio > 0.9 && ratio < 1.1){ $(this).children('div').css('top', '+=20') };
				}
				
			});
			
			// controle des flèches
			me.arrowTrigger();
		}
		
		if(mosaic === true){
			$('#carroussel').append('<a href="#" id="close"></a>');
			$('#close').bind('click', function(event){
				event.preventDefault();
				me.closeCarroussel();					   
			});
		}
		/*
		
		*/
		
	},
	
	closeCarroussel: function(){
		if($('.actualiteLink').length > 0){
			$('.actualiteLink').show();
		}
		$('#carroussel').fadeOut(400, function(){
			$(this).remove();
		});
	},
	
	arrowTrigger: function(){
		var me = this;
		
		$('.arrow').click(function(event){
			event.preventDefault();	
			me.move($(this));
		});
	},
	
	onMove: false,
	
	move: function(arrow){
		var me = this;
		
		if(me.onMove == false){

			var $conteneur = $('#carroussel_content');
			me.onMove = true;
			
			if(arrow.attr('id') == 'right_arrow'){
				// $('.fade').animate({'opacity': 0}, 0);
				
				$conteneur.animate({'right': '+=575'}, 400,
					function(){
						me.currentPosition++;
						if(me.currentPosition == me.maxItem){
							me.currentPosition = 2;
							$conteneur.css('right', me.itemWidth);
						}
						/*
						$('.fade').animate({'opacity' : 1}, 600, 
							function(){
								me.onMove = false;
							}
						);
						*/
						me.onMove = false;
					}
				);
			} else if(arrow.attr('id') == 'left_arrow'){
				// $('.fade').animate({'opacity': 0}, 0);
				
				$conteneur.animate({'right': '-=575'}, 400, 
					function(){
						me.currentPosition--;
						if(me.currentPosition == 1){
							me.currentPosition = me.maxItem - 1;
							$conteneur.css('right', me.itemWidth * (me.maxItem - 2));	
						}
						/*
						$('.fade').animate({'opacity' : 1}, 600,
							function(){
								me.onMove = false;
							}				   
						);
						*/
						me.onMove = false;
					}		   
				);
			}
		}
	}
}
