var lg_url_compare, lg_lang, lg_img_magnifying_glass, lg_img_locked_product, lg_img_unvailable_product_small, lg_img_unvailable_product_middle, lg_img_clear_pix, lg_img_bpu_path, lg_msg_err_compare, lg_image_size;
var lg_bad_mail;

// Default image - can be init before calling this script
if (lg_lang == null) {
	lg_lang = "fr";
}
if (lg_img_magnifying_glass == null) {
	lg_img_magnifying_glass = "http://img.leguide.com/v4/commun/loupe.gif";
}
if (lg_img_locked_product == null) {
	lg_img_locked_product = "http://img.leguide.com/v4/commun/interogation.gif";
}
if (lg_img_unvailable_product_small == null) {
	lg_img_unvailable_product_small = "http://img.leguide.com/v3/fr/nondispo.gif";
}
if (lg_img_unvailable_product_middle == null) {
	lg_img_unvailable_product_middle = "http://img.leguide.com/v4/commun/nondispo.gif";
}
if (lg_img_clear_pix == null) {
	lg_img_clear_pix = "http://img.leguide.com/v4/commun/pixel_transparent.gif";
}
if (lg_img_bpu_path == null) {
	lg_img_bpu_path = "http://img.shopwahl.de/de/bpu/";
}
if (lg_msg_err_compare) {
	lg_msg_err_compare = "You have to choice between 2 and 3 products of the same category.";
}
if (lg_image_size == null) {
	lg_image_size = 70;
}
if (lg_url_compare == null) {
	lg_url_compare = '/';
}
if (lg_bad_mail == null) {
	lg_bad_mail = "Le format de l'email est incorrect";
}
function lg_get_image_size() {
	return lg_image_size;
}
function lg_set_image_size(image_size) {
	lg_image_size = image_size;
}

function lg_open(u){l = (document.all ? screen.width : screen.width) * 0.9;h = (document.all ? screen.height : screen.height-100) * 0.9;conf = 'scrollbars=yes,toolbar=yes,location=yes,directories=yes,menubar=yes,resizable=yes,status=yes,width=' + l + ',height=' + h + ',screenX=0,screenY=0,top=0,left=0';open(u,'',conf);}
function lg_r(u) { document.location.href = u;}
function lg_p(n,p) { u = "/" + n + ".php";	if (p) u += "?" + p; lg_open(u);}

function goan(d,ext,s,id_mag,idx,lang,tag,vars) {
	lggo_goan(id_mag,idx,lang,tag,vars,"","");	
}
function gopt(d,ext,s,id_mag,idx,idlg,id_rech,org,ms,lang,tag,idbp,vars) {
	lggo_gopt(id_mag,idx,idlg,id_rech,org,ms,lang,tag,idbp,vars,"","");
}
function gopn(d,ext,s,id_mag,idx,id_promo,lang,tag,vars) {
	lggo_gopn(id_mag,idx,id_promo,lang,tag,vars,"","");
}
function lg_redim_image(inImg, inT, n) {
	objImage = new Image();
	objImage.src = inImg;
	
	var w = 0;
	var h = 0;
	if (objImage.width < inT && objImage.height < inT) {
		w = objImage.width; h = objImage.height;
	} else if ( objImage.width > objImage.height ) {
		w = inT;
		h = objImage.height / (objImage.width / inT);
	} else if ( objImage.width == objImage.height ) {
		w = inT;
		h = inT;
	} else {
		h = inT;
		w = objImage.width / (objImage.height / inT);
	}
	if (w > 0 && h > 0) {
		document.images[n].width = w;
		document.images[n].height = h;
	}
}

