// Javascript
function body_init(){
	
	jQuery('#main').css({'opacity': 0, 'margin-left': '0'}); // Masque le menu "principal" à l'ouverture de la page
	jQuery('#cart').css({'opacity': 0, 'margin-right': '-10'}); // Masque le menu "panier" à l'ouverture de la page
	jQuery('#footer').css({'opacity': .4}); // Masque le menu "panier" à l'ouverture de la page
	
	// On redefinit les marges du conteneur principale (#page)
	var widthPage = jQuery('#header').width();
	var heightPage = jQuery(document).height() - jQuery('#header,#footer').height();
	/*
	jQuery('#page').css({
						'top': 30,
						'right': 70,
						'bottom': 30,
						'left': 70,
						'width': widthPage,
						'height': heightPage
						});
	*/
	jQuery('#body').css({
						'width': jQuery(document).width(),
						'height': jQuery(document).height()
						});
	jQuery('#pageSize').val(widthPage+'|'+heightPage);
	
	
}

function main_fx(){
	// Affiche les menus au survol de l'entête
	jQuery('#header,#footer').mouseenter(function(){
											jQuery('#main').css({'opacity': 1});
											jQuery('#cart').css({'opacity': 1});
										  	jQuery('#main').find('li').each(function(){
																		  	jQuery(this).animate({'opacity': 1, 'margin-left': '20'}, 200);
																		  });
										  	jQuery('#cart').find('li').each(function(){
																		  jQuery(this).animate({'opacity': 1, 'margin-right': '10'}, 200);
																		  });
										  	jQuery('#shopp').animate({'opacity': .5}, 400);
										  	jQuery('#footer').animate({'opacity': 1}, 400);
											
										  });
	// Masque les menus lorsqu'on quitte les entêtes
	jQuery('#header,#footer').mouseleave(function(){
										  	jQuery('#main').find('li').each(function(){
																		  	jQuery(this).animate({'opacity': 0, 'margin-left': '10'}, 200);
																		  }).queue(function(){
																			jQuery('#main').css({'opacity': 0});
																			jQuery(this).dequeue();
																		  }, 400);
										  	jQuery('#cart').find('li').each(function(){
																		  jQuery(this).animate({'opacity': 0, 'margin-right': '0'}, 200);
																		  }).queue(function(){
																			jQuery('#cart').css({'opacity': 0});
																			jQuery(this).dequeue();
																		  }, 400);
										  	jQuery('#shopp').animate({'opacity': 1}, 400);
										  	jQuery('#footer').animate({'opacity': .4}, 400);
										  });
										  
}

var scrollbar;
var totalRow = null;
var contentHeight = null;
var height = null;
var widthPage = null;
var rowWidth = null;
var rowCount = null;
var loadCount = 0;

function loadProduct(uri, width, height){
	
	var widthContent = width;
	var heightContent = height;
		
	var template_directory = jQuery('#template_directory').val();
	var site_directory = jQuery('#site_directory').val();
	var shadowValue = '0 0 10px #666';
	
	//loader('cart', 'Chargement de votre panier');
	
	jQuery('#popup').remove();
	jQuery('#popup-background').remove();
	
	jQuery('#body').append('<div id="popup-background"></div>');
	jQuery('#popup-background').css({
									'z-index': 5,
									'opacity': 0
									}).queue(function() {
										jQuery(this).animate({
											'opacity': .5,
											backgroundColor: '#666'
										}, 600);
										jQuery(this).dequeue();
									});
	
	jQuery('#body').append('<div id="popup"></div>');
	
	jQuery('#popup').css({
						'z-index': 9999,
						backgroundColor: '#FFFFFF',
						'position': 'absolute',
						'opacity': 0,
						'top': ((jQuery(document).height() / 2) - (10 / 2)),
						'width': 10,
						'height': 10,
						'left': ((jQuery(document).width() / 2) - (10 / 2)),
						'box-shadow': shadowValue,
						'-o-box-shadow': shadowValue,
						'-moz-box-shadow': shadowValue,
						'-webkit-box-shadow': shadowValue
						}).queue(function() {
							
							containerWidth = (Number(widthContent) / 2) + 350;
							containerHeight = (Number(heightContent) / 2) + 150;
							
							jQuery(this).animate({
								'opacity': 1,
								'top': ((jQuery(document).height() / 2) - (containerHeight / 2)),
								'width': containerWidth,
								'height': containerHeight,
								'left': ((jQuery(document).width() / 2) - (containerWidth / 2))
							}, 600).queue(function(){
								jQuery('#popup').html('<iframe id="iframe" frameborder="0" src="'+uri+'/?height='+heightContent+'&width='+widthContent+'" height="100%" width="100%" scrolling="no"></iframe>');
								//loaderRemove('cart');
								jQuery(this).dequeue();
							});
							
							jQuery(this).dequeue();
						});

	jQuery('#popup-background').click(function(){
												jQuery(this).animate({
																	'opacity': 0
																	}, 600).queue(function() {
																		jQuery(this).remove();
																		jQuery(this).dequeue();
																	});
												jQuery('#popup').animate({
																		'opacity': 0
																		}, 600).queue(function() {
																			jQuery(this).remove();
																			jQuery(this).dequeue();
																		});
												
												
											   });
}

