// load_new_xml
function load_new_xml()
{
	var elt = $('frm_s_produit').getElements();
	var query = new Hash();
	elt.each(function(e, i){

		if(e.value)
			query.set(e.name, e.value);
	});
	
	var url = BASE_URL + 'search.xml.php?lg=' + LANG + '&' + query.toQueryString();
	//prompt('', url)
	$('slide_search').startid('' , encodeURI(url) );
	return false;
}


// load_new_xml
function load_new_xml_search()
{
	var elt = $('frm_s_produit').getElements();
	var query = new Hash();
	elt.each(function(e, i){

		if(e.value)
			query.set(e.name, e.value);
	});
	
	var url = BASE_URL + 'search.xml.php?lg=' + LANG + '&' + query.toQueryString();
	new Ajax.Request(url,
	{
		onFailure    : ajax_failure
		,onException : ajax_exception
		,onLoading   : function(t){	Element.show('loading');}
		,onComplete  : function(t) {
				new Effect.Fade($('loading'),{ duration:1.5 });
		}
		,onSuccess   : function(t){

			$('glider_content').innerHTML = "";
			var xml_doc = t.responseXML;
			nodes = xml_doc.getElementsByTagName("item");
			var nb = nodes.length;
			var tmpl = new Template('<a href="#{url}" title="#{nom}" class="slide"><img src="#{url_image}" alt="#{nom}" id_slide="#{id}"/><span>#{info}</span></a>');

			for(var i=0, html=""; i<nb; i++)
			{
				

				if(!i || !(i%7))
				{
					if(i)
					{
						div.innerHTML = html + "\n";
						$('glider_content').appendChild(div);
						html = "";
					}

					div = document.createElement("div");
					$(div).addClassName("section left");
					//div.style.float = "left"; 
				}
					
				id =  nodes[i].attributes[0].nodeValue;
				info =  nodes[i].attributes[1].nodeValue;
				url_image =  nodes[i].attributes[2].nodeValue;
				url =  nodes[i].attributes[3].nodeValue;
				target =  nodes[i].attributes[4].nodeValue;
				nom =  nodes[i].attributes[5].nodeValue;

				html+= tmpl.evaluate({
					id: id
					,info: info
					,url_image: url_image
					,url: url
					,target: target
					,nom: nom
				});
				
			}
			div.innerHTML = html + "\n";
			$('glider_content').appendChild(div);

			var w = (105*i);
			$('glider_content').style.width = w + "px";
			
			//alert("W=>" + w)
			my_glider = new Glider('my-glider', {duration:0.5, initialSection:$$('#glider_content .section')[0] });		
		}		
	});

			


	return false;
}


// get_produit
function get_produit(id, scategorie)
{
	
	new Ajax.Request(BASE_URL + "produit.xml.php?lg=" + LANG + "&produit=" + id + "&scategorie=" + scategorie,
	//new Ajax.Request(BASE_URL + "test1.xml" ,
	{
		onFailure    : ajax_failure
		,onException : ajax_exception
		//,onLoading   : function(t){	Element.show('z');}
		,onComplete  : function(t) {
				//new Effect.Fade($('z'),{ duration:1.5 });
		}
		,onSuccess   : show_produit		
	});
}






// make_row_detail
function make_row_detail(id, content)
{
	if($(id))
	{	
		if(content)
		{
			Element.update( $(id), content);
			if($('row_' + id))
				Element.show('row_' + id);
		}
		else
		{
			if($('row_' + id))
				Element.hide('row_' + id);
		}
	}
}

