/**
 * The map object, null until script loads in.
 * @type {GMap2}
 */
var map = null;  
var mapDiv = null;
/**
 * The dom element that everything is a child of.
 * @type {Element}
 */
var containerDiv = null;

/**
 * Position of mouse click (clientX) on map div when in static mode.
 * @type {Number}
 */
var clickedX = 0;

/**
 * Position of mouse click (clientY) on map div when in static mode.
 * @type {Number}
 */
var clickedY = 0;
var hideMarker = false;
/**
 * Indicates whether we've created a script tag with Maps API yet
 * @type {Boolean}
 */
var geo = null;
var defaultLang = 'en';

var mapbounds = false;
var isLoaded = false;
var gmarkers = [];
var GMapIsDone = false;
/**
 * Called after script is asynchronously loaded in.
 * Creates the GMap2, GMarker objects and performs actions according to 
 * what the user did to trigger the map load (search, zoom, click etc).
 */
function loadMap() {
	try{
	  if (GBrowserIsCompatible()) {
		  if ($('googleMap')){
		    map = new GMap2(mapDiv);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
		  }
		}
		showMap1();
  } catch(err) {
	debug(err);
  }
}


/**
 * Loads in the Maps API script. This is called after some sort of user interaction.
 * The script loads asynchronously and calls loadMap once it's in.
 */
function loadScript() {
try {
	mapDiv = document.getElementById('googleMap');
//	mapDiv.style.position="relative";
	mapDiv.style.height="400px";
	mapDiv.style.width="796px";
	
    var div = document.createElement('div');
    div.className = 'gmessage';
    div.innerHTML = 'Loading...';
    div.style.left = (796/2 - 53) + 'px';
    div.style.top = 400/2 + 'px'; 
	div.style.position="absolute";
    mapDiv.appendChild(div);
	
  if (!isLoaded) {
    isLoaded = true;
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://maps.google.com/maps?file=api&v=2.x&hl='+defaultLang+ 
                 '&async=2&callback=loadMap&key='+googleKey;
    document.body.appendChild(script);
	addUnLoadEvent(function () { GUnload(); });
	
  }
  else {
  loadMap();
  map.checkResize();
  }
 } catch (err) {
	debug (err);
 }
}

function fitBounds(){
	var bn = bounds.replace(/[\(\) ]/g,'').split(',');
	var sw = new GLatLng(bn[0], bn[1]);  
	var ne = new GLatLng(bn[2], bn[3]);  
	var mapbounds = new GLatLngBounds(sw, ne);  
	map.setCenter(mapbounds.getCenter(), map.getBoundsZoomLevel(mapbounds));  
}
function createMarker(object,icon) {
/*
	var iconObject = new GIcon();
	iconObject.image = "bs/designs/default/images/gm_icons/start_point.png";
	iconObject.shadow = "bs/designs/default/images/gm_icons/start_point_shadow.png";
	iconObject.iconSize = new GSize(19, 26);
	iconObject.shadowSize = new GSize(32, 26);
	iconObject.iconAnchor = new GPoint(1, 26);
	iconObject.infoWindowAnchor = new GPoint(8, 8);
	*/
	
  var marker = new GMarker(new GLatLng(object.lat, object.lng),{draggable: object.drag});
  return marker;
}