/**
 * @author mario
 */
var nCurrentRegione=8;

$(document).ready(function() {
	
	drawPuntiItalia();
	nCurrentRegione=8;
	$("#negozi2").append("<img id=\"immagine_regione\" style=\"display: none\" src=\"\" width=\"180\" height=\"315\" />")
	drawCurrentRegione();
	$(window).resize(function() {
	
		drawPuntiItalia();
		drawCurrentRegione();
		
	});
});

function showRegione(nId)
{
	nCurrentRegione=nId;
	drawCurrentRegione();
	
}

function drawPuntiItalia()
{
	$(".punto").remove();
	// otteniamo i punti via ajax
	$.post("ajax_get_punti.php", { regione: ""}, onGetPuntiItalia);
		
}

function onGetPuntiItalia(xmlText)
{
	var immagineTop=parseInt($("#immagine_italia").offset().top);
	var immagineLeft=parseInt($("#immagine_italia").offset().left);

	$("punto",xmlText).each(function() {
		
		posTop=parseInt($("top",this).text());
		posLeft=parseInt($("left",this).text());
		citta=$("citta",this).text();
		id_punto=$("id_punto",this).text();
		//alert("append a "+(posTop+immagineTop)+" "+(posLeft+immagineLeft));
		$("#lista_punti").append("<img src=\"images/punto.gif\" id=\""+id_punto+"\" class=\"punto\" style=\"top: "+(immagineTop+posTop)+"px; left: "+(immagineLeft+posLeft)+"px;\" title=\""+citta+"\" alt=\""+citta+"\"/>");
		
		
		
	});
	$(".punto").hover(function() {
		
		$(this).addClass("pointer");
		
		
	}, function() {
		
		$(this).removeClass("pointer");
	});
	
	$(".punto").click(onClickPunto);
	
	
}

function drawCurrentRegione()
{
	$.post("ajax_get_data_regione_by_id.php",{ id: nCurrentRegione },onGetImgRegione);
}



function onGetImgStartRegione(xmlText)
{
	
	drawPuntiRegione($("id",xmlText).text());
}

function onClickPunto()
{
	nId=$(this).attr("id");
	$.post("ajax_get_data_regione_by_punto.php",{ id: nId},onGetImgRegione);
}

function onGetImgRegione(xmlText)
{
	var nId;
	var strPath;
	
	nId=$("id_regione",xmlText).text();
	strPath=$("path",xmlText).text();
	nCurrentRegione=nId;
	drawRegione(strPath);
	drawPuntiRegione(nId);
}

function drawRegione(strPath)
{
	$("#immagine_regione").attr("src","lib_showimage/showimage.php?filename="+escape(strPath)+"&x=180&y=100&keep=x");
	$("#immagine_regione").show();
}

function drawPuntiRegione(nId)
{
	
	// otteniamo i punti via ajax
	$.post("ajax_get_punti.php", { regione: nId}, onGetPuntiRegione);
		
}

function onGetPuntiRegione(xmlText)
{
	$(".punto_regione").remove();

	var immagineTop=parseInt($("#immagine_regione").offset().top);
	var immagineLeft=parseInt($("#immagine_regione").offset().left);

	$("punto",xmlText).each(function() {
		
		posTop=parseInt($("top",this).text());
		posLeft=parseInt($("left",this).text());
		citta=$("citta",this).text();
		id_punto=$("id_punto",this).text();
		//alert("append a "+(immagineTop+posTop)+" "+(immagineLeft+posLeft));
		$("#lista_punti").append("<img src=\"images/punto.gif\" id=\""+id_punto+"\" class=\"punto_regione\" style=\"top: "+(immagineTop+posTop)+"px; left: "+(immagineLeft+posLeft)+"px;\" title=\""+citta+"\" alt=\""+citta+"\"/>");
		
		
		
	});
	$(".punto_regione").hover(function() {
		
		$(this).addClass("pointer");
		
		
	}, function() {
		
		$(this).removeClass("pointer");
	});
	
	$(".punto_regione").click(onShowPuntoRegione);
	$("#negozi3").load("ajax_get_lista_punti.php", { id: nCurrentRegione });
	
	
}

function onShowPuntoRegione()
{
	
}