// JavaScript Document
var map = null;
var arrMarkers = new Array();

$(document).ready( function() {

	$.ajaxSetup({
		type: "POST", 
		url: "/distributor_map.asp", 
		dataType: "xml", 
		async: false				
	});
	

	$.ajax({
		data: "action=getdata", 
		success: function(xml) {
				populateRegions(xml); 
				$("#selRegion").attr("disabled", "");
				}, 
		error: function() { alert("AJAX failed ??"); $("#selRegion").attr("disabled", ""); }	
	});

	
	$("#selRegion").change( function() {
		var type = $("#selType").val();
		var region = $(this).val();
		if (region != '') {
			$("#selCountry").attr("disabled", "disabled");
			$.ajax({
				data: "action=getdata&Region="+region, 
				success: function(xml) { 
					getCountries(region); 
					updateMap(xml); }, 
				error: function() { alert("AJAX failed ??"); $("#selCountry").attr("disabled", ""); }	
			});
		}
	});	
	
	$("#selCountry").change( function() {
		var type = $("#selType").val();
		var region = $("#selRegion").val();
		var country = $(this).val();
		if (country != '') {
			$.ajax({
				data: "action=getdata&Country="+country+"&Region="+region, 
				success: function(xml) { updateMap(xml); }, 
				error: function() { alert("AJAX failed ??"); }	
			});
		}
	});

	initMap();
	
});

function initMap() {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		resetMap();
}

function resetMap() {
		map.clearOverlays();
		var loc = new GLatLng(0, 0);			
		map.setCenter(loc, 1);
		arrMarkers = new Array();		
}

function locAddress(lat, lng, address) {
	
	var loc = new GLatLng(lat, lng);
	var marker = new GMarker(loc);
	
	map.addOverlay(marker);
	
	GEvent.addListener(marker, "click", function() {    
		marker.openInfoWindowHtml(address);  
	});
	
	arrMarkers.push(marker);
}

function getCountries(region) {
	$.ajax({
		data: "action=getdata&Filter="+region, 
		success: function(xml) { populateCountries(xml); $("#selCountry").attr("disabled", ""); }, 
		error: function() { alert("AJAX failed ??"); }	
	});	
}

function populateRegions(xml) {
		$("#selRegion").removeOption(/./).addOption("", "-- Choose a region --", true);
		$("row", xml).each( function(i) { $("#selRegion").addOption($(this).attr("Region_id"), $(this).attr("Region"), false); });
}

function populateCountries(xml) {
		$("#selCountry").removeOption(/./).addOption("", "-- Choose a country --", true);
		$("row", xml).each( function(i) { $("#selCountry").addOption($(this).attr("Country_id"), $(this).attr("Country"), false); });	
}

function updateMap(xml) {
		resetMap();
		
		$("#results, #addresses").empty();
		
		var i = 0;
		var latlng = new Array();
		var str = "";
		
		str = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";
		str += "<tr>";
		str += "<th>Company</th>";
		str += "<th>Address</th>";
		str += "<th>Contact</th>";
		str += "<th>Website</th>";
		str += "<th width=\"200\">Products</th>";
		str += "</tr>";
		
		$("row", xml).each( function(i) { 

			var prod1 = $(this).attr("Anthos");
			var prod2 = $(this).attr("ASYS");
			var prod3 = $(this).attr("Scie_plas");
			var prod4 = $(this).attr("AAA");
			var prod5 = $(this).attr("WPA");
			var prod6 = $(this).attr("Libra");
			var prod7 = $(this).attr("Novaspec");
			var prod8 = $(this).attr("Ultrospec");
			var prod9 = $(this).attr("Genequant");
			
			if (prod1 == "1") { prod1 = "<a href=\"/brand/12/biochrom-anthos.html\">Biochrom Anthos Microplate Instrumentation</a><br />"; } else { prod1 = ""; }
			if (prod2 == "1") { prod2 = "<a href=\"/brand/13/biochrom-asys.html\">Biochrom Asys Microplate Instrumentation</a><br />"; } else { prod2 = ""; }
			if (prod3 == "1") { prod3 = "Scie_plas<br />"; } else { prod3 = ""; }
			if (prod4 == "1") { prod4 = "<a href=\"/brand/3/biochrom-aaa.html\">Biochrom Amino Acid Analyzers</a><br />"; } else { prod4 = ""; }
			if (prod5 == "1") { prod5 = "<a href=\"/biochrom-wpa/\">Biochrom WPA Spectrophotometers and Colorimeters</a><br />"; } else { prod5 = ""; }
			if (prod6 == "1") { prod6 = "<a href=\"/brand/4/biochrom-libra.html\">Biochrom Libra Spectrophotometers</a><br />"; } else { prod6 = ""; }
			if (prod7 == "1") { prod7 = "Novaspec<br />"; } else { prod7 = ""; }
			if (prod8 == "1") { prod8 = "Ultrospec<br />"; } else { prod8 = ""; }
			if (prod9 == "1") { prod9 = "Genequant<br />"; } else { prod9 = ""; }
			
			var web = $(this).attr("Website");
			if (web != "n/a") { web = '<a href="http://' + web + '" target="_blank">' + web + '</a>'; }
			
			//$("#results").append('<li><a href="#maptop" class="result" rel="'+(i)+'">('+$(this).attr("ID")+') '+$(this).attr("Company")+', '+$(this).attr("Country")+'</a></li>');
			str += "<tr><td>(" + $(this).attr("ID") + ") " + $(this).attr("Company") + "</td>";
			str += "<td>" + $(this).attr("geo_address_") + "</td>";
			str += "<td>" + $(this).attr("Tel") + "<br />" + $(this).attr("Email") + "</td>";
			str += "<td>" + web + "</td>";
			str += "<td>" + prod1 + prod2 + prod3 + prod4 + prod5 + prod6 + prod7 + prod8 + prod9 + "</td></tr>";
			
			$("#addresses").append('<div id="addr-'+i+'"><p>('+$(this).attr("ID")+') <strong>'+$(this).attr("Company")+'</strong><br/>'+$(this).attr("geo_address_")+'<br/><strong>Tel:</strong> '+$(this).attr("Tel")+'<br/><strong>Web:</strong> '+web+'</p></div>');
			locAddress($(this).attr("lat"), $(this).attr("lng"), $("#addr-"+i).html()); 			
			
			latlng[i] = new GLatLng($(this).attr("lat"), $(this).attr("lng"));

		});
		
		str += "</table>";
		
		$("#results").html(str);
		
		$(".result").click( function() {  
			var x = $(this).attr("rel");			
			var addr = $("#addr-"+x).html();
			map.setCenter(arrMarkers[x].getLatLng(), 4);
			arrMarkers[x].openInfoWindowHtml(addr);
			return true;
		});
		
		
		var latlngbounds = new GLatLngBounds();
		for(var i = 0; i < latlng.length; i++)
		{
			latlngbounds.extend(latlng[i]);
		}
		var mapzoom = 5;
		
		if(map.getBoundsZoomLevel(latlngbounds) > 5) { mapzoom = 5; } else { mapzoom = map.getBoundsZoomLevel(latlngbounds); }
		map.setCenter(latlngbounds.getCenter(), mapzoom);
}
