var map;
var markers = new Array();
var target = "_blank";
var options= {};

if (!Array.prototype.join) {
	Array.prototype.join = function(st) {
		if (arguments.length == 0)
			st = ",";
			
		var r = "";
		if (this.length > 0)
			r += this[0];
		for (var i = 1; i < this.length; i++)
			r += st + this[i];
		return r;
	}
}

GMap.prototype.addOverlays = function(a) { for (var i = 0; i < a.length; i++) this.addOverlay(a[i]); }

function getWindowHeight()
{
	return window.innerHeight || (document.documentElement && document.documentElement.clientHeight) || (document.body && document.body.clientHeight);
}

function getWindowWidth()
{
	return window.innerWidth || (document.documentElement && document.documentElement.clientWidth) || (document.body && document.body.clientWidth);
}

//function $(i) { return document.getElementById(i); }

function onMoveEndMap() {

	var center = map.getCenter();
	
	plotMap(map,center.lat(),center.lng(),map.getZoom()); 
	
	//alert(map.getZoom());
	
	document.formSearch.lat.value=center.lat();
	document.formSearch.lng.value=center.lng();
	document.formSearch.z.value=map.getZoom();
	
}

function HtmlControl(html, y_pos) { this.html = html; this.y_pos = y_pos || 30;}
HtmlControl.prototype = new GControl();

HtmlControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  var extra = document.createElement("div");
  extra.className = "map_control";
  extra.innerHTML = this.html;
  container.appendChild(extra); 
  map.getContainer().appendChild(container);
  return container;
}

HtmlControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, this.y_pos));
}

function setupGMap(m, opts) {
	options=opts;
	var m = document.getElementById(m);
	
	opts = opts||{};
	m.style.height = getWindowHeight() + "px";
	map = new GMap(m);
	
	var height = getWindowHeight();
	var width = getWindowWidth();
	
	//map.addControl(height > 250 ? new GLargeMapControl() : new GSmallZoomControl());
	
	//if (width > 250)
	//	map.addControl(new GMapTypeControl());

	map.addControl(new HtmlControl("<span>Powered by </span><a href='http://www.tecnes.com/' target='" + target + "'>Tecnes Milano</a>", 30 ));
	
	//function wheelZoom(a) { (a.detail || -a.wheelDelta) < 0 ? map.zoomIn() : map.zoomOut(); }
	//if (opts.zm)
	//{
	//	GEvent.addDomListener(m, "DOMMouseScroll", wheelZoom); 
		//GEvent.addDomListener(m, "mousewheel", wheelZoom); 
	//}
	
	map.setUIToDefault();
    //map.enableRotation();
	
	
	loadPosition(map, opts);
	
	// Creating the icon for the Pictures
	iconPicture = new GIcon();
	iconImage = new GIcon();
	
		
	iconPicture.image = "../images/red-pushpin.png";
	iconPicture.shadow = "../images/shadow.gif";
	
	iconImage.image = "../images/images.gif";
	iconImage.shadow = "../images/shadow.gif";
	
	
	iconPicture.iconSize = new GSize(18, 18);
	iconPicture.shadowSize = new GSize(22, 22);
	iconPicture.iconAnchor = new GPoint(9, 9);
	iconPicture.infoWindowAnchor = new GPoint(9, 0);
	
	
	iconImage.iconSize = new GSize(18, 26);
	iconImage.shadowSize = new GSize(22, 22);
	iconImage.iconAnchor = new GPoint(9, 9);
	iconImage.infoWindowAnchor = new GPoint(9, 0);
	
	// Add overlays contained in the database
	
	
	plotMap(map, opts.lt||"", opts.ln||"",opts.z||"");
	GEvent.addListener(map, "moveend", onMoveEndMap);
}

GMarker.prototype.onPhotoClick = function()
{
	this.openInfoWindowHtml(getHTML(this)); //, { onCloseFn: function() { map.panTo(center) } });
}

function plotMap(map, lat, lng, zoo)
{

	
	
	var bounds = map.getBounds();
	var sw = bounds.getSouthWest(), ne = bounds.getNorthEast();
	
	var min_x = sw.lng(), max_x = ne.lng(), min_y = sw.lat(), max_y = ne.lat();
	
	var request = GXmlHttp.create();
	
	var ts = new Date().getTime() ;
	var url = "../locator/loadMarkers.jsp?lat="+lat+"&lng="+lng+"&zoo="+zoo+"&ts="+ts;
	
	request.open("GET", url , true);


	request.onreadystatechange = function()
	{
	
		
		if (request.readyState == 4 && request.status == 200)
		{
			var text = request.responseText;

			/* a void response from the server means that nothing changes */
			if (text == "")
				return;
				
			//alert(text);
			
		    var xml = GXml.parse(text);
		    
		   if(xml.documentElement){
		    
			    var markers = xml.documentElement.getElementsByTagName("marker");
			  	
			  	map.clearOverlays();
			    for (var i = 0; i < markers.length; i++) {
			    
			    	var marker;
			    	if(markers[i].getAttribute("photo")=="")
			    	 	marker= new GMarker(new GPoint(markers[i].getAttribute("lng"), markers[i].getAttribute("lat")), iconPicture);
			    	else
			    		marker= new GMarker(new GPoint(markers[i].getAttribute("lng"), markers[i].getAttribute("lat")), iconImage);
			    	
			    	
			    	marker.id=markers[i].getAttribute("id");
			    	marker.photo=markers[i].getAttribute("photo");
			    	
			    	marker.altitude=markers[i].getAttribute("alt");
			    	marker.bearing=markers[i].getAttribute("bea");
			    	marker.speed=markers[i].getAttribute("spe");
			    	
			    	marker.time=markers[i].getAttribute("tim");
			    	
			    	
			    	map.addOverlay(marker);
			    	GEvent.bind(marker, "click", marker, marker.onPhotoClick);
			    	
			    }
		    }
		    
		}
	}
	request.send(null);
}

//function getMapType(i) { return map.getMapTypes()[i]; }
function ifu(x, d) { return x != undefined ? x : d; }
function loadPosition(map, opts)
{
	var center = new GLatLng(parseFloat(ifu(opts.lt, 40.48333)), parseFloat(ifu(opts.ln, -3.45)));

	var zoom = ifu(opts.z, 13);

	map.setCenter(center, zoom);
		
}
