/**
 * 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/"
 * });
 */
jQuery.fn.gmaps = function(settings){
	return this.each(function(){
		new jQuery.gmaps(this, settings);
	});
}

jQuery.gmaps = function(obj, settings){
	var map = false;
	var currentMapType=0;
	var defaultZoomLevel=0;
	var gitems = new Array();
	
	if(mapZoom == "-2")
		defaultZoomLevel=7;
	else
		defaultZoomLevel=mapZoom;
		
	if(defaultMapType == 0) {
		currentMapType=G_NORMAL_MAP;
	}
	else {
		currentMapType=defaultMapType;
	}
	
	settings = jQuery.extend({
		data: {locations:[]},
		infopanel: "none",
		maptype: currentMapType,
		center: [37.98547493214612 , -1.1305618286132812],
		zoom: defaultZoomLevel,
		relativepath: "http://127.0.0.1:8000/"
	},settings);
		
	if (GBrowserIsCompatible()) {
	    map = new GMap2(obj);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		if (typeof(settings.infopanel) != Boolean){
			GEvent.addListener(map, "moveend", function() {
				var center = map.getCenter();
				var zoom = map.getZoom();
				defaultMapType=map.getCurrentMapType();
				mapZoom=zoom;
				$(settings.infopanel).html(center.toString() + " zoom: " + zoom);
			});
		}
		
		map.setCenter(new GLatLng(settings.center[0],settings.center[1]), settings.zoom, settings.maptype);
		
		// Create the text for the marker
        function createMarker(point, number) {
			// create custom icon
			
			var icon = new GIcon();
			icon.image = (settings.data.locations[number].icon.indexOf("http:") < 0)?
							settings.relativepath + settings.data.locations[number].icon:settings.data.locations[number].icon;
			icon.iconSize = new GSize(18, 18);
			icon.iconAnchor = new GPoint(6, 18);
			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>"
            		marker.openInfoWindowHtml(simpleContent, 
									{maxUrl:(settings.data.locations[number].maximizedContent.indexOf("http:") < 0)?
											settings.relativepath + settings.data.locations[number].maximizedContent:
											settings.data.locations[number].maximizedContent
									}
					);*/
					marker.openInfoWindowHtml(settings.data.locations[number].simpleContent);
				}else{
					marker.openInfoWindowHtml(settings.data.locations[number].simpleContent);
				}				
			});
			
			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 ="<img align='left' src='"+div_gmap.gmap.jq_maps.settings.data.locations[indice].user_image+"'><br>"+div_gmap.gmap.jq_maps.settings.data.locations[indice].simpleContent + "<br>" +div_gmap.gmap.jq_maps.settings.data.locations[indice].username + "<br><br><span style='font-size:11px;'>" +div_gmap.gmap.jq_maps.settings.data.locations[indice].user_message+ "</span><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>"*/
simpleContent ="<table width=\"350\" style=\"width:350px;color:#000000;background-color:#FFFFFF;\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr  width=\"350\"><td width=\"350\" valign=\"top\"><a title='"+div_gmap.gmap.jq_maps.settings.data.locations[indice].username+"' href='http://shuanda.net/"+div_gmap.gmap.jq_maps.settings.data.locations[indice].maximizedContent+"' target='_blank'><img align='left' border='0' src='"+div_gmap.gmap.jq_maps.settings.data.locations[indice].user_image+"'></a><div style='font-size:14px;padding-top:2px;'>" +div_gmap.gmap.jq_maps.settings.data.locations[indice].user_message+ "</div><div style='padding-top:5px;'><a href='http://shuanda.net/"+div_gmap.gmap.jq_maps.settings.data.locations[indice].maximizedContent+"' class='style6 kalin renk_mavi' target='_blank'>"+div_gmap.gmap.jq_maps.settings.data.locations[indice].username + "</a><span class='style5'> " +div_gmap.gmap.jq_maps.settings.data.locations[indice].simpleContent +" "+div_gmap.gmap.jq_maps.settings.data.locations[indice].time+" </span> <span class='renk_yesil'>"+div_gmap.gmap.jq_maps.settings.data.locations[indice].source +"</span></div></td></tr></table>";
            		/*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
									}
					);*/
					div_gmap.gmap.jq_maps.gitems[indice].openInfoWindowHtml(simpleContent);
				}else{
					div_gmap.gmap.jq_maps.gitems[indice].openInfoWindowHtml(div_gmap.gmap.jq_maps.settings.data.locations[indice].simpleContent);
				}	
			}
		}
	}
	
	obj.gmap = map;
}