delai=200000;

function AddCaddieAndGoDoc(id,exp) {
	AddCookie(id);
	window.location = '/decoration/documentations.cfm?src=' + src + '&lang=' + lang + '&exp=' + exp;
}



function AddCaddie(id) {
	AddCookie(id);
	RefreshCaddie();
}

function AddCaddie2(id) {
	AddCookie2(id);
	RefreshCaddie();
}



function isInCaddie(id) {
	var ids = getCookie('CADDIE');
	if (ids == null) return false;
	if (ids.indexOf('_' + id + ',') >= 0) {
		return true;
	}
	else {
		return false;
	}
}



function RemoveCaddie(id) {
	RemoveCookie(id);
	if (document.getElementById('m' + id)) {
		document.getElementById('m' + id).innerHTML = trad_select();
		document.getElementById('m' + id).href = "javascript:AddCaddie('"+id+"');";
	}
	if (document.getElementById('m_tg_' + id)) {
		document.getElementById('m_tg_' + id).innerHTML = trad_select();
		document.getElementById('m_tg_' + id).href = "javascript:AddCaddie('"+id+"');";
	}
	RefreshCaddie();
}


function EmptyCaddie() {
	var ids = getCookie('CADDIE');
	var idSplitted = ids.split(',');
	for (i=0;i<idSplitted.length;i++) {
		var id = idSplitted[i];
		RemoveCookie(id);
		if (document.getElementById('m' + id)) {
			document.getElementById('m' + id).innerHTML = trad_select();
			document.getElementById('m' + id).href = "javascript:AddCaddie('"+id+"');";
		}
		if (document.getElementById('m_tg_' + id)) {
			document.getElementById('m_tg_' + id).innerHTML = trad_select();
			document.getElementById('m_tg_' + id).href = "javascript:AddCaddie('"+id+"');";
		}
	}
	RefreshCaddie();
}


function bidon() {
	;
}

var rCad;