function lg_img_onerror_product(i,width) {
	if (width == lg_get_image_size()) {
		document.images[i].src= lg_img_unvailable_product_small;
	} else {
		document.images[i].src= lg_img_unvailable_product_middle;
	}
	document.images[i].width=width;
	document.images[i].height=width;
	if (document.images['zoom_'+i] != null) {
		document.images['zoom_'+i].src= lg_img_clear_pix;
		document.images['zoom_'+i].style.visibility='hidden';
	}

}
function lg_img_onerror_logo(i) {
	document.images[i].src="http://img.leguide.com/v4/commun/pixel_transparent.gif";
}

function lg_display_image(inImg, outImg, inT, outT, n, u, type){

	if (inImg && inImg.substr(0,7) != 'http://' && inImg.substr(0,8) != 'https://') {
		inImg = 'http://' + inImg;
	}
	if (outImg && outImg.substr(0,7) != 'http://' && outImg.substr(0,8) != 'https://') {
		outImg = 'http://' + outImg;
	}

	document.write('<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="middle" align="center" width="'+inT+'" height="'+inT+'">');
	document.write('<IMG border=0');
	if (u) {
		document.write(' style="cursor:pointer;" onClick="' + u + ';return(false);"');
	}
	document.write(' WIDTH="' + inT + '" HEIGHT="' + inT + '" NAME="' + n + '" BORDER="0" onerror=lg_img_onerror_product("' + n + '","'+lg_get_image_size()+'")');
	if (inImg) {
		document.write(' SRC="' + inImg + '" onload="lg_redim_image(\'' + inImg + '\', \'' + inT + '\', \'' + n + '\')" ');
	} else {
		document.write(' SRC="' + lg_img_unvailable_product_small + '"');
	}
	document.write('>');
	document.write('</td></tr>');
	if (inImg != '') {
		var image = '';
		if (type == 'cadenas') {
			image = lg_img_locked_product;
		} else if (type == 'loupe'){
			image = lg_img_magnifying_glass;
		}
		if (image) {
			document.write('<tr><td valign="top" align="right"><div class="zone_loupe"><img NAME="zoom_' + n + '" onmouseover="return overlib(\'<img class=zoom src='+outImg+'>\',FULLHTML, OFFSETY,-'+lg_get_image_size()+', FOLLOWMOUSE, 0);" onmouseout="return nd(500);" src="'+image+'" alt=""></div></td></tr>');
		}
	}

	document.write('</table>');
}
function lg_display_image_offer(inImg, n, u){

	inT = lg_get_image_size();
	outT = 0;
	outImg = inImg;
	lg_display_image(inImg, outImg, inT, outT, n, u, 'loupe')
}
function lg_display_image_offer_simple(inImg, n, u, inT){

	outT = 0;
	outImg = inImg;
	lg_display_image(inImg, outImg, inT, outT, n, u, '') // without magnifying glass
}

function lg_sub_path_img_bpu(idbp) {
	
	var sub_path = lg_img_bpu_path;
	var t = sub_path.length;
	
	//if (lg_lang == 'de') { // image gfk
	if (sub_path.substr(t-5,5) == '/bpu/') {
		if (idbp.length == 1) {
			sub_path += "0/0/" + idbp + "/";
		} else if (idbp.length == 2) {
			sub_path += "0/" + idbp.substring(0,1) + "/" + idbp.substring(1,2) + "/";
		} else {
			sub_path += idbp.substring(0,1) + "/" + idbp.substring(1,2) + "/" + idbp.substring(2,3) + "/";
		}
	}
	return sub_path;
}

function lg_display_image_bpu(idbp, img_name, u, type){
	inT = lg_get_image_size();
	outT = 200;
	sub_path = lg_sub_path_img_bpu(idbp);
	inImg = sub_path + lg_get_image_size() + "/"+idbp+".jpg";
	outImg = sub_path + "200/"+idbp+".jpg";
	lg_display_image(inImg, outImg, inT, outT, img_name, u, type);
}

