﻿
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();
	
	settings = jQuery.extend({
		data: {locations:[]},
		infopanel: "none",
		simpleContent: locations_data.locations[0].simpleContent,
		center: [locations_data.locations[0].latitude , locations_data.locations[0].longitude],
		zoom: locations_data.locations[0].zoom,
		relativepath: "http://tours-tv.com/",
		googleBarOptions : {
              style : "new",
              adsOptions : {
                client: "partner-google-maps-api",
                channel: "0217910351",
                adsafe: "high",
                language: l
              }
            }
	},settings);
		
	if (GBrowserIsCompatible()) {
	    map = new GMap2(obj);
		map.setMapType(G_HYBRID_MAP);
		map.enableScrollWheelZoom();
		map.enableGoogleBar();
		map.setUIToDefault()
		//map.addControl(new GLargeMapControl());
		//map.addControl(new GMapTypeControl());
		//map.addControl(new GOverviewMapControl());
		
		//var adsManager = new GAdsManager(map, "pub-3057689494288496");
        //adsManager.enable();
		
				
		var publisher_id = 'pub-3057689494288496';
		var adsManagerOptions = {
		maxAdsOnMap : 2,
		style: 'adunit',
		// The channel field is optional - replace this field with a channel number 
		// for Google AdSense tracking
		channel: '8479763286'  
		};

		adsManager = new GAdsManager(map, publisher_id, adsManagerOptions);
		adsManager.enable();
		
		
		/*
		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);
		
		// 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
									}
					);
				}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);
			//var gmarker=createMarker(point, i);
			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(){
			//alert(data.locations[obj.id].latitude);
		
			
				$("#"+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;
}