//Dichiarazione costanti


//Server e parametri per visualizzare le province
var geocoder;
var server = "http://vacanze-rimini.com/kmz/"
var show_province = {
	 "rn":					{"url" : server+"rn.kmz"},
	 "ra":					{"url" : server+"ra.kmz"},
	 "fc":					{"url" : server+"fc.kmz"},
	 "pu":					{"url" : server+"pu.kmz"},
	 "fe":					{"url" : server+"fe.kmz"},
	 "bo":					{"url" : server+"bo.kmz"},
	 "mo":					{"url" : server+"mo.kmz"},
	 "pr":					{"url" : server+"pr.kmz"},
	 "re":					{"url" : server+"re.kmz"},
	 "pc":					{"url" : server+"pc.kmz"},
	 "sm":					{"url" : server+"sm.kmz"},
	 "an":					{"url" : server+"an.kmz"}
}



//Funzione che sposta un oggetto a tot pixel dal top e dal left
function moveIt(obj, mvTop, mvLeft) {
	obj.style.position = "absolute";
	obj.style.top = mvTop;
	obj.style.left = mvLeft;
}



function createMarker(point, icon, title, descr, image, lid) {
		var marker = new GMarker(point, icon);
		var infoWinOptions = { maxWidth : "280" };
		GEvent.addListener(marker, "click", function() {
			if (image != "") imagetag = "<td width='120' valign='top'><img src='"+image+"'></td>";
			else imagetag = "";
			if (lid != "") link = "<br><a href='http://www.riminibeach.it/"+lid+"'>vai alla scheda &raquo;</a>";
			else link = "";
            marker.openInfoWindowHtml("<table width='100%' cellpadding='1' cellspacing='2'><tr><td width='99' valign='top'><img src='"+image+"'></td><td valign='top'><font style='font-size:14px; font-family: sans-serif; font-weight: bold;'>"+title+"</font><font style='font-size:12px; font-family: sans-serif; line-height: 12px;'><br>"+descr+"</font><br><a href='http://www.riminibeach.it/"+lid+"'>vai alla scheda</a></td></tr></table>", infoWinOptions);
        });
		return marker;
	}


function wheelZoom(a) {
    	if ((a.detail || -a.wheelDelta) < 0)
	        map.zoomIn();
	    else
	        map.zoomOut();
} 


function toggleMapVisibility() {
		var el = document.getElementById("mapdiv");
		var el2 = document.getElementById("backdiv");
		var el3 = document.getElementById("coldiv");
//		alert(el);
		if (el2.style.visibility == "hidden")  {
			el.style.visibility = 'hidden';
			el3.style.visibility = 'hidden';
			el.style.display = 'none';
			el2.style.visibility = 'visible';
			el2.style.display = 'block';
		} else {
			el.style.visibility = 'visible';
			el3.style.visibility = 'visible';
			el.style.display = 'block';
		 	el2.style.visibility = 'hidden';
			el2.style.display = 'none';
		}
	}	

//contenitore di tutte le categorie abilitate
markerlist = new Object();
	