// function show_produit
function show_produit(t)
{

	var xml_doc = t.responseXML;
	if(xml_doc.getElementsByTagName("error")[0].firstChild != null)
	{
		alert(xml_doc.getElementsByTagName("error")[0].firstChild.nodeValue)
	}
	else
	{
		
		var delai        = "";
		rating_flag = false;
		
		var id        	 = xml_doc.getElementsByTagName("produit")[0].attributes[0].nodeValue;
		var url        	 = xml_doc.getElementsByTagName("produit")[0].attributes[1].nodeValue;
		var sc_id      	 = xml_doc.getElementsByTagName("produit")[0].attributes[2].nodeValue;
		var sc_url     	 = xml_doc.getElementsByTagName("produit")[0].attributes[3].nodeValue;
		var target     	 = xml_doc.getElementsByTagName("produit")[0].attributes[4].nodeValue;
		
		var type       	 = xml_doc.getElementsByTagName("produit")[0].attributes[5].nodeValue;
		var genre        = xml_doc.getElementsByTagName("produit")[0].attributes[6].nodeValue;
		var prix  		 = xml_doc.getElementsByTagName("produit")[0].attributes[7].nodeValue;
		var prix_retail  = xml_doc.getElementsByTagName("produit")[0].attributes[8].nodeValue;
		
		var metal        = xml_doc.getElementsByTagName("produit")[0].attributes[9].nodeValue;
		var dimension    = xml_doc.getElementsByTagName("produit")[0].attributes[10].nodeValue;
		var gravable     = xml_doc.getElementsByTagName("produit")[0].attributes[11].nodeValue;
		
		var poids_diamant    = xml_doc.getElementsByTagName("produit")[0].attributes[12].nodeValue;
		var nb_diamant       = xml_doc.getElementsByTagName("produit")[0].attributes[13].nodeValue;
		var min_color        = xml_doc.getElementsByTagName("produit")[0].attributes[14].nodeValue;
		var min_clarity      = xml_doc.getElementsByTagName("produit")[0].attributes[15].nodeValue;
		var poids_or         = xml_doc.getElementsByTagName("produit")[0].attributes[16].nodeValue;
		var qualite_or       = xml_doc.getElementsByTagName("produit")[0].attributes[17].nodeValue;
		var url_image_detail = xml_doc.getElementsByTagName("produit")[0].attributes[18].nodeValue;
	
	
		var nom   	  = xml_doc.getElementsByTagName("nom")[0].firstChild.nodeValue;
		
		
		if(xml_doc.getElementsByTagName("description")[0].firstChild != null)
			var texte 	  = xml_doc.getElementsByTagName("description")[0].firstChild.nodeValue;
		else
			var texte 	  = "";
		
		stock 	      = xml_doc.getElementsByTagName("taille")[0];
		var livraison = xml_doc.getElementsByTagName("livraison")[0];
		var media 	  = xml_doc.getElementsByTagName("media")[0];
		var verbatim  = xml_doc.getElementsByTagName("verbatim")[0];
		var chaine    = xml_doc.getElementsByTagName("chaine")[0];
		
		
		// msg livraison
		livraison_ko = livraison_txt_ko + " " + livraison.getElementsByTagName("ko")[0].firstChild.nodeValue + ".";
		livraison_ok = "";
		if(livraison.getElementsByTagName("ok")[0].firstChild != null)
		{
			delai = livraison_txt_ok + " " + livraison.getElementsByTagName("ok")[0].firstChild.nodeValue + ".";
			livraison_ok = delai;
		}
		else
			delai = livraison_ko;
		
		// gravable
		if($('gravable'))
		{
			$('gravable').checked = false;
			
			if(gravable == 1)
				Element.show('bloc_gravable');
			else
				Element.hide('bloc_gravable');
			
		}	
		if($('perso'))
			$('perso').value = "";
			
		// frm
		$('produit_id').value    =  id;
		$('produit_type').value  =  type;
		$('produit_genre').value =  genre;
		if($('chaine_id'))
			$('chaine_id').value 	 =  "";
		
		
		// wish link
		if($('picto_cadeau2'))
			$('picto_cadeau2').href = "wish.php?produit=" + id + "&lg=" + LANG;
		
		if($('picto_letter'))
			$('picto_letter').href = $('picto_cadeau2').firstChild.href;


		if($('produit_link'))
		{
			
			$('produit_link').href = decodeURIComponent(target);
			Element.update( $('produit_link'), decodeURIComponent(target));
			$('fbiframe').src ='http://www.facebook.com/plugins/like.php?href='+encodeURIComponent(target)+'&amp;layout=button_count&amp;show_faces=false&amp;width=300&amp;action=like&amp;colorscheme=light';
		}

		if(type == 13)
		{
			$('link-alliance').show();
			$('link-alliance').href = "creation-alliance.php?produit=" + id 
		}		
		else
			$('link-alliance').hide();

		if(type == 14)
		{
			$('link-bague').show();
			$('link-bague').href = "creation-bague.php?produit=" + id 
		}		
		else
			$('link-bague').hide();


		if($('produit_btn_z'))
			$('produit_btn_z').title = nom;

		if($('produit_btn_zz'))
			$('produit_btn_zz').title = nom;
		
		
		// details
		if($('produit_prix_retail'))
			Element.update( $('produit_prix_retail'), prix_retail);
		
		if($('produit_prix'))
			Element.update( $('produit_prix'), prix);
		if($('produit_nom'))
			Element.update( $('produit_nom'), nom);
		
		if($('produit_texte'))
			Element.update( $('produit_texte'), texte);
		
		//Element.update( $('detail_stock'), stock_commun);
		if($('detail_id'))
			Element.update( $('detail_id'), id);
		if($('points'))
			Element.update( $('points'), Math.round(point*(Number(prix.replace(/€/, '')))/100));
		
		if($('produit_conversion'))
			Element.update( $('produit_prix_hu'), '&nbsp;(~ ' + Math.round((Number($('produit_conversion').value))*(Number(prix.replace(/€/, '')))));
			

		make_row_detail('detail_poids_diamant', poids_diamant);
		make_row_detail('detail_nb_diamant', nb_diamant);
		make_row_detail('detail_min_color', min_color);
		make_row_detail('detail_min_clarity', min_clarity);
		make_row_detail('detail_metal', metal);
		make_row_detail('detail_poids_or', poids_or);
		make_row_detail('detail_qualite_or', qualite_or);
		if($('detail_img'))
		{
			if(url_image_detail)
			{
				$('detail_img').src = url_image_detail;
				Element.show('detail_img');
			}
			else
				Element.hide('detail_img');
		}
		
		if($('produit_livraison'))	
			Element.update( $('produit_livraison'), delai);
		//Element.hide('produit_vote_msg');
		
		// select visible ou pas
		// et remise à zero de la section
		$('taille').selectedIndex = 0;
		if(genre == 1)
			Element.show('taille');
		else
			Element.hide('taille');
		
		// taille
		var nodes = stock.getElementsByTagName("option");
		var nbtaille = nodes.length;
		tab_taille.clear();
		tab_date.clear();
		
		
		if(nbtaille)
		{
			for (var i=0; i<nbtaille; i++)
			{
				tab_taille.push(nodes[i].attributes[1].nodeValue);
				tab_date[nodes[i].attributes[1].nodeValue] = nodes[i].attributes[0].nodeValue;
			}
		}
		
		
		// chaine
		if($('chaine_id'))
		{
			$('get_chaine').selectedIndex = 0;
			if(genre == 2)
				Element.show('content_get_chaine');
			else
				Element.hide('content_get_chaine');
		}
	
		
		// delete les images actuelles
		var li = $$('#p_img dd:not([class~=produit_zoom])');
		for(var i=0, nbli=li.length; i<nbli; i++)
			li[i].remove();
		

		// add les nouvelles images
		nodes = media.getElementsByTagName("item");
		nbnodes = nodes.length;
		
		if(nbnodes)
		{
			
			for (i=0, t="", l="", lst=""; i<nbnodes; i++)
			{
				a = new Array();
				
				// thumb
				t = nodes[i].attributes[0].nodeValue;
				zoom_status = nodes[i].attributes[2].nodeValue;
				a.push(t);
				
				// media image
				if(nodes[i].attributes[1].nodeValue == "img")
				{
					l = t.replace(/-t/, "-l");
					a.push(l);
				}
				else
				{
					l = t.replace(/-t.jpg|-t.gif/, "-l.swf");
				}
				
				preload(a);
				
				
				
				// hilite le premier item
				css = (!i) ? 'on' : '';
				if(css)
				{
					switch_img(l, zoom_status)
					//load_zoom(l, zoom_status)
				}
				
				lst = document.createElement("dd");
				lst.className = css;
				
				lien = document.createElement("a");
				lien.href = "javascript:void(0)"
				lien.media_large = l;
				lien.zoom = zoom_status
				lien.onclick = function(){
					return hilite_img(this.parentNode, this.media_large, this.zoom)
				}
				
				toto = document.createElement("img");
				
				toto.src = t;
				toto.style.width = 39 + "px";
				toto.style.height = 39 + "px";
				
				lien.appendChild(toto);
				
				lst.appendChild(lien);
				
				$('p_img').appendChild(lst);
				
			}
		}	
		else
		{
			alert("Aucune images trouvées pour ce bijou");
		}


		//Verbatim
		if($('tab_verbatim'))
		{
			nodes = verbatim.getElementsByTagName("customer");
			nb_elt = nodes.length;
			if(!nb_elt)
			{
				$('tab_verbatim').hide();
				switch_tab(0);
			}
			else
			{
				$('tab_content3').innerHTML = "";
				for (i=0; i<nb_elt; i++)
				{
					if(nodes[i].attributes[0].nodeValue != null)
						info  = nodes[i].attributes[0].nodeValue;
					
					if(nodes[i].firstChild.nodeValue)
						txt  = nodes[i].firstChild.nodeValue;
					
					
					div = document.createElement('div');
					div.className = 'verbatim_produit';
					
					blockquote = document.createElement('blockquote');
					v = document.createTextNode(txt);
					blockquote.appendChild(v);
					
					h = document.createElement('h5');
					n = document.createTextNode(info);
					h.appendChild(n);
					
					
					div.appendChild(blockquote);
					div.appendChild(h);
					$('tab_content3').appendChild(div);
				
				}
				
				$('tab_verbatim').show();
				
			}
		}

		// produit associer
		var pp  = xml_doc.getElementsByTagName("produit_produit")[0];
		nodes = pp.getElementsByTagName("item");
		
		$$('#liaison_metal dd').each(function(e, i){ if(i) {e.remove(); } });
		$$('#liaison_collection dd').each(function(e, i){ if(i) {e.remove(); } });
		$('liaison_metal').hide();
		$('liaison_collection').hide();
		
		
		if(nodes.length && !box_id)
		{
				
			var nb_elt = nodes.length;
			for (i=0, nom="", q=false, prix="", url_image="", target="", type="", m= ""; i<nb_elt; i++)
			{
				if(nodes[i].attributes[0].nodeValue != null)
					nom  = nodes[i].attributes[0].nodeValue;
				
				prix 	  = nodes[i].attributes[1].nodeValue;
				url_image = nodes[i].attributes[2].nodeValue;
				target    = nodes[i].attributes[3].nodeValue;
				type      = nodes[i].attributes[4].nodeValue;
				nb        = nodes[i].attributes[5].nodeValue;
				
					
				li = document.createElement("dd");
				a = document.createElement("a");
				a.href = decodeURIComponent(target)
					
				imag = document.createElement("img");
				imag.src = url_image;
				imag.alt = nom;
				imag.style.width = 45 + "px" ;
				imag.style.height = 45 + "px" ;
					
				a.appendChild(imag); 
				span = document.createElement("span");
				span.className = "left";
				stxt = document.createTextNode(nom+" ");
				span.appendChild(stxt);
				
				nob = document.createElement("nobr");
				sprix = document.createTextNode(prix);
				nob.appendChild(sprix);
				span.appendChild(nob);
				
				a.appendChild(span);
				li.appendChild(a);
				
				if(!type)
				{
					
					
					$('liaison_collection').appendChild(li);
					$('liaison_collection').show();
				}
				else
				{
					
					$('liaison_metal').appendChild(li);
					$('liaison_metal').show();
					
				}
					
			}
			
		}
		else
		{	
			switch_tab(0);
		}
		
		
	}



	// stat
	vue();
	current_hit('consultation=1');
	pageTracker._trackPageview("/AJAX/" + id);

	if($('criteo_track_img'))
	{
		$('criteo_track_img').src = 'http://edenly.widget.criteo.com/pdf/display.js?p1=' + escape('v=2&wi=7709670&pt1=2&i=' + id ) + '&t1=sendEvent&resptype=gif&cb='+Math.floor(Math.random()*99999999999)
		pcto_dis();
	}
}