function loadCart(){
	
	var widthContent = jQuery(document).width() - 300;
	var heightContent = jQuery(document).height() - 300;
		
	var template_directory = jQuery('#template_directory').val();
	var site_directory = jQuery('#site_directory').val();
	var shadowValue = '0 0 10px #666';
	
	//loader('cart', 'Chargement de votre panier');
	
	jQuery('#popup').remove();
	jQuery('#popup-background').remove();
	
	jQuery('#body').append('<div id="popup-background"></div>');
	jQuery('#popup-background').css({
									'z-index': 5,
									'opacity': 0
									}).queue(function() {
										jQuery(this).animate({
											'opacity': .5,
											backgroundColor: '#666'
										}, 600);
										jQuery(this).dequeue();
									});
	
	jQuery('#body').append('<div id="popup"></div>');
	
	jQuery('#popup').css({
						'z-index': 9999,
						backgroundColor: '#FFFFFF',
						'position': 'absolute',
						'opacity': 0,
						'top': ((jQuery(document).height() / 2) - (10 / 2)),
						'width': 10,
						'height': 10,
						'left': ((jQuery(document).width() / 2) - (10 / 2)),
						'box-shadow': shadowValue,
						'-o-box-shadow': shadowValue,
						'-moz-box-shadow': shadowValue,
						'-webkit-box-shadow': shadowValue
						}).queue(function() {
							jQuery(this).animate({
								'opacity': 1,
								'top': ((jQuery(document).height() / 2) - (heightContent / 2)),
								'width': widthContent,
								'height': heightContent,
								'left': ((jQuery(document).width() / 2) - (widthContent / 2))
							}, 600).queue(function(){
								jQuery('#popup').html('<iframe id="iframe" frameborder="0" src="'+site_directory+'/cart" height="100%" width="100%" scrolling="no"></iframe>');
								loaderRemove('cart');
								jQuery(this).dequeue();
							});
							jQuery(this).dequeue();
						});

	jQuery('#popup-background').click(function(){
												jQuery(this).animate({
																	'opacity': 0
																	}, 600).queue(function() {
																		jQuery(this).remove();
																		jQuery(this).dequeue();
																	});
												jQuery('#popup').animate({
																		'opacity': 0
																		}, 600).queue(function() {
																			jQuery(this).remove();
																			jQuery(this).dequeue();
																		});
												
												
											   });
	
	
}

function loadContact(){
	
	var widthContent = 720;
	var heightContent = 550;
	
	var template_directory = jQuery('#template_directory').val();
	var site_directory = jQuery('#site_directory').val();
	var shadowValue = '0 0 15px #999';
	
	//loader('cart', 'Chargement de votre panier');
		   
	jQuery('#popup').remove();
	jQuery('#popup-background').remove();
	
	jQuery('#body').append('<div id="popup-background"></div>');
	jQuery('#popup-background').css({
									'z-index': 5,
									'opacity': 0
									}).queue(function() {
										jQuery(this).animate({
											'opacity': .5,
											backgroundColor: '#666'
										}, 600);
										jQuery(this).dequeue();
									});
	
	jQuery('#body').append('<div id="popup"></div>');
	jQuery('#popup').css({
						'z-index': 9999,
						backgroundColor: '#FFFFFF',
						'position': 'absolute',
						'opacity': 0,
						'top': ((jQuery(document).height() / 2) - (10 / 2)),
						'width': 10,
						'height': 10,
						'left': ((jQuery(document).width() / 2) - (10 / 2)),
						'box-shadow': shadowValue,
						'-o-box-shadow': shadowValue,
						'-moz-box-shadow': shadowValue,
						'-webkit-box-shadow': shadowValue
						}).queue(function() {
							jQuery(this).animate({
								'opacity': 1,
								'top': ((jQuery(document).height() / 2) - (heightContent / 2)),
								'width': widthContent,
								'height': heightContent,
								'left': ((jQuery(document).width() / 2) - (widthContent / 2))
							}, 600).queue(function(){
								jQuery('#popup').html('<iframe id="iframe" frameborder="0" src="'+site_directory+'/contact" height="100%" width="100%" scrolling="no"></iframe>');
								jQuery(this).dequeue();
							});
							//loaderRemove('cart');
							jQuery(this).dequeue();
						});
						
	jQuery('#popup-background').click(function(){
												jQuery(this).animate({
																	'opacity': 0
																	}, 600).queue(function() {
																		jQuery(this).remove();
																		jQuery(this).dequeue();
																	});
												jQuery('#popup').animate({
																		'opacity': 0
																		}, 600).queue(function() {
																			jQuery(this).remove();
																			jQuery(this).dequeue();
																		});
												
												
											   });
	
	
}

