

function showcri(segid,e)	{
	var doc = document.getElementById ? document.getElementById(segid).style : document.all? document.all[segid].style: null;

	if (doc.display == 'none') {
		var posx = 0;
		var posy = 20;
		if (!e) var e = window.event;
		if (e.pageX) { 
			posx = e.pageX;
			// posy = e.pageY
		}
		else if (e.clientX) {
			posx = e.clientX + document.body.scrollLeft;
			// posy = e.clientY + document.body.scrollTop;
		}
		with (doc) {
			display='';
			top = parseInt(posy) - 5
			left = parseInt(posx) - 20
		}
		window.setTimeout("showcri('" + segid + "',window.event)",5000)
	}
	else doc.display = 'none'
}

function chgEtat(objImage,id,nom) {	
	var imageMoins = false;
	var doc = document.getElementById? document.getElementById('cri'+id) : document.all? document.all['cri'+id]: null;
	if(doc.length != void(0)) {
		for (var i=0;i<doc.length;i++)	{
			if(doc[i].style.display == 'none') {
				doc[i].style.display = '';
				imageMoins = true;
				}
			else doc[i].style.display = 'none';
			}
		}
	else {
		if(doc.style.display == 'none') {
			doc.style.display = '';
			imageMoins = true;
			}
		else doc.style.display = 'none';
		}
	if(imageMoins) objImage.src = '/images/tab/desc' + nom + '.gif';
	else objImage.src = '/images/tab/fl' + nom + '.gif';
	}
	
function getElementbyClass(classname){
	ccollect=new Array()
	var inc=0
	var alltags=document.all? document.all : document.getElementsByTagName("*")
	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==classname)
			ccollect[inc++]=alltags[i]
	}
}
		
function contractcontent(omit){
	var inc=0
	while (ccollect[inc]){
		if (ccollect[inc].id!=omit)
			ccollect[inc].style.display="none"
		inc++
	}
}
		
function expandcontent(cid){
	if (typeof ccollect!="undefined")
		document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
}

/***** CheckList *****/
$(document).ready(function() {
	$(".checkList dt").click(function(e) {
		$(".checkList dd ul").not( $(this).parent("dl").find("dd ul") ).hide("fast");
		$(this).parent("dl").find("dd ul").toggle("fast");
		e.stopPropagation();
	});
   $(".checkList dd ul li a").click(function(e) {
		$(this).find("input").attr("checked", !$(this).find("input").attr("checked"));
		updateListHeader($(this).parents("dd:eq(0)"), $(this).find("input").eq(0));
		$(this).attr("href","javascript:void(0)");
		e.stopPropagation();
		return false;
    });
    $(".checkList dd ul li a input").click(function(e) {
    	updateListHeader($(this).parents("dd:eq(0)"),$(e.target));
    	$(this).parents("a:eq(0)").attr("href","javascript:void(0)");
    	e.stopPropagation();
   	});
    $(document).bind('click', function(e) {
		$(".checkList dd ul").hide("fast");
    });
    $("dl.checkList").each(function(){ 
    	$(this).find("ul").width( ( $(this).find("ul").width() < $(this).find("dt").width() )?$(this).find("dt").width()-2:$(this).find("ul").width() );
    });
	if(typeof $("#ta" + "bAu" + "tre" + "sCat") != 'undefined')
	{
		$("#tabBestSearch").before( $("#ta" + "bAu" + "tre" + "sCat").remove() );
		$("#ta" + "bAu" + "tre" + "sCat").css("display", "block");
	}	
});
function updateListHeader(dd,chk)
{
	(dd.find("input:checked").length > 0)?dd.parents("dl:eq(0)").find("dt a").addClass("sel"):dd.parents("dl:eq(0)").find("dt a").removeClass("sel");
	if ( chk.is(':checked') && jQuery.inArray(chk.val().toLowerCase(),["oui","non","si","no"]) != -1)
	{
		TabCheckState = (jQuery.inArray(chk.val().toLowerCase(),["oui","si"]) != -1)?["non","no"]:["oui","si"];
		chk.parents("ul:eq(0)").find("input").each(function(){if( jQuery.inArray($(this).val().toLowerCase(),TabCheckState) != -1 ){$(this).attr("checked",false)}})
	} 
}
function onSubmitForm()
{
	$(".checkList dd ul").each(function(){ $("#"+$(this).attr("id")+"_list").attr("value", $(this).find("input:checked").map(function(){return $(this).val()}).get().join(","))});
}
/***** Fin CheckList *****/ function formatPrice (inn) {
	if ( typeof inn ) inn = parseFloat(inn);
	return inn.toFixed(2).toString().replace(/\./gi,",");
}

