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

$j(document.body).unload(function() {
	if (GBrowserIsCompatible()) {
		GUnload();
	}
});


$j(document).ready(function() {
	loadMapScript(GMapsKey);
});



function loadGMap() {
	if (GBrowserIsCompatible()) {

		var locations;
		$j.getJSON(document.URL,{ajax: 'true', query:'GlobalMapData', eventful:eventful}, 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;    

			map = new GMap2(document.getElementById("dbem_global_map"));
			map.setCenter(new GLatLng(45.4213477,10.952397), 3);

			$j.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 GLatLngBounds(new GLatLng(max_latitude + vertical_compensation,min_longitude),new GLatLng(min_latitude,max_longitude) );
			//console.log(locationsBound);
			var locationsZoom = map.getBoundsZoomLevel(locationsBound);
			map.setCenter(new GLatLng(center_lat + vertical_compensation,center_lon), locationsZoom); 
			var letters = new Array('A','B','C','D','E','F','G','H');

			var customRedIcon = new GIcon(G_DEFAULT_ICON);
			var customYellowIcon = new GIcon(G_DEFAULT_ICON);
			var customGreenIcon = new GIcon(G_DEFAULT_ICON);
			var customOrangeIcon = new GIcon(G_DEFAULT_ICON);
			var customPinkIcon = new GIcon(G_DEFAULT_ICON);
			
			customRedIcon.image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png'
			customYellowIcon.image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/yellow-dot.png'
			customGreenIcon.image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png'
			customOrangeIcon.image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/orange-dot.png'
			customPinkIcon.image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/pink-dot.png'
			
			customRedIcon.iconSize = new GSize(32, 32);
			customYellowIcon.iconSize = new GSize(32, 32);
			customGreenIcon.iconSize = new GSize(32, 32);
			customOrangeIcon.iconSize = new GSize(32, 32);			
			customPinkIcon.iconSize = new GSize(32, 32);
			
			customRedIcon.shadowSize = new GSize(0, 0);
			customYellowIcon.shadowSize = new GSize(0, 0);
			customGreenIcon.shadowSize = new GSize(0, 0);
			customOrangeIcon.shadowSize = new GSize(0, 0);
			customPinkIcon.shadowSize = new GSize(0, 0);
			
			var customIcons = new Array(customRedIcon, customYellowIcon, customGreenIcon, customOrangeIcon, customPinkIcon);
			var customIcon;
			var randomNumber;
			
			$j.each(locations, function(i, item){
				//var letter = letters[i];
				
				//return a random integer between 0 and 4
				randomNumber = Math.floor(Math.random()*5)
				customIcon = customIcons[randomNumber];
				
				//customIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
				//customIcon.image = "http://www.google.com/mapfiles/marker.png";
				//customIcon.image = "http://www.vivalashermans.co.uk/images/marker.png";
				//customIcon.image = "http://www.vivalashermans.co.uk/images/music-live.png";

				markerOption = { icon:customIcon };
				var point = new GLatLng(parseFloat(item.location_latitude), parseFloat(item.location_longitude));
				var marker = new GMarker(point, markerOption);
				map.addOverlay(marker);
						map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
				//var li_element = "<li id='location-"+item.location_id+"' style='list-style-type: upper-alpha'><a >"+ item.location_name+"</a></li>";
				//$j('ol#dbem_locations_list').append(li_element);
				//$j('li#location-'+item.location_id+' a').click(function(){
				//	displayLocationInfo(marker, item);
				//});
				GEvent.addListener(marker, "click", function() {
					displayLocationInfo(marker, item);

				});


			});



		});


	}
}


function loadMapScript(key) {
	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);
}

function displayLocationInfo(marker, location) {
	var location_infos = "<strong>"+ location.location_name +"</strong><br/>" + location.location_address + ", " + location.location_town + "<br/><small><a href='" + events_page + "&location_id=" + location.location_id + "'>Details<a>";
	window.map.openInfoWindowHtml(marker.getLatLng(),location.location_baloon,  {pixelOffset: new GSize(0,-32)});
}
