var pagecache = new Array();
var hostname = window.location.hostname;
 
$(document).ready(function() {

	var ajaxManager = $.manageAjax.create('ajaxManager',{queue: true, maxRequests: 1, preventDoubbleRequests : true});
	
	$(document).bind("loadPrices", 
					function (e){
						$("div[id^='dynPriceDetail_']:visible").each(function(){							
							var id = jQuery(this).attr("id");
							if(pagecache[id] != null)return;
							pagecache[id]=true;
							var codiceAziendale = id.substring(15,id.length);
							var stage="";
							if(hostname.indexOf('cmstage') >= 0)stage="&stage=S";
							ajaxManager.add({
								url: "/solutions/carrefour/price/carrefourPriceDetailSlim.jsp?codiceAziendale="	+ codiceAziendale + stage +"&_=" + new Date().getTime(),
								dataType: 'html',
								success: function(data,textstatus){			
											var cod = jQuery(data).find("input[id^='codiceAziendale']").val();
   											var volantino =	jQuery(data).find("input[id^='nomeVolantino']").val();
											var valido = jQuery(data).find("input[id^='valiVolantino']").val();					
											$("div[id='dynPriceDetail_" + cod + "']").each(function(){
												jQuery(this).html(data);
												jQuery("#v_valido_"+cod).html(valido);
												jQuery("#v_volantino_"+cod).html(volantino);							
											});
								 		}
							});		
						});	
	});
	$(document).trigger("loadPrices");
	$(document).bind("loadPricesExt", 
					function (e){
						$("div[id^='dynPriceDetailExt_']:visible").each(function(){
							var id = jQuery(this).attr("id"); 
							if(pagecache[id] != null)return;
							var stage="";
							if(hostname.indexOf('cmstage') >= 0)stage="&stage=S";
							pagecache[id]=true;
							var codiceAziendale = id.substring(18,id.length);
							ajaxManager.add({
								url: "/solutions/carrefour/price/carrefourPriceDetail.jsp?codiceAziendale="	+ codiceAziendale + stage +"&_=" + new Date().getTime(),
								dataType: 'html',
								success: function(data,textstatus){			
											var multi = jQuery(data).find("input[id='multi']").val();
											if ("true"==multi){
												var cod = jQuery(data).find("input[id^='codiceAziendale']").val();
												var superiore = jQuery("#v_volantino_"+cod).parent();
												var nome = jQuery("#v_volantino_"+cod);
												var validit = jQuery("#v_valido_"+cod);
												var count = 0;
												jQuery(data).find("input[id^='nomeVolantino']").each(function(){
													count++;
													if (count>1){
														var cloneNome = nome.clone();
														cloneNome.attr("id",cloneNome.id+count)
														cloneNome.html(jQuery(this).val());
														//superiore.html(superiore.html()+cloneNome.html());
														var flyerNumber = jQuery(this).attr("id").substring(22);
														var findParam = "input[id='valiVolantino_"+cod+"_"+flyerNumber+"']";
														var cloneVal = validit.clone();
														cloneVal.attr("id",cloneNome.id+count)
														cloneVal.html(jQuery(data).find(findParam).val());														
														validit.after(cloneVal);
														validit.after(cloneNome);
														//superiore.html(superiore.html()+cloneVal.html());
													} else {
														$("div[id='dynPriceDetailExt_" + cod + "']").html(data);
														nome.html(jQuery(this).val());
														var flyerNumber = jQuery(this).attr("id").substring(22);
														var findParam = "input[id='valiVolantino_"+cod+"_"+flyerNumber+"']";
														validit.html(jQuery(data).find(findParam).val());							
													}
												});
											} else {
												var cod = jQuery(data).find("input[id^='codiceAziendale']").val();
												var volantino =	jQuery(data).find("input[id^='nomeVolantino']").val();
												var valido = jQuery(data).find("input[id^='valiVolantino']").val();					
												$("div[id='dynPriceDetailExt_" + cod + "']").each(function(){
													jQuery(this).html(data);
													jQuery("#v_valido_"+cod).html(valido);
													jQuery("#v_volantino_"+cod).html(volantino);							
												});
											}
								 		}
							});		
						});	
	});
	$(document).trigger("loadPricesExt");	
});
