var minLat;
var maxLat;
var minLng;
var maxLng;
var markerCount;
var map_prj;
var markerOptions;
var mappa_visibile=true;
var aSegnalini = new Array();
var bounds;
var marginTop = "";
var new_center=new GLatLng();
var old_center=new GLatLng();
var i_bound=new GLatLngBounds();
var bound=new GLatLngBounds();
var i_zoom;
var baloon;

function eventi_mappa(map_prj,idContainer) {
	map_prj.enableDragging();
	i_bound=map_prj.getBounds();
	i_center=map_prj.getCenter();
	i_zoom=map_prj.getZoom();
	var d=new GLatLng(i_bound.getSouthWest().lat(),i_bound.getSouthWest().lng());
	var b=new GLatLng(i_bound.getNorthEast().lat(),i_bound.getNorthEast().lng());
	var a=new GLatLng(i_bound.getNorthEast().lat(),i_bound.getSouthWest().lng());
	var c=new GLatLng(i_bound.getSouthWest().lat(),i_bound.getNorthEast().lng());
	GEvent.addListener(map_prj, "zoomend", function(oldLevel,newLevel) {
		map_prj.enableDragging();
		if (newLevel<=i_zoom) {
			document.getElementById("zoom_m").style.display="";
			document.getElementById("zoom_p").style.display="block";
			map_prj.setCenter(i_center);
		}
		else if (newLevel == (map_prj.getCurrentMapType()).getMaximumResolution()) {
			document.getElementById("zoom_p").style.display="none";
			document.getElementById("zoom_m").style.display="block";
		}
		else if(newLevel > (map_prj.getCurrentMapType()).getMaximumResolution())
		{
			document.getElementById("zoom_p").style.display="none";
			document.getElementById("zoom_m").style.display="block";
			map_prj.setZoom((map_prj.getCurrentMapType()).getMaximumResolution());
		}
		else
		{	document.getElementById("zoom_p").style.display="block";
			document.getElementById("zoom_m").style.display="block";
		}


		  if (oldLevel==i_zoom && newLevel == i_zoom-1)
			map.zoomIn();

		if (newLevel<oldLevel)
		{
			map_prj.setCenter(map.getCenter());
		}

		if (baloon)
			baloon.destroy();
	});

	function doZoomIn(){
		app=map.getCenter();
		map.zoomIn();
		map.setCenter(app);
		document.getElementById("zoom_m").style.display="";
	}
	
	function doZoomOut(){
		app=map.getCenter();
		map.zoomOut();
		map.setCenter(app);
		document.getElementById("zoom_p").style.display="block";
	}

	GEvent.addListener(map_prj, "drag", function() {
		old_center=new_center;
		new_center=map_prj.getCenter();
		bound=map_prj.getBounds();

		if(!(i_bound.containsBounds(bound))) {
			if(!isNaN(old_center.lat()))
			{
				new_center=old_center;
				map_prj.setCenter(old_center);
			}
		}

		if (baloon)
			baloon.destroy();
	});
}

function designMap(idContainer) {
	map_prj = bigMap(idContainer , latitudine , longitudine , 13);
	icon = new GIcon();
	icon.image = '/vimages/default/marker.png';
	icon.iconSize = new GSize(19, 25);
	icon.iconAnchor = new GPoint(7, 31);
	icon.infoWindowAnchor = new GPoint(7, 7);
	markerOptions = {icon:icon};
	Ajax.Request('GET',url, fillArraySegnalini, idContainer);
}

function fillArraySegnalini(doc,idContainer){
	xml_doc = doc.responseXML;
	var items_num = xml_doc.getElementsByTagName('ad').length;
	var urls = xml_doc.getElementsByTagName('url');
	var agencyLogos=xml_doc.getElementsByTagName('agencyLogo');
	var fotos=xml_doc.getElementsByTagName('adThumb');
	var latitudini=xml_doc.getElementsByTagName('latitude');
	var longitudini=xml_doc.getElementsByTagName('longitude');
	var nomiProgetto=xml_doc.getElementsByTagName('projectName');

	for (i=0;i<items_num;i++) {
		var _latitudine = latitudini[i].childNodes[0] ? latitudini[i].childNodes[0].nodeValue : "";
		var _longitudine = longitudini[i].childNodes[0] ? longitudini[i].childNodes[0].nodeValue : "";
		var _indirizzo = "";
		var _foto = fotos[i].childNodes[0] ? fotos[i].childNodes[0].nodeValue : "";
		var _agencyLogo = agencyLogos[i].childNodes[0] ? agencyLogos[i].childNodes[0].nodeValue : "";
		var _url=urls[i].childNodes[0] ? urls[i].childNodes[0].nodeValue : "";
		var _nomeProgetto = nomiProgetto[i].childNodes[0] ? nomiProgetto[i].childNodes[0].nodeValue : "";

		aSegnalini[i]=new Array(_latitudine, _longitudine, _indirizzo, _foto, _agencyLogo, _url, _nomeProgetto);
	}
	addSegnalini(map_prj,aSegnalini,idContainer);
}