function lg_display_image_bpu_simple(idbp, inT) {
	var sub_path = lg_sub_path_img_bpu(idbp);
	var outT = 0;
	if (inT == lg_get_image_size()) {
		var outImg = inImg = sub_path + lg_get_image_size() + "/" + idbp + ".jpg";
	} else {
		var outImg = inImg = sub_path + "200/" + idbp + ".jpg";
	}
	var img_name = "img_bpu_" + idbp;
	var u = '';
	lg_display_image(inImg, outImg, inT, outT, img_name, u, '') // without magnifying glass
}

function lg_compare_check_id(form) {
	var idx = 0;
	var idxtmp = 0;
	var m;
	if(form.cb.length>1) {
		first=true;
		for(n=0; n<form.cb.length; n++) {
			if(form.cb[n] != null && form.cb[n].checked) {
				var a = new Array();
				a = form.cb[n].value.split(",");
				idxtmp = a[1];
				if(first || m>=2) {
					idx = idxtmp;
					first=false;
					m = 0;
				}
				else if(idx!=idxtmp && m<2)
				m++;
			}
		}
		return idx;
	}
	return 0;
}
function lg_compare(form) {
	
	var first = false;
	var counter = 0;
	idx = lg_compare_check_id(form);
	if (idx > 0) {
		u = lg_url_compare + "?idx=" + idx + "&idbps=";
		for (n=0; n<form.cb.length; n++)
		{
			if (form.cb[n] != null && form.cb[n].checked)
			{
				var a = new Array();
				a = form.cb[n].value.split(",");
				if(a[1]!=idx) {
					continue;
				}	
				if (first) {
					u += ",";
				}	
				else {
					first = true;
				}	
				u += a[0];
				counter ++;
			}
		}
		if (counter <= 1 || counter > 3) {
			alert(lg_msg_err_compare);
		}	
		else {
			location.href = u;
		}	
	} else {
		alert(lg_msg_err_compare);
	}
}
function lg_search_submit(form) {
	var u = "/?";
	var nb_param = 0;
	if (form.w != null && form.w.value) {
		u += "w=" + form.w.value;
		nb_param++;
	}
	if (form.idx != null && form.idx.value > 0) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "idx=" + form.idx.value;
		nb_param++;
	}
	if (form.ps != null && form.ps.value) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "ps=" + form.ps.value;
		nb_param++;
	}
	if (form.i1 != null && form.i1.value) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "i1=" + form.i1.value;
		nb_param++;
	}
	if (form.i2 != null && form.i2.value) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "i2=" + form.i2.value;
		nb_param++;
	}
	if (form.i3 != null && form.i3.value) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "i3=" + form.i3.value;
		nb_param++;
	}
	if (form.s1 != null && form.s1.value) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "s1=" + form.s1.value;
		nb_param++;
	}
	if (form.pmin != null && form.pmin.value) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "pmin=" + form.pmin.value;
		nb_param++;
	}
	if (form.pmax != null && form.pmax.value) {
		if (nb_param > 0) {
			u += "&";
		}
		u += "pmax=" + form.pmax.value;
		nb_param++;
	}
	document.location.href = u;
}

