var map;
var gmarkers = [];
var new_icon = new GIcon(G_DEFAULT_ICON, "/images/blue-dot.png")  
new_icon.iconSize = new GSize(32, 32);

function loadMap () {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		document.getElementById("map").style.backgroundImage = "url(/images/mapblue.gif)";
		map.setCenter(new GLatLng(0,0), 1);
	}
	addOptions('Global View');
	getDealerResults();
}

function myclick(i) {
	GEvent.trigger(gmarkers[i], "click");
}

function createMarker(point,name,html) {
	var marker = new GMarker(point, new_icon);
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	gmarkers.push(marker);
	return marker;
}

function getMarkers(id) {
	map.clearOverlays();
	GDownloadUrl("find_a_dealer.html?markers=" + id, function(doc) {
		var xmlDoc = GXml.parse(doc);
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");

		for (var i = 0; i < markers.length; i++) {
			// obtain the attribues of each marker
			var lat = parseFloat(markers[i].getAttribute("lat"));
			var lng = parseFloat(markers[i].getAttribute("lng"));
			var point = new GLatLng(lat,lng);
			var html = markers[i].getAttribute("html");
			var label = markers[i].getAttribute("label");
			// create the marker
			var marker = createMarker(point,label,html);
			map.addOverlay(marker);
		}
	});
}

function clearOptions() {
	// Always clear an option list from the last entry to the first
	for (x = document.dealerform.country.length; x >= 0; x--) {
		document.dealerform.country[x] = null;
	}
}

function addOptions(region) {
	// Add option to the bottom of the list
	document.dealerform.country[document.dealerform.country.length] = new Option('Select Country', '');
	// region is passed with format country:lat,long;
	var temparray = new Array();
	var countrycoords = new Array();
	if (region == 'Global View') {
		temparray = countries.split(';');
		for (x in temparray) {
			countrycoords = temparray[x].split(':');
			document.dealerform.country[document.dealerform.country.length] = new Option(countrycoords[1], temparray[x]);
		}
	} else {
		temparray = jsregion[region].split(';');
		for (x in temparray) {
			countrycoords = temparray[x].split(':');
			document.dealerform.country[document.dealerform.country.length] = new Option(countrycoords[1], temparray[x]);
		}
	}
}

function moveMap(info, zoom, location) {
	if (info.search('0,0:') != -1 && info.search('Global View') == -1) { // if location is 0:0, don't move the map
		getDealerResults();
		return false;
	}
	
	if (info == '') {return;} // when initial select is chosen
	var coords = new Array();
	coords = info.split(':'); // : splits coords:region/country
	var latlng = new Array();
	latlng = coords[0].split(',');

	if (location == 'region') { // reset the country to null if region is changed
		// populate the countries by region
		clearOptions();
		addOptions(coords[1]);
		document.dealerform.country.selectedIndex = 0;
	}
	
	// global view should have zoom = 1
	if (info.search('Global View') != -1) {
		zoom = 1;
	}
	
	if (GBrowserIsCompatible()) {
		map.setCenter(new GLatLng(latlng[0],latlng[1]), zoom);
		//map.setZoom(zoom);
		//map.panTo(new GLatLng(latlng[0], latlng[1]));
	}
	// make the ajax call to fill in the location results below the map
	getDealerResults();
}

function getDealerResults() {
	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) {
					alert("Your browser does not support AJAX!");
					return false;
			}
		}
  }
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {
			document.getElementById('dealerresults').innerHTML = xmlHttp.responseText;
    }
  }
	var region = document.dealerform.region.options[document.dealerform.region.selectedIndex].value 
	var country = document.dealerform.country.options[document.dealerform.country.selectedIndex].value

	var regionname = region.split(':')
	var countryname = country.split(':')

	if (countryname[1] != undefined) {
		document.getElementById("locationsheader").innerHTML = countryname[1] + " Locations";
	} else {
		if (regionname[1] == 'Global View') {
			document.getElementById("locationsheader").innerHTML = "BioTek Direct Offices";
		} else {
			document.getElementById("locationsheader").innerHTML = regionname[1] + " Locations";
		}
	}

  xmlHttp.open("GET","find_a_dealer.html?country=" + country + "&region=" + region + "",true);
  xmlHttp.send(null);
	if (country.length > 0) {
		getMarkers(country);
	} else {
		getMarkers(region);
	}
}
