var markers = new Array();
var IN_PANORAMIO = false;
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();
	
	plotPanoramas(map,center.lat(),center.lng(),options.dt); 
}

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 = $(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.spaghettihotel.com/' target='" + target + "'>Spaghettihotel</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); 
	}

	loadPosition(map, opts);

	// Creating the icon for the Pictures
	iconPicture = new GIcon();
	
	iconPicture.image = "../images/hotel.gif";
	iconPicture.shadow = "../images/shadow.gif";
	//iconPicture.image = "http://www.google.com/mapfiles/marker.png";
	//iconPicture.shadow = "http://www.google.com/mapfiles/shadow50.png";
	
	
	iconPicture.image = "../images/hotel.gif";
	iconPicture.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);
	
	// Add overlays contained in the database
	
	
	plotPanoramas(map, opts.lt||"", opts.ln||"", opts.dt||"");
	GEvent.addListener(map, "moveend", onMoveEndMap);
}

GMarker.prototype.onPhotoClick = function()
{
	this.openInfoWindowHtml(getHTML(this)); //, { onCloseFn: function() { map.panTo(center) } });
}

function plotPanoramas(map, lat, lng, dst)
{

	
	
	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 = "../search/loadGeoHotels.jsp?lat="+lat+"&lng="+lng+"&dst="+dst+"&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);
		    
		   
		    
		    var markers = xml.documentElement.getElementsByTagName("marker");
		  

		    for (var i = 0; i < markers.length; i++) {
		    
		    	var marker = new GMarker(new GPoint(markers[i].getAttribute("lng"), markers[i].getAttribute("lat")), iconPicture);
		    	marker.id=markers[i].getAttribute("id");
		    	marker.photo=markers[i].getAttribute("photo");
		    	marker.name=markers[i].getAttribute("name");
		    	marker.address=markers[i].getAttribute("address");
		    	marker.stars=markers[i].getAttribute("stars");
		    	
		    	
		    	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 GPoint(parseFloat(ifu(opts.ln, -3.45)), parseFloat(ifu(opts.lt, 40.48333)));
	var zoom = ifu(opts.z, 13);
	var type = opts.k != undefined ? getMapType(opts.k) : G_HYBRID_TYPE;

	map.setMapType(type);
	map.centerAndZoom(center, zoom);
}