// hilite_img
function hilite_img(elt, url_image, zoom)
{
	var li = $$('#p_img dd:not([class~=produit_zoom])');
	for(var i=0, nb=li.length; i<nb; i++)
		li[i].className = (li[i] != elt) ? "" : "on";
	
	
	switch_img(url_image, zoom);
	return false;
}


// change_img
function switch_img(url_media, zoom)
{
	
	s = false;
	// pour eviter le sautillement dù au flash
	if($('produit_media_content').firstChild.tagName != "IMG")
		$('produit_media_content').innerHTML = "";
		
	new Effect.Fade($('produit_media_content'),{
		duration:0.5
		,beforeStart : function(){ if($('produit_btn_z')){Element.hide('produit_btn_z');}}
		,afterFinish : function(){
			$('produit_media_content').innerHTML = "";
			if(!url_media.match(/swf/))
			{
				image = document.createElement("img");
				image.width  = "360";
				image.height = "360";
				image.src = url_media;
				$('produit_media_content').appendChild(image); 
			}
			else
				s = true;
				
			
			Effect.Appear($('produit_media_content'),{
				duration:0.8
				,afterFinish : function(){
					if(s)
						load_swf(url_media, 'produit_media_content', 360, 360)
					
					load_zoom(url_media, zoom);
					
				}
			});
		}
		,queue:{scope:'scope', position:'front', limit: 1}
	});
	
}

