var W3CDOM = (document.createElement && document.getElementsByTagName);


$(function(){
	/* Fix png*/
	$.ifixpng('img/pixel.gif');
	$('img[src$=.png]').ifixpng();
	$('input[type=image][src$=.png]').ifixpng();

});



window.onload = init;

function init ()
{
	if (!W3CDOM) return;
	initmenucatalogue();
	initegalisehauteurh2();
	initpopup();
	initvisuel();
	initquestionsreponses();
}

/**********************/
/**********************/
/* Fonctions diverses */
/**********************/
/**********************/

/*******************************************************/
/* Gestion de la Pop-up pour les zoom d'images produit */
/*******************************************************/

function popzoom(lien) {
	window.open("zoom_popup.php?path=" + lien.href, "ZOOM", "directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,width=0,height=0");
}

/*********************/
/* Autres Fonctions  */
/*********************/

function getElementsByClassName(needle, tag, idlimit) {
	if (!tag || !document.getElementsByTagName(tag))
		tag == '*';
	if (!idlimit || !document.getElementById(idlimit)) {
		var my_array = document.getElementsByTagName(tag);
	}
	else {
		var my_array = document.getElementById(idlimit).getElementsByTagName(tag);
	}
	var retvalue = new Array();
	var i, j;
	for (i = 0, j = 0; i < my_array.length; i++)
	{
		var c = " " + my_array[i].className + " ";
		if (c.indexOf(" " + needle + " ") != -1)
		retvalue[j++] = my_array[i];
	}
	return retvalue;
}

function adjustheight(my_array, limit, offset) {
	if (offset < 1) offset = 0;
	if (limit < 1) limit = my_array.length;
	if (!my_array[0] || offset > (limit-1))
		return;
	var rows = Math.ceil(my_array.length/limit);
	for (var i=0; i<rows; i++)
	{
		var index = (limit * i);
		var rowmaxheight=0;
		if (index+limit-offset < my_array.length) var portee = index+limit-offset;
		else var portee = my_array.length;
		if (i==0) var depart = 0;
		else var depart = index-offset;
		for (var j=depart; j<portee; j++)
		{
			if (my_array[j].offsetHeight > rowmaxheight) rowmaxheight = my_array[j].offsetHeight;
		}
		for (var j=depart; j<portee; j++)
		{
			navigator.appName == 'Microsoft Internet Explorer' ? my_array[j].style.height = rowmaxheight+"px" : my_array[j].style.minHeight = rowmaxheight+"px";
		}
	}
}
function changedisplay(elementcible, tagcible, displayvalue) {
	var cible = elementcible.getElementsByTagName(tagcible)[0];
	cible.style.display = displayvalue;
}

/******************/
/******************/
/* Fonctions init */
/******************/
/******************/

/*************************/
/*** initmenucatalogue ***/
/*************************/

function initmenucatalogue() {
	var menucatalogue = document.getElementById('menu');
	if (menucatalogue) {
		var dd = menucatalogue.getElementsByTagName('dd');
		if (dd) {
			for (i=0; i<dd.length; i++) {
				if ( dd[i].getElementsByTagName('ul')[0] ) {
					 dd[i].getElementsByTagName('ul')[0].style.display = 'none';
					 dd[i].onmouseover = function() {changedisplay(this, 'ul', 'block');}
					 dd[i].onfocus = function() {changedisplay(this, 'ul', 'block');}
					 dd[i].onmouseout = function() {changedisplay(this, 'ul', 'none'); }
					 dd[i].onblur = function() {changedisplay(this, 'ul', 'none');}
				}
			}
		}
	}
}

/****************************/
/*** initegalisehauteurh2 ***/
/****************************/

