//initialise some variables and objects
var theMap;
var customMap;
var startpoint;
var minZoomLvl, maxZoomLvl, defZoomLvl;
var items = new Object();
var itemGroups = new Object();
var areas = new Object();
var xmlHttp = GXmlHttp.create();
var extWin;
var mediaWin;
var presetLevel;
var baseUrl;
var markers = new Array();

window.onunload = GUnload;

//INITIALISATION FUNCTION FIRED AFTER LOADING OF DOCUMENT
function init() {
	getConfig();
	theMap = prepareMap(document.getElementById('map'), 'map', [new GMapTypeControl(), new compassControl()], startPoint, defZoomLvl);

	//determine preset zoom level from actual level. Used to set class on the right number image
	switch (parseInt(defZoomLvl)) {
		case 17:
			zoomLevel = 1;
			break;
		case 15:
			zoomLevel = 2;
			break;
		case 13:
			zoomLevel = 3;
			break;
		case 12:
			zoomLevel = 4;
			break;
	}

	document.getElementById('zoom' + zoomLevel).className = 'active'; //set indicator for current zoomlevel image

	getAreas();       //fill the areas object with the info about the areas. Need that in misc. functions
	getItemGroups();  //fill the itemGroups object with the info about the itemgroups. Need that in misc. functions
	getItems(theMap); //place markers on the map for all items

	//eventhandler for postcode search button
	document.getElementById('searchpc').onclick = function () { searchPostcode(theMap, document.getElementById('postcode').value)}

	//bind eventhandlers to mapcontrol elements (mainly zooming)
	document.getElementById('zoomin').onclick  = function () { zoom(theMap, 'in');}
	document.getElementById('zoomout').onclick = function () { zoom(theMap, 'out');}
	document.getElementById('zoom1').onclick   = function () { zoom(theMap, 1);}
	document.getElementById('zoom2').onclick   = function () { zoom(theMap, 2);}
	document.getElementById('zoom3').onclick   = function () { zoom(theMap, 3);}
	document.getElementById('zoom4').onclick   = function () { zoom(theMap, 4);}
	//document.getElementById('popup').onclick = extMap;

	//get all sidebarlistbuttons (<span>) and attach event to show hide the appropriate ul
	listbtns = document.getElementById('sidebars').getElementsByTagName('li');
	for (i=0; i<listbtns.length; i++) {
		if (listbtns[i].getElementsByTagName('span')[0]) {
			listbtns[i].getElementsByTagName('span')[0].onclick = function () {
				sidebarBlocks = document.getElementById('sidebars').getElementsByTagName('li');

				for (j=0; j<sidebarBlocks.length; j++) {
					if (sidebarBlocks[j].getElementsByTagName('span')[0] && sidebarBlocks[j].getElementsByTagName('div')[0]) {
						sidebarBlocks[j].getElementsByTagName('div')[0].style.display = 'none';
						sidebarBlocks[j].getElementsByTagName('span')[0].className = 'foldedin';
					}
				}

				this.parentNode.getElementsByTagName('div')[0].style.display = 'block';
				this.className = 'foldedout';

				//replace the footer to counteract IE's stupid render engine which doesn't update it's position correctly
				newFooter = document.createElement('p');
				newFooter.id = 'footer';
				swisLink = document.createElement('a');
				swisLink.href = 'http://swis.nl/';
				swisLink.title = '...en een snufje Plerion';
				swisLink.appendChild(document.createTextNode('SWIS'));
				googleLink = document.createElement('a');
				googleLink.href = 'http://maps.google.com/';
				googleLink.appendChild(document.createTextNode('Google Maps'));
				newFooter.appendChild(document.createTextNode('Powered by '));
				newFooter.appendChild(swisLink);
				newFooter.appendChild(document.createTextNode(' en '));
				newFooter.appendChild(googleLink);

				document.getElementById('content').removeChild(document.getElementById('footer'));
				document.getElementById('content').appendChild(newFooter);
			}
		}
	}

	//attach events to districtnames (will make map pan to desired location) use the id of the <li> to extract the coordinates from teh areas object
	x = document.getElementById('districts');
	eleDistricts = x.getElementsByTagName('li');

	for (i=0; i<eleDistricts.length; i++) {
		eleDistricts[i].onclick = function () {zoom(theMap, 2); theMap.panTo(areas[this.id]['point'])}
	}


	//close districts and candidates
	eleDists = document.getElementById('districts');
	eleCands = document.getElementById('candidates');

	eleDists.style.display = 'none';
	eleCands.style.display = 'none';

	//change the classNames on the <span> elements so the arrow point in the right direction for the closed sidebars
	if (eleDists.previousSibling.nodeName == '#text') {
		eleDists.previousSibling.previousSibling.className = 'foldedin';
		eleCands.previousSibling.previousSibling.className = 'foldedin';
	} else {
		eleDists.previousSibling.className = 'foldedin';
		eleCands.previousSibling.className = 'foldedin';
	}

	//bind eventhandlers to show and hide itemgroups to checkboxes
	checkboxes = document.getElementById('items').getElementsByTagName('input');
	for (i=0; i<checkboxes.length; i++) {
		checkboxes[i].onclick = function () {
			if (this.checked == true) {
				showHideItemGroup(theMap, this.id, 'show');
			} else {
				showHideItemGroup(theMap, this.id, 'hide');
			}
		}
	}

	//check if the element for the minimap exists. If so, make it a map
	if (document.getElementById('minimap')) {
		miniMap = prepareMap(document.getElementById('minimap'), null, null, startPoint, 16);
		miniMap.disableDragging();

		miniMapIcon = new GIcon();
		miniMapIcon.image = '/img/minimap_marker_icon.png';
		miniMapIcon.iconSize = new GSize(33, 33);
		miniMapIcon.iconAnchor = new GPoint(16, 16);

		marker = new GMarker(startPoint, miniMapIcon);
		miniMap.addOverlay(marker);
	}

	//create close function for introbox
	if (document.getElementById('closeintro')) {
		document.getElementById('closeintro').onclick = function() {
			if (document.getElementById('nevershow').checked == true) {
				date = new Date();
				date.setTime(date.getTime() + 31536000000); // cookie expires in one year
				document.cookie = "lodk=nointro;expires=" + date.toGMTString() + '; path=/';
			} else {
				document.cookie = "lodk=nointro";
			}

			document.getElementById('map').removeChild(document.getElementById('intro'));
		}
	}

	//create eventhandler to hide/show all itemgroups at once. Change button label aswell
	document.getElementById('switchitemgroups').onclick = function () {
		if (this.value.indexOf('uit') != -1) {
			switchAllItemGroups(theMap, 'hide');
			this.value = 'Zet alle thema\'s aan';
		} else {
			switchAllItemGroups(theMap, 'show');
			this.value = 'Zet alle thema\'s uit';
		}
	}

	document.getElementById('postcode').onkeyup = enterPress;

	//remove preloaded marker images
	if (document.getElementById('markerpreload')) {
		document.getElementById('markerpreload').parentNode.removeChild(document.getElementById('markerpreload'));
	}
}

//do postcode search on Enter key keydown event
function enterPress(e) {
	if (!e) e = window.event;

	code = e.keyCode ? e.keyCode : e.which;

	if (code) {
		if (code == 13) searchPostcode(theMap, this.value);
	}
}