//
//
// ***** Load or unload the Google Map *****
//
//
//
marker_count = 0;
function googleMap(command) {
	
	if (command == "load") {	
		
		// Create new map object
		var map = new GMap2(document.getElementById("map"))
		
		// Declare a global reference to the map, otherwise other functions can't access it
		// Necessary because in IE you can't pass parameters to functions via javascript setInterval
		global_map = map;

		// Add controls
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
		
		// Retrieve the latitude and longitude
		point = new GLatLng(29.382175,-8.964844);

		// Center the map on this point
		map.setCenter(point, 1);
		
		// Show terrain
		map.setMapType(G_HYBRID_MAP);
		
		// Start adding markers to the map - stop this process if it is already running
		if (marker_count > 0) {
			clearInterval(marker_interval);
			marker_count = 0;
		}
		if (mapping_array.length > 0) {
			marker_interval = setInterval(showMarkers, 100);
			// Function to control what happens when you click the map
			GEvent.addListener(map, "click", function(marker) {
				// If you've clicked a marker:
  				if (marker) {
					// If you've clicked a marker, not an info window
					if (marker.id != undefined) {
						// If you're using Firefox - are you using less than v3?
						var ffVersion = navigator.userAgent.slice(navigator.userAgent.indexOf("Firefox") + 8, navigator.userAgent.length);
						var ffVersionMajor = Number(ffVersion.slice(0,3));
						if (ffVersionMajor < 3) {
							// You're using Firefox, less than version 3, so just open a simple info window
							// Because overflow:auto on DIVs inside Google Maps' tabbed info windows breaks everything in Firefox
							marker.openInfoWindowHtml("<span class='textBlackLarge'>" + mapping_array[marker.id].name + "</span>");
						} else {
							// You're using Firefox v3 or another browser, so open the full tabbed window
							// Attach a tabbed window to this marker
							// Fill it with default Loading messages until you can make an AJAX call to get the real data
							var infoTabs = [
  								new GInfoWindowTab("Details", "<div style='width: 280px; height: 36px; overflow: auto;' id='details'><span class='error'>Loading Details</span><img src='../images/global/please_wait.gif'></div><div style='width: 280px; height: 89px; overflow: auto;' id='details_content'><span class='error'>Loading Details</span><img src='../images/global/please_wait.gif'></div>"),
  								new GInfoWindowTab("Designers", "<div style='width: 280px; height: 36px; overflow: auto; id='designers'><span class='error'>Loading Designers</span><img src='../images/global/please_wait.gif'></div><div style='width: 280px; height: 89px; overflow: auto;' id='designers_content'><span class='error'>Loading Designers</span><img src='../images/global/please_wait.gif'></div>")
							];
							marker.openInfoWindowTabsHtml(infoTabs);
							// Populate the tabs with info relevant to this marker
							populateTab(marker, mapping_array[marker.id].id);
						}
					}
  				}
			});
		}
		
	} else if (command == "unload") {
		
		// Stop adding map markers
		marker_count = 0;
		clearInterval(marker_interval);
		
		// Set the background of the map DIV back to black
		document.getElementById("map").style.backgroundColor = "#000000";
		
		// Remove the map
		document.getElementById("map").innerHTML = "&nbsp;";
		
		// Stop memory leaks
		GUnload();
		
	}
}
//
//
// ***** Add map markers *****
//
//
function showMarkers() {
	// Increase the marker counter, or stop calling the adding function
	if (marker_count < mapping_array.length) {
		// Create a marker
		this["marker" + marker_count] = new GMarker(new GLatLng(mapping_array[marker_count].lat,mapping_array[marker_count].long), {title:mapping_array[marker_count].name});
		// Add the marker to map
		global_map.addOverlay(this["marker" + marker_count]);
		// Give the marker a unique ID
		this["marker" + marker_count].id = marker_count;
		// Increment counter
		marker_count++;
	} else {
		marker_count = 0;
		clearInterval(marker_interval);
	}
}
//
//
// ***** AJAX function to populate an info window's tabs *****
//
//
function populateTab(marker, id) {
	// Create xmlHttp object for the relevant browser
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				// Eval the xmlHttp response to turn its text string into functioning javascript
				var response = eval(xmlHttp.responseText);
				// Declare the variables here that you'll get from the AJAX javascript response, otherwise IT WON'T WORK!!!!!!!!!!
				var details, details_content, designers, designers_content;
				// Construct the two strings
				details_string = details + details_content;
				designers_string = designers + designers_content;
				// 'Overwrite' the existing Loading infoWindow with a new one containing the correct information
				var infoTabs = [
  					new GInfoWindowTab("Details", details_string),
  					new GInfoWindowTab("Designers", designers_string)
				];
				marker.openInfoWindowTabsHtml(infoTabs);
			}
		}
	};
	xmlHttp.open("GET", "../asp/outlet_details.asp?outlet_id=" + id + '&time=' + Math.floor(Math.random() * 100000), true);
	xmlHttp.send(null);
}
//
//
// ***** Javascript to Flash functions - open a designer's profile from a Google Map Info Tab *****
//
//
function gmapProfileJump(id) {
	// First close the Google Map
	googleMap("unload");
	// Access a callback function inside the SWF movie with the ID 'base'
    thisMovie("base").gmapProfileJumpCallBack(id);
}
function thisMovie(movieName) {
	// Return a reference to the requested Flash movie
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}
//
//
// ***** Create the smaller, outlet-specific Google Map *****
//
//
function googleMapSmall(command, lat, long, outlet) {
	
	if (command == "load") {
	
		// Create new map object
		var map = new GMap2(document.getElementById("mapsmall"))

		// Add controls
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
		
		// Retrieve the latitude and longitude
		point = new GLatLng(lat,long);

		// Center the map on this point
		map.setCenter(point, 13);
		
		// Create a marker to show the outlet
		marker = new GMarker(new GLatLng(lat,long), {title:outlet});
		// Add the marker to map
		map.addOverlay(marker);
		
	} else if (command == "unload") {
		
		// Set the background of the map DIV back to black
		document.getElementById("mapsmall").style.backgroundColor = "#000000";
		
		// Remove the map
		document.getElementById("mapsmall").innerHTML = "&nbsp;";
		
		// Stop memory leaks
		GUnload();
		
	}
}