function initegalisehauteurh2() {
	var sousrubriques = getElementsByClassName('sousrubrique', 'div');
	var produits = getElementsByClassName('produit', 'div');
	if (sousrubriques[0]) {
		var h2sousrubriques = new Array();
		for (i=0; i<sousrubriques.length; i++)
		{
			h2sousrubriques[i]=sousrubriques[i].getElementsByTagName('h2')[0];
		}
		adjustheight(h2sousrubriques, 3, 1);
	}
	if (produits[0]) {
		var h2produits = new Array();
		for (i=0; i<produits.length; i++)
		{
			h2produits[i]=produits[i].getElementsByTagName('h2')[0];
		}
		(document.getElementById('resultatrecherche') || document.getElementById('soustheme')) ? adjustheight( h2produits, 3, 0) : adjustheight( h2produits, 3, 1);
	}
}

/**********************************************************/
/* Gestion du div pour l'ajout de produits dans le panier */
/**********************************************************/

function initpopup() {
	var form_fermer_popup = document.getElementById('form_fermer_popup');
	if (form_fermer_popup)
		form_fermer_popup.onsubmit = function() { if (document.getElementById('popup')) {document.getElementById('popup').style.display = "none"}; return false;}
	if (document.getElementById('dispo'))
		document.getElementById('dispo').onclick = function() { if (document.getElementById('popup')) {document.getElementById('popup').style.display = "block"}; return true; };
}

function showPopup(ID) {
	if (document.getElementById(ID)) {
		document.getElementById(ID).style.display = "block";
		document.getElementById(ID).style.background= "#ffffff";
		document.getElementById(ID).style.border = "1px solid #000";
		document.getElementById(ID).style.left = "50%";
		document.getElementById(ID).style.marginLeft = "-20em";
		document.getElementById(ID).style.marginTop = "-5.5em";
		document.getElementById(ID).style.paddingBottom = "1em";
		document.getElementById(ID).style.position = "absolute";
		document.getElementById(ID).style.textAlign = "center";
		document.getElementById(ID).style.top = "50%";
		document.getElementById(ID).style.width = "40em";
		document.getElementById(ID).style.zIndex = "10";
	}

	if (document.getElementsByTagName) {
		var arrayForm = document.getElementsByTagName('form');
		if (arrayForm.length) {
			for (cnt = 0; cnt < arrayForm.length; cnt++) {arrayForm[cnt].style.margin = "1em";}
		}
		var arrayA = document.getElementsByTagName('a', 'dispo');
		if (arrayA.length) {

			for (cnt = 0; cnt < arrayA.length; cnt++) {arrayA[cnt].style.cursor = "pointer";}
		}
	}
	if (document.getElementsByClassName) {
		var arrayButton = getElementsByClassName('button', '*', 'popup');
		if (arrayButton.length) {
			for (cnt = 0; cnt < arrayButton.length; cnt++) {
				arrayButton[cnt].style.float = "left";
				arrayButton[cnt].style.margin = "10px 15px";
			}
		}
		var arrayInput = getElementsByClassName('input_email', '*', 'popup');
		if (arrayInput.length) {
			for (cnt = 0; cnt < arrayInput.length; cnt++) {
				arrayInput[cnt].style.border = "1px solid #666";
				arrayInput[cnt].style.height = "15px";
				arrayInput[cnt].style.marginBottom = "1em";
			}
		}
	}
}

function hidePopup(ID) {
	if (document.getElementById(ID)) {
		document.getElementById(ID).style.display = "none";
	}
	return false;
}

/************************/
/* initreferenceproduit */
/************************/

function initvisuel() {
	var arrayvisuel = getElementsByClassName('visuel', 'div');
	if (arrayvisuel.length) {
		for (i=0; i<arrayvisuel.length; i++) {
			centreimageproduit(arrayvisuel[i]);
			zoom(arrayvisuel[i]);
		}
	}
	var arrayaccroche = getElementsByClassName('accroche', 'p');
	(document.getElementById('resultatrecherche') || document.getElementById('soustheme')) ? adjustheight( arrayaccroche, 3, 0) : adjustheight( arrayaccroche, 3, 1);
}

