//<![CDATA[

var map = false;
var baseIcon = false;
var strikeIcon = false;
var geocoder = false;
var aSpritePositions = false;

function load()
{
	aSpritePositions = {	1: { 0: [60, 160],  1: [0, 160]},
							2: { 0: [70, 160],  1: [10, 160]},
							3: { 0: [80, 160],  1: [20, 160]},
							4: { 0: [90, 160],  1: [30, 160]},
							5: { 0: [100, 160], 1: [40, 160]},
							6: { 0: [110, 160], 1: [50, 160]}};

	if(GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));

		map.addControl(new GLargeMapControl3D());
		map.removeMapType(G_HYBRID_MAP);
		map.removeMapType(G_NORMAL_MAP);

		initMapForCountry();
		addLogo();

		geocoder = new GClientGeocoder();

		baseIcon = new GIcon();
		baseIcon.shadow = null;
		baseIcon.sprite = {};
		baseIcon.sprite.image = "/images/weericons/mastericon_small.png";
		baseIcon.transparent = "/images/weericons/clickcatcher.png";
		baseIcon.iconSize = new GSize(40,40);
		baseIcon.iconAnchor = new GPoint(20,20);
		baseIcon.infoWindowAnchor = new GPoint(25,15);

		strikeIcon = new GIcon();
		strikeIcon.shadow = null;
		strikeIcon.sprite = {};
		strikeIcon.sprite.image = "/images/weericons/mastericon_small.png";
		strikeIcon.transparent = "/images/weericons/clickcatcher_small.png";
		strikeIcon.iconSize = new GSize(10,10);
		strikeIcon.iconAnchor = new GPoint(5,5);
		strikeIcon.infoWindowAnchor = new GPoint(5,5);

		loadGmapIcons(userIcons);
		loadStrikes();
	}
}


function addLogo()
{
	logo = new GScreenOverlay("http://www.onweer-online.nl/images/oologogmaps.png", new GScreenPoint(575, 372, 'pixels', 'pixels'), new GScreenPoint(0, 0), new GScreenSize(143, 49));
	map.addOverlay(logo);
}

function initMapForCountry()
{
	if(sCountry == 'nl')
	{
		map.setCenter(new GLatLng(52.10, 5.25), 7);
		sPolygonFile = 'nederland.js';
	}
	if(sCountry == 'be')
	{
		map.setCenter(new GLatLng(50.6, 4.2), 7);
		sPolygonFile = 'belgie.js';
	}

	$.get("/json/" + sPolygonFile, function(data)
	{
		eval(data);
	});
}

function loadGmapIcons(aIcons)
{
	for(var iPos in aIcons["data"])
	{
		switch(aIcons["data"][iPos]["icontype"])
		{
			case "user":
				oDate = new Date(aIcons["data"][iPos]["time"]);
				sTijd = getTimeString(oDate);
				sThisCountry = aIcons["data"][iPos]["country"];

				sHTML = '<strong>'+aIcons["data"][iPos]["city"]+', '+sTijd+' uur</strong><br />'+aIcons["data"][iPos]["weather"]+'<br /><br />Poster: '+aIcons["data"][iPos]["poster"];

				break;
			case "knmi":
			case "kmi":
				sThisCountry = "nl";

				sHTML = '<strong>'+aIcons["data"][iPos]["city"]+', KNMI</strong><br />'+aIcons["data"][iPos]["weather"];

				if(aIcons["data"][iPos]["temp"]) sHTML += '<br />Temperatuur: '+aIcons["data"][iPos]["temp"]+' &deg;C';
				if(aIcons["data"][iPos]["lv"]) sHTML += '<br />Luchtvochtigheid: '+aIcons["data"][iPos]["lv"]+' %';

				sWind = aIcons["data"][iPos]["wind"];
				oWindRegEx = /[\d\,]/;
				fWind = oWindRegEx.exec(sWind);

				sHTML += '<br />Wind: '+sWind+' m/s ('+getWindBft(fWind)+' bft)';

				if(aIcons["data"][iPos]["sight"]) sHTML += '<br />Zicht: '+aIcons["data"][iPos]["sight"]+' meter';
				if(aIcons["data"][iPos]["hpa"]) sHTML += '<br />Luchtdruk: '+aIcons["data"][iPos]["hpa"]+' hPa';
				break;
		}

		if(aIcons["data"][iPos]["icontype"] == "kmi") sThisCountry = "be";

		aIconPos = aIcons["data"][iPos]["spos"].split("x");
		appendGmapIcon(aIcons["data"][iPos]["city"], sThisCountry, aIconPos[0], aIconPos[1], aIcons["data"][iPos]["lat"], aIcons["data"][iPos]["long"], sHTML);
	}
}

function loadStrikes()
{
	for(var iStrike in lightningData["data"])
	{
		oDate = new Date(lightningData["data"][iStrike]["time"]);

		var iIcon = lightningData["data"][iStrike]["icon"];
		var iType = lightningData["data"][iStrike]["type"];

		var iX = lightningData["data"][iStrike]["x"];
		var iY = lightningData["data"][iStrike]["y"];

		if(sCountry == 'be')
		{
			iX += 96;
			iY -= 219;
		}

		var pPos = map.fromDivPixelToLatLng(new GPoint(iX, iY));

		var sHtml = (iType?"IC":"CG")+(lightningData["data"][iStrike]["pol"]?"-":"+")+" ontlading, "+getTimeString(oDate)+" uur<br />lat: "+(Math.round(pPos.lat() * 100) / 100)+"<br />long: "+(Math.round(pPos.lng() * 100) / 100)+"<br />gemeten door: "+boltekNames[lightningData["data"][iStrike]["boltekID"]];

		showStrike(pPos, iType, iIcon, sHtml);
	}
}

function getTimeString(oDate)
{
	sTime = prefixZero(oDate.getHours())+":"+prefixZero(oDate.getMinutes())+":"+prefixZero(oDate.getSeconds());
	return sTime;
}

function prefixZero(iInput)
{
	if(iInput < 10)
		return "0"+iInput;
	else
		return iInput;
}


function createMarker(latlng, icoonX, icoonY, oIcon)
{
	var userIcon = new GIcon(oIcon);
	userIcon.sprite.top = icoonY;
	userIcon.sprite.left = icoonX;
	return new GMarker(latlng, {icon:userIcon});
}

function appendGmapIcon(address, country, icoonX, icoonY, fLat, fLong, sHTML)
{
	if(!fLat || !fLong)
	{
		if(country == 'nl') countryname = 'Netherlands';
		if(country == 'be') countryname = 'Belgium';

		geocoder.getLatLng(address+', '+countryname, function(latlng)
		{
			if(latlng)
			{
				// save results to our cache, so we do not "overload" the google geocoder
				$.get("/controller/geocache/?xpos="+latlng["x"]+"&ypos="+latlng["y"]+"&plaats="+urlencode(address)+"&land="+country);

				var marker = createMarker(latlng, icoonX, icoonY, baseIcon);
				GEvent.addListener(marker, "click", function()
				{
					marker.openInfoWindowHtml(sHTML);
				});
				map.addOverlay(marker);
			}
		});
	}
	else
	{
		thisplace = new GLatLng(fLat, fLong);
		var marker = createMarker(thisplace, icoonX, icoonY, baseIcon);
		GEvent.addListener(marker, "click", function()
		{
			marker.openInfoWindowHtml(sHTML);
		});
		map.addOverlay(marker);
	}
}

function showStrike(pPos, iType, iIcon, sHtml)
{
	var marker = createMarker(pPos, aSpritePositions[iIcon][iType][0], aSpritePositions[iIcon][iType][1], strikeIcon);
	GEvent.addListener(marker, "click", function()
	{
		marker.openInfoWindowHtml(sHtml);
	});

	map.addOverlay(marker);
}

function loadIconsByType(sType)
{
	switch(sType)
	{
		case "user_icons":
			loadGmapIcons(userIcons);
			break;
		case "knmi_icons":
			loadGmapIcons(knmiIcons);
			break;
		case "kmi_icons":
			loadGmapIcons(kmiIcons);
			break;
		case "strikes":
			loadStrikes();
			break;

	}
}

function addGmapCheckboxListeners()
{
	$("#map_legend input[type=checkbox]").click(function()
	{
		bChecked = $(this).is(":checked");

		if(bChecked == true)
		{
			loadIconsByType($(this).attr('name'));
		}
		else
		{
			map.clearOverlays();
			addLogo();
			initMapForCountry();

			$("#map_legend input:checked").each(function()
			{
				loadIconsByType($(this).attr('name'));
			});
		}

	});
}

//]]>

