var map = null;
var geocoder;
var address;
var evnt;
var center_lat = 52.15615;
var center_long = 5.29295;
var zoom = 7;
var map_width = 650;
var map_height = 450;
var tooltip;

function Unload(){	
	GUnload();
	document.getElementById("gm_container").style.display = "none";
}

function showAddress(response){
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    /*point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(
        '<b>orig latlng:</b>' + response.name + '<br/>' + 
        '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
        '<b>Status Code:</b>' + response.Status.code + '<br>' +
        '<b>Status Request:</b>' + response.Status.request + '<br>' +
        '<b>Address:</b>' + place.address + '<br>' +
        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);*/
	//document.getElementById('pcode').value = place.address;
	get_postcode(place.Point.coordinates[1],place.Point.coordinates[0]);
  }
}

function LoadMap(){

	if (GBrowserIsCompatible())
		LoadStates();
	else
		alert("Browser isn't Compatible");
}

function get_postcode(lat,lng){
	var xmlHttp = HTTPObject();

	xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4)
      {		  
		  if(xmlHttp.responseText != "")
		    {				
				//alert(xmlHttp.responseText);
				document.getElementById('pcode').value = xmlHttp.responseText;										
			}				
      }
    } 
  	var req = "/ajax/remote/get_postcode/"+lat+"/"+lng;  
  	xmlHttp.open("GET",req,true);
  	xmlHttp.send(null); 	
}

function getAddress(latlng) {
  if (latlng != null) {
    address = latlng;
	alert(address);
    //geocoder.getLocations(latlng, showAddress);
	//Unload();
  }
}

function LoadStates(){
	$('#body_content').hide();
	$('#result').hide();
	document.getElementById('instruction').innerHTML = "Klik op de kaart om leveranciers te vinden";
	map = new GMap2(document.getElementById("map"),{size:new GSize(map_width,map_height)});
	document.getElementById("gm_container").style.display = "block";
	map.setCenter(new GLatLng(center_lat, center_long), zoom);
	//map.setMapType(G_PHYSICAL_MAP);
	
	LoadSupplier();

	var customUI = map.getDefaultUI();
	//customUI.controls.largemapcontrol3d = true;
	map.setUI(customUI);
	 	 
	//evnt = GEvent.addListener(map, "click", ZoomIn);
	geocoder = new GClientGeocoder();			
}

function ZoomIn(overlay,latlong){
	//map.zoomIn(latlong);
	map.setCenter(latlong, 9);
	
	//var customUI = map.getDefaultUI();
	//customUI.controls.largemapcontrol3d = true;
	//map.setUI(customUI);
}

function LoadSupplier(overlay, latlong){	
	/* Ajax Request to get supplier Info */
	var xmlHttp = HTTPObject();
	
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.responseText != "") {				
				var gmarkersA = [];			
				var gmarkersAlabels = [];
				//var gmarkersB = [];
				
				var xmlstring = xmlHttp.responseText;
				if (typeof DOMParser == "undefined") {
					var xmlobject = GXml.parse(xmlstring);
					var list = xmlobject.documentElement.getElementsByTagName("supplier");
				}
				else {
					var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");
					var temp = xmlobject.firstChild;
					var list = temp.childNodes;
				}
				
				//var temp = xmlobject.firstChild;
				//var list = temp.childNodes;
				for(var i = 0; i < list.length; i++) {
					// obtain the attribues of each marker
					var id = list[i].childNodes[0].firstChild.nodeValue;
					var lat = parseFloat(list[i].childNodes[1].firstChild.nodeValue);
					var lng = parseFloat(list[i].childNodes[2].firstChild.nodeValue);
					var point = new GLatLng(lat,lng);
					var naam = list[i].childNodes[3].firstChild.nodeValue;
					var straat = list[i].childNodes[4].firstChild.nodeValue;
					var nr = list[i].childNodes[5].firstChild.nodeValue;
					var postcode = list[i].childNodes[6].firstChild.nodeValue;
					var stad = list[i].childNodes[7].firstChild.nodeValue;
					
					// split the markers into  arrays, with different GIcons
					var marker = createCustomMarker(point, id, naam, naam + "<br><br>"+straat+" "+nr+"<br>"+postcode+" "+stad);
					gmarkersA.push(marker);
				}
				
				var mm = new GMarkerManager(map, {borderPadding: 1});
				mm.addMarkers(gmarkersA, 7, 17);
				mm.refresh();
				//om.Clear(); // Clear the loading message
			}
		}
	}
	var req = "/ajax/remote/get_latlong_list";
	xmlHttp.open("GET", req, true);
	xmlHttp.send(null);
	/* Ajax Request to get supplier Info */
	
	return;
}

function createCustomMarker(point,id,name,html) {
	//var marker = new GMarker(point, {icon:icon});

	var marker = new PdMarker(point);
	marker.setTooltip(html);
	//marker.setDetailWinHTML(html);
	marker.setHoverImage("/system/application/views/images/pin-over.png");
	marker.setOpacity(85);
	GEvent.addListener(marker, "click", function() {
		window.location="/supplier/"+id+"/"+name.replace(/ /g,'-');
	})
	return marker;
}
