var map;
var geocoder = null;
var Markers = new Array();
var Icon;
var countries = new Array();
countries["Slovenija"] = "Slovenia"; 
countries["Danmark/Denmark"] = "Denmark"; 
countries["Norge/Norway"] = "Norway"; 
countries["Deutschland/Germany"] = "Germany"; 
countries["Ireland"] = "Ireland"; 
countries["UK"] = "UK"; 
countries["Sverige/Sweden"] = "Sweden"; 
countries["Nederlands"] = "Nederlands"; 
countries["Belgium/La Belgique"] = "Belgium";
countries["United_Arab_Emirates_(UAE)"] = "United Arab Emirates (UAE)";
countries["Oman"] = "Oman";
countries["Bahrain"] = "Bahrain";
countries["Saudi_Arabia"] = "Saudi Arabia";
countries["Kuwait"] = "Kuwait";
countries["Jordan"] = "Jordan";

function placeMarkerAtPoint(cords, info)
{
	cords = cords.split(",")
	var point = new GLatLng(cords[1],cords[0]);
	
	var marker = new GMarker(point, Icon);
	
	map.addOverlay(marker);
	
	var Markerslength = Markers.length;
	Markers[Markerslength] = marker;
	
   GEvent.addListener(marker, "click", function() {												
	   marker.openInfoWindowHtml(info);
   });
	
}


function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		Icon = new GIcon();
		Icon.image = "images/map_marker.png";
		Icon.iconSize=new GSize(22,24);
		Icon.iconAnchor=new GPoint(11,24);  
	    Icon.infoWindowAnchor=new GPoint(11,0);  
		
		geocoder = new GClientGeocoder();
		
		
		geocoder.getLocations("Europe", function (locations) { 

		var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
		var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
		var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
		var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;
	
		var bounds = new GLatLngBounds(new GLatLng(south, west), 
									   new GLatLng(north, east));
	
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	});

	
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

function PostVars(country) {

for(var i=0; i<Markers.length; i++) {
	map.removeOverlay(Markers[i]);
}

var sector = $("#sector").val();

setCenter(countries[country]);
$.getJSON('json.php', {country: countries[country], sector: sector, get_dealers: 1 }, function(res){
	for( var i=0; i<res.dealers.length; i++ ) {
		placeMarkerAtPoint(res.dealers[i].cords, res.dealers[i].html);
	}
	
});	
}

function setCenter(country) {
	
geocoder.getLocations(country, function (locations) { 

	var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
	var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
	var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
	var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;
	
	var bounds = new GLatLngBounds(new GLatLng(south, west), 
									   new GLatLng(north, east));
	
	map.setCenter(bounds.getCenter(), 5);
});
	
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
