// Slider homepage

var timer_slider = null;
var timer_slider_delay = 10000;
var total_slider_element;
var current_slide = 0;

function initWindowLoad(){
}

function initDomReady(){
	
	//////////// CUFON
	cufonAll();
	////////////////////////////////////////////////////////
	
	/**** MISE EN PLACE DU SOUS MENU ******/
	var COLMARGIN = 25; 
	var COLWIDTH = 224; 
	var columns = 4;
	$('.sub-child').css('display', 'block');
	$('.sub-child').css('visibility', 'hidden');
    $('.sub-child').css('position', 'absolute');
	$('.sub-child > ul').css('position', 'absolute').css('width', COLWIDTH  + 'px');
	$('.sub-child > ul#block_push').css('width', '96%');
	
 
	var compteurBox = 0;
	
	$('.sub-child').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+20 + 'px');
				$(this).css('left', tempLeft+20 + 'px');
				 
				
				if (pointer === columns) { pointer = 0; }
		
		});
		$(this).height(max_height+35 +'px');
		$(this).parent().find('.block_push').css('top' , max_height+70 +'px')
	});
	
    $('.sub-child').css('display', 'none');
	$('.sub-child').css('visibility', 'visible');
	//$('#page #menu li.item_0 .sub-child').css('display', 'block');
	
	$('#menu > ul > li').hover(function(){
		$(this).find('.sub-child').css('display', 'block');
		$(this).find('.block_push').css('display', 'block');
		
		if(!$(this).find('.sub-child ul').length){
			$(this).find('.sub-child').css('display', 'none');
			$(this).find('.block_push').css('display', 'none');		
			}
		
		},function(){
		$(this).find('.sub-child').css('display', 'none');
		$(this).find('.block_push').css('display', 'none');	
		})
	
	
}

$(window).load(function(){ initWindowLoad();

	//////////// SLIDER HOME
	if($('#gallery-home').length > 0){
		$('#gallery-home').slideView({toolTip: false, ttOpacity: 0.7});
		$('.stripTransmitter > ul > li > a').each(function(j){ 
			$(this).click(function(){
				current_slide = j;
				});
			});
		timer_slider = setInterval('display_next_home_slider_title();',timer_slider_delay);
	}
	////////////////////////////////////////////////////////	
});

$(document).ready(function(){ initDomReady();

	
	$('.sub-child-ambiance').css('height', '255px');
	$('.sub-child-ambiance ul').css({position: 'relative', left: '0px', top: '25px', width: '980px'});
	$('.item_ambiance .block_push').css('top', '285px')
	
	
	

	
	
	
	//////////// CUFON HOVER MENU
	$('li.category').hover(function(){
	Cufon.replace($(this).find('> a.museo500'), { color: 'white', fontFamily: 'museo500' });
	}, function(){
	Cufon.replace($(this).find('> a.museo500'), { color: '#918574', fontFamily: 'museo500' });	
	})
	////////////////////////////////////////////////////////
	
	
	
	//////////// SPLIT PRODUCT PRICE
	splitPrice();
	////////////////////////////////////////////////////////
	
	
	
	//////////// PRODUCT K font-size auto
	if( $('.productK_price .before_comma').length > 0 ){
		if( parseInt($('.productK_price .before_comma').width()) > 64 ){
			$('.productK_price .devise').css({'font-size' : '16px', 'margin-top' : '0px'});
			changeSizePrice();
		}
	}
	////////////////////////////////////////////////////////
	
	
	
	//////////// Product-list font-size auto
	
	changeSizeH3();
	/*window.setTimeout(function(){
		changeSizeH3();
	}, 2000);*/
	
	////////////////////////////////////////////////////////



	//////////// Centrage pagination
	var paginationWidth = $('ul.pagination').width();
	if($('body').attr('id')=='category'){
		var widthP = 290;
	}
	else {
		var widthP = 500;
	}
	var marginLeft = (widthP - paginationWidth)/2
	$('ul.pagination').css('margin-left', marginLeft);
	////////////////////////////////////////////////////////
	
	
	
	//////////// SELECT BOX
	$(".selectCustom").selectbox();
	$('.customSelect2 .jquery-selectbox, .customSelect2 .jquery-selectbox-list').each(function(){
		$(this).css('width', '208px')
	})
	$('.customSelect3 .jquery-selectbox, .customSelect3 .jquery-selectbox-list').each(function(){
		$(this).css('width', '308px')
	})
	$('#select_radius .jquery-selectbox, #select_radius .jquery-selectbox-list').each(function(){
		$(this).css('width', '80px')
	})
	////////////////////////////////////////////////////////
	
	
	//////////// Suppresion bordure bas
	var height = $('#product_list').height();
	$('#product_list').css('height', (height-1)+'px');
	////////////////////////////////////////////////////////	
	
	
	/*$("#mon8select option").each(f(){
		if($(this).val()==$("#mon8select2 option:seleted").val()){
			(this).attr('selected', 'selected')
		}
	});*/
	
	
	
 });
 