//funzione che precarica una categoria e imposta a cheched la checkbox
function autotoggleCategory(catid,chk) {

	GDownloadUrl("/layout/set/xml/content/view/full/"+catid, function(data, responseCode) {
	  var xml = GXml.parse(data);
	  var markers = xml.documentElement.getElementsByTagName("marker");
	  for (var i = 0; i < markers.length; i++) {
	    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
	                            parseFloat(markers[i].getAttribute("lng")));
								
		
		gid = markers[i].getAttribute("gid");
		title = markers[i].getAttribute("title");
		descr = markers[i].getAttribute("descr");
		image = markers[i].getAttribute("image");
		icon = markers[i].getAttribute("icon");
		lid = markers[i].getAttribute("lid");
		eval ("var m = createMarker(point, icon"+icon+", title, descr, image, lid);");
		markerlist[""+gid] = m;
	 	map.addOverlay(m);
		document.getElementById(chk).checked=true;
	  }
	});	
}
	
	
var map;


    function load(lat,lon,zoom,map_type) {
      if (GBrowserIsCompatible()) {

        map = new GMap2(document.getElementById("mapdiv"));
        map.setCenter(new GLatLng(lat,lon), zoom);
		if (map_type == 1){
			map.setMapType(G_SATELLITE_MAP);
		}
		if (map_type == 2){
			map.setMapType(G_HYBRID_MAP);
		}
		if (map_type == 3){
			map.setMapType(G_TERRAIN_MAP);
		}
		map.addControl(new GOverviewMapControl(),
        	new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));
		map.addControl(new GLargeMapControl(),
			new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(2, 2)));
		map.addControl(new GMapTypeControl(),
			 new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(68, 5)));

		map.enableContinuousZoom();
		map.enableScrollWheelZoom();


		icon = new GIcon(G_DEFAULT_ICON);

		icon24 = new GIcon(G_DEFAULT_ICON);
		icon24.image = "http://maps.google.com/mapfiles/kml/pal2/icon11.png";
		icon24.shadow = "http://maps.google.com/mapfiles/kml/pal2/icon11s.png";
		icon24.iconSize = new GSize(32, 32);
		icon24.shadowSize = new GSize(37, 34);

		icon13 = new GIcon(G_DEFAULT_ICON);
		icon13.image = "http://www.riminibeach.it/images/museo.png";
		icon13.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon13.iconSize = new GSize(20, 34);
		icon13.shadowSize = new GSize(37, 34);

		icon12 = new GIcon(G_DEFAULT_ICON);
		icon12.image = "http://www.riminibeach.it/images/shopping.png";
		icon12.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon12.iconSize = new GSize(20, 34);
		icon12.shadowSize = new GSize(37, 34);
		
		icon11 = new GIcon(G_DEFAULT_ICON);
		icon11.image = "http://www.riminibeach.it/images/disco.png";
		icon11.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon11.iconSize = new GSize(20, 34);
		icon11.shadowSize = new GSize(37, 34);

		icon10 = new GIcon(G_DEFAULT_ICON);
		icon10.image = "http://maps.google.com/mapfiles/kml/pal2/icon10.png";
		icon10.shadow = "http://maps.google.com/mapfiles/kml/pal2/icon10s.png";
		icon10.iconSize = new GSize(32, 32);
		icon10.shadowSize = new GSize(37, 34);
		
		icon9 = new GIcon(G_DEFAULT_ICON);
		icon9.image = "http://www.riminibeach.it/images/residence.png";
		icon9.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon9.iconSize = new GSize(20, 34);
		icon9.shadowSize = new GSize(37, 34);

		icon8 = new GIcon(G_DEFAULT_ICON);
		icon8.image = "http://maps.google.com/mapfiles/kml/pal2/icon27.png";
		icon8.shadow = "http://maps.google.com/mapfiles/kml/pal2/icon27s.png";
		icon8.iconSize = new GSize(32, 32);
		icon8.shadowSize = new GSize(37, 34);

		icon7 = new GIcon(G_DEFAULT_ICON);
		icon7.image = "http://maps.google.com/mapfiles/kml/pal2/icon27.png";
		icon7.shadow = "http://maps.google.com/mapfiles/kml/pal2/icon27s.png";
		icon7.iconSize = new GSize(32, 32);
		icon7.shadowSize = new GSize(37, 34);

		icon6 = new GIcon(G_DEFAULT_ICON);
		icon6.image = "http://www.riminibeach.it/images/risto.png";
		icon6.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon6.iconSize = new GSize(20, 34);
		icon6.shadowSize = new GSize(37, 34);
		icon5w = new GIcon(G_DEFAULT_ICON);
		icon5w.image = "http://www.riminibeach.it/images/4w.png";
		icon5w.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon5w.iconSize = new GSize(20, 34);
		icon5w.shadowSize = new GSize(37, 34);

		icon4w = new GIcon(G_DEFAULT_ICON);
		icon4w.image = "http://www.riminibeach.it/images/4w.png";
		icon4w.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon4w.iconSize = new GSize(20, 34);
		icon4w.shadowSize = new GSize(37, 34);

		icon3w = new GIcon(G_DEFAULT_ICON);
		icon3w.image = "http://www.riminibeach.it/images/3w.png";
		icon3w.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon3w.iconSize = new GSize(20, 34);
		icon3w.shadowSize = new GSize(37, 34);

		icon2w = new GIcon(G_DEFAULT_ICON);
		icon2w.image = "http://www.riminibeach.it/images/2w.png";
		icon2w.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon2w.iconSize = new GSize(20, 34);
		icon2w.shadowSize = new GSize(37, 34);

		icon1w = new GIcon(G_DEFAULT_ICON);
		icon1w.image = "http://www.riminibeach.it/images/1w.png";
		icon1w.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon1w.iconSize = new GSize(20, 34);
		icon1w.shadowSize = new GSize(37, 34);
		
		icon5 = new GIcon(G_DEFAULT_ICON);
		icon5.image = "http://www.riminibeach.it/images/4.png";
		icon5.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon5.iconSize = new GSize(20, 34);
		icon5.shadowSize = new GSize(37, 34);

		icon4 = new GIcon(G_DEFAULT_ICON);
		icon4.image = "http://www.riminibeach.it/images/4.png";
		icon4.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon4.iconSize = new GSize(20, 34);
		icon4.shadowSize = new GSize(37, 34);

		icon3 = new GIcon(G_DEFAULT_ICON);
		icon3.image = "http://www.riminibeach.it/images/3.png";
		icon3.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon3.iconSize = new GSize(20, 34);
		icon3.shadowSize = new GSize(37, 34);

		icon2 = new GIcon(G_DEFAULT_ICON);
		icon2.image = "http://www.riminibeach.it/images/2.png";
		icon2.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon2.iconSize = new GSize(20, 34);
		icon2.shadowSize = new GSize(37, 34);

		icon1 = new GIcon(G_DEFAULT_ICON);
		icon1.image = "http://www.riminibeach.it/images/1.png";
		icon1.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon1.iconSize = new GSize(20, 34);
		icon1.shadowSize = new GSize(37, 34);
		
		icon35 = new GIcon(G_DEFAULT_ICON);
		icon35.image = "http://www.riminibeach.it/images/varie.png";
		icon35.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon35.iconSize = new GSize(20, 34);
		icon35.shadowSize = new GSize(37, 34);

		icon34 = new GIcon(G_DEFAULT_ICON);
		icon34.image = "http://www.riminibeach.it/images/villa.png";
		icon34.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon34.iconSize = new GSize(20, 34);
		icon34.shadowSize = new GSize(37, 34);

		icon33 = new GIcon(G_DEFAULT_ICON);
		icon33.image = "http://www.riminibeach.it/images/chiesa.png";
		icon33.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon33.iconSize = new GSize(20, 34);
		icon33.shadowSize = new GSize(37, 34);

		icon32 = new GIcon(G_DEFAULT_ICON);
		icon32.image = "http://www.riminibeach.it/images/museo.png";
		icon32.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon32.iconSize = new GSize(20, 34);
		icon32.shadowSize = new GSize(37, 34);

		icon31 = new GIcon(G_DEFAULT_ICON);
		icon31.image = "http://www.riminibeach.it/images/borgo.png";
		icon31.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon31.iconSize = new GSize(20, 34);
		icon31.shadowSize = new GSize(37, 34);
		
		icon30 = new GIcon(G_DEFAULT_ICON);
		icon30.image = "http://www.riminibeach.it/images/castello.png";
		icon30.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon30.iconSize = new GSize(20, 34);
		icon30.shadowSize = new GSize(37, 34);

		icon40 = new GIcon(G_DEFAULT_ICON);
		icon40.image = "http://www.riminibeach.it/images/musica.png";
		icon40.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon40.iconSize = new GSize(20, 34);
		icon40.shadowSize = new GSize(37, 34);

		icon41 = new GIcon(G_DEFAULT_ICON);
		icon41.image = "http://www.riminibeach.it/images/party.png";
		icon41.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon41.iconSize = new GSize(20, 34);
		icon41.shadowSize = new GSize(37, 34);

		icon42 = new GIcon(G_DEFAULT_ICON);
		icon42.image = "http://www.riminibeach.it/images/concert.png";
		icon42.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon42.iconSize = new GSize(20, 34);
		icon42.shadowSize = new GSize(37, 34);
		
		icon43 = new GIcon(G_DEFAULT_ICON);
		icon43.image = "http://www.riminibeach.it/images/sport.png";
		icon43.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon43.iconSize = new GSize(20, 34);
		icon43.shadowSize = new GSize(37, 34);
		
		icon44 = new GIcon(G_DEFAULT_ICON);
		icon44.image = "http://www.riminibeach.it/images/cultura.png";
		icon44.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon44.iconSize = new GSize(20, 34);
		icon44.shadowSize = new GSize(37, 34);
		
		icon45 = new GIcon(G_DEFAULT_ICON);
		icon45.image = "http://www.riminibeach.it/images/teatro.png";
		icon45.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon45.iconSize = new GSize(20, 34);
		icon45.shadowSize = new GSize(37, 34);
		
		icon46 = new GIcon(G_DEFAULT_ICON);
		icon46.image = "http://www.riminibeach.it/images/fiere.png";
		icon46.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon46.iconSize = new GSize(20, 34);
		icon46.shadowSize = new GSize(37, 34);
		
		icon47 = new GIcon(G_DEFAULT_ICON);
		icon47.image = "http://www.riminibeach.it/images/moda.png";
		icon47.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon47.iconSize = new GSize(20, 34);
		icon47.shadowSize = new GSize(37, 34);
		
		icon50 = new GIcon(G_DEFAULT_ICON);
		icon50.image = "http://www.riminibeach.it/images/virtual.png";
		icon50.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon50.iconSize = new GSize(20, 34);
		icon50.shadowSize = new GSize(37, 34);

      }
    }
	
	