var html;
function addSegnalini(map_prj, aSegnalini,idContainer){
	for (i=0;i<aSegnalini.length;i++) {
		if (aSegnalini[i][0]!=0 && aSegnalini[i][1]!=0) {
			mappa_visibile=true;
			if (!minLat && !minLng) {
				minLat = aSegnalini[i][0];
				maxLat = aSegnalini[i][0];
				minLng = aSegnalini[i][1];
				maxLng = aSegnalini[i][1];
			} else {
				if (minLat>aSegnalini[i][0]) minLat=aSegnalini[i][0];
				if (maxLat<aSegnalini[i][0]) maxLat=aSegnalini[i][0];
				if (minLng>aSegnalini[i][1]) minLng=aSegnalini[i][1];
				if (maxLng<aSegnalini[i][1]) maxLng=aSegnalini[i][1];
			}

			point = new GLatLng(aSegnalini[i][0],aSegnalini[i][1]);
			map_prj.addOverlay(createMarker(point,aSegnalini[i]));
		}
	}
	if (mappa_visibile) {
		setMapVisibility(map_prj.getContainer().id);
	}
	
	
	
	if(aSegnalini.length > 0)
	{
		bounds = new GLatLngBounds;
		minLat = parseFloat(minLat) - 0.1;
		maxLat = parseFloat(maxLat) + 0.1;
		minLng = parseFloat(minLng) - 0.1;
		maxLng = parseFloat(maxLng) + 0.1;
		bounds.extend (new GLatLng (minLat , minLng ));
		bounds.extend (new GLatLng (maxLat ,maxLng ));
		map_prj.setZoom(map_prj.getBoundsZoomLevel(bounds));
		map_prj.setCenter(bounds.getCenter());	
	}
	eventi_mappa(map_prj,idContainer);
	c_zoom=new MEzoom_control(map_prj,map_prj.getZoom());
	c_zoom.top="10px";
	c_zoom.left="10px";
	c_zoom.create();
	}
	
	function setMapVisibility(mapId)
	{
		showMappa(mapId);
		timerCityFlag = setTimeout("fadeOutLoading(7.5)",1500);
		setTimeout("map_prj.checkResize()", 1000);
	}

function createMarker(latlng, data) {
	var marker = new GMarker(latlng,markerOptions);
	GEvent.addListener(marker,"click", function() {
		if (baloon)
			baloon.destroy();
		baloon = new MEgmbaloon(map_prj,data[0]);
		var baloonDiv = document.createElement('div');

		if (data[3]>0)
			var immagine_im = "/images/projects/small/"+data[3];
		else
			var immagine_im = '/vimages/default/no_image_80x60.png';

		var agencyLink = "&nbsp;";
		if (data[4]>0)
			var agencyLink = "<a href=\""+data[5]+"\"><img src=\"/images/agencies/logo/" + data[4] + "\"></a>"

		var myHtml = "\
			<div class=\"baloon\">\
				<div>\
					<span class=\"floatLeft padderBottom strong\">"+data[6]+"</span>\
					<a class=\"floatRight\" onclick=\"baloon.destroy();\"><img src=\"/vimages/default/closeBaloon.png\"></a>\
					<div class=\"clear\"></div>\
				</div>\
				<div id=\"wrap_immobile\"><a href=\""+data[5]+"\"><img src=\""+immagine_im+"\"></a></div>\
				<div id=\"wrap_agenzia\">" + agencyLink + "</div>\
				<div id=\"baloonDetail\" class=\"floatRight\">\
					<a href=\""+data[5]+"\"  class=\"hoverNonUnderlined\">\
						<span style=\"background:url(/vimages/default/detail.png) right no-repeat;  padding:10px 20px;\">Zobacz szczegóły</span>\
					</a>\
				</div>\
				<div class=\"clear\"></div>\
			</div>";

		baloonDiv.innerHTML = myHtml;
		baloon.create(latlng,baloonDiv);
	}
	);
	return marker;
}


function hideFlag() {
	var el = document.getElementById('cityFlag');
	if (el)
		evidenzia(el,10);

	timerCityFlag = setTimeout("fadeOutCityFlag(10)",5000);
}

function fadeOutCityFlag(value) {
	
	if (value<=0)
		return;
	else {
		var el = document.getElementById('cityFlag');
		if (el) {
			value=value-0.1;
			evidenzia(el,value);
		}
		timerCityFlag = setTimeout("fadeOutCityFlag("+(value)+")",0);
	}
}

function fadeOutLoading(value) {
	var el = document.getElementById('map_loader');
	if (value<=0) {
		if (el)
			el.style.display="none";
		return;
	}
	else {
		if (el) {
			value=value-0.1;
			evidenzia(el,value);
		}
		timerCityFlag = setTimeout("fadeOutLoading("+(value)+")",0);
	}
}

function evidenzia(img,valore) {
	img.style.opacity = valore/10;
	img.style.filter = 'alpha(opacity=' + valore*10 + ')';
}

  function showMappa(mapId) {
	document.getElementById(mapId).style.display= 'block';
	setTimeout("map_prj.checkResize()", 1000);
    }


