doneprice=0; donecarat=0;
function setCook(nom,valeur) {
        document.cookie = nom + "=" + escape(valeur)
        }
function getCook(nom) {
        deb = document.cookie.indexOf(nom + "=")
        if (deb >= 0) {
            deb += nom.length + 1
            fin = document.cookie.indexOf(";",deb)
            if (fin < 0) fin = document.cookie.length
            return unescape(document.cookie.substring(deb,fin))
            }
        return ""
        }
function delCook(nom) { setCook2(nom,"",-1) }
function setCook2(nom,valeur,jours) {
        var expDate = new Date()
        expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000))
        document.cookie = nom + "=" + escape(valeur)
            + ";expires=" + expDate.toGMTString()
        }
Math.log10 = function(n){ return this.log(n)/this.log(10); }
function InitSlider(lib,deb,fin,setDeb,setFin,setEntier,setLoga,tauxEuroDollar,setSnap,setNumStep) {
	var setDeb2 = getCook(lib+'_setDeb'); if (setDeb2 != '') {setDeb2 = setDeb2-1+1;  setDeb=setDeb2; }
	var setFin2 = getCook(lib+'_setFin'); if (setFin2 != '') {setFin2 = setFin2-1+1;  setFin=setFin2; }
	
	window.addEvent('domready', function(){
		var mySlideA = new Slider($('slider_'+lib+'_gutter_m'), $('slider_'+lib+'_minKnobA'),$('slider_'+lib+'_bkg'), {
																							   
			start: deb,
			end: fin,
			offset:6,
			entier:setEntier,
			loga:setLoga,
			range:true,
			snap:setSnap,
			numsteps:setNumStep,
			onChange: function(pos){
				
				if(this.options.loga == true ) {
	
					var minpospx = parseInt($('slider_'+lib+'_minKnobA').getStyle('left'))+this.options.offset;
					var maxpospx = getCook(lib+'_maxpospx');if (maxpospx != ''){
							maxpospx -=this.options.offset; 
							$('slider_'+lib+'_maxKnobA').setStyle('left', maxpospx+'px');
							delCook(lib+'_maxpospx');
							//maxpospx +=this.options.offset; 
							//maxpospx +=this.options.offset; 
							}
					var maxpospx = parseInt($('slider_'+lib+'_maxKnobA').getStyle('left'))+this.options.offset;
					var minposValue = this.options.start;
					var maxposValue = this.options.end;
					var baselog = 10; 
					var longueur = this.full ;
					var largeurlog = custLog(maxposValue,baselog) - custLog(minposValue,baselog) ;
					
					if (lib == 'price' && doneprice<=3) { 
					doneprice++;
					var val = getCook('input_price_min_manu');  if (val != '') { val = Math.log10(val); minpospx = (((val -  custLog(minposValue,baselog))*longueur)/largeurlog); minpospx2 = minpospx  -this.options.offset; $('slider_'+lib+'_minKnobA').setStyle('left', minpospx2+'px'); }
					var val = getCook('input_price_max_manu');  if (val != '') { val = Math.log10(val); maxpospx = (((val -  custLog(minposValue,baselog))*longueur)/largeurlog); maxpospx2 = maxpospx  -this.options.offset; $('slider_'+lib+'_maxKnobA').setStyle('left', maxpospx2+'px'); }
					if (doneprice==3) {delCook('input_price_min_manu'); delCook('input_price_max_manu');}
					}
					if (lib == 'carat' && donecarat<=3) { 
					donecarat++;
					var val = getCook('input_carat_min_manu'); val=val*100;  if (val != '') { val = Math.log10(val); minpospx = (((val -  custLog(minposValue,baselog))*longueur)/largeurlog); minpospx2 = minpospx  -this.options.offset; $('slider_'+lib+'_minKnobA').setStyle('left', minpospx2+'px'); }
					var val = getCook('input_carat_max_manu'); val=val*100;  if (val != '') { val = Math.log10(val); maxpospx = (((val -  custLog(minposValue,baselog))*longueur)/largeurlog); maxpospx2 = maxpospx  -this.options.offset; $('slider_'+lib+'_maxKnobA').setStyle('left', maxpospx2+'px'); }
					if (donecarat==3) {delCook('input_carat_min_manu'); delCook('input_carat_max_manu');}
					}

					tmpprix1 = ((minpospx * largeurlog) / longueur) + custLog(minposValue,baselog) ;
					prix1 = Math.pow(baselog,tmpprix1);
					
					tmpprix2 = ((maxpospx * largeurlog) / longueur) + custLog(minposValue,baselog) ;
					prix2 = Math.pow(baselog,tmpprix2);
					
					if(this.options.entier == false) {
						prix1 = prix1 / 100;
						prix2 = prix2 / 100;
						prix1 = prix1.toFixed(2);
						prix2 = prix2.toFixed(2);
						
					}else{
						prix1 = Math.ceil(prix1);
						prix2 = Math.ceil(prix2);
					}
				}else{
				
					if(this.options.entier == false) {
						prix1 = pos.minpos / 100;
						prix2 = pos.maxpos / 100;
						prix1 = prix1.toFixed(2);
						prix2 = prix2.toFixed(2);
						
					}else{
						prix1 = pos.minpos;
						prix2 = pos.maxpos;
					}
				}
				
				
				$('input_'+lib+'_min').value = prix1;	
				$('input_'+lib+'_max').value = prix2;
				
				
				
				var langue = Cookie.read("langue");
				if (langue=="EN" && lib == "price") {
					var prix1 =	convert(prix1);
					var prix2 =	convert(prix2);
				}
				
				$('slider_'+lib+'_min').set('html', prix1);	
				$('slider_'+lib+'_max').set('html', prix2);


			},
			onComplete: function(pos){
				if (pos.minpos && lib && pos.maxpos) {
				setCook(lib+'_setDeb',pos.minpos);
				setCook(lib+'_setFin',pos.maxpos);
				var maxpospx = parseInt($('slider_'+lib+'_maxKnobA').getStyle('left'))+this.options.offset;
				if (maxpospx != 222) setCook(lib+'_maxpospx',maxpospx);
				//alert(maxpospx);
				}
				
				
				//à l'init du dernier slider on met la secu à 1 pour run l'ajax
				if(lib == 'clarity') {
					$("lestRockAjax").value=1;
				}
				if($("lestRockAjax").get('value')==1) {
					$("pagenumber").value = '1';
					doRequest('recherche');
				}
			}
		}, $('slider_'+lib+'_maxKnobA')).setMin(setDeb).setMax(setFin);		
	});
}