function changeNb_item(myValue){
			
			$('.nb_item').each(function(){
				//$(this).children('option').attr('selected', false);
				$(this).children().each(function() {
				if($(this).val()==myValue) {
					$(this).attr('selected', 'selected')
					}
				})
			});
		if($('body').attr('id')=='category'){
			reloadContent();
		}
		else {
			$('#form_n').submit();
			$("html").animate({scrollTop:0}, 1000);
		}
}


function cufonAll() {
	Cufon.replace('.museo300', { color:'white',fontFamily: 'museo300' });
	Cufon.replace('.museo500', { fontFamily: 'museo500' });
	Cufon.replace('.museo700', { color:'white',fontFamily: 'museo700' });
	Cufon.replace('.museo700_brown', { color:'#867152',fontFamily: 'museo700' });
	Cufon.replace('.ags400', { color:'#ec732c',fontFamily: 'AG_Book_Stencil' });
	
}


//////////// DECOUPE PRIX EN 3 PARTIES POUR STYLISATION QUI TUE !
function splitPrice(){
	
	$('.homeproducts_price_temp, .old_price_temp, .productK_price_temp, .product_list_price_temp, .product_price_temp, #old_price_display').each(function(){
		
	// Initialisation
	var beforeComma = '';
	var afterComma = '';
	var lastChar = '';	
		
	//Suppression des espaces
	var price = jQuery.trim($(this).text());
	
	//Récupération de la devise (dernier caractère)
	var lastChar = price.substr(price.length - 1);
	
	//Suppression de la devise + l'espace vide avant le split
	var price = price.substring(0, price.length-2);
			
	//Split du prix afin de le skinner
	//Récuperation valeurs avant et apres la virgule
	var exp=new RegExp(",");
	var tabPrice=price.split(exp);
	if (tabPrice==null) {}
	else {
		//var affichage="Nombre d'elements = " + tabPrice.length;
		//for (var i=0;i<tabPrice.length;i++){
		//  affichage=affichage + " ["+ tabPrice[i] + "]";  
		//}
	//console.log(affichage);
	
	//Affectation des valeurs
	if(tabPrice[0])
		var beforeComma = "<span class='before_comma'>"+tabPrice[0]+"</span>";
	if(tabPrice[1])
		var afterComma = "<span class='after_comma'>,"+tabPrice[1]+"</span>";
	if(lastChar)
		var lastChar = "<span class='devise'>"+lastChar+"</span>";
		
	//Remplacement de la balise contenant le prix 
	$(this).replaceWith(beforeComma+afterComma+lastChar);
  	}
	});
}

 
function display_next_home_slider_title() {
	var next = parseInt(current_slide)+1;
	if(next > $('#gallery-home > ul > li').length ) {
		current_slide = 0;
	}else{
		current_slide = next;
	}
	$('.stripTransmitter > ul > li:eq('+(current_slide)+') > a').trigger('click');
}

function changeSizePrice(){
	var fontSize = parseInt($('.productK_price .before_comma').css('font-size'));
	$('.productK_price .before_comma').css('font-size', (fontSize-1)+'px');
	
	if( parseInt($('.productK_price .before_comma').width()) > 50 ){
		changeSizePrice();
	}
}
function changeSizeH3(){
	/*var fontSize = parseInt($('.product_list_keria li h3 a').css('font-size'));
	$('.product_list li h3 a, .product_list_keria li h3 a').each(function(){
		if ($(this).height() > 28){
			$(this).css('font-size', (fontSize-1)+'px');
			changeSizeH3();
		}
		else {
			$(this).css('height', '28px');
		}
	})*/
	
	var height_max = 0 ;
	$(".product_name_link").each(function(){
		if($(this).height() > height_max){
			height_max = $(this).height();
		}
	});
	
	$(".product_name_link").each(function(){
		$(this).height(height_max);	
	});
}



// PRODUCT LIST SUPP BORDER-RIGHT
function supprBorder () {
	var limit=0;
	var nbLi = 1;
	
	if ($('body').attr('id')=='category'){
		limit=4;
	}
	else if($('body').attr('id')=='product'){
		limit=200
	}
	else{
		limit=5
	}
	$('#product_list li .product_list_content').each(function(){
		if (nbLi == limit){
			$(this).css('border', 'none');
			nbLi = 0;
		}
		nbLi++;
		})
}