function RefreshCaddie() {
	//clearTimeout("RefreshCaddie()");
	if (rCad != 'undefined')
		clearTimeout(rCad);

	if (document.getElementById('caddie')) {
		var caddie = document.getElementById('caddie');
	}
	if (document.getElementById('bouton_caddie')) {
		var bouton_caddie = document.getElementById('bouton_caddie');
	}
	if (document.getElementById('bouton2_caddie')) {
		var bouton2_caddie = document.getElementById('bouton2_caddie');
	}
	var ids = getCookie('CADDIE');
	var html_caddie = "";//affichage du contenu du caddie : tableau html
	var html_bouton = "";//affichage du bouton sous le caddie : "voir les x produits"
	var html_bouton2 = "";//affichage du contenu sous le caddie : "vider la selection" - pas utilisé
	if (ids != null) {
		html_caddie = "<table border=0 cellspacing=5 cellpadding=0 align=center><tr>";
		var idSplitted = ids.split(',');
		var nbre = idSplitted.length-1;// nombre d'elements dans le caddie
		for (i=0;i<idSplitted.length;i++) {
			var id = idSplitted[i].substring(5);
			var dimx = idSplitted[i].substring(0,2);
			var dimy = idSplitted[i].substring(2,4);
			//var id = idSplitted[i];
			if ((id != '') && (id!=null)) {
				var repertoire = id.substring(0,id.length-3);
				var image1 = new Image();
				image1.src = '/vig/' + repertoire + '/' + id + '.jpg';
				var the_width = dimx;
				var the_height = dimy;
				// ********************************************************
				if (the_width==0 || the_height==0 || the_width=="00" || the_height=="00") {
				// resolution du bug du au temps de chargement de 
				// l'image pour en calculer les dimensions.
				// ce test n'a normalement plus de raison d'être
				// depuis l'introduction des dims dans le cookie
					if (document.getElementById('position_'+id)) {
						image1 = document.getElementById('position_'+id);
						the_width = Math.round((image1.width*5)/15);
						the_height = Math.round((image1.height*5)/15);
					}
					if (document.getElementById('position_tg_'+id)) {
						image1 = document.getElementById('position_tg_'+id);
						the_width = Math.round((image1.width*5)/15);
						the_height = Math.round((image1.height*5)/15);
					}
				}
				// ********************************************************
				html_caddie += "<td align=center valign=middle width=52 class=tourCaddie>";
				html_caddie += "<table border=0 cellpadding=0 cellspacing=0>";
				html_caddie += "<tr>";
				html_caddie += "<td><img src=/decoration/pix/vide.gif width=1 height=1 border=0></td>";
				html_caddie += "<td><img src=/decoration/pix/vide.gif width=1 height=1 border=0></td>";
				html_caddie += "<td><img src=/decoration/pix/vide.gif width=1 height=1 border=0></td>";
				html_caddie += "</tr><tr>";
				html_caddie += "<td><img src=/decoration/pix/vide.gif width=1 height=50 border=0></td>";
				html_caddie += "<td width=50 height=50 align=center valign=middle><IMG SRC="+image1.src+" width="+the_width+" height="+the_height+"></td>";
				html_caddie += "<td><img src=/decoration/pix/vide.gif width=1 height=50 border=0></td>";
				html_caddie += "</tr><tr>";
				html_caddie += "<td><img src=/decoration/pix/vide.gif width=1 height=15 border=0></td>";
				html_caddie += "<td heigh=15 valign=top align=center>";
				html_caddie += "<A HREF=javascript:RemoveCaddie('"+id+"')><img src=/decoration/pix/cut.gif width=40 height=16 border=0></A></td>";
				html_caddie += "<td><img src=/decoration/pix/vide.gif width=1 height=1 border=0></td></tr></table></td>";				
			}
			if ( Math.round((i+1)/imgsrow) == ((i+1)/imgsrow) ) {
				html_caddie += "</tr><tr>";
			}
		}
		html_bouton = "<a href=/decoration/documentations.cfm?lang=" + lang + "&src=" + src + " class=\"link_black_no_underline size9\">"+trad_voir_les_prod(nbre)+"</a>";
		
		html_caddie += "</table>";
		html_caddie += "<div align=\"center\">"+html_bouton+"</div>"; // *mauro*
		html_bouton2 += "<a href=javascript:EmptyCaddie(); class=link_red_no_underline>VIDER la sélection</a>";
		if (ids=="") {// cas particulier firefox qui ne comprend pas [ids != null] et préfererait [ids != Null] (qui a son tour genere une erreur dans IE)
			html_caddie = trad_no_prod_selected();
			html_bouton = "";
			html_bouton2 = "";
		}
	}
	else {
		html_caddie = trad_no_prod_selected();
		html_bouton = "";
		html_bouton2 = "";
	}
	caddie.innerHTML = html_caddie;
	//bouton_caddie.innerHTML = html_bouton; *mauro commented out*
	
	//bouton2_caddie.innerHTML = html_bouton2;
	RefreshMasqueCaddie();

	rCad = setTimeout("RefreshMasqueCaddie()",delai*10);
}

var rMem;