function loadLogin(){
	var widthContent = 330;
	var heightContent = 230;
	
	var template_directory = jQuery('#template_directory').val();
	var site_directory = jQuery('#site_directory').val();
	var shadowValue = '0 0 15px #999';
	
	//loader('cart', 'Chargement de votre panier');
		   
	jQuery('#popup').remove();
	jQuery('#popup-background').remove();
	
	jQuery('#body').append('<div id="popup-background"></div>');
	jQuery('#popup-background').css({
									'z-index': 5,
									'opacity': 0
									}).queue(function() {
										jQuery(this).animate({
											'opacity': .5,
											backgroundColor: '#666'
										}, 600);
										jQuery(this).dequeue();
									});
	
	jQuery('#body').append('<div id="popup"></div>');
	
	jQuery('#popup').css({
						'z-index': 9999,
						backgroundColor: '#FFFFFF',
						'position': 'absolute',
						'opacity': 0,
						'top': ((jQuery(document).height() / 2) - (10 / 2)),
						'width': 10,
						'height': 10,
						'left': ((jQuery(document).width() / 2) - (10 / 2)),
						'box-shadow': shadowValue,
						'-o-box-shadow': shadowValue,
						'-moz-box-shadow': shadowValue,
						'-webkit-box-shadow': shadowValue
						}).queue(function() {
							jQuery(this).animate({
								'opacity': 1,
								'top': ((jQuery(document).height() / 2) - (heightContent / 2)),
								'width': widthContent,
								'height': heightContent,
								'left': ((jQuery(document).width() / 2) - (widthContent / 2))
							}, 600).queue(function(){
								jQuery('#popup').html('<iframe id="iframe" frameborder="0" src="'+site_directory+'/login" height="100%" width="100%" scrolling="no"></iframe>');
								jQuery(this).dequeue();
							});
							//loaderRemove('cart');
							jQuery(this).dequeue();
						});

	jQuery('#popup-background').click(function(){
												jQuery(this).animate({
																	'opacity': 0
																	}, 600).queue(function() {
																		jQuery(this).remove();
																		jQuery(this).dequeue();
																	});
												jQuery('#popup').animate({
																		'opacity': 0
																		}, 600).queue(function() {
																			jQuery(this).remove();
																			jQuery(this).dequeue();
																		});
												
												
											   });
	
	
}

function loadAccount(){
	var widthContent = 480;
	var heightContent = 500;
	
	var template_directory = jQuery('#template_directory').val();
	var site_directory = jQuery('#site_directory').val();
	var shadowValue = '0 0 15px #999';
	
	//loader('cart', 'Chargement de votre panier');
		   
	
	jQuery('#popup').remove();
	jQuery('#popup-background').remove();
	
	jQuery('#body').append('<div id="popup-background"></div>');
	jQuery('#popup-background').css({
									'z-index': 5,
									'opacity': 0
									}).queue(function() {
										jQuery(this).animate({
											'opacity': .5,
											backgroundColor: '#666'
										}, 600);
										jQuery(this).dequeue();
									});
	
	jQuery('#body').append('<div id="popup"></div>');
	
	jQuery('#popup').css({
						'z-index': 9999,
						backgroundColor: '#FFFFFF',
						'position': 'absolute',
						'opacity': 0,
						'top': ((jQuery(document).height() / 2) - (10 / 2)),
						'width': 10,
						'height': 10,
						'left': ((jQuery(document).width() / 2) - (10 / 2)),
						'box-shadow': shadowValue,
						'-o-box-shadow': shadowValue,
						'-moz-box-shadow': shadowValue,
						'-webkit-box-shadow': shadowValue
						}).queue(function() {
							jQuery(this).animate({
								'opacity': 1,
								'top': ((jQuery(document).height() / 2) - (heightContent / 2)),
								'width': widthContent,
								'height': heightContent,
								'left': ((jQuery(document).width() / 2) - (widthContent / 2))
							}, 600).queue(function(){
								jQuery('#popup').html('<iframe id="iframe" frameborder="0" src="'+site_directory+'/account" height="100%" width="100%" scrolling="no"></iframe>');
								jQuery(this).dequeue();
							});
							//loaderRemove('cart');
							jQuery(this).dequeue();
						});

	jQuery('#popup-background').click(function(){
												jQuery(this).animate({
																	'opacity': 0
																	}, 600).queue(function() {
																		jQuery(this).remove();
																		jQuery(this).dequeue();
																	});
												jQuery('#popup').animate({
																		'opacity': 0
																		}, 600).queue(function() {
																			jQuery(this).remove();
																			jQuery(this).dequeue();
																		});
												
												
											   });
	
	
}