function centreimageproduit(visuel) {
	var image = visuel.getElementsByTagName('img')[0];
	if (!visuel.style.width) visuel.style.width = visuel.offsetWidth+'px';
	if (!visuel.style.marginLeft) visuel.style.marginLeft = '0px';
	var marginleft = Math.ceil((parseInt(visuel.style.width) - image.offsetWidth )/2);
	image.offsetHeight > 157 ? hauteur = image.offsetHeight : hauteur = 157;
	var margintop  = Math.ceil((hauteur - image.offsetHeight)/2);
	var marginbottom  = (hauteur - image.offsetHeight - margintop);
	if (image) {
		image.style.marginLeft = marginleft+"px";
		image.style.marginTop  = margintop+"px";
		image.style.marginBottom  = marginbottom+"px";
		image.style.display = "block";
	}
}

function zoom(visuel) {
	var image = visuel.getElementsByTagName('img')[0];
	var pzoom  = visuel.getElementsByTagName('p')[0];
	if ( pzoom ) var azoom  = visuel.getElementsByTagName('p')[0].getElementsByTagName('a')[0];
	var rightedge  = (parseInt(image.style.marginLeft) + image.offsetWidth);
	var bottomedge = (parseInt(image.style.marginBottom) + 12);
	if ( image && pzoom ) {
		pzoom.style.left = (rightedge - pzoom.offsetWidth )+'px';
		pzoom.style.bottom = bottomedge+'px';
	}
	if ( azoom ) azoom.onclick = function (azoom) { popzoom(this); return false; };
}

/*************************/
/* initquestionsreponses */
/*************************/
function initquestionsreponses() {
	var qr = document.getElementById('questionsreponses');
	if (qr) {
		var liens = qr.getElementsByTagName('A');
		for (var i=0; i<liens.length; i++) {
			document.getElementById(('cible'+liens[i].id)).style.display = 'none';
			liens[i].onclick = function() {document.getElementById(('cible'+this.id)).style.display == 'none' ? document.getElementById(('cible'+this.id)).style.display = 'block' : document.getElementById(('cible'+this.id)).style.display = 'none'; return false;};
		}
	}
}

function test(cible,liste) {
	alert ('onmouseover');
	if (cible) {
		for( j=0; j<liste.getElementsByTagName('h3').length; j++) {
			document.getElementById(('cible'+liste.getElementsByTagName('h3')[j].id)).style.display = 'none';
		}
		cible.style.display = 'block';
	}
}



/**********************************/
/* Ajout du site dans les favoris */
/**********************************/
function favoris(url, title) {
	if ( navigator.appName != 'Microsoft Internet Explorer' ) {
		window.sidebar.addPanel(title , url, '');
	} else {
		window.external.AddFavorite(url, title);
	}
}

/********************************************************************/
/* fonctions verification formulaire mode livraison et mode paiement*/
/********************************************************************/

function isRadioChecked(radioObj) {
	for (var i = 0; i < radioObj.length; i++)
		if (radioObj[i].checked) return true ;
	return false ;
}

function isEmail(eml) {
	a = eml.value.search(/^[-a-z0-9_]+([-._]+[-a-z0-9_]+)*@[a-z0-9]([.-]?[a-z0-9])*\.[a-z]{2,4}$/i);
	if(a!=-1)
		return true
	else
		return false
}

function isInputEmpty(inputObj) {
	return (inputObj.value.length == 0) ;
}

function VerifFormPaiement() {
	if (isRadioChecked(document.choice_paiement.paiement) == false)
	{
		alert("Veuillez selectionner un mode de paiement");
		return (false);
	}
	return (VerifFormPaiement.submit);
};