gmhtTpp = function (_cid, _cat_id, _objectgroup_id, _coeffpromo, _coeffpromoseuil) {
	
	this.cid = _cid;
	this.cat_id = _cat_id;
	this.objectgroup_id = _objectgroup_id;
	this.coeffpromo = _coeffpromo;
	this.coeffpromoseuil = _coeffpromoseuil;
	_this = this;
	$.disposContents = {"S" : "<img src='http://www.rueducommerce.fr/images/locales/fr-fr/dispo/mini_en_stock.gif'/>", "E" : "En cours de reappro", "R" : "En cours de réappro", "P" : "En précommande", "C" : "Sur commande"};
	$.mpid_masters = new Array();
	$.cp = this.coeffpromo;
	$.cpt = this.coeffpromoseuil;
	//nbre max de mpid récupérés en une seule fois
	var nbre_mpid_a_recup = 20;
	
	this.load = function () {
		$("tr.zm").each (function(i) {$.mpid_masters.push( $(this).attr('mpid_master'))});
		this.loadTpp();
	}
	
	this.loadTpp = function () {
		if ($.mpid_masters.length > 0) {
			
			this.getMpid = function (mpids,nbre_mpid_a_recup) {
				if (mpids.length > nbre_mpid_a_recup){
					return mpids.slice(0,nbre_mpid_a_recup);
				}
				else {
					return mpids.slice(0,mpids.length);
				}
			}

			this.url = function (mpids,cp,cpt) {
				var domaine = document.location.href;
  			 	domaine = domaine.substr ( 7 , domaine.indexOf("/",7) - 7 ); // 7 c'est pour http://
				if (( domaine == "www.rueducommerce.fr" ) || ( domaine == "search.rueducommerce.fr" ))
					return "http://www.rueducommerce.fr/m/high_tech/get_v3_response.php?url=0&mpids=" + mpids+ "&cp=" + cp+ "&cpt=" + cpt + "&fpgmht=1";
				else {
					return "/proxy.cfm?url=http://www.alapage.com/m/_scripts/get_products_representant.php?url=0" + escape( "&mpids="+mpids + "&cp=" + cp + "&cpt=" + cpt);
				}			
			}
			
			if ($.mpid_masters.length > nbre_mpid_a_recup){
				callHttpRequestRdc( this.url(this.getMpid($.mpid_masters,nbre_mpid_a_recup),$.cp,$.cpt) , this );
				for (var j = 0; j < nbre_mpid_a_recup; j++){
					$.mpid_masters.shift();
				}					
			} else {
				callHttpRequestRdc( this.url($.mpid_masters,$.cp,$.cpt) , this );
				$.mpid_masters = "";
			}	
		}
	}
	
	this.callback = function ( xmlrequest ) {	
		var products = xmlrequest.getElementsByTagName("product");
		//on boucle sur tous les produits 	
		for (var i=0; i<products.length; i++){
			var tr = $("tr.zm[mpid_master="+products[i].getAttribute('mpid')+"]");
			//parcours des différents noeuds
			var minprice =  products[i].getElementsByTagName("min_price")[0].childNodes[0].nodeValue;
			$("td.dispo", tr).html($.disposContents[products[i].getElementsByTagName('best_availability')[0].childNodes[0].nodeValue]);
			var nbmerchant = products[i].getElementsByTagName('nb_merchant')[0];
			if (nbmerchant.childNodes.length > 0 && products[i].getElementsByTagName('nb_merchant')[0].childNodes[0].nodeValue > 1) {
				$("td.price span", tr).show();
				$("td.price span:first", tr).html("<p class='ap'>A partir de</p> " + formatPrice(minprice) + " €");
				$("td .nm", tr).show().html("Vendu par " + products[i].getElementsByTagName('nb_merchant')[0].childNodes[0].nodeValue+ " marchands");
			} else {
				$("td.price span", tr).show();
				$("td.price span:first", tr).html(formatPrice(minprice) + " €");
			}
			var pricetype = products[i].getElementsByTagName("offer")[0].getElementsByTagName('price')[0];
			if (pricetype.childNodes.length > 0 && products[i].getElementsByTagName("offer")[0].getElementsByTagName('price')[0].getAttribute('type')!== 'base'){
				date = new Date();
				var sd = (date.getYear()+1900) + "-"+(date.getMonth()+1) + "-"+date.getDate() + " "+date.getHours() + ":"+date.getMinutes() + ":"+date.getSeconds() 
				if (sd >= $('date_start',pricetype).text() &&  sd <= $('date_end',pricetype).text()) {
					$("td:nth-child(2)", tr).prepend('<img class="vfS" src="/images/locales/fr-fr/tpp/'+products[i].getElementsByTagName('offer')[0].getElementsByTagName('price')[0].getAttribute('type')+'.png"/>');
				}
			}
			$(".zm td.price span:hidden").show().parent().next("td.dispo").html("Temporairement indisponible").show();
		}
		if ($.mpid_masters.length > 0){
			setTimeout(this.loadTpp(),1000);
		}		
	}
	

	
	this.callbackError = function() {
		$(".zm td.price span").show();
		$(".zm td.dispo").html($.disposContents['S']);				
	}
} 