function loadRegister(){
	var widthContent = 2;
	var heightContent = 2;
	
	var template_directory = jQuery('#template_directory').val();
	var site_directory = jQuery('#site_directory').val();
	var shadowValue = '0 0 15px #999';
	
	//loader('cart', 'Chargement de votre panier');
	
	jQuery('#popup').remove();
	jQuery('#popup-background').remove();
	
	jQuery('#body').append('<div id="popup-background"></div>');
	jQuery('#popup-background').css({
									'z-index': 5,
									'opacity': 0
									}).queue(function() {
										jQuery(this).animate({
											'opacity': .5,
											backgroundColor: '#666'
										}, 600);
										jQuery(this).dequeue();
									});
	
	jQuery('#body').append('<div id="popup"></div>');
	
	jQuery('#popup').css({
						'z-index': 9999,
						backgroundColor: '#FFFFFF',
						'position': 'absolute',
						'opacity': 0,
						'top': ((jQuery(document).height() / 2) - (10 / 2)),
						'width': 10,
						'height': 10,
						'left': ((jQuery(document).width() / 2) - (10 / 2)),
						'box-shadow': shadowValue,
						'-o-box-shadow': shadowValue,
						'-moz-box-shadow': shadowValue,
						'-webkit-box-shadow': shadowValue
						}).queue(function() {
							jQuery(this).animate({
								'opacity': 1,
								'top': ((jQuery(document).height() / 2) - (heightContent / 2)),
								'width': widthContent,
								'height': heightContent,
								'left': ((jQuery(document).width() / 2) - (widthContent / 2))
							}, 600).queue(function(){
								jQuery('#popup').html('<iframe id="iframe" frameborder="0" src="'+site_directory+'/register" height="100%" width="100%" scrolling="no"></iframe>');
								jQuery(this).dequeue();
							});
							//loaderRemove('cart');
							jQuery(this).dequeue();
						});

	jQuery('#popup-background').click(function(){
												jQuery(this).animate({
																	'opacity': 0
																	}, 600).queue(function() {
																		jQuery(this).remove();
																		jQuery(this).dequeue();
																	});
												jQuery('#popup').animate({
																		'opacity': 0
																		}, 600).queue(function() {
																			jQuery(this).remove();
																			jQuery(this).dequeue();
																		});
												
												
											   });
	
	
}
var iThumb = 1;
var totalWidthThumb = 0;
function loadImg(img_src){
	
	var img = new Image();
	//var idLI = img_src.attr('id').replace('img-', '');
	var container = jQuery('.container-catalog');
	var containerHeight = container.height();
	var containerWidth = container.width();
	var containerThumbnail = jQuery('.container-thumbnail');
	var containerUL = jQuery('.container-plane');
	var totalImgRow = containerUL.find('li').length;
	
	img_src.hide();
	
	jQuery(img).load(function(){
		
		var src = img_src.attr('src');
		var width = img_src.width();
		var height = img_src.height();
		var ratio = (height / cellHeight);
		var newWidth = (width / ratio);
		var actualWidth = containerUL.innerWidth();
		var newWidthRow = (actualWidth + newWidth);
		var heightUl = containerUL.height();
		var containerPageWidth = jQuery('#page').width();
		
		containerUL.width(newWidthRow);
		
		img_src.fadeIn(2000);
		img_src.css({
			height: 600,
			width: newWidth
		});
		
		var thumbScrollContainer = img_src.parent();
		thumbScrollContainer.css({
			width: newWidth
		});
		
		if(newWidth > containerPageWidth){
		
			var thumbContainer = img_src.parent().parent();
			thumbContainer.css({
				width: containerPageWidth
			});
			thumbContainer.mousemove(function(e){
											  
				var left = (e.pageX - 70) * (thumbScrollContainer.width() - thumbContainer.width()) / thumbContainer.width();
				thumbScrollContainer.animate({'left': '-'+left}, {queue:false, duration:300}); //, 'easeOutElastic'
				//jQuery('#footer').html(left);
				
			});
		}
		
		if(newWidth >= containerPageWidth){
			img_src.parent().parent().mousemove(function(e){
				//jQuery('#footer').html(e.pageX);
			});
		}
		
		containerThumbnail.find('ul').append('<li id="thumb-'+iThumb+'" style="display: none;"><img src="'+src+'" /></li>');
			
		var ratioThumb = (height / cellHeightThumb);
		var newWidthThumb = (width / ratioThumb);
		var thumb = containerThumbnail.find('#thumb-'+iThumb+' > img');
		thumb.css({
			height: cellHeightThumb,
			width: newWidthThumb,
			opacity: .2,
			bottom: '-15px'
		});
		
		totalWidthThumb += newWidthThumb;
		if(totalWidthThumb <= containerPageWidth){
			var posLeftThumb = ((containerWidth / 2) - (totalWidthThumb / 2));
			containerThumbnail.css({
				width: 30 + totalWidthThumb,
				left: posLeftThumb +'px'
			});
		} else {
			var posLeftThumb = ((containerWidth / 2) - (containerPageWidth / 2));
			containerThumbnail.css({
				overflow: 'hidden',
				width: containerPageWidth,
				left: posLeftThumb +'px'
			});
			containerThumbnail.find('ul').css({
				position: 'relative',
				display: 'block',
				width: totalWidthThumb
			});
			
			containerThumbnail.mousemove(function(e){
											  
				var left = (e.pageX) * ((totalWidthThumb) - containerThumbnail.innerWidth()) / containerThumbnail.innerWidth();
				containerThumbnail.find('ul').animate({'left': '-'+left}, {queue:false, duration:300}); //, 'easeOutElastic'
				//jQuery('#footer').html(left);
				
			});
		}
		iThumb++;
		
		countImg++;
		jQuery('#loader-thumbnail').find('span').text(countImg+'/'+totalImg);
		
		if(countImg >= totalImg){
			loaderRemove('thumbnail');
		}
		
		if(typeof(countImgRow) != 'undefined'){
			countImgRow++;
		} else {
			countImgRow = new Number(1);
		}
		
		if(countImgRow >= totalImgRow){
			
			container.css({
				left: 0
			});
			
			var ii = 1;
			containerUL.find('li').each(function(){
				
				var srcBig = jQuery(this).find('img').attr('src');
				var el = containerThumbnail.find("img[src="+srcBig+"]");
				var html = el.parent().html();
				el.remove();
				containerThumbnail.find('ul').append('<li id="thumb-'+ii+'" class="thumb">'+html+'</li>');
				ii++;
				
			});
			
			slideshow('first');
			
		}
		
	}).attr('src', img_src.attr('src'));
	
}

