/**
 * Plugin for jQuery 1.1 to manage Google Maps
 * @author SeViR
 *
 * the method wait locations data as JS Object with this structure:
 *
 * data: {locations: [
 * 	{
 *  	simpleContent: String with the HTML content in the info window.
 * 		maximizedContent : URL of the HTML page (AJAX loaded) of the maximized info window.
 * 		latitude: latitude of the geographic poing (you can use infopanel for obtain that)
 * 		longitude: longitude of the geographic poing (you can use infopanel for obtain that)
 * 		zoom: zoom level for hotspot links
 * 		icon: url of the custom icon for this place
 *  },
 *  ...
 * ]}
 * 
 * The "infopanel" param is a DOM object to show the latitude and longitude of the
 * center of the map, also, the level zoom applied.
 * 
 * The "maptype" param is some of map type that admits Google MAPS like G_MAP_TYPE, G_SATELLITE_TYPE y G_HYBRID_TYPE.
 * 
 * The "center" param is a point [latitude, longitude of the default point of the map center
 * 
 * The "zoom" param is the default zoom applied
 * 
 * The "relativepath" param is the absolute url folder path of the document (used for relative paths
 * of icons and maximized content).
 * 
 * sample:
 * $("#mylayer").gmaps({
 * 	data: myplaces, 
 * 	infopanel: $("#infolayer").get(0), 
 * 	tipomapa: G_HYBRID_TYPE,
 *  centro: [1, -0.3],
 *  zoom: 10,
 *  relativepath: "http://mydomain.com/myproject/"
 * });
 */
	/* Contantes de las posiciones de la información dentro del array de hoteles */
		var HOTEL_CODIGO    = 0;
		var HOTEL_NOMBRE    = 1;
		var HOTEL_CATEGORIA = 2;
		var HOTEL_PAIS      = 3;
		var HOTEL_CIUDAD    = 4;
		var HOTEL_DIRECCION = 5;
		var HOTEL_LATITUD   = 6;
		var HOTEL_LONGITUD  = 7;
		var HOTEL_MARCA     = 8;
		var HOTEL_PRECIO    = 9;
		var HOTEL_PATH_FOTO = 10;
		
 
jQuery.fn.gmaps = function(settings){
	return this.each(function(){
		new jQuery.gmaps(this, settings);
	});
}