var mapobj = document.getElementById("mapdiv"); 
//GEvent.addDomListener(mapobj, "DOMMouseScroll", wheelZoom);
//GEvent.addDomListener(mapobj, "mousewheel", wheelZoom); 

setTimeout("positionOverview(558,254)",500);

    //  ======== A function to adjust the positioning of the overview ========
      function positionOverview(x,y) {
        var omap=document.getElementById("mapdiv_overview");
        //omap.style.top = "0px";
        
        // == restyling ==
        omap.firstChild.style.border="1px solid white";
        omap.firstChild.firstChild.style.left="0px";
        omap.firstChild.firstChild.style.top="0px";
        omap.firstChild.firstChild.style.width="140px";
        omap.firstChild.firstChild.style.height="130px";
		
      }



function centerMap() {
	map.setCenter(new GLatLng(45.464116, 9.187872), 11);
}

function getAbsLeft(element){
	var width;
	if (!element.offsetParent)
		return 0;
	with (element)
	{
		width = offsetLeft + getAbsLeft(offsetParent);
	}
	return width;
}

function getAbsTop(element){
	var top;
	if (!element.offsetParent)
		return 0;
	with (element)
	{
		top = offsetTop + getAbsTop(offsetParent);
	}
	return top;
}

function getPos (obj) {
	var output = new Object();
	var mytop=0, myleft=0;
	while( obj) {
		mytop+= obj.offsetTop;
		myleft+= obj.offsetLeft;
		obj= obj.offsetParent;
	}
	output.left = myleft;
	output.top = mytop;
	return output;
}