var scrollbar = false;
var cellHeight = 600;
var cellHeightThumb = 40;

function containerSize(scrollbar){
	
	jQuery('#footer').css({'display': 'block'});
	
	var containerUL = jQuery('.container-plane');
	var totalRow = jQuery('.container-plane').find('.row').length;
	var contentWidth = jQuery(document).width();
	var contentHeight = ( jQuery('#page').innerHeight() - ( jQuery('#header').innerHeight() + jQuery('#footer').innerHeight() ) );
	var widthPage = jQuery('#page').width();
	var heightPage = jQuery('#page').height();
	var countRows = jQuery('.row').length;
	var containerShopp = jQuery('#shopp');
	var container = jQuery('.container-catalog');
	var containerThumbnail = jQuery('.container-thumbnail');
	
	containerShopp.height(contentHeight);
	
	containerThumbnail.css({
		position: 'absolute',
		bottom: 0,
		left: (contentWidth / 2)
	});
	
	container.css({
		position: 'absolute',
		width: contentWidth,
		height: cellHeight,
		top: '50%',
		left: contentWidth + 100,
		marginTop: '-'+(cellHeight/2)+'px',
	});
	var firstLi = containerUL.find('li:first');
	firstLi.addClass('select');
	
	// Déplace tout ce qui se trouve dans le conteneur gallerie dans le niveau body
	var htmlGallery = jQuery('#shopp').html();
	jQuery('#shopp').empty();
	jQuery('#body').append(htmlGallery);
	
	jQuery(window).mousemove(function(e){
		var pos = e.pageX;
		//jQuery('#footer').html(pos +'>'+ (jQuery(window).width() / 2));
		if(pos <= (jQuery(window).width() / 2) - 300){
			jQuery('.btn-back').css({
				opacity: 1,
			});
			jQuery('.btn-next').css({
				opacity: 0,
			});
		}
		else if(pos > (jQuery(window).width() / 2) + 300){
			jQuery('.btn-next').css({
				opacity: 1,
			});
			jQuery('.btn-back').css({
				opacity: 0,
			});
		} else {
			jQuery('.btn-next').css({
				opacity: 0,
			});
			jQuery('.btn-back').css({
				opacity: 0,
			});
		}
	}).mouseleave(function(){
		jQuery('.btn-next').css({
			opacity: 0,
		});
		jQuery('.btn-back').css({
			opacity: 0,
		});
	});
	jQuery('.btn-back').css({
		display: 'block',
		opacity: 0,
		position: 'absolute',
		top: '48%',
		left: '0px'
	});
	jQuery('.btn-next').css({
		display: 'block',
		opacity: 0,
		position: 'absolute',
		top: '48%',
		right: '0px'
	});
	jQuery(document).bind('keydown', function(e){
		switch (e.which) {
			case 37:
				slideshow('back');
				break;
			case 39:
				slideshow('next');
				break;
			default:
		}      							 
	})
	
	jQuery('.btn-back').click(function(){
		slideshow('back');
	});
	
	jQuery('.btn-next').click(function(){
		slideshow('next');
	});
}

var orientation = null;
function slideshow(orientation){
	
	var container = jQuery('.container-catalog');
	var containerHeight = container.height();
	var containerWidth = container.width();
	var containerUL = jQuery('.container-plane');
	var containerThumbnail = jQuery('.container-thumbnail');
	
	var selectLi = null;
	if(orientation == 'back'){
		selectLi = containerUL.find('.select').prev();
		if(typeof(selectLi.attr('id')) == 'undefined' || !typeof(selectLi.attr('id'))){
			selectLi = containerUL.find('li:last');
		}
	} else if(orientation == 'next'){
		selectLi = containerUL.find('.select').next();
		if(typeof(selectLi.attr('id')) == 'undefined' || !typeof(selectLi.attr('id'))){
			selectLi = containerUL.find('li:first');
		}
	} else if(orientation == 'first'){
		selectLi = containerUL.find('.select');
	} else if(typeof(orientation) == 'number'){
		selectLi = containerUL.find('li:eq('+(orientation-1)+')');
	}
	
	var posFirstLi = selectLi.position().left;
	var widthFirstLi = selectLi.outerWidth();
	var leftPos = (( containerWidth / 2) - (widthFirstLi / 2)) - (posFirstLi - 0);
	var currentID = selectLi.attr('id');
	
	containerThumbnail.find('img').each(function(){
		if(jQuery(this).parent().attr('id') == 'thumb-'+currentID){
			jQuery(this).animate({
						 opacity: 1,
						 bottom: '-4px'
						}, 300);
		} else {
			jQuery(this).animate({
						 opacity: .2,
						 bottom: '-15px'
						}, 300);
		}
	});
	
	containerUL.find('li').each(function(){
		jQuery(this).removeClass('select');
		jQuery(this).stop().animate({
						opacity: .5,
						display: 'none'
					}, 300);
	});
	
	selectLi.animate({
					 opacity: 1,
					 display: 'block'
					}, 600);
	containerUL.animate({
		left: leftPos
	}, 600).queue(function(){
		selectLi.addClass('select');
		jQuery(this).dequeue();
	});
		
	containerThumbnail.find('img').each(function(){
		var thumb = jQuery(this);
		var thumbID = thumb.parent().attr('id').replace('thumb-', '');
		
		thumb.unbind('mouseenter');
		thumb.unbind('mouseleave');
		thumb.unbind('click');	
			
		if(thumbID != currentID){
			thumb.bind('mouseenter', function(){
				jQuery(this).animate({
					opacity: .8,
					bottom: '-5px'
				}, 200);
			});
			thumb.bind('mouseleave', function(){
				jQuery(this).animate({
					opacity: .2,
					bottom: '-15px'
				}, 200);
			});
			thumb.bind('click', function(){
				slideshow(Number(thumbID));
			});
		}
	});
}