jQuery.gmaps = function(obj, settings){
	var map = false;
	var gitems = new Array();
// The following line makes the map Earth-enabled by adding the
      // "Earth" button to the map type control.  Note that you still
      // need to add a map type control (GMapTypeControl,
      // GMenuMapTypeControl, or GHierarchicalMapTypeControl) to the
      // the map (as is done below) for the "Earth" button and the rest
      // of the map type buttons to show up at all.
      // Also, be sure you are loading v=2.x of the Maps API and not
      // v=2, otherwise the G_SATELLITE_3D_MAP map type will not
      // work properly.
     
	  
		/* Devuelve el texto que se mostrara dentro de la ventana de información de cada marcador */
		function formatearContenido() {
			var texto = new String();
			
			texto    =		"<table bgcolor='#ffffff' border='0' cellpadding='0' cellspacing='0' width='305'>";
			texto    +=			"<tr>";
			texto    +=				"<td width='8'></td>";
			texto    +=				"<td width='160'></td>";
			texto    +=				"<td width='5'></td>";
			texto    +=				"<td width='132'></td>";
			texto    +=			"</tr>";
			texto    +=			"<tr bgcolor='#EEEEEE'>";
			texto    +=				"<td align='right' class='v_333333_11n' height='15' valign='top'></td>";
			texto    +=				"<td colspan='3' class='v_333333_11b'>&nbsp;"+contenedorHoteles[HOTEL_NOMBRE]+" "+contenedorHoteles[HOTEL_CATEGORIA]+"</td>";
			texto    +=			"</tr>";
					
			texto    +=			"<tr>";
			texto    +=				"<td colspan='4'></td>";
			texto    +=			"</tr>";
					   
			texto    +=			"<tr>";
			texto    +=				"<td valign='top'></td>";
			texto    +=				"<td valign='top'>";	
							
			texto    +=					"<table bgcolor='#ffffff' border='0' cellpadding='0' cellspacing='0' width='160'>";
			texto    +=						"<tr>";
			texto    +=							"<td height='5'></td>";
			texto    +=						"</tr>";
			texto    +=						"<tr>";
			texto    +=							"<td class='v_333333_11n'>";
			texto    +=								""+contenedorHoteles[HOTEL_DIRECCION]+"<br/>"
			texto    +=								""+contenedorHoteles[HOTEL_CIUDAD]+"<br/>"
			texto    +=								""+contenedorHoteles[HOTEL_PAIS]+"<br/>";
			texto    +=								"<br/>";
			texto    +=								"<br/>";
			texto    +=								"<br/>";
			texto    +=							"</td>";
			texto    +=						"</tr>";
			texto    +=					"</table>";		
							
			texto    +=				"</td>";
			texto    +=				"<td>&nbsp;</td>";
			texto    +=				"<td><img class='mapPopImg' src='http://www.solmelia.com/solNew/img/hoteles/"+contenedorHoteles[HOTEL_CODIGO]+"/"+contenedorHoteles[HOTEL_PATH_FOTO]+"' border='1' width='130' height='90' alt='"+contenedorHoteles[HOTEL_NOMBRE]+"'></td>";
			texto    +=			"</tr>";
						
			texto    +=			"<tr>";
			texto    +=				"<td colspan='4'></td>";
			texto    +=			"</tr>"; 	
			texto    +=		"</table>";
			
			return texto;
	}

	settings = jQuery.extend({
		data: {locations:[]},
		infopanel: "none",
		/*maptype: G_MAP_TYPE,*/
		center: [contenedorHoteles[HOTEL_LATITUD] , contenedorHoteles[HOTEL_LONGITUD]],
		zoom: 16,
		relativepath: "/",
	},settings);
		
	if (GBrowserIsCompatible()) {
	    map = new GMap2(obj);
/*		map.addMapType(G_SATELLITE_3D_MAP);*/
		
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
/*		map.addControl(new GHierarchicalMapTypeControl());
        map.addControl(new GLargeMapControl());*/
		
		if((msCodSolRes)&&(msCodSolRes=="5950")){
			map.addOverlay(new GLayer("com.panoramio.all"));
			map.addOverlay(new GLayer("org.wikipedia.en"));
		}
		
		if (typeof(settings.infopanel) != Boolean){
			GEvent.addListener(map, "moveend", function() {
				var center = map.getCenter();
				var zoom = map.getZoom();
				
				$(settings.infopanel).html(center.toString() + " zoom: " + zoom);
			});
		}
		
		// map.setCenter(new GLatLng(settings.center[0],settings.center[1]), settings.zoom, settings.maptype);
		map.setCenter(new GLatLng(settings.center[0],settings.center[1]), settings.zoom);
		// Create the text for the marker
        function createMarker(point, number) {
			// create custom icon
			var icon = new GIcon();
			var iconSizeX = new Number();
			var iconSizeY = new Number();
			
			icon.image = (settings.data.locations[number].icon.indexOf("http:") < 0)?
							settings.relativepath + settings.data.locations[number].icon:settings.data.locations[number].icon;
			iconSizeX = settings.data.locations[number].iconSizeX;
			iconSizeY = settings.data.locations[number].iconSizeY;
			halfSizeX = (Math.round(iconSizeX/2));
			//alert (iconSizeX+''+iconSizeY);
			
			icon.iconSize = new GSize(iconSizeX, iconSizeY);
			icon.iconAnchor       = new GPoint(150, 40);
		    icon.infoWindowAnchor = new GPoint(halfSizeX, 2);
			/*
			icon.iconAnchor = new GPoint((iconSizeX-(iconSizeX*2), iconSizeY);
			icon.infoWindowAnchor = new GPoint(5, 1);
			*/
          	var marker = new GMarker(point, icon);
		  	
         	GEvent.addListener(marker, "click", function() {
				if (settings.data.locations[number].maximizedContent != ""){
					simpleContent = settings.data.locations[number].simpleContent + "<a href='#' style='font-size:10px;margin-top:20px;display: block;' onclick='$(\"#"+obj.id+"\").get(0).mapa.getInfoWindow().maximize();return false;'>Ver informaci&oacute;n expandida</a>" + " " + formatearContenido();
            		marker.openInfoWindowHtml(simpleContent, 
									{maxUrl:(settings.data.locations[number].maximizedContent.indexOf("http:") < 0)?
											settings.relativepath + settings.data.locations[number].maximizedContent:
											settings.data.locations[number].maximizedContent
									}
					);
				}else{
					marker.openInfoWindowHtml(formatearContenido());
				}				
			});
			
			gitems.push(marker);
          
          return marker;
        }
		//Inserts markers
		for (i=0;i<settings.data.locations.length;i++){
			var point = new GLatLng(settings.data.locations[i].latitude,
	                                  settings.data.locations[i].longitude);
			map.addOverlay(createMarker(point, i));
		}	
		
		//Loads the events of the link of the hotspots
		$("." +obj.id).each(function(){
			this.associated_map = obj.id;
			$(this).click(function(){
				$("#"+this.associated_map).get(0).gmap.jq_maps.goTo($("#"+this.associated_map).get(0), this.tabIndex - 1);
				return false;
			});
		});
		
		//save the properties in gmap property, and set the new options for goTo point and maximize Window
		map.jq_maps = {
			"settings": settings,
			"gitems": gitems,
			"goTo": function(div_gmap, indice){
				div_gmap.gmap.setZoom(div_gmap.gmap.jq_maps.settings.data.locations[indice].zoom);
				div_gmap.gmap.panTo(new GLatLng(div_gmap.gmap.jq_maps.settings.data.locations[indice].latitude, div_gmap.gmap.jq_maps.settings.data.locations[indice].longitude));
				if (div_gmap.gmap.jq_maps.settings.data.locations[indice].maximizedContent != ""){
					simpleContent = div_gmap.gmap.jq_maps.settings.data.locations[indice].simpleContent + "<a href='#' style='font-size:10px;margin-top:20px;display: block;' onclick='$(\"#"+div_gmap.id+"\").get(0).gmap.getInfoWindow().maximize();return false;'>Show extended info</a>"
            		div_gmap.gmap.jq_maps.gitems[indice].openInfoWindowHtml(simpleContent, 
									{maxUrl:(div_gmap.gmap.jq_maps.settings.data.locations[indice].maximizedContent.indexOf("http:") < 0)?
											div_gmap.gmap.jq_maps.settings.relativepath + div_gmap.gmap.jq_maps.settings.data.locations[indice].maximizedContent:
											div_gmap.gmap.jq_maps.settings.data.locations[indice].maximizedContent
									}
					);
				}else{
					div_gmap.gmap.jq_maps.gitems[indice].openInfoWindowHtml(div_gmap.gmap.jq_maps.settings.data.locations[indice].simpleContent);
				}	
			}
		}
	}
	
	obj.gmap = map;
}