
var widthThumbs = 0;
var currentImage = 0;
var positionThumbs = [0,-618];
var currentPositionThumbs = 0;
var flecheEnabled = false;

var initGalerie = function()
{
	$j('.bigImage img:first').css('marginTop','0');
	$j('.bigImage img').each(function(index) {
		$j('<div></div>').appendTo('.thumbs');
		$j(this).clone().appendTo('.thumbs div:eq('+index+')');
		/*var imgClone = '<div>' + $j(this).clone(true) + '</div>';
		$j(imgClone).appendTo('.thumbs');
		console.log($j($j(this).clone()[0]).html());*/
		
		if (index != 0) $j(this).hide();
	});
	$j('.thumbs').height(0);
	setTimeout("createThumbs()",1000);
	$j('#prevPanel').hide();
	$j('#nextPanel').hide();
}


function createThumbs() {
	$j('.thumbs img').each(function(index) {
		widthThumbs +=  $j('.thumbs img:eq('+index+')').width() + 7;
		$j('.thumbs').css('width',widthThumbs+'px');
		if (index != 0) $j(this).css('opacity','0.6')
	});	
	$j('.thumbs').fadeOut(0).delay(200).height(80).fadeIn(400);
	
	$j('.thumbs img').bind('mouseover', function(e) {
		$j(this).animate({'opacity':'1'},0);
	});
	$j('.thumbs img').bind('mouseout', function(e) {
		if( $j(this).index('.thumbs img') != currentImage ) {
			$j(this).animate({'opacity':'0.4'},0);
		}
	});
	
	$j('.thumbs img').bind('click', function(e) {
		if (currentImage != $j(this).index('.thumbs img')) {
			var oldImage = currentImage;
			$j('.thumbs img:eq('+currentImage+')').animate({'opacity':'0.4'},200);
			currentImage = $j(this).index('.thumbs img');
			$j('.thumbs img:eq('+currentImage+')').delay(100).animate({'opacity':'1.0'},200);
			$j('.bigImage img:eq('+oldImage+')').css({'z-index':'1'});
			$j('.bigImage img:eq('+currentImage+')').css({'z-index':'2','opacity':0,'display':'block'});
			$j('.bigImage img:eq('+currentImage+')').animate({'opacity':1},600);
			$j('.bigImage img:eq('+oldImage+')').animate({'opacity':0},600);
		}
	});
	
	$j('#nextPanel').bind('click', function(e) {
		e.preventDefault();
		if(currentPositionThumbs < 3) {
			var nextPosition = currentPositionThumbs+1;
			currentPositionThumbs +=1;
			$j('.thumbs').animate({'marginLeft':positionThumbs[nextPosition]},600,function() {
				//flecheEnabled = true;
				if (currentPositionThumbs == (positionThumbs.length - 1))  $j('#nextPanel').hide();
			});
		}
		if(currentPositionThumbs > 0) $j('#prevPanel').show();
	});
	
	$j('#prevPanel').bind('click', function(e) {
		e.preventDefault();
		if(currentPositionThumbs > 0) {
			var nextPosition = currentPositionThumbs-1;
			currentPositionThumbs -=1;
			$j('.thumbs').animate({'marginLeft':positionThumbs[nextPosition]},600,function() {
				//flecheEnabled = true;
				if (currentPositionThumbs == 0)  $j('#prevPanel').hide();
			});
		} 
		if(currentPositionThumbs < 2) $j('#nextPanel').show();
	});
	
	$j('#nextPanel').delay(600).show();
	
}