function toggleCategory(catid, src, loader_id, main_id) {
	
	if(loader_id) { 
		absTop = getPos(document.getElementById(main_id)).top+document.getElementById(main_id).offsetHeight/2+'px';
		absLeft = getPos(document.getElementById(main_id)).left +document.getElementById(main_id).offsetWidth/2 +'px';
		moveIt(document.getElementById(loader_id),absTop,absLeft)
		document.getElementById(loader_id).style.display='block';
		document.getElementById(loader_id).style.zIndex='20';
		document.getElementById(main_id).style.opacity='0.3';
	}
		
	GDownloadUrl("/layout/set/xml/content/view/full/"+catid, function(data, responseCode) {
	  var xml = GXml.parse(data);
	  var markers = xml.documentElement.getElementsByTagName("marker");
	  for (var i = 0; i < markers.length; i++) {
	    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
	                            parseFloat(markers[i].getAttribute("lng")));
		gid = markers[i].getAttribute("gid");
		title = markers[i].getAttribute("title");
		descr = markers[i].getAttribute("descr");
		image = markers[i].getAttribute("image");
		icon = markers[i].getAttribute("icon");
		lid = markers[i].getAttribute("lid");
	eval ("var m = createMarker(point, icon"+icon+", title, descr, image, lid);");
		if (src.checked) {
			markerlist[""+gid] = m;
	 		map.addOverlay(m);
		} else {
			map.removeOverlay(markerlist[""+gid]);
			markerlist[""+gid] = null;
		}
	  }
	});	
	//if(loader_id) { //If the optional argument is not there, create a new variable with that name.
	setTimeout("document.getElementById('loader').style.display='none'; document.getElementById('mapdiv').style.opacity='1';",2000);	
	//}

}