function VerifFormUserInfo() {
	if (isRadioChecked(document.infos_client.cli_civilite) == false)
	{
		alert("Veuillez l'attribut Mlle Mme ou Mr");
		return (false);
	}

	if (isInputEmpty(document.infos_client.cli_nom))
	{
		alert ("Veuillez entrer le nom");
		return (false)	;
	}

	if (isInputEmpty(document.infos_client.cli_prenom))
	{
		alert ("Veuillez entrer le prénom");
		return (false)	;
	}

	if (isInputEmpty(document.infos_client.cli_mail))
	{
		alert ("Veuillez une adresse mail valide");
		return (false)	;
	}

	if (isInputEmpty(document.infos_client.cli_rue))
	{
		alert ("Veuillez entrer une rue");
		return (false)	;
	}

	if (isInputEmpty(document.infos_client.cli_ville))
	{
		alert ("Veuillez entrer la ville");
		return (false)	;
	}

	if (isInputEmpty(document.infos_client.cli_codepostal))
	{
		alert ("Veuillez entrer le code postal");
		return (false)	;
	}

	if (isInputEmpty(document.infos_client.cli_pays))
	{
		alert ("Veuillez entrer le pays");
		return (false)	;
	}

	if (!isInputEmpty(document.infos_client.liv_nom))
	{
		if (isRadioChecked(document.infos_client.liv_civilite) == false)
		{
			alert("Veuillez l'attribut Mlle Mme ou Mr de livraison");
			return (false);
		}

		if (isInputEmpty(document.infos_client.liv_prenom))
		{
			alert ("Veuillez entrer le prénom de livraison");
			return (false)	;
		}

		if (isInputEmpty(document.infos_client.liv_mail))
		{
			alert ("Veuillez une adresse mail de livraison valide");
			return (false)	;
		}

		if (isInputEmpty(document.infos_client.liv_rue))
		{
			alert ("Veuillez entrer la rue de livraison");
			return (false)	;
		}

		if (isInputEmpty(document.infos_client.liv_ville))
		{
			alert ("Veuillez entrer la ville de livraison");
			return (false)	;
		}

		if (isInputEmpty(document.infos_client.liv_codepostal))
		{
			alert ("Veuillez entrer le code postal de livraison");
			return (false)	;
		}

		if (isInputEmpty(document.infos_client.liv_pays))
		{
			alert ("Veuillez entrer le pays");
			return (false)	;
		}
	}

	return (VerifFormUserInfo.submit);
}

/*******************************************************/
/*        retour to home (pour la 404)                */
/*******************************************************/
function return_to_home()
{
	location.href="/";
}
function checkForm() {
	if ((document.infos_client.cli_pays.value != "France") && (document.infos_client.cli_pays.value != "france")) {
		if (isRadioChecked(document.infos_client.liv_civilite) == false) {
			alert("Veuillez selectionner l'attribut de livraison : Mlle, Mme ou Mr");
			return (false);
		}
		if (isInputEmpty(document.infos_client.liv_nom)) {
			alert ("Veuillez entrer le nom de livraison");
			return (false)	;
		}
		if (isInputEmpty(document.infos_client.liv_prenom)) {
			alert ("Veuillez entrer le prénom de livraison");
			return (false)	;
		}
		if (isInputEmpty(document.infos_client.liv_mail)) {
			alert ("Veuillez une adresse mail de livraison valide");
			return (false)	;
		}
		if (isInputEmpty(document.infos_client.liv_rue)) {
			alert ("Veuillez entrer une rue de livraison");
			return (false)	;
		}
		if (isInputEmpty(document.infos_client.liv_ville)) {
			alert ("Veuillez entrer la ville de livraison");
			return (false)	;
		}
		if (isInputEmpty(document.infos_client.liv_codepostal)) {
			alert ("Veuillez entrer le code postal de livraison");
			return (false)	;
		}
		if (isInputEmpty(document.infos_client.liv_pays)) {
			alert ("Veuillez entrer le pays de livraison");
			return (false)	;
		}
	}
}

/* Declaration du tableau pour les variations */
var tabProduits=new Array();


function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function limitTextarea(textarea, limit){
	var val=textarea.value.replace(/\r/g,'').split('\n');
	if(val.length>limit){
		alert('Vous êtes limités à '+limit+' lignes');
		textarea.value=val.slice(0,-1).join('\n');
	}
}

