/*
	# IF YOU NEED TO FIND LAT & LANG FOR YOUR LOCATION USE THE CODE LINE BELOW IN IE ADDRESS BAR!
	# NOTE CENTER YOUR GOOGLE MAP WINDOW TO YOUR LOCATION BEFORE RUNNING CODE BELOW IN THE ADDRESS BAR!
	javascript:void(prompt('',gApplication.getMap().getCenter()));

	
	# ADD THE FOLLOWING 3 LINES TO YOU HTML PAGE HEADER!
	<script type="text/javascript" src="googlemaps.js"></script>
	<script type="text/javascript"src="http://www.google.com/jsapi?key=ABQIAAAAstDVhYFvohdetAzHqH8_rxQzCKWWcagojIm8AISTHt8pHIFZghQKQLBWF3mQns3G3jJKmN7JKVK-Xw"></script>
	<script type="text/javascript"charset="utf-8">google.load("maps","2.x");</script>
	
	
	# USE THE FOLLOWING LINE TO CREATE A MAP ON YOUR PAGE!
	<script type="text/javascript">create_map("PALCE-YOUR-MAP-DIV-ID-HERE",PLACE-LATITUDE-HERE,PLACE-LONGITUDE-HERE,ZOOM-LEVEL-HERE,"default","SET TRUE OR FALSE TO TURN OF MAP DRAGGING");</script>
	eg. <script type="text/javascript">create_map("map_canvas",51.2156086,-0.8005446,17,"default", false);</script>
	
	
	# USE THE FOLOWING LINE TO ADD A MARKER TO YOUR MAP!
	<script type="text/javascript">create_map("MARKER-ID-HERE", SET-MARKER-FOCUS, SET-MARKER-ZOOM,PLACE-LATITUDE-HERE,PLACE-LONGITUDE-HERE,ZOOM-LEVEL-HERE,"MARKER-DATA-GOES-HERE","SET_MARKER-SHOW", "SET CUSTOM MARKER IMAGE PATH HERE");</script>
	e.g add_marker("marker_a", false, 0, 51.2156086,-0.8005446, "HELLO WORLD!", false, "images/my_marker.png");
	e.g add_marker("marker_a", false, 0, 51.2156086,-0.8005446, "<div>HELLO WORLD!<br /><font color='red' style='font-size:40px'>WITH HTML TAGS</font></div>", true, "images/my_marker.png");
	
	
	# USE THE FOLOWING LINE TO REMOVE A MARKER FROM YOUR MAP!
	<script type="text/javascript">create_map("MARKER-ID-HERE");</script>
	e.g add_marker("marker_a");
	
	
*/

/*
	FUNCTIONS
	---------
	
	create_map 			:	Input Variables
							---------------
							# map_id 			(this will be the div you want the map to aper in)
							# map_latitude 		(latitude...)
							# map_longitude 	(Longitude...)
							# map_zoom 			(this will be an integer between 1 and 21)
							# map_controls 		(this will let you set user navigation for maps,
													## default
													## basic_small
													## basic_large
													## "" BLANK! leaving the variable blank will remove zoom and panning controls!
												)
							# map_disableUIdrag	(this will a boolean flag, set to true or false)
												
	add_marker 			:	Input Variables
							---------------
							# marker_id 		(this is an optional local tracking id as a string in "")
							# marker_focus		(this variable is boolean (true/false) it will center the map on the marker)
							# marker_zoom		(this is an integer between 1 to 21 AND WILL ONLY TAKE EFFECT IF marker_focus is set to TRUE)
							# marker_latitude 	(latitude...)
							# marker_longitude 	(Longitude...)
							# marker_data 		(this is a single string input and can include HTML TAGS)
							# marker_show		(this variable is boolean (true/false) it will show a marker window, 
												 YOU CAN ONLY SHOW ONE MARKER WINDOW AT A TIME)
	
	remove_marker 			:	Input Variables
							---------------
							# marker_id 		(this is an optional local tracking id as a string in "")
*/


// Global map and marker variable...
var map;
var markersArray = [];

function create_map(map_id, map_latitude, map_longitude, map_zoom, opt_map_controls, map_disableUIdrag){
	
	if (GBrowserIsCompatible()){
		
		// set map frame and create a new instance...
		map = new GMap2(document.getElementById(map_id));
		
		//set map location and zoom levels...
		map.setCenter(new GLatLng(map_latitude, map_longitude), map_zoom);
		
		// set map movement/navigation controls...
		switch(opt_map_controls){
			case "basic_small":
				map.addControl(new GSmallMapControl());
			break;
			case "basic_large":
				map.addControl(new GLargeMapControl());
			break;
			case "default":
				map.setUIToDefault();
			break;
		}
		
		if(map_disableUIdrag == true){
			map.disableDragging();
		}
		
	}else{
		alert("Google maps is not currently compatiable with your internet browser");
	}
	
}

function add_marker(marker_id, marker_focus, marker_zoom, marker_latitude, marker_longitude, marker_data, marker_show, marker_custom) {
	
	// flags and variables...
	var bexists = false;
	
	// set marker coordinates...
	var point = new GLatLng(marker_latitude, marker_longitude);
	if(marker_focus == true){
		map.setCenter(point, marker_zoom);
	}
	
	// load custom marker icons if option is set...
	if(marker_custom != ""){
		var customIcon = new GIcon(G_DEFAULT_ICON);
		customIcon.image = marker_custom;
		markerOptions = { icon:customIcon };
	}
	
	// overlay new marker on the map...
	if(marker_custom != ""){
		var marker = new GMarker(point, markerOptions);
	}else{
		var marker = new GMarker(point);
	}
	
	// add html data to the marker...
	GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(marker_data);});
	
	// show marker if the option is set...
	if(marker_show == true){
		GEvent.trigger(marker, marker.point);
	}
	
	// add custom id to marker and then push it to array...
	marker.id = marker_id;
	if (markersArray){
		// ok we have markers lets check to see if this is a duplicate...
		for (i in markersArray){
			if(markersArray[i].id == marker_id){
				bexists = true;
			}
		}
		
		if(bexists == false){
			// marker does not exist so lets add it...
			markersArray.push(marker);
			map.addOverlay(marker);
		}
		
	}else{
		//nothing in array so lets add the first marker...
		markersArray.push(marker);
		map.addOverlay(marker);
	}
	
}

function remove_marker(marker_id){

	if (markersArray){
		for (i in markersArray){
			if(markersArray[i].id == marker_id){
				map.removeOverlay(markersArray[i]);
				markersArray.splice(i,1);
			}
			
			// ok below is a special flag that will remove all markers from the map...
			if(marker_id == "KILL-9"){
				map.removeOverlay(markersArray[i]);
			}
		}
		// ok below is a special flag that will remove all markers from the markersArray...
		if(marker_id == "KILL-9"){
			markersArray.length = 0;
		}
	}
	
}
