$(document).ready(function(){
	if($('#compara-container').is('div')){
		popolaProdotti();
	}
});


function popolaProdotti(){

	$(".fixed-row").each(function(){
		$("#specchio-sx table tbody").append('<tr class="data"><td>&nbsp;</td></tr>');
		$("#specchio-dx table tbody").append('<tr class="data"><td>&nbsp;</td></tr>');
		$("#compara-intermezzo table tbody").append('<tr class="data"><td>&nbsp;</td></tr>');
	});

	var html = '';
	$.ajax({
			type: "GET",
			url: sRootPath+langXml+"/XML/PRODOTTI/data.xml",
			dataType: 'xml',
			async:false,
			success: function(msg){

					$(msg).find("Item").each(function(cnt){
								var idn=$(this).attr('id');
								var sel1="";
								var sel2="";
								var sel3="";

								if(cnt==0){
									sel1='selected="selected"';
									changeProduct(idn,1);
								}

								if(cnt==1){
									sel2='selected="selected"';
									changeProduct(idn,2);
								}
								
								if(cnt==2){
									sel3='selected="selected"';
									changeProduct(idn,3);
								}

								$("select[name=product1]").append("<option value=\""+idn+"\" "+sel1+">"+$("titolo",this).text().replace(/(<([^>]+)>)/ig,"")+"</option>");
								$("select[name=product2]").append("<option value=\""+idn+"\" "+sel2+">"+$("titolo",this).text().replace(/(<([^>]+)>)/ig,"")+"</option>");
								$("select[name=product3]").append("<option value=\""+idn+"\" "+sel3+">"+$("titolo",this).text().replace(/(<([^>]+)>)/ig,"")+"</option>");
					})
			}
	});
}

function changeProductSel(){
	var a=$('#compareform select[name=product1]').val();
	var b=$('#compareform select[name=product2]').val();
	var c=$('#compareform select[name=product3]').val();
if(a!=b && b!=c && a!=c){
	changeProduct(a,1);
	changeProduct(b,2);
	changeProduct(c,3);
}else{
	if(langXml=='it'){
		alert("Il prodotto selezionato e' gia' presente nella tabella comparativa, scegli un nuovo prodotto");
	}else{
		alert("the requested product is already in the comparing table, choose a new one");
	}
	}
}

function changeProduct(idprodotto,iddivcontainer){
	var html = '';
	$.ajax({
			type: "GET",
			url: sRootPath+langXml+"/XML/PRODOTTI/data.xml",
			dataType: 'xml',
			async:false,
			success: function(msg){
								var news = $(msg).find("Item").filter("[id="+idprodotto+"]");
								tabella = $("tabella_compara", news).text().replace(/(&#160;)/ig,"");
								$("#table"+iddivcontainer).html(tabella)
			}
	});
	//alert("aa");
	$(".compara-prodotto .data td").each(function(cn){
	//	alert("+"+"cn"+cn+"---"+$(this).html()+"+"+($(this).html()==''));
		if($(this).html()==''){
			$(this).html('<span style="color:#fff;">--</span>');
		}
	});
}

