var markers = new Array();
var IN_PANORAMIO = false;
var target = "_blank";

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() { plotPanoramas(map); }

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) {
//	var m = $(m);
	opts = opts||{};
//	m.style.height = "300px";
	//getWindowHeight() + "px";

    //map = new GMap(m);
	map = new GMap2(document.getElementById("map"));
	var height = 300;
	var width = 512;
	
	map.addControl(height > 250 ? new GLargeMapControl() : new GSmallZoomControl());
	

	if (width * height > 25000 && !IN_PANORAMIO)
		map.addControl(new HtmlControl("<span class='powered'>Powered by</span><br /><a href='http://www.3dids.com/' target='" + target + "'>3DIDs</a>", width > 250 ? 30 : 7));
	
	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.setCenter(new GLatLng(ori_x, ori_y), ori_z);
	map.setMapType(G_HYBRID_MAP);

}

GMarker.prototype.onPuntoClick = function()
{
	this.openInfoWindowHtml(getPuntoHTML(this)); //, { onCloseFn: function() { map.panTo(center) } });
}

// returns the html that will be used when the user clicks on a marker
function getPuntoHTML(overlay)
{
	var id = overlay.Id;
	var fichero = overlay.Fichero;
	var width = overlay.W;
	var height= overlay.H;
	var size = 100;
	
	//alert(width+":"+height);
	if (width > height)
	{
		height = size * height / width;
		width = size;
	}
	else
	{
		width = size * width / height;
		height = size;
	}

	if (overlay.CodGen!=0) {
		var cad = ""
		
		cad = cad + "<div style='width:200px; background-color:#FFFFFF;'>" 
		cad = cad + "<b style='color:#c7271b;'>"+overlay.Nombre+"</b><br>";
		if(overlay.Fichero!="pixel.gif") {
			cad = cad + "<a href='default.asp?pagina=2&sec="+overlay.Itinerario+"&com="+overlay.Id+"&pos=0' target='_self'>";
			cad = cad + "<img src='Ficheros/" + fichero + "' style='border:0' width='"+width+"' height='"+height+"' />";
			cad = cad + "</a><br>";
		}
		cad = cad + overlay.Direccion + "<br />"
		cad = cad + "<a class='rojo' href='default.asp?pagina=2&sec="+overlay.Itinerario+"&com="+overlay.Id+"&pos=0' target='_self'>";
		cad = cad + "+ info" ;
		cad = cad + "</a></div>";
//cad ="aqui";
		return(cad);
	}
	else {
		var cad = ""
		cad = cad + "<div style='width:200px;background-color:#FFFFFF;'>" + overlay.Direccion + "<br />"
		cad = cad +"<b style='color:#c7271b;'>"+overlay.Nombre+"</b><br>";
		if(overlay.Fichero!="img/pixel.gif") {
			cad = cad + "<a href='default.asp?pagina=2&sec="+overlay.Itinerario+"&hito="+overlay.Id+"&pos=0' target='_self'>";
			cad = cad + "<img src='Ficheros/" + fichero + "' style='border:0' width='"+width+"' height='"+height+"' />";
			cad = cad + "</a><br>";
		}
		cad = cad + "<a class='rojo' href='default.asp?pagina=2&sec="+overlay.Itinerario+"&hito="+overlay.Id+"&pos=0' target='_self'>";
		cad = cad + "+ info" ;
		cad = cad + "</a></div>";
//cad ="alli";
		return(cad);
	}
}

function plotPuntos(map, iti, tip, iae, com, hit)
{
	limpiar();
	if(iti!=0) {
		itinerario();
	}
	var request = GXmlHttp.create();
	
	ajax_rq = "cargar_puntos.asp?iti=" + iti + "&tip=" + tip + "&iae=" + iae + "&com=" + com + "&hit=" + hit;
	request.open("GET", ajax_rq, true);
	
	//alert(ajax_rq);
	request.onreadystatechange = function()
	{
		if (request.readyState == 4 && request.status == 200)
		{
			var text = request.responseText;
			//alert("Entramos\n:"+text);
			if (text == "")
				return;
			if (text == "none")
				text = "";
			
			//text = '{ "count": 2, "puntos": [{"id": 287, "itinerario": 2, "codgen": 2, "nombre": "PERFUMER&Icute;A AZOR&Icute;N", "direccion": "AVENIDA ALFONSO EL SABIO 45", "Guia": 1, "x": 38.34626, "y": -0.489822, "fichero": "azorin1.jpg", "width": 513, "height": 341}, {"id": 287, "itinerario": 2, "codgen": 2, "nombre": "PERFUMER&Icute;A AZOR&Icute;N", "direccion": "AVENIDA ALFONSO EL SABIO 45", "Guia": 1, "x": 38.34626, "y": -0.489822, "fichero": "azorin1.jpg", "width": 513, "height": 341}]}';
			var rs = eval("(" + text + ")");
			//alert(rs);			
			var puntos = rs.puntos;

			//alert("N puntos: "+puntos.length);
			for (var i = 0; i < puntos.length; i++)
			{
				var p = puntos[i];
				//alert("Vamos a hacer:" + p.codgen);
				var icon = new GIcon();
				icon.image = "http://www.comercioalicante.com/img/bolsa_"+p.codgen+".png";
				icon.shadow = "http://www.comercioalicante.com/img/sombra.png";
				icon.iconSize = new GSize(12, 20);
				icon.shadowSize = new GSize(22, 20);
				icon.iconAnchor = new GPoint(6, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);

				var marker = new GMarker(new GLatLng(p.x, p.y), icon);

				marker.Id = p.id;
				marker.Itinerario = p.itinerario;
				marker.CodGen = p.codgen;
				marker.Nombre = p.nombre;
				marker.Direccion = p.direccion;
				marker.Guia = p.Guia;
				marker.X = p.x;
				marker.Y = p.y;
				marker.Fichero = p.fichero;
				marker.W = p.width;
				marker.H = p.height;

	 			map.addOverlay(marker);
				GEvent.bind(marker, "click", marker, marker.onPuntoClick);

			}
			if (puntos.length==1) {
				map.setCenter(new GLatLng(p.x, p.y), parseFloat(ori_z+1));
			}
		}
	}
	request.send(null);
}

function getMapType(i) { return map.getMapTypes()[i]; }
function ifu(x, d) { return x != undefined ? x : d; }
function loadPosition()
{
	var center = new GLatLng(parseFloat(ifu(ori_y, -0.4802227020263672)), parseFloat(ifu(ori_x, 38.34535865432956)));
	var zoom = ifu(ori_z, 13);
	var type = ori_k != undefined ? getMapType(ori_k) : G_HYBRID_TYPE;

	map.setMapType(type);
	map.setCenter(new GLatLng(parseFloat(ori_x), parseFloat(ori_y)), parseFloat(ori_z));
}
