$(document).ready(function(){						   
	/*$(".pro_img").empty();
	$('.pro_img').append('<img src="'+$('.pro_img').attr('rel')+'"></img>');
	$('.pro_img img').fadeIn(500);*/

	//selProdotto();
	$(".pro_dx a").click(function(){
		var item = $(this).attr('rel');
		window.setTimeout(function(){
			selProdotto(item);
		}, 20);
	}); 
	$(".pro_dx a:first").click();
	//Click cambio prodotto
	$(".pro_sx_01 li").click(function(){
		$(".pro_sx_01 li").removeClass("first");
		$(this).addClass('first');
		//$("li.first a").click();
	});
	openLayer();
});
		
function openLayer(){
	//Schede Tecniche/Organolettiche/Abbinamenti
	$("#close").click(function(){
		MyChiudi();
	});
	$(".pro_sx_02").click(function(){
		$(".overlayer").fadeIn(500);
	});
	$("#scheda_tec").click(function(){
		MyChiudi();
		$(".tecnica").fadeIn(500);
	});	
	$("#abbinamenti").click(function(){
		MyChiudi();
		$(".abbinamenti").fadeIn(500,function(){
			var h = parseInt($("#abbinamenti_js").css('height'))+120;
			var h2 = parseInt($("#generale").css('height'))+67;
			if(h2>= h){
				var h3 = h2;
			}else{
				var h3 = h;
			}
			$(".overlayer").height(h3+'px');
		});
	});
	$("#scheda_org").click(function(){
		MyChiudi();
		$(".organolettica").fadeIn(500);
	});
}
//funzione chiusura overlayer
MyChiudi = function() {
	$('.overlayer').hide();
	$('.tecnica').hide();
	$('.abbinamenti').hide();
	$('.organolettica').hide();
}
	
function selProdotto(item) {
	if($(".pro_dx")){
		$.ajax({
			type: "GET",
			url: "cms/prodottixml.php?lang="+$("#lang").text()+"&cat="+$("#cate").text(),
			data: "",
			dataType: "xml",
			success: function(xmlRespnse) {
				$(xmlRespnse).find(item).each(function () {
					$("#nomeprodotto").empty();
					$("#nomeprodotto").append($(this).find("Title", xmlRespnse).text());
					$(".pro_sx_02").empty();
					$(this).find('dettagli').each(function(){
						$(".pro_sx_02").append('<li><a style="cursor:pointer;display:block;" title="' + $(this).text() +'" id="'+$(this).attr('id')+'">' + $(this).text() +'</a></li>');						   
					});
					$("#premi").attr('src',''+$(this).find("premi", xmlRespnse).text())
					$(".pro_sx_02 li a:last").addClass("last");
					$(".pro_img").empty();
					$('.pro_img').append('<img src="'+$(this).find("image", xmlRespnse).text()+'"></img>');
					$('.pro_img img').fadeIn(500);
					if($(this).find("schedatecnica").text()!= ""){
						$.ajax({
							type: "GET",
							url: "cms/schedatecnica.php?lang="+$("#lang").text()+"&id="+$(this).find("id", xmlRespnse).text(),
							data: "",
							dataType: "html",
							success: function(responsehtml) {
								$("#tecnica_js").empty();
								$("#tecnica_js").append(responsehtml);
								openLayer();
							}
				    	});
					}else{
						$("#tecnica_js").remove();
					}
					$.ajax({
						type: "GET",
						url: "cms/schedaorganolettica.php?lang="+$("#lang").text()+"&id="+$(this).find("id", xmlRespnse).text(),
						//url: "schede/"+$(this).find("schedaorg", xmlRespnse).text(),
						data: "",
						dataType: "html",
						success: function(responsehtml) {
							$("#organolettica_js").empty();
							$("#organolettica_js").append(responsehtml);
							openLayer();
						}
			    	});
					//if($(this).find("abbinamenti").text()!= ""){
						$.ajax({
							type: "GET",
							url: "cms/abbinamenti.php?lang="+$("#lang").text()+"&id="+$(this).find("id", xmlRespnse).text(),
							//url: "schede/"+$(this).find("schedaorg", xmlRespnse).text(),
							data: "",
							dataType: "html",
							success: function(responsehtml) {
								$("#abbinamenti_js").empty();
								$("#abbinamenti_js").append(responsehtml);
								openLayer();
							}
				    	});
					/*}else{
						$("#abbinamenti_js").remove();
					}*/
					openLayer();
				});
			}
    	});
		/*chiudi overlayer*/
		$(document).keydown( function( e ) {
			if( e.which == 27) {  // escape, close box
				MyChiudi();
			}
		});
  	}          
};

				
		
		