function RefreshMasqueCaddie() {
	//clearTimeout("RefreshMasqueCaddie()");
	var transparence=40;
	clearTimeout(rCad);
	var html_masque = "";//affichage des div par dessus les photos des produits dans la mosaique (icone carree sur les photos)
	var ids = getCookie('CADDIE');
	
	if (ids != null) {
		var idSplitted = ids.split(',');
		for (i=0;i<idSplitted.length;i++) {
			var id = idSplitted[i].substring(5);
			//var id = idSplitted[i];
			if ((id != '') && (id!=null)) {
			
				if (document.getElementById('position_'+ id)) {
				//jquery shortcut
				$(document).ready(function(){
					el=$('#position_'+ id);
					offset=el.offset();
				});
				//alert(offset.left+'   '+offset.top);
					var div_position = document.getElementById('position_'+ id);
					var longueur = div_position.offsetWidth;
					var hauteur = div_position.offsetHeight;
					var curleft = 0;
					var curtop = 0;
					if (div_position.offsetParent) {
						curleft = div_position.offsetLeft;
						curtop = div_position.offsetTop;
						while (div_position.offsetParent) {
							div_position = div_position.offsetParent;
							curleft += div_position.offsetLeft;
							curtop += div_position.offsetTop;
						}
					}
						//alert(curleft+" "+curtop);
					// correctif IE MAC ------------------------------------
					var div_main_marker = document.getElementById('main_marker_1');
					var main_marker_x = div_main_marker.offsetLeft;
					var main_marker_y = div_main_marker.offsetTop;
					if (main_marker_x==0) {
						div_main_marker = document.getElementById('main_marker_2');
						var main_marker_x = div_main_marker.offsetLeft;
						var main_marker_y = div_main_marker.offsetTop;
					}
					//alert(main_marker_x+" "+main_marker_y);
					// -----------------------------------------------------
					//var div_x = curleft+((longueur-68)/2) - main_marker_x;
					var div_x = offset.left+(el.width()-68)/2;
					//var div_y = curtop+((hauteur-68)/2) - main_marker_y;
					var div_y =offset.top+(el.height()-68)/2;
					html_masque += "<div background-color: FF0000; layer-background-color: FF0000; id=\"layer_caddie_"+id+"\"  style=\"position:absolute; -moz-opacity:"+(transparence/100)+"; filter:Alpha(opacity='"+transparence+"'); opacity:"+(transparence/100)+"; left:"+div_x+"px; top:"+div_y+"px; width:68px; height:68px; z-index:"+id+"; border: 1px none #000000;\"><a href=\"javascript:void(0);\" onClick=\"popupDF('/decoration/zoom.cfm?src=" + src + "&lang=" + lang + "&item=" + id + "',530,850,'"+id+"')\"><img src=/decoration/pix/" + lang + "/logo_caddie.gif width=68 height=68 border=0></a></div>";
				}
				if (document.getElementById('position_tg_'+ id)) {// la meme pour marquer la tete de gondole
					$(document).ready(function(){
						el=$('#position_'+ id);
						offset=el.offset();
					});
					var div_position = document.getElementById('position_tg_'+ id);
					var longueur = div_position.offsetWidth;
					var hauteur = div_position.offsetHeight;
					var curleft = 0;
					var curtop = 0;
					if (div_position.offsetParent) {
						curleft = div_position.offsetLeft;
						curtop = div_position.offsetTop;
						while (div_position.offsetParent) {
							div_position = div_position.offsetParent;
							curleft += div_position.offsetLeft;
							curtop += div_position.offsetTop;
						}
					}
						//alert(curleft+" "+curtop);
					// correctif IE MAC ------------------------------------
					var div_main_marker = document.getElementById('main_marker_1');
					var main_marker_x = div_main_marker.offsetLeft;
					var main_marker_y = div_main_marker.offsetTop;
					if (main_marker_x==0) {
						div_main_marker = document.getElementById('main_marker_2');
						var main_marker_x = div_main_marker.offsetLeft;
						var main_marker_y = div_main_marker.offsetTop;
					}
					//alert(main_marker_x+" "+main_marker_y);
					// -----------------------------------------------------
					//var div_x = curleft+((longueur-68)/2) - main_marker_x;
					var div_x = offset.left+(el.width()-68)/2;
					//var div_y = curtop+((hauteur-68)/2) - main_marker_y;
					var div_y =offset.top+(el.height()-68)/2;
					html_masque += "<div background-color: FF0000; layer-background-color: FF0000; id=\"layer_caddie_tg_"+id+"\"  style=\"position:absolute; -moz-opacity:"+(transparence/100)+"; filter:Alpha(opacity='"+transparence+"'); opacity:"+(transparence/100)+"; left:"+div_x+"px; top:"+div_y+"px; width:68px; height:68px; z-index:"+id+"; border: 1px none #000000;\"><a href=\"javascript:void(0);\" onClick=\"popupDF('/decoration/zoom.cfm?src=" + src + "&lang=" + lang + "&item=" + id + "',530,850,'"+id+"')\"><img src=/decoration/pix/" + lang + "/logo_caddie.gif width=68 height=68 border=0></a></div>";
				}
			}
		}
	}	
	
	if (document.getElementById('masque')) {

		var masque = document.getElementById('masque');
		masque.innerHTML = html_masque;
	}
	rMem = setTimeout("RefreshMemoriser()",delai);
	RefreshMemoriser();
}


function RefreshMemoriser() {
	//clearTimeout("RefreshMemoriser()");
	clearTimeout(rMem);

	var ids = getCookie('CADDIE');
	if (ids != null) {
		var idSplitted = ids.split(',');
		for (i=0;i<idSplitted.length;i++) {
			var id = idSplitted[i].substring(5);
			//var id = idSplitted[i];
			if ((id != '') && (id!=null)) {
				if (document.getElementById('m' + id)) {
					document.getElementById('m' + id).innerHTML = trad_unselect();
					document.getElementById('m' + id).href = "javascript:RemoveCaddie('"+id+"');";
				}
				if (document.getElementById('m_tg_' + id)) {
					document.getElementById('m_tg_' + id).innerHTML = trad_unselect();
					document.getElementById('m_tg_' + id).href = "javascript:RemoveCaddie('"+id+"');";
				}
			}
		}
	}
}