function custLog(x,base) {
	return (Math.log(x))/(Math.log(base));
}

//fonction qui scan la page pour les squeeze box apres un chargement ajax
function loadSqueezeDiams() {
	//SqueezeBox texte Pour les diams     
	window.addEvent('domready', function() {
		SqueezeBox.assign($$('a.SqueezeBoxDiams'), {
			parse: 'rel'
		});
	});
}

// slimbox image
// prend tout les lien avec un rel=lightbox
function loadSlimDiams() {

	Slimbox.scanPage = function() {
		$$(document.links).filter(function(el) {
			return el.rel && el.rel.test(/^lightbox/i);
		}).slimbox({/* Put custom options here */}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
	};
	window.addEvent("domready", Slimbox.scanPage);
}




//fonction pour la définition manuel des valeur du slider

function lunchManuSeek () {
	var input_price_min_manu =$('input_price_min_manu').value; setCook('input_price_min_manu',input_price_min_manu); 
	var input_price_max_manu =$('input_price_max_manu').value; setCook('input_price_max_manu',input_price_max_manu);
	var input_carat_min_manu =$('input_carat_min_manu').value; setCook('input_carat_min_manu',input_carat_min_manu);
	var input_carat_max_manu =$('input_carat_max_manu').value; setCook('input_carat_max_manu',input_carat_max_manu);
	var langue = Cookie.read("langue");
	
	
	if(input_price_min_manu != "" && input_price_max_manu == "") {
		$('input_price_min').value = input_price_min_manu;
		if (langue=="EN") {
			var input_price_min_manu =	convert(input_price_min_manu);
		}
		$('slider_price_min').set('html', input_price_min_manu);	
	}
	if(input_price_max_manu != "" && input_price_min_manu == "") {
		$('input_price_max').value = input_price_max_manu;
		if (langue=="EN") {
			var input_price_max_manu =	convert(input_price_max_manu);
		}
		$('slider_price_max').set('html', input_price_max_manu);
	}
	if(input_price_min_manu != "" && input_price_max_manu != "" && input_price_min_manu <= input_price_max_manu) {
		$('input_price_min').value = input_price_min_manu;
		$('input_price_max').value = input_price_max_manu;
		
		if (langue=="EN") {
			var input_price_min_manu =	convert(input_price_min_manu);
			var input_price_max_manu =	convert(input_price_max_manu);
		}
		$('slider_price_min').set('html', input_price_min_manu);	
		$('slider_price_max').set('html', input_price_max_manu);
	}
	
	if(input_carat_min_manu != "" && input_carat_max_manu == "") {
		$('input_carat_min').value = input_carat_min_manu;
		$('slider_carat_min').set('html', input_carat_min_manu);	
	}
	if(input_carat_max_manu != "" && input_carat_min_manu == "") {
		$('input_carat_max').value = input_carat_max_manu;
		$('slider_carat_max').set('html', input_carat_max_manu);
	}
	
	if(input_carat_min_manu != "" && input_carat_max_manu != "" && input_carat_min_manu <= input_carat_max_manu) {
		$('input_carat_min').value = input_carat_min_manu;
		$('slider_carat_min').set('html', input_carat_min_manu);
		$('input_carat_max').value = input_carat_max_manu;
		$('slider_carat_max').set('html', input_carat_max_manu);
	}

	doRequest('recherche');
}

window.addEvent('domready', function(){
	var myTimer = "";	
	var input_manu = $$('#input_price_min_manu','#input_price_max_manu','#input_carat_min_manu','#input_carat_max_manu');
	
	var val = getCook('input_price_min_manu'); if (val !='') {document.getElementById('input_price_min_manu').value = val;  } 
	var val = getCook('input_price_max_manu'); if (val !='') {document.getElementById('input_price_max_manu').value = val;  }
	var val = getCook('input_carat_min_manu'); if (val !='') {document.getElementById('input_carat_min_manu').value = val;  }
	var val = getCook('input_carat_max_manu'); if (val !='') {document.getElementById('input_carat_max_manu').value = val;  }
	
	
	if(input_manu) {

		input_manu.each(function(item){
								 					 
			item.addEvents({
				'keyup': function(event){
					
					//sécu pour remise à zéro des champs
					var val = "";
					if(item.value == ""){
						switch (item.get('id')) {
							case 'input_price_min_manu':
								var val = $('save_min_price').value;
								$('input_price_min').value = val;
								$('slider_price_min').set('html', val);
							break;
							case 'input_price_max_manu':
								var val = $('save_max_price').value;
								$('input_price_max').value = val;
								$('slider_price_max').set('html', val);
							break;
							case 'input_carat_min_manu':
								var val = $('save_min_carat').value;
								$('input_carat_min').value = val;
								$('slider_carat_min').set('html', val);
							break;
							case 'input_carat_max_manu':
								var val = $('save_max_carat').value;
								$('input_carat_max').value = val;
								$('slider_carat_max').set('html', val);
							break;
						}
					}
					
					if(event.key == 'enter') {
						$clear(myTimer);
						lunchManuSeek();
					}else if(IsNumeric(item.value)){	
						$clear(myTimer);
						myTimer = lunchManuSeek.delay(1500);
					}else{
						$clear(myTimer);
						item.value = item.value.substring(0,(item.value.length-1));
					}
				}
			});
		});
	}
});




function ficheDiamantaffDef(name,id){
	$('title_'+name).set('html','<i>'+$('name_'+name+'_'+id).innerHTML+'</i>');
	$('desc_'+name).setStyles({'width': '265px'});
	$('desc_'+name).setStyles({'padding-bottom': '20px'});
	$('desc_'+name).set('html',$('desc_'+name+'_'+id).value);
}

function ficheDiamantcacheDef(name,id,oldId){
	$('title_'+name).set('html','<i>'+$('name_'+name+'_'+oldId).innerHTML+'</i>');
	$('desc_'+name).setStyles({'width': '265px'});
	$('desc_'+name).setStyles({'padding-bottom': '20px'});
	$('desc_'+name).set('html',$('desc_'+name+'_'+oldId).value);
}


function remplirSlid(id){
	var langue = Cookie.read("langue");
	if (langue=="FR") {
		var txtBague = "&Agrave; ma bague";
		var txtCollier = "&Agrave; mon collier";
		var txtBoucle = "&Agrave; mes boucles d\'oreilles";
		var txtPanier = "&Agrave; mon panier";
	}else{
		var txtBague = "To your ring";
		var txtCollier = "To your pendant";
		var txtBoucle = "To your studs";
		var txtPanier = "To your shopping bag";
	}
	
	if($('slidingProduct').innerHTML==""){
		$('slidingProduct').set('html','<a href="#" onclick="ajaxAddProduct2(\''+id+'\',\'2\',\'0\');">'+txtBague+'</a><br><a onclick="ajaxAddProduct2(\''+id+'\',\'2\',\'1\');" href="#" >'+txtCollier+'</a><br><a href="#" onclick="ajaxAddProduct2(\''+id+'\',\'2\',\'2\');">'+txtBoucle+'</a>');
		//<br><a href="#" onclick="ajaxAddProduct2(\''+id+'\',\'2\',\'3\');">'+txtPanier+'</a>');	
		$('slidingProduct').setStyles({'display': 'block'});
	}else{
		$('slidingProduct').set('html','');
		$('slidingProduct').setStyles({'display': 'none'});
	}
}

//fonction sur la recherche de diamants
function goTry(champ) {
	
	var tri = $("tri").value; 
	if(champ == tri) {
		var reg = new RegExp("(asc)", "i");
		var newTri = tri.replace(reg,"desc");
		$("tri").value = newTri;
	}else{
		$("tri").value = champ;
	}
	doRequest('recherche');
}

function goPerPage(nbPerPage, host) {
	var myCookie = Cookie.write('perPageDiamantCook', nbPerPage ,{path: '/'} , {domain: host},{duration: 3600000});
	$("pagenumber").value = '1';
	doRequest('recherche');
}

function goChangePage(page) {
	$("pagenumber").value = page;
	doRequest('recherche');
}

function closeCompaDiv(host){ 
	$('comparaison_cont').setStyles({'display': 'none'});
	var myCookie = Cookie.write('listediamantids', '' ,{path: '/'});
}

function checkForme(div){
	window.addEvent('domready', function(){
		myId = div.substring(11);
		if($('forme_'+myId).value==0) {
			$('forme_'+myId).value=1;
			var oldImg = $(div).getProperty('src');
			var reg = new RegExp("(white)", "i");
			var newImg = oldImg.replace(reg,"grey");
			$(div).setProperty('src', newImg);
			doRequest('recherche');
		}else{
			$('forme_'+myId).value=0;
			var oldImg = $(div).getProperty('src');
			var reg = new RegExp("(grey)", "i");
			var newImg = oldImg.replace(reg,"white");
			$(div).setProperty('src', newImg);
			doRequest('recherche');
		}
	});
}

function checkTheForme(){
	window.addEvent('domready', function(){
		var inputsCheck = $$('.inputForme');
		inputsCheck.each(function(item){
			var myId = item.get('id');
			myId = myId.substring(6);
			
			if(item.value==0) {
				var oldImg = $('formeImage_'+myId).getProperty('src');
				var reg = new RegExp("(grey)", "i");
				var newImg = oldImg.replace(reg,"white");
				$('formeImage_'+myId).setProperty('src', newImg);		
			}else{
				var oldImg = $('formeImage_'+myId).getProperty('src');
				var reg = new RegExp("(white)", "i");
				var newImg = oldImg.replace(reg,"grey");
				$('formeImage_'+myId).setProperty('src', newImg);		
			}
		});
	});
}

function getForme(){
	window.addEvent('domready', function(){
		var tableForme="";
		var inputs = $$('.inputForme');
		inputs.each(function(item){
			if(item.value==1) {
				var myId = item.get('id');
				myId = myId.substring(6);
				tableForme += ','+myId ;
			}
		});
		return tableForme;
	});
}

function fondbleu(diamantid) {
	
	$('tddiamantg'+diamantid).set('class','trdiamantgbleu') ;
	$('tddiamantm1'+diamantid).set('class','trdiamantmbleu') ;
	$('tddiamantm2'+diamantid).set('class','trdiamantmbleu') ;
	$('tddiamantm3'+diamantid).set('class','trdiamantmbleu') ;
	$('tddiamantm4'+diamantid).set('class','trdiamantmbleu') ;
	$('tddiamantm5'+diamantid).set('class','trdiamantmbleu') ;
	$('tddiamantm6'+diamantid).set('class','trdiamantmbleu') ;
	$('tddiamantm7'+diamantid).set('class','trdiamantmbleu') ;
	$('tddiamantd'+diamantid).set('class','trdiamantdbleu') ;
	
	var oldImg = $('tddiamantmImg1_'+diamantid).getProperty('src');
	var reg = new RegExp("(white)", "i");
	var newImg = oldImg.replace(reg,"grey");
	$('tddiamantmImg1_'+diamantid).setProperty('src', newImg);
	
}

function fondbase(diamantid, row) {
	
	$('tddiamantg'+diamantid).set('class','trdiamantg'+row) ;
	$('tddiamantm1'+diamantid).set('class','trdiamantm'+row) ;
	$('tddiamantm2'+diamantid).set('class','trdiamantm'+row) ;
	$('tddiamantm3'+diamantid).set('class','trdiamantm'+row) ;
	$('tddiamantm4'+diamantid).set('class','trdiamantm'+row) ;
	$('tddiamantm5'+diamantid).set('class','trdiamantm'+row) ;
	$('tddiamantm6'+diamantid).set('class','trdiamantm'+row) ;
	$('tddiamantm7'+diamantid).set('class','trdiamantm'+row) ;
	$('tddiamantd'+diamantid).set('class','trdiamantd'+row) ;
	
	var oldImg = $('tddiamantmImg1_'+diamantid).getProperty('src');
	var reg = new RegExp("(grey)", "i");
	var newImg = oldImg.replace(reg,"white");
	$('tddiamantmImg1_'+diamantid).setProperty('src', newImg);
	
}

