//GOOGLE MAPS
var map = null;
var mgr = null;
var geocoder = null;
var markers_small = [];
var markers_large = [];
var IMAGES = [ "dot", "arrow"];
var ICONS = [];

function setupMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(48.25, 11.00), 3);
		map.enableDoubleClickZoom();
		window.setTimeout(setupWeatherMarkers, 0);		
	}
}

function setupStepMarkers(s, b) {
	mgr = new MarkerManager(map, {trackMarkers:true});
	mgr.addMarkers(b,0,3);
	mgr.addMarkers(s,4,14);	
	mgr.refresh();
}


function TextualZoomControl() {
}


function initialize() {
  
  if (GBrowserIsCompatible()) {
	//init geocoder
  geocoder = new GClientGeocoder();

	// Custom zoom buttons
	
	TextualZoomControl.prototype = new GControl();

	TextualZoomControl.prototype.initialize = function(map) {
	  var container = document.createElement("div");	
	  var zoomInDiv = document.createElement("div");
	  this.setButtonStyle_(zoomInDiv);
	  container.appendChild(zoomInDiv);
	  zoomInDiv.appendChild(document.createTextNode("+"));
	  GEvent.addDomListener(zoomInDiv, "click", function() {
		if(map.getZoom() < 10) {
			map.zoomIn();
		}
	  });
	
	  var zoomOutDiv = document.createElement("div");
	  this.setButtonStyle_(zoomOutDiv);
	  container.appendChild(zoomOutDiv);
	  zoomOutDiv.appendChild(document.createTextNode("-"));
	  GEvent.addDomListener(zoomOutDiv, "click", function() {
		if(map.getZoom() > 2) {
			map.zoomOut();
		}
	  });
	
	  map.getContainer().appendChild(container);
	  return container;
	};
	
	// By default, the control will appear in the top left corner of the
	// map with 7 pixels of padding.
	TextualZoomControl.prototype.getDefaultPosition = function() {
	  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
	};
	
	// Sets CSS for the given button element.
	TextualZoomControl.prototype.setButtonStyle_ = function(button) {
	  button.style.color = "#0000cc";
	  button.style.backgroundColor = "white";
	  button.style.font = "small Arial";
	  button.style.border = "1px solid black";
	  button.style.padding = "2px";
	  button.style.marginBottom = "3px";
	  button.style.textAlign = "center";
	  button.style.width = "25px";
	  button.style.cursor = "pointer";
	  button.style.textDecoration = "none";
	};

	map = new GMap2(document.getElementById("map_canvas"));
	map.disableDoubleClickZoom();

	//center on default location
	map.setCenter(new GLatLng(20, -34.00), 2);
	
	//Get users country from IP and center the map on location
	script = document.createElement('script');
	script.src = "http://ipinfodb.com/ip_query_country.php?output=json&callback=centerMapOnLocation";
	document.body.appendChild(script);

	//init marker manager
	mgr = new MarkerManager(map);	
	
	map.addControl(new TextualZoomControl());

	GDownloadUrl("step_map/xml/markers.xml", function(data) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		
		//loop through markers
		for (var i = 0; i < markers.length; i++) {
			
			//if marker coords are defined
			if(markers[i].getAttribute("lat") != "") {
			
				//point
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),	parseFloat(markers[i].getAttribute("lng")));
				
				//Custom Marker // Set up our GMarkerOptions object
				var stepMarker = new GIcon(G_DEFAULT_ICON);		
				if(markers[i].getAttribute("colour") !==  null ) {
					var colour = markers[i].getAttribute("colour");
					stepMarker.image = "step_map/imgs/" + colour + ".png";
				} else {
					stepMarker.image = "step_map/imgs/dot.png";
				}
				
				//shadows
				if(stepMarker.image == "step_map/imgs/dot.png"){
					stepMarker.shadow = "step_map/imgs/dot_shadow.png";
					stepMarker.shadowSize = new GSize(10, 7);
				} else if(stepMarker.image == "step_map/imgs/arrow.png") {
					stepMarker.shadow = "step_map/imgs/arrow_shadow.png";
					stepMarker.shadowSize = new GSize(10, 8);
				}
				
				stepMarker.iconAnchor = new GPoint(5, 10);
				stepMarker.infoWindowAnchor = new GPoint(5, 10);

				//marker size
				stepMarker.iconSize = new GSize(10, 10);				
				markerOptions = { icon:stepMarker };
				
				if(markers[i].getAttribute("lat") !== null) {
					//Address
					var address = markers[i].getAttribute("url");
					var marker = new GMarker(point, markerOptions);
					marker.address = address;
					
					if(markers[i].getAttribute("cat") == "top") {						
						GEvent.addListener(marker, "click", function(marker) {
							map.setCenter(marker, 4);
						});
						markers_large.push(marker);
					} else {	
						GEvent.addListener(marker, "click", function() {
							this.address = this.address;
							window.location = "http://" + this.address;
						});						
					  markers_small.push(marker);
					}				
				}			
			}		
		}
		setupStepMarkers(markers_small,markers_large);	
	});	

  }
}

function showAddress(address) {
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  alert(address + " not found");
		} else {
		  map.setCenter(point, 6);
		}
	  }
	);
  }
}


function adjustMarkerText(text) {
	text = text.replace(/,/g, "<br />");
	text = "<div style='font-size:10px; color:#000000;'>" + text + "</div>";
	return text;
}


//center on uses contry if any than London
function centerOnAddress(address) {
  if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  map.setCenter(new GLatLng(20, -34.00), 2);
		} else {
		  map.setCenter(point, 2);			  
		}
	  }
	);
  }
}	



function centerMapOnLocation(location) {
	//init geocoder
	geocoder = new GClientGeocoder();
	
	//Get the users country
	CountryCode = location['CountryName'];
	
	if(CountryCode != "") {
		centerOnAddress(CountryCode);
	}	
	
}



//----------------------------------------------- jQuery - init the map after the page is loaded
(function($) {
	$(document).ready(function() {
			//If map exist initialize it
			if( document.getElementById("map_canvas") ) {		
				initialize();		
			}	
	});
})(jQuery);