// zoom
function load_zoom(url_media, zoom)
{
	if(zoom == "z")
	{
		if($('produit_btn_z'))
			Element.show('produit_btn_z');
		param = new Array();
		param[0] = url_media.replace(/-l/, "-g");
		param[1] = url_media.replace(/-l/, "-z");
		load_swf("img/zoom.swf", 'z_swf', 600, 600, param);
	}
	else if(zoom == "3d")
	{
		if($('produit_btn_z'))
			Element.show('produit_btn_z');
		load_swf(url_media, 'z_swf', 600, 600);
	}
	else
	{
		if($('produit_btn_z'))
			Element.hide('produit_btn_z');
	}	
}

// load_swf
function load_swf(media, contener, width, height, param)
{
	zoom = new SWFObject(media, "zoom_swf", width, height, "8", "#ffffff");
	zoom.addParam("wmode", "transparent");
	
	if(param && param.length)
		zoom.addVariable("image", param[0] + '?zoom=' + param[1]);
	
	
	zoom.write(contener);
	
}


// count_row
function count_row(nb)
{
	return;
	if($('nb_row'))
		$('nb_row').innerHTML =  nb;
}

// check_shipping
function check_shipping(value)
{
	delai = (tab_taille.include(value)) ? livraison_txt_ok + " " + tab_date[value] : livraison_ko;
	Element.update( $('produit_livraison'), delai);
}

// check_taille
function produit_check(frm, msg1, msg2, msg3)
{

	if($('produit_genre').value == 1 && !$('taille').selectedIndex)
	{
		alert(msg1);
		return false;
	}
	

	if($('produit_genre').value == 2 && !$('get_chaine').selectedIndex)
	{
		alert(msg3);
		return false;
	}
	
	if(frm.gravable.checked && !frm.perso.value.length)
	{
		alert(msg2);
		return false;
	}

		
	return true;
}



// window open
function wOpen(url, w, h)
{
	obj = window.open(url, "popup", "top=80,left=250,status=no,scrollbars=yes,width=" + w + ",height=" + h);
	obj.focus(); 
	return false; 
}