function setCookie (name, value, lifespan, access_path) {
	var cookietext = name + "=" + escape(value);
    if (lifespan != null) {
		var today=new Date();
		var expiredate = new Date();
		expiredate.setTime(today.getTime() + 1000*60*60*24*lifespan);
		cookietext += "; expires=" + expiredate.toGMTString();
	}
	if (access_path != null) {
		cookietext += "; PATH="+access_path;
	}
	document.cookie = cookietext;
	return null;
}


function setDatedCookie(name, value, expire, access_path) {
    var cookietext = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
     if (access_path != null) { 
      cookietext += "; PATH="+access_path;
     }
   document.cookie = cookietext;
   return null;        
}


function getCookie(Name) {
  var search = Name + "=";                       
  var CookieString = document.cookie;            
  var result = null;                               
  if (CookieString.length > 0) {                
    offset = CookieString.indexOf(search);       
    if (offset != -1) {                         
      offset += search.length;                   
      end = CookieString.indexOf(";", offset);   
      if (end == -1)                            
        end = CookieString.length;               
      result = unescape(CookieString.substring(offset, end));         
                                                
      } 
    }
   return result; 
}


function AddCookie2(id) {
	var ids = getCookie('CADDIE');
	if (ids == null) ids = '';
	ids += id + ",";
	setCookie('CADDIE',ids,365,'/');
}
function AddCookie(id) {
	// ajout d'un produit dans le cookie 'caddie' : liste des id(produit.numero) ET dimensions des produits
	// et c'est là la grande nouveauté!
	// Pas de problème de temps de chargement d'image pour en calculer 
	// les dims car on calcule les dims sur la vig affichée à l'écran 
	// (celle que l'on sélectionne). Ensuite, les dims sont stockées 
	// dans le cookie, plus besoin de les recalculer à l'avenir.
	var ids = getCookie('CADDIE');
	if (ids == null) ids = '';
	var dimx="00";
	var dimy="00";
	var the_number = String(id);
	var the_length = the_number.length-3;
	var repertoire = the_number.substring(0,the_length);
	if (repertoire=="") {
		repertoire="0";
	}
	var image2 = new Image();
	image2.src = '/vig/' + repertoire + '/' + id + '.jpg';
	if (!isInCaddie(id)) {
		if (document.getElementById('position_'+id)) {
			var image1 = new Image();
			image1 = document.getElementById('position_'+id);
			dimx0 = '0'+Math.round((image1.width*5)/15);
			dimx = dimx0.substring(dimx0.length-2,dimx0.length);
			dimy0 = '0'+Math.round((image1.height*5)/15);
			dimy = dimy0.substring(dimy0.length-2,dimy0.length);
		}
		if (document.getElementById('position_tg_'+id)) {
			var image1 = new Image();
			image1 = document.getElementById('position_tg_'+id);
			dimx0 = '0'+Math.round((image1.width*5)/15);
			dimx = dimx0.substring(dimx0.length-2,dimx0.length);
			dimy0 = '0'+Math.round((image1.height*5)/15);
			dimy = dimy0.substring(dimy0.length-2,dimy0.length);
		}
		if (dimx=="00") {
			//var le_zoom = frames["le_zoom"];
			dimx0 = '0'+Math.round((image2.width*5)/15);
			dimx = dimx0.substring(dimx0.length-2,dimx0.length);
			dimy0 = '0'+Math.round((image2.height*5)/15);
			dimy = dimy0.substring(dimy0.length-2,dimy0.length);
		}
	}
	ids += dimx +''+ dimy + "_" + id + ",";
	setCookie('CADDIE',ids,365,'/');
}


function RemoveCookie(id) {
	var ids = getCookie('CADDIE');
	if (ids == null) ids = '';
	var pos = ids.indexOf('_'+id+',');
	var ids1 = ids.substring(0,pos-4);
	var ids2 = ids.substring(pos);
	var pos2 = ids2.indexOf(',');
	var ids3 = ids2.substring(pos2+1);
	ids = ids1+''+ids3;
	//ids = ids.replace(new RegExp(id + ','),'');
	setCookie('CADDIE',ids,365,'/');
}


function deleteCookie(Name, Path) {
  setCookie(Name,"Deleted", -1, Path);
}