function toggleCategories(formname, src) {
	var els = document.getElementsByName("hpi");
	for (i = 0; i < els.length - 1; i++) 
		if (src.checked) {
			els[i].checked = true;
			toggleCategory(els[i].value, els[i]);
		} else {
			els[i].checked = false;
			toggleCategory(els[i].value, els[i]);
		}
}


markerarray = new Array();
markerarrayh = new Array();
markerarrayw = new Array();

function selectCategoryCombo(src, tipo) {
	if (tipo == 0) marray = markerarray;
	else if (tipo == 2) marray = markerarrayw;
	else marray = markerarrayh;
	if (src == -1) catid = -1;
	else catid = src.options[src.selectedIndex].value;
	//alert("catid="+catid);
    for (var j = 0; j < marray.length; j++) {
        if (marray[j] != null) map.removeOverlay(marray[j]);
        marray[j] = null;
    }

	//marray = new Array();

	if (src == -2) {
		return;
	}

	//alert("catid="+catid);
    GDownloadUrl("/layout/set/xml/content/view/full/"+catid, function(data, responseCode) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));
	gid = markers[i].getAttribute("gid");
	title = markers[i].getAttribute("title");
	descr = markers[i].getAttribute("descr");
	image = markers[i].getAttribute("image");
	icon = markers[i].getAttribute("icon");
	lid = markers[i].getAttribute("lid");
	eval ("var m = createMarker(point, icon"+icon+", title, descr, image, lid);");
	marray[i] = m;
	map.addOverlay(m);
      }
    });
}


function toggleGeoXML(id, checked, layers) {
	if (checked) {
		if(!layers[id].geoXml){
  			var geoXml = new GGeoXml(layers[id].url);
			layers[id].geoXml = geoXml;
			map.addOverlay(geoXml);
		}
	} 
	else if (layers[id].geoXml) {
		map.removeOverlay(layers[id].geoXml);
		layers[id].geoXml=false
	}
}


    