var GoogleMap;
var vacancyIcon = new GIcon();
var vacancySelectedIcon = new GIcon();

function loadMap()
{
	if (!GBrowserIsCompatible()) {
		return true;
	}
	
	if(!toload) { return true; }

	// Google map initialiseren
	GoogleMap = new GMap2(document.getElementById("map"));

	// Erase junk
	GoogleMap.clearOverlays();
	
	// Location
	GoogleMap.setCenter(new GLatLng(Settings.y, Settings.x), Settings.z);
	
	// Set controls
	GoogleMap.addControl(new GSmallMapControl());

	// Map type
	GoogleMap.setMapType(G_PHYSICAL_MAP);
	
	vacancyIcon.image = Settings.RootPath + "images/icons/marker.png";
	vacancyIcon.iconSize = new GSize(19, 30);
	vacancyIcon.shadowSize = new GSize(0, 0);
	vacancyIcon.iconAnchor = new GPoint(18, 26);
	vacancyIcon.infoWindowAnchor = new GPoint(9, 2);
	vacancyIcon.infoShadowAnchor = new GPoint(18, 25);
	
	vacancySelectedIcon.image = Settings.RootPath + "images/icons/marker_selected.png";
	vacancySelectedIcon.iconSize = new GSize(19, 31);
	vacancySelectedIcon.shadowSize = new GSize(0, 0);
	vacancySelectedIcon.iconAnchor = new GPoint(18, 26);
	vacancySelectedIcon.infoWindowAnchor = new GPoint(9, 2);
	vacancySelectedIcon.infoShadowAnchor = new GPoint(18, 25);
	
	var helpDiv = document.getElementById("helpDiv");
	if(document.getElementById("helpDiv")) {
		pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(30, 20));
		pos.apply(helpDiv);
		GoogleMap.getContainer().appendChild(helpDiv);
	}
}

function loadVacancies()
{
	if(!toload) { return true; }
	
	var XMLHTTP = false
	if ( window.ActiveXObject ) XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP")
	if ( window.XMLHttpRequest ) XMLHTTP = new XMLHttpRequest()

	//var bounds = GoogleMap.getBounds()

	// retrieve the bounds of the detail area
	//var southWest = bounds.getSouthWest();
	//var northEast = bounds.getNorthEast();
	
	// determine the pixel position of the corners
	//var swPixels = GoogleMap.fromLatLngToDivPixel(bounds.getSouthWest());
	//var nePixels = GoogleMap.fromLatLngToDivPixel(bounds.getNorthEast());
	
	XMLHTTP.onreadystatechange = function()
	{
		if (XMLHTTP.readyState == 4)
		{
			var sText = XMLHTTP.responseText;
			
			var aVacancies = sText.split('\n');
			XMLHTTP = null;

			for(i=0; i < aVacancies.length; i++) {
				if(i == 0)
				{
					var point = aVacancies[i].split('|');
				}
				else
				{
					// the rest are vacancies
					var mapdata = aVacancies[i].split('|');
					createMarker(mapdata);
				}
			}

			// first line contains the map center
			//GoogleMap.setCenter(new GLatLng(point[0], point[1]), 11);
			GoogleMap.setCenter(new GLatLng(point[0], point[1]), 9);
			
			//GoogleMap.panTo(new GLatLng(point[0], point[1]));
			//GoogleMap.setZoom(11);
		}
	}
	
	//var getVars = '?ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + '&nePixels=' + nePixels.x + ',' + nePixels.y + '&swPixels=' + swPixels.x + ',' + swPixels.y + '&z=' + GoogleMap.getZoom() + new Date;
	// check if we are on vacancy detail or not

	if(loadType == "Vacancy") {
		URL = Settings.RootPath + "index.php?controller=Ajax&action=vacancies&vacancyID="+vacancyID;
	} else {
		URL = Settings.RootPath + "index.php?controller=Ajax&action=vacancies";
	}
	
	XMLHTTP.open("GET", URL, true)
	XMLHTTP.send(null)		
}

function createMarker(mapdata)
{
	if(mapdata[1] != '' && mapdata[0] != '')
	{
		if(mapdata[5] == "marker")
		{
			var marker = new GMarker(new GLatLng(mapdata[1], mapdata[0]), vacancyIcon);
		} else {
			var marker = new GMarker(new GLatLng(mapdata[1], mapdata[0]), vacancySelectedIcon);
		}
		marker.mapdata = mapdata;
		
		GEvent.addListener(marker, 'mouseover', function() {
			marker.openMapToolTip(this.mapdata[2]);
		});
		GEvent.addListener(marker, 'mouseout', function() {
			marker.closeMapToolTip();
		});

		GEvent.addListener(marker, 'click', function() {
			if(marker.LittleInfoWindowInstance) {
				marker.closeLittleInfoWindow();
			} else {
				marker.openLittleInfoWindow('<strong>'+this.mapdata[2]+'</strong><br>'+this.mapdata[3]+'<br>'+this.mapdata[4]);
			}
		});

		GoogleMap.addOverlay(marker);
	}
}