function initWindowLoad(){
	$('.content_image_lang').each(function(){
		$(this).height($(this).children('img').height());
	});
}

function initDomReady(){
	$('#product_thickbox_filter').click(function(){ closeProductThickbox(); });
	$('#pre_panier_filter').click(function(){ closePrePanier(); });
	$('#share_with_friend_filter').click(function(){ closeShareWithFriend(); });
	$('#ici_relais_filter').click(function(){ closeIciRelais(); });
	
	/* SELECT */
	$('.select_bg').click(function(){ selectCustom( $(this).parent().attr('id')); });
	$('.select_bg', $('#attributes')).each(function(){ choiceSelectCustom($(this).parent().attr('id')); });
	/* ****** */
	
	/* SEARCH LIST */
	if($('#affinage_search > .content_list > .list').length > 0){
		var heightList = 0;
		$('#affinage_search > .content_list > .list:last').addClass('last_list');
		$('#affinage_search > .content_list > .list').each(function(){
		   if($(this).height() > heightList)
			   heightList = $(this).height();
		});
		$('#affinage_search > .content_list > .list').height( heightList);
		$('#affinage_search > .content_list > .list').width(Math.floor(($('.content_list').width()-$('#affinage_search > .content_list > .list').length)/$('#affinage_search > .content_list > .list').length)-2);
		
		$('#affinage_search > .content_list > .list > li.selected').click(function(){
			document.location.href = baseDir + $(this).children('a').attr('href');	
		});
	}
	/* ****** */
	
	if( $('input[name="id_carrier"]').length > 0 ){
		$('input[name="id_carrier"]:checked').parent().parent().children('td').css('background', '#ffcc05');
			
		$('input[name="id_carrier"]').change(function(){
			$('input[name="id_carrier"]').parent().parent().children('td').css('background', '');
			$('input[name="id_carrier"]:checked').parent().parent().children('td').css('background', '#ffcc05');
		});
	}
	
	$('#cart_block > .block_content').hover(function(){
		$(this).addClass('block_content_hover');
	}, function(){
		$(this).removeClass('block_content_hover');
	});
	
	if( $('#short_description_content').length > 0 )
		$('#garanties_product').css('top', $('#short_description_content').height()+$('#average_product').height()+30 );
	else
		$('#garanties_product').css('bottom', 0);
	
	/**** MISE EN PLACE DU MENU ******/
	var COLMARGIN = 25; 
	var COLWIDTH = 146; 
	var columns = 5;
	
	$('.subCategory').css('display', 'block');
    $('.subcategories_content').css('position', 'relative');
	$('.subcategories_content > ul').css('position', 'absolute').css('width', COLWIDTH  + 'px');
 
	var compteurBox = 0;
	
	$('.subcategories_content').each(function() {
		var pointer = 0;
		var compteur = 0;
		var arr = [];
		var max_height = 0;
		$(this).children('ul').each(function() {
				
				compteur++;
				var tempTop;
				var tempLeft;
			
				if (compteur <= columns) { 
					
					tempTop = 0;
					arr[pointer] = $(this).height()+COLMARGIN;
					tempLeft = (pointer * (COLWIDTH));
					max_height = Math.max(max_height,$(this).height()+COLMARGIN);
					pointer++;
					
				}else{
					
					tempTop = arr[0] ;
					indice = 0;
					for(var i = 1 ; i  < arr.length ; i++){
						if(tempTop > arr[i]){
							tempTop = arr[i];
							indice = i;
						}
					}
					tempLeft = (indice * (COLWIDTH));
					arr[indice] += $(this).height()+COLMARGIN;
					max_height = Math.max(max_height,arr[indice]);
				
				}
				
				$(this).css('top', tempTop + 'px');
				$(this).css('left', tempLeft + 'px');
				 
				
				if (pointer === columns) { pointer = 0; }
		
		});
		$(this).height(max_height+'px');
	});
    $('.subCategory').css('display', 'none');

	
}

$(window).load(function(){ initWindowLoad(); });
$(document).ready(function(){ initDomReady(); });


/* MINI FICHE PRODUIT */
function openProductThickbox(id_product)
{
	closeAllThickbox();
	$('#product_thickbox_filter, #product_thickbox_filter > img').show();
	$.ajax({
		type: "GET",
		url: baseDir+"product-thickbox.php",
		data: "id_product="+id_product,
		success: function(response){
			$('#product_thickbox > #product_thickbox_content').html(response);
			$(document).scrollTop(0);
			initDomReady();
			initProduct();
			tb_init('a.thickbox');
			$('#product_thickbox').show();
			$('#product_thickbox_filter > img').hide();
		}
	});
}

function closeProductThickbox()
{
	$('#product_thickbox').fadeOut('fast', function(){$('#product_thickbox_filter').hide()});
	$('#product_thickbox > #product_thickbox_content').html('<br />');
}
/* ****************************** */

/* PRE-PANIER */
function openPrePanier(idProduct)
{
	closeAllThickbox();
	$('#pre_panier_filter, #pre_panier_filter > img').show();
	$.ajax({
		type: "GET",
		url: baseDir+"pre-panier.php",
		data: "content_only=1&id_product="+idProduct,
		success: function(response){
			$('#pre_panier > #pre_panier_content').html(response);
			$(document).scrollTop(0);
			initDomReady();
			initProduct();
			tb_init('a.thickbox');
			$('#pre_panier').show();
			$('#pre_panier_filter > img').hide();
		}
	});
}

function closePrePanier()
{
	$('#pre_panier').fadeOut('fast', function(){$('#pre_panier_filter').hide()});
	$('#pre_panier > #pre_panier_content').html('<br />');
}
/* ****************************** */


/* PRE-PANIER */
function openShareWithFriend(id_product)
{
	closeAllThickbox();
	$('#share_with_friend_filter, #share_with_friend_filter > img').show();
	$.ajax({
		type: "GET",
		url: baseDir+"share_with_friend.php",
		data: "content_only=1&id_product="+id_product,
		success: function(response){			
			$('#share_with_friend > #share_with_friend_content').html(response);
			$(document).scrollTop(0);
			initDomReady();
			initProduct();
			tb_init('a.thickbox');
			$('#share_with_friend').show();
			$('#share_with_friend_filter > img').hide();
		}
	});
}

function closeShareWithFriend()
{
	$('#share_with_friend').fadeOut('fast', function(){$('#share_with_friend_filter').hide()});
	$('#share_with_friend > #share_with_friend_content').html('<br />');
}
/* ****************************** */


/* ICI RELAIS */
function openIciRelais(html)
{
	closeAllThickbox();
	$('#ici_relais_filter, #ici_relais_filter > img').show();
	$('#ici_relais > #ici_relais_content').children(".contenu").append(html);
	$(document).scrollTop(0);
	$('#ici_relais').show();
	$('#ici_relais_filter > img').hide();
}

function closeIciRelais()
{
	$('#ici_relais').fadeOut('fast', function(){$('#ici_relais_filter').hide()});
}
/* ****************************** */

function closeAllThickbox()
{
	closeProductThickbox();
	closePrePanier();
	closeShareWithFriend();
}



/* SELECT CUSTOM FUNCTION
********************************** */
function selectCustom(id)
{
	$('#'+id+' > select > option').click(function(){
		choiceSelectCustom(id)
		$('#'+id+' > select').hide();
	});
	var optionNumber = $('#'+id+' > select > option').length;
	if( $('#'+id+' > select:hidden').length > 0){
		if( optionNumber == 1 ){
			$('#'+id+' > select').attr('size', 2).show();
		}else if( optionNumber < 12 ){
			$('#'+id+' > select').attr('size', optionNumber).show();
		}else{
			$('#'+id+' > select').attr('size', 12).show();
		}
		$('#'+id+' > select').focus();
	}else{
		$('#'+id+' > select').hide();
	}
}
/* ******************************* */

/* SELECT CUSTOM FUNCTION ONCHANGE 
*********************************** */
function choiceSelectCustom(id)
{
	var selectValue = $('#'+id+' > select > option:selected').text();
	$('#'+id+' > .select_bg').text(selectValue);
	//$('.'+className+' > select').hide();
}
/* ******************************** */


function changeListStyle()
{
	if($('#product_list').length > 0){
		if($('#product_list').hasClass('line_style')){
			$('#product_list').removeClass('line_style');
			$('.choice_list_style').removeClass('line_style');
			$.get(baseDir+"change_style_list.php", {list_style:"list"} );
		}else{
			$('#product_list').addClass('line_style');
			$('.choice_list_style').addClass('line_style');
			$.get(baseDir+"change_style_list.php", {list_style:"line_style"} );
		}
	}
}


$(function(){
    $('ul.navigation_categorie>li').hover(
    function(){
        $(this).css('background', '#666');
    },
    function(){
        $(this).css('background', 'none');
    });
});