function lg_display_sheets() {
	
	html = '<ul><li class="empty">&nbsp;</li>';
	for (sheet=1; sheet<sheets.length; sheet++) {
		
		if (sheets[sheet] != null) {
			if (sheet <= sheets_max) {
				html += '<li onmouseover="lg_select_sheet_more(0);return false;" id="sheet'+sheet+'"><div class="txt"><a href="#" onClick="lg_select_sheet('+sheet+','+sheets[sheet][1]+');return false;"}">'+sheets[sheet][0]+'</a></div></li>';
			} else {
				if (sheet == (sheets_max+1)) {
					html += '<li onmouseover="lg_select_sheet_more(1);return false;" id="sheet_more">';
					html += '<ul id="page_more">';
				}
				html += '<li class="link"><div class="padding1"><div class="padding2"><div class="padding3"><a href="#" onClick="lg_invert_sheet('+sheet+','+sheets_max+');lg_display_sheets();lg_select_sheet('+sheets_max+','+sheets[sheet][1]+');return false;"}">'+sheets[sheet][0]+'</a></div></div></div></li>';
				
				if (sheet == (sheets.length - 1)) {
					html += '<li class="bottom"></li>';
					html += '</ul>';
					html += '<div class="txt">'+sheets_name_more+'</div>';
					html += '</li>';
				}
			}
		}	
	}
	html += '</ul>';
	document.getElementById("sheets_bar").innerHTML = html;
}
function lg_invert_sheet(sheet_a,sheet_b) {
	
	var tmp_sheet = sheets[sheet_b];
	sheets[sheet_b] = sheets[sheet_a];
	sheets[sheet_a] = tmp_sheet;
}
function lg_select_sheet_more(select) {
	if (document.getElementById('page_more') != null) {
		if (select) {
			document.getElementById('page_more').style.display = 'block';			
		} else {
			document.getElementById('page_more').style.display = 'none';
		}	
	}
}
function lg_select_sheet(sheet, page) {
	for (n=1;n<sheets.length;n++) {
		if (document.getElementById('page' + sheets[n][1]) != null) {
			document.getElementById('page' + sheets[n][1]).style.display = 'none';	
		}	
		if (document.getElementById('sheet' + n) != null) {
			document.getElementById('sheet' + n).className="sheet_unselected";	
		}	
	}
	if (document.getElementById('page' + page) != null) {
		document.getElementById('page' + page).style.display = 'block';
	}
	if (document.getElementById('sheet' + sheet) != null) {
		document.getElementById('sheet' + sheet).className="sheet_selected";
	}
	lg_select_sheet_more(0);
}
function lg_check_mail(mail) {
	var mail_ok = false;
	if (mail.length > 0)
	{
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(mail)) mail_ok = true;
	}
	return mail_ok;
}
function lg_login(f){
	if (!lg_check_mail(f.mail.value)) {
		alert('Mail invalide !');
		f.mail.focus();
		return;
	}
	f.submit();
}

function lg_shoppinglist(idbp, idlg, del) {
	u = '/myaccount/';
	if (del == 1) {
		if (idbp > 0) {
			u += '?action=myfavorite_del&idbp=' + idbp;
		} else if (idlg) {
			u += '?action=myfavorite_del&idlg=' + idlg;
		}
	} else {
		if (idbp > 0) {
			u += '?action=myfavorite&idbp=' + idbp;
		} else if (idlg) {
			u += '?action=myfavorite&idlg=' + idlg;
		}
	}
	if (u) location.href = u;
}

function lg_url_opinion(idbp,type,p,idlg,s,id) {
	var u = '/opinion.php?';
	var param = 0;
	if (idbp > 0) {if (param > 0) {u += "&";} u += "idbp=" + idbp;param++;}
	else if (idlg) {if (param > 0) {u += "&";} u += "idlg=" + idlg;param++;}
	else if (id > 0) {if (param > 0) {u += "&";} u += "id=" + idlg;param++;}
	
	if (type) {if (param > 0) {u += "&";} u += "type=" + type;param++;}
	if (p > 0) {if (param > 0) {u += "&";} u += "p=" + p;param++;}
	if (s > 0) {if (param > 0) {u += "&";} u += "s=" + s;param++;}
	location.href = u;
}
function lg_myaccount_identification(f) {
	if (!lg_check_mail(f.mail.value)) {
		alert(lg_bad_mail);
		f.mail.focus();
		return;
	}
	f.submit();
}
function lg_seolog_user_search(idx,w) {
	if (idx == '') {idx = 0;}
	u = "/log/user_search/idx/"+idx+"/w/"+w;
	$.ajax({
  		url: u,
  		async: false
 	});
}
function lg_seolog_noresult(idx,w) {
	if (idx == '') {idx = 0;}
	u = "/log/noresult/idx/"+idx+"/w/"+w;
	$.ajax({
  		url: u,
  		async: false
 	});
}