var countImg = Number(0);
var totalImg = Number(0);
var countImgRow = Number(0);
function thumbnail(){

	countImg = 0;
	countImgRow = new Array();
	loader('thumbnail', 'Chargement <span></span>');
	totalImg = jQuery('li.cat,li.prod').length;
	/*
	jQuery('img').each(function(){
		 var el = jQuery(this);
		 loadImg(el);
	});
	*/
	
}

var timeout = null;
function loader(id, text){
	//clearTimeout(timeout);
	var t = '';
	t += '<div id="loader-'+id+'" class="loader">';
		t += '<div class="loadbar"></div>';
		t += '<div class="loadtext">'+text+'</div>';
	t += '</div>';
	jQuery('#body').append(t).css({
								'opacity': 1,
								'width': 0
								}).queue(function() {
									
									jQuery(this).find('.loadbar').animate({
										'width': jQuery(document).width() + 'px'
									}, 5000).queue(function(){
											jQuery(this).animate({
												'z-index': 1,
												'opacity': 0
											}, 300).queue(function(){
												loaderRemove(id);
												jQuery(this).dequeue();
											});
											
										jQuery(this).dequeue();
									});
									
									jQuery(this).dequeue();
								});
	//timeout = setTimeout(function() {loaderRemove(id)}, 5000);
}
function loaderRemove(id){
	if(jQuery('#loader-'+id).html() != null){
		jQuery('#loader-'+id).animate({
									'opacity': 0
									}, 300).queue(function() {
										jQuery(this).remove();
										jQuery(this).dequeue();
									});
	}
}

function openProduct(uri){
	loader('pictData', 'Chargement de la page produit');
	jQuery('#popup').find('img, div').animate({
												'opacity': 0,
											}, 600).queue(function() {
												jQuery(this).find('img, div').remove();
												//var imgWidth = ((jQuery('#popup').width() * 65 ) / 100);
												
												var pageHeight = (jQuery('#page').height() / 2);
												var heightPopup = jQuery('#popup').height();
												var topPopup = (pageHeight / 2) - (heightPopup / 2);
												
												var pageWidth = (jQuery('#page').width() / 2);
												var widthPopup = (jQuery('#popup').width() + 300);
												var leftPopup = (pageWidth / 2) - (widthPopup / 2);
												
												
												var imgWidth = pageWidth;
												var imgHeight = pageHeight;
												jQuery('#popup').html('<iframe id="iframe" frameborder="0" src="'+uri+'/?height='+imgHeight+'&width='+imgWidth+'" height="100%" width="100%" scrolling="no"></iframe>');
											
												jQuery('#popup-background').animate({
																				'opacity': .5,
																				backgroundColor: '#666666'
																			}, 400);
												
												
												jQuery(this).dequeue();
											});
	
	loaderRemove('pictData');
}

var i_next = 1;
var ajaxLoaderAuto = jQuery.ajax();
var changeLoaded = 0;
function loadOtherImage(i_next){
	
	ajaxLoaderAuto.abort();
	
	var i = 1;
	var nextImageID = null;
	
	var currentCatID = jQuery('#currentCatID').val();
	var currentProductID = jQuery('#currentProductID');
	var currentProductIDval = currentProductID.val();
	
	var positionContent = jQuery('#page').position();
	var widthContent = jQuery('#page').width();
	var heightContent = jQuery('#page').height();
	var widthBody = (jQuery(document).width() - 60);
	var heightBody = (jQuery(document).height() - 60);
	var template_directory = jQuery('#template_directory').val();
	var site_directory = jQuery('#site_directory').val();
	var type = 'prod';
	
	var a = 1;
	var elID = null;
	var objThumbList = jQuery('#thumb-list').find('.thumb-pict');	
	objThumbList.each(function(){
		if(i == i_next){
			if(i <= objThumbList.length){
				nextImageID = jQuery(this).attr('id').replace('thumb-', '');
				elID = a;
			} else {
				return;
			}
		}
		i++;
		a++;
	});

	if(jQuery('#popup-background').html() === null){
		
		loader('first_load', "Chargement de l'image");
		
		jQuery('#body').append('<div id="popup-background"></div>');
		jQuery('#popup-background').css({
										'opacity': 0,
										'z-index': '999'
										}).queue(function() {
											jQuery(this).animate({
												'opacity': 1
											}, 600);
											jQuery(this).dequeue();
										});
	}
	
	ajaxLoaderAuto = jQuery.ajax({
		   type: "POST",
		   dataType: 'json',
		   url: template_directory + "/ajax-loadpicture.php",
		   data: "currentCatID="+currentCatID+"&type="+type+"&id="+nextImageID+"&width="+widthBody+"&height="+heightBody+"",
		   success: function(jsonObj){
			   
				if(jsonObj.url !== ''){
					
					jQuery(function(){
						
						var img = new Image();
					
						jQuery(img).load(function() {
											
							loaderRemove('first_load');
							
							jQuery('#body').append('<div id="popup-'+i_next+'" class="popup"></div>');
							
							var el = jQuery('#popup-'+i_next+'');
							
							el.css({
									'z-index': '-99999',
									'opacity': 0,
									'top': ((jQuery(document).height() / 2) - (jsonObj.height / 2)) - 20,
									'width': jsonObj.width,
									'height': jsonObj.height,
									'left': ((jQuery(document).width() / 2) - (jsonObj.width / 2)),
									'visibility': 'hidden'
								});
							
							el.append(img);
							
							el.append('<div class="preview-description">'+jsonObj.description+'<div class="button-thumb"></div><div class="button-buy"></div></div>');
							el.find('.preview-description').css({
																'position': 'absolute',
																'background-color': '#FFF',
																'line-height': '20px',
																'width': el.width() - 50,
																'top': el.height(),
																'opacity': 1
															}).hide();
							
							
						
							if(i_next == 1 && changeLoaded == 0){
								changeLoaded = 1;
								changeImageAuto(i_next);	
							}
							
							
							el.mouseenter(function() {
														jQuery(this).find('.preview-description').fadeIn(400);
														stopSlideshow(el);
														
													});
							el.mouseleave(function() {
														jQuery(this).find('.preview-description').fadeOut(400);
														playSlideshow(el, i_next);
														
													});
							
							el.find('.button-buy').click(function(){
																  
								el.fadeOut(400);
								
								el.unbind('mouseleave');
								
								stopSlideshow(el);
								
								/*
								el.find('img, div').animate({
														'opacity': 0,
													}, 600).queue(function() {
														
									jQuery(this).find('img, div').remove();
									*/
									
									var pageHeight = jQuery(document).height();
									var heightPopup = el.height();
									var topPopup = (pageHeight) - (heightPopup / 2);
									
									var pageWidth = jQuery(document).width();
									var widthPopup = (el.width() + 200);
									var leftPopup = (pageWidth) - (widthPopup / 2);
									
									var leftPopup = el.position().left;
									
									var imgWidth = widthPopup;
									var imgHeight = heightPopup;
									
									jQuery('#body').append('<div id="popup-product-background"></div>');
									jQuery('#popup-product-background').css({'opacity': 0}).animate({
																		'z-index': '999999',
																		'opacity': .5,
																		backgroundColor: '#666666',
																		}, 400).queue(function() {
																			jQuery(this).dequeue();
																		});
									
									jQuery('#body').append('<div id="popup-product" class="popup-product"></div>');
									var popupProduct = jQuery('#popup-product');
									popupProduct.html('<iframe id="iframe" frameborder="0" src="'+site_directory+'/shop/'+jsonObj.slug+'/?height='+imgHeight+'&width='+imgWidth+'" height="100%" width="100%" scrolling="no"></iframe><div class="btn-close" id="button-close-product">&nbsp;</div>');
									
									popupProduct.animate({
											'width': widthPopup,
											'height': heightPopup,
											'z-index': '999999',
											'top': ((jQuery(document).height() / 2) - (heightPopup / 2)) - 20,
											'width': widthPopup,
											'height': heightPopup,
											'left': ((jQuery(document).width() / 2) - (widthPopup / 2))
									}, 400);
									
									popupProduct.find('#button-close-product').click(function(){
										
										el.fadeIn(400);
										el.find('.preview-description').fadeOut(400);
										
										el.mouseleave(function() {
											jQuery(this).find('.preview-description').fadeOut(400);
											playSlideshow(el, i_next);
											
										});
										
										jQuery('#popup-product-background').animate({
											backgroundColor: '#FFFFFF',
											'opacity': 0,
											'z-index': '999'
										}, 400).queue(function() {
										
											jQuery(this).remove();
											jQuery(this).dequeue();
										});
										
										popupProduct.animate({
											'opacity': 0,
										}, 600).queue(function() {
										
											playSlideshow(el, i_next);
										
											jQuery(this).remove();
											jQuery(this).dequeue();
										});
										
									});
									/*
									jQuery(this).dequeue();
								});
								*/
								
							});
							
							el.find('.button-thumb').click(function(){
								
								stopSlideshow(el);
								fadeOutPopup(el);
								el.unbind('mouseleave');
											   
								jQuery('.container-thumbnail').css({
																	'z-index': '999999',
																	'visibility': 'visible',
																	'opacity': 0})
																		.animate({
																			'opacity': 1
																		}, 600).queue(function() {
																			
																			jQuery(this).dequeue();
																			
																		});
								
								jQuery('.container-thumbnail').find('#btn-close-thumbnail').click(function(){
								
									var currentCatID = jQuery('#currentProductID').val();
									//changeImageAuto(currentCatID);
											
									jQuery('.container-thumbnail').animate({
																			'z-index': 99999,
																			'opacity': 0
																			}, 300).queue(function() {
																				jQuery(this).css({
																									'z-index': '-99999',
																									'visibility': 'hidden',
																								});
																				pause = false;
																				changeImageAuto(currentCatID);
																				
																				jQuery(this).dequeue();
																				
																			});
								/*
									fadeInPopup(el);
									var elCurrent = el.find('input').val();
									
									el.mouseenter(function() {
																jQuery(this).find('.preview-description').fadeIn(400);
																stopSlideshow(el);
															});
									el.mouseleave(function() {
																jQuery(this).find('.preview-description').fadeOut(400);
																playSlideshow(el, elCurrent);
															});
									
									
									
								*/
								});
							});
							jQuery('#thumb-'+nextImageID).animate({'opacity': 1}, 900).queue(function(){
								
							
								jQuery(this).css({'cursor': 'pointer'});
								jQuery(this).click(function(){
									
									jQuery('.container-thumbnail').animate({
																			'z-index': '99999',
																			'visibility': 'visible',
																			'opacity': 0
																		}, 600).queue(function() {
																			jQuery(this).css({
																				'z-index':'-99999',
																				'visibility': 'hidden'
																			});
																			jQuery(this).dequeue();
																			
																		});
									
									
									var elCurrent = jQuery(this).find('input').val();
									
									currentProductID.val(elCurrent);
									pause = false;
									changeImageAuto(elCurrent);
									
									
								});
							
								jQuery(this).dequeue();
								
							});
							i_next++;
							loadOtherImage(i_next);
							
						}).error(function(){
							//alert('erreur loading image');
							loadOtherImage(i_next);
						}).attr({
							'id': 'img-'+i_next,
							'src': jsonObj.url,
							'height': jsonObj.height,
							'width': jsonObj.width,
							'alt': jsonObj.name,
							'title': jsonObj.name
						});
						
					});
				
				}
				
		   }
	 });
	
}

function fadeInPopup(el){
	el.css({
			'visibility': 'visible',
		 	'z-index': '99999',
			'opacity': 0
		 })
	.animate({
			'opacity': 1
			}, 400);
}

function fadeOutPopup(el){
	el.animate({
			'opacity': 0,
		 	'z-index': '-99999'
		}, 400).queue(function(){
			jQuery(this).css({
					'visibility': 'hidden'
					});
			jQuery(this).dequeue();
		});
}

var pause = false;

function stopSlideshow(el){
	el.stop(true, true);
	el.find('.loadbar').stop(true, true);
	el.find('.mini-loader').css({'opacity': 0});
	pause = true;
}
function transLoadImage(i){
	var el = jQuery('#popup-'+i+'');
	playSlideshow(el, i);
}

function playSlideshow(el, i){
	var currentCatID = jQuery('#currentCatID').val();
	var currentProductID = jQuery('#currentProductID');
	var currentProductIDval = currentProductID.val();
	
	el.find('.mini-loader').css({'opacity': 1});
	el.find('.loadbar').css({'width': '0%'}).animate({
		'width': '100%'
	}, 4000).queue(function(){
		
		jQuery(this).parent().remove();
		el.animate({
			'z-index': '-99999',
			'opacity': 0
		}, 400).queue(function() {
			jQuery(this).css({
				'visibility': 'hidden'
			});
			jQuery(this).dequeue();
		});
		
		var objThumbList = jQuery('#thumb-list').find('.thumb-pict');
	
		if(i == objThumbList.length){
		
			currentProductID.val(1);
			changeImageAuto(1);
		
		} else {
			
			if(pause === false){
				i = new Number(i) + 1;
			} else {
				pause = false;
			}
			
			currentProductID.val(i);
			changeImageAuto(i);
		}
		
		jQuery(this).dequeue();
	});	
	
}

function changeImageAuto(i){
	
	var el = jQuery('#popup-'+i);
	
	fadeInPopup(el);
	
	if(el.find('.mini-loader').html() == null){
		
		el.append('<div class="mini-loader"><div class="loadbar"></div></div>');
		el.find('.mini-loader').css({
										'position': 'relative',
										'margin-top': 20,
										'margin-left': (el.find('img').width() / 2) - (100)
									});
	}
	
	el.find('.preview-description').fadeOut(400);
	el.mouseenter(function() {
							jQuery(this).find('.preview-description').fadeIn(400);
							stopSlideshow(el);
						});
	el.mouseleave(function() {
							jQuery(this).find('.preview-description').fadeOut(400);
							playSlideshow(el, i);
						});
	
	playSlideshow(el, i);
}

function open_link(url){
	window.location = url;
}