var i=0;

var MEgmbaloon = function(map,baloonId) {
	mapId = map.getContainer().id;
	this.map = map;
	this.content = document.createElement('div');
	this.content.id = baloonId;
	this.content.style.border="1px solid #693";
	this.content.style.width="252px";
	this.content.style.height="100px"
	this.content.style.position="absolute";
	this.content.style.padding="5px";
	this.content.style.fontSize="12px";
	this.content.style.backgroundColor="#FFF";
	this.hOffset = "0px";
	this.vOffset = "42px";
	if (baloonId)
		this.content_id=baloonId;
	else
		this.content_id = "baloon_"+(i++);
	this.content.className = "baloon";
	this.mapId = mapId;

	this.destroy = function () {
		if (document.getElementById(this.content_id))
			document.getElementById(mapId).removeChild(document.getElementById(this.content_id));
	};

	this.create = function(point,content) {
		this.content.innerHTML = content.innerHTML;
		this.px_coords = this.map.fromLatLngToContainerPixel(point);
		var baloon_height = parseInt(this.content.style.height);
		var baloon_width = parseInt(this.content.style.width);
		var hOffset = this.hOffset ? parseInt(this.hOffset) : 0;
		var vOffset = this.vOffset ? parseInt(this.vOffset) : 0;
		this.content.style.top=(parseInt(this.px_coords.y)-(baloon_height + parseInt(this.vOffset)))+"px";
		this.content.style.left=(this.px_coords.x + hOffset)+"px";
		if (parseInt(this.px_coords.x) + baloon_width + hOffset +16>  parseInt(map.getContainer().style.width))
			this.content.style.left=(parseInt(this.px_coords.x) - (baloon_width+8-hOffset))+"px";
		if (parseInt(this.content.style.top) - vOffset <0)
			this.content.style.top=(parseInt(this.px_coords.y))+"px";
		document.getElementById(this.mapId).appendChild(this.content);
		var body = document.getElementsByTagName("body")[0];
		if(body.addEventListener)
		{	body.addEventListener('click',function() {
				if(baloon) baloon.destroy();
			},false);
		}
		else if(body.attachEvent)
		{	body.attachEvent('onclick',function() {
				if(baloon) baloon.destroy();
			});
		}
	}

	this.setVOffset = function(offset) {
		this.vOffset = offset;
	}

	this.setHOffset = function(offset) {
		this.hOffset = offset;
	}

	this.setBackground = function(string) {
		this.content.style.background = string;
	}

	this.setBorder = function(string) {
		this.content.style.border = string;
	}

	this.setWidth = function(string) {
		this.content.style.width = string;
	}

	this.setHeight = function(string) {
		this.content.style.height = string;
	}

	this.setPadding = function(string) {
		this.content.style.padding = string;
	}
	this.setFont = function(string) {
		this.content.style.font = string;
	}

}

var MEzoom_control = function(map,zoom_start) {
	this.top="100px";
	this.left="50px";
	this.mapId = map.getContainer().id;
	this.map = map;
	this.content = document.createElement('div');
	this.content.id = "zoom";
	this.content.style.width="30px";
	this.content.style.height="53px"
	this.content.style.position="relative";
	this.zoom_p =document.createElement('div');
	this.zoom_p.id = "zoom_p";
	this.zoom_p.style.width="28px";
	this.zoom_p.style.height="28px"
	this.zoom_p.style.position="absolute";
	this.zoom_p.style.top="7px";
	this.zoom_p.style.left="7px";
	this.zoom_p.style.background="url(/vimages/default/zoom_in.png) no-repeat";
	this.zoom_p.style.cursor="pointer";
	this.zoom_m =document.createElement('div');
	this.zoom_m.id = "zoom_m";
	this.zoom_m.style.width="28px";
	this.zoom_m.style.height="28px"
	this.zoom_m.style.position="absolute";
	this.zoom_m.style.top="36px";
	this.zoom_m.style.left="7px";
	this.zoom_m.style.background="url(/vimages/default/zoom_out.png) no-repeat";
	this.zoom_m.style.display="none";
	this.zoom_m.style.cursor="pointer"
	this.zoom_start=zoom_start;

	this.f_zoom_p = function() {
		app=map.getCenter();
		map.zoomIn();
		map.setCenter(app);
		document.getElementById("zoom_m").style.display="";
	}

	this.zoom_p.onclick=this.f_zoom_p;

	this.f_zoom_m = function() {
		app=map.getCenter();
		map.zoomOut();

		app=map.getCenter();
			if ( map.getZoom()==(zoom_start)) {
				this.style.display="none";
		}
			else 
				this.style.display="";
	}

	this.zoom_m.onclick=this.f_zoom_m;

	this.create = function(zoom_start) {
		document.getElementById(this.mapId).appendChild(this.content);
		this.content.style.top=this.top;
		this.content.style.left=this.left;
		this.content.appendChild(this.zoom_p);
		this.content.appendChild(this.zoom_m);
	}
}

