function setupWR() {
	//document.getElementById('today').innerHTML = new Date().format('dddd dd mmm yyyy');
	loadingBusy('n/a');
	preload();
	showOverview();
}

function preload() {
	var i = new Image();
	i.src = "/extra/meteo/Today.gif";
	i = new Image();
	i.src = "/extra/meteo/Tomorrow.gif";
	i = new Image();
	i.src = "/extra/meteo/Tonight.gif";
}

	var listIndex = 0;
	var listLength = 0;
	
	function keyevent(event)
	{
		var ev = getEvent(event);
		var el = getEventElement(event);
		var v = el.value;
		var kc = ev["keyCode"]

		if (kc == 40) { //arrow down
			markInList('next', el == document.getElementById('citySearchField') ? 0 : (el == document.getElementById('worldCitySearchField')? 1 : 2));
			
		} else if (kc == 38) { //arrow up
			markInList('prev', el == document.getElementById('citySearchField') ? 0 : (el == document.getElementById('worldCitySearchField')? 1 : 2));
			
		} else if (kc == 13) { //enter
			markInList('show', el == document.getElementById('citySearchField') ? 0 : (el == document.getElementById('worldCitySearchField')? 1 : 2));
			
		} else if (v != undefined && v.length>0) {
			if (el == document.getElementById('citySearchField')) {
				filterCityList(v);
			} else if (el == document.getElementById('worldCitySearchField')) {
				filterWorldCityList(v);
			} else if (el == document.getElementById('skiSearchField')) {
				filterSkiList(v);
			}
			
		} else if (v != undefined && v.length==0) {
			document.getElementById('list').style.display = 'none';
			
		}
		return false;
	}
	
	var lastEl = -1;
	
	function markInList(t, wc) {
		var el1 = document.getElementById('list' + listIndex);
		var el2 = document.getElementById('listH' + listIndex);
		if (el1) el1.focus();
		
		if (t == 'show') {
			if (el1 != undefined && wc == 0) {
				var n = el2.innerHTML.split('#');
				showCity(n[0], n[1]);
			} else if (el1 != undefined && wc == 1) {
				var n = el2.innerHTML.split('#');
				showWorldCity(n[0], n[1], n[2], n[3]);
			} else if (el1 != undefined && wc == 2) {
				var n = el2.innerHTML.split('#');
				showSki(n[0], n[1], n[2]);
			} 
			
		} else if (t == 'next') {
			if (lastEl != wc) {
				lastEl = wc;
				listIndex = 0;
			}
			if (el1 != undefined) el1.className = 'off';
			if (listIndex < listLength-1) listIndex++;
			else listIndex = listLength-1;
			if (document.getElementById('list' + listIndex)!=undefined) document.getElementById('list' + listIndex).className = 'on';
			
		} else if (t == 'prev') {
			if (lastEl != wc) {
				lastEl = wc;
				listIndex = 0;
			}
			if (el1 != undefined) el1.className = 'off';
			if (listIndex > 1) listIndex--;
			else listIndex = 1;
			document.getElementById('list' + listIndex).className = 'on';
			
		}
	}
	
	function getEventElement(e) {
		return (e.srcElement ? e.srcElement: (e.target ? e.target : e.currentTarget));
	}
	
	function getEvent(event) {
		return (event ? event : window.event);
	}
	
	function removeCityList() {
		document.getElementById('list').style.display = 'none';
		document.getElementById('citySearchField').value = '';
		document.getElementById('worldCitySearchField').value = '';
		document.getElementById('skiSearchField').value = '';
	}
	
	function showLocation(l) {
		document.getElementById('locationLabel').innerHTML = l;
	}
	
	// by lode
	function removeChart() {
		
	}
	
    function searchCity(s) {
	    loadingBusy('n/a');
    	var found = false;
    	for (var x=0; x<cities.length; x++) {
    		if (cities[x].label.toLowerCase() == s.toLowerCase() ) {
    			showCity(cities[x].id, cities[x].label);
    			found = true;
    			break;
    		}
    	}
    	if (!found)  {
    		loadingBusy('De gemeente die je zocht werd niet gevonden.');
    		showLocation('De opgegeven gemeente werd niet gevonden.');
    		//setTimeout('loadingDone()', 3000);
   		}
	}

function showOverview() {
	loadingBusy('n/a');
	var today = new Date();
	adhese_addAjaxAd(urlO,xslUrlO,0, 0);
	removeCityList();
	showLocation(today.format('dddd, d mmmm yyyy'));
}

function showCity(id, label) {
	loadingBusy('n/a');
	currId = id;
	adhese_addAjaxAd(urlC,xslUrlC,0, id);
	removeCityList();
	showLocation(label);
}

function showWorldCity(id, label, country, continent) {
	loadingBusy('n/a');
	currId = id;
	adhese_addAjaxAd(urlW,xslUrlW,0, id);
	removeCityList();
	showLocation(continent + ", " + country + ", " + label);
}

function showSki(id, label, country) {
	loadingBusy('n/a');
	currId = id;
	adhese_addAjaxAd(urlS,xslUrlS,0, id);
	removeCityList();
	showLocation(label + ", " + country);
}

	function searchWorldCity(s) {
		loadingBusy('n/a')
    	var sa = s.split(',');
    	var found = false;
    	for (var x=0; x<worldCities.length; x++) {
    		if (sa.length == 1) {
	    		if (worldCities[x].label.toLowerCase() == sa[0].toLowerCase() ) {
	    			showWorldCity(worldCities[x].id, worldCities[x].label, worldCities[x].country, worldCities[x].cont);
	    			found = true;
	    			break;
	    		}
    		} else if (sa.length > 1) {
	    		if (worldCities[x].label.toLowerCase() == sa[0].toLowerCase() && worldCities[x].country.toLowerCase().replace(' ','') == sa[1].toLowerCase().replace(' ','') ) {
	    			showWorldCity(worldCities[x].id, worldCities[x].label, worldCities[x].country, worldCities[x].cont);
	    			found = true;
	    			break;
	    		}
    		}
    	}
    	if (!found)  {
    		loadingBusy('De stad die je zocht werd niet gevonden.');
    		showLocation('De opgegeven stad werd niet gevonden.');
    		//setTimeout('loadingDone()', 3000);
   		}
	}
	
	function searchSki(s) {
		loadingBusy('n/a');
    	var sa = s.split(',');
    	var found = false;
    	for (var x=0; x<skiStations.length; x++) {
    		if (sa.length == 1) {
	    		if (skiStations[x].label.toLowerCase() == sa[0].toLowerCase() ) {
	    			showSki(skiStations[x].id, skiStations[x].label, skiStations[x].country);
	    			found = true;
	    			break;
	    		}
    		} else if (sa.length > 1) {
	    		if (skiStations[x].label.toLowerCase() == sa[0].toLowerCase() && skiStations[x].country.toLowerCase().replace(' ','') == sa[1].toLowerCase().replace(' ','') ) {
	    			showSki(skiStations[x].id, skiStations[x].label, skiStations[x].country);
	    			found = true;
	    			break;
	    		}
    		}
    	}
    	if (!found)  {
    		loadingBusy('Het skigebied dat je opgegeven hebt werd niet gevonden.');
    		showLocation('Het opgegeven skigebied werd niet gevonden.');
   		}
	}
	
	function filterCityList(f) {
		
		var s = '';
		listIndex = 0;
		var fl = new Array();
		
		if (!parseInt(f)) { //filter on name		
			for (var i=0; i<cities.length; i++) {
				if (cities[i].label.substring(0,f.length).toLowerCase() == f.toLowerCase()) {
					fl.push( new Array(cities[i].id, cities[i].label) );
				}
			}
		} else if (parseInt(f) != NaN) { // filter on zip code (numeric)
			for (var i=0; i<cities.length; i++) {
				if (cities[i].zip.substring(0,f.length) == f) {
					fl.push( new Array(cities[i].id, cities[i].label) );
				}
			}
		}
		
		listLength = fl.length+1;
		bubbleSort(fl, fl.length);

		for (var i=0; i<fl.length; i++) {
			s += '<p class="off" id="list'+ (i+1) +'"><span style="display:none;" id="listH'+ (i+1) +'">'+ fl[i][0] +'#'+ fl[i][1] +'</span><a href="" onclick="showCity('+ fl[i][0] +', \''+ fl[i][1] +'\');return false;">'+ fl[i][1] +'</a></p>';
		}
		/*
		document.getElementById('list').innerHTML = s;
		document.getElementById('list').className = 'listC';
		document.getElementById('list').style.display = 'block';
		*/
		
		document.getElementById('list').innerHTML = s;
		var x1 = document.getElementById('listRegio').offsetTop;
		var x2 = document.getElementById('listRegio').offsetLeft;
		var x3 = document.getElementById('listRegio').offsetWidth;
		
		var navi = navigator.appVersion;
		if(navi.indexOf("MSIE")>-1){} 
		else {x3 = (x3 - 5);}
		
		document.getElementById('list').className = 'listC';
		document.getElementById('list').style.top = x1 + 'px';
		document.getElementById('list').style.left = x2 + 'px';
		document.getElementById('list').style.width = x3 + 'px';
		document.getElementById('list').style.display = 'block';
		
	}
	
	function filterWorldCityList(f) {
		
		var s = '';
		listIndex = 0;
		var fl = new Array();

		//world cities
		for (var i=0; i<worldCities.length; i++) {
			if (worldCities[i].label.substring(0,f.length).toLowerCase() == f.toLowerCase()) {
				fl.push( new Array(worldCities[i].id, worldCities[i].label, worldCities[i].country, worldCities[i].cont) );
			}
		}
		
		//countries
		for (var i=0; i<worldCities.length; i++) {
			if (worldCities[i].country.substring(0,f.length).toLowerCase() == f.toLowerCase()) {
				fl.push( new Array(worldCities[i].id, worldCities[i].label, worldCities[i].country, worldCities[i].cont) );
			}
		}
		
		//continents
		for (var i=0; i<worldCities.length; i++) {
			if (worldCities[i].cont.substring(0,f.length).toLowerCase() == f.toLowerCase()) {
				fl.push( new Array(worldCities[i].id, worldCities[i].label, worldCities[i].country, worldCities[i].cont) );
			}
		}
		
		listLength = fl.length+1;
		bubbleSort(fl, fl.length);

		for (var i=0; i<fl.length; i++) {
			s += '<p class="off" id="list'+ (i+1) +'"><span style="display:none;" id="listH'+ (i+1) +'">'+ fl[i][0] +'#'+ fl[i][1] +'#'+ fl[i][2] +'#'+ fl[i][3] + '</span><a href="" onclick="showWorldCity('+ fl[i][0] +',\''+ fl[i][1] +'\',\''+ fl[i][2] +'\',\''+ fl[i][3] +'\');return false;">'+ fl[i][1].toLowerCase() +', '+ fl[i][2].toLowerCase() + '</a></p>';
		}
		/*
		document.getElementById('list').innerHTML = s;
		document.getElementById('list').className = 'listW';
		document.getElementById('list').style.display = 'block';
		*/
		
		document.getElementById('list').innerHTML = s;
		var x1 = document.getElementById('listSteden').offsetTop;
		var x2 = document.getElementById('listSteden').offsetLeft;
		var x3 = document.getElementById('listRegio').offsetWidth;
		
		var navi = navigator.appVersion;
		if(navi.indexOf("MSIE")>-1){} 
		else {x3 = (x3 - 5);}
		
		document.getElementById('list').className = 'listW';
		document.getElementById('list').style.top = x1 + 'px';
		document.getElementById('list').style.left = x2 + 'px';
		document.getElementById('list').style.width = x3 + 'px';
		document.getElementById('list').style.display = 'block';
	}
		
	function filterSkiList(f) {
		
		var s = '';
		listIndex = 0;
		var fl = new Array();

		//ski stations
		for (var i=0; i<skiStations.length; i++) {
			if (skiStations[i].label.substring(0,f.length).toLowerCase() == f.toLowerCase()) {
				fl.push( new Array(skiStations[i].id, skiStations[i].label, skiStations[i].country) );
			}
		}
		
		//countries
		for (var i=0; i<skiStations.length; i++) {
			if (skiStations[i].country.substring(0,f.length).toLowerCase() == f.toLowerCase()) {
				fl.push( new Array(skiStations[i].id, skiStations[i].label, skiStations[i].country) );
			}
		}
		
		listLength = fl.length+1;
		bubbleSort(fl, fl.length);

		for (var i=0; i<fl.length; i++) {
			s += '<p class="off" id="list'+ (i+1) +'"><span style="display:none;" id="listH'+ (i+1) +'">'+ fl[i][0] +'#'+ fl[i][1] +'#'+ fl[i][2] + '</span><a href="" onclick="showSki('+ fl[i][0] +',\''+ fl[i][1] +'\',\''+ fl[i][2] +'\');return false;">'+ fl[i][1].toLowerCase() +', '+ fl[i][2].toLowerCase() + '</a></p>';
		}
		/*
		document.getElementById('list').innerHTML = s;
		document.getElementById('list').className = 'listS';
		document.getElementById('list').style.display = 'block';
		*/
		
		document.getElementById('list').innerHTML = s;
		var x1 = document.getElementById('listSki').offsetTop;
		var x2 = document.getElementById('listSki').offsetLeft;
		var x3 = document.getElementById('listRegio').offsetWidth;
		
		var navi = navigator.appVersion;
		if(navi.indexOf("MSIE")>-1){} 
		else {x3 = (x3 - 5);}
		
		document.getElementById('list').className = 'listS';
		document.getElementById('list').style.top = x1 + 'px';
		document.getElementById('list').style.left = x2 + 'px';
		document.getElementById('list').style.width = x3 + 'px';
		document.getElementById('list').style.display = 'block';
	}
		
function bubbleSort(arrayName,length) {
    for (var i=0; i<(length-1); i++)
        for (var j=i+1; j<length; j++)
            if (arrayName[j][1] < arrayName[i][1]) {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
            }
}

var sat = new Array();
var i = new Image();
i.src="/extra/meteo/sat18.jpg";
sat.push(i.src);
i = new Image();
i.src="/extra/meteo/sat00.jpg";
sat.push(i.src);
i = new Image();
i.src="/extra/meteo/sat06.jpg";
sat.push(i.src);
i = new Image();
i.src="/extra/meteo/sat12.jpg";
sat.push(i.src);
var satX = 0;
var satInterval = undefined;

function startSat() {
	satInterval = setInterval('animateSat()', 1500);
}

function stopSat() {
	clearInterval(satInterval);
	satX = 0;
	animateSat();
}

function animateSat() {
	if (satX==sat.length) satX=0;
	document.getElementById('satImage').src = sat[satX];
	satX++;
}

function loadingDone() {
	//
}

function loadingBusy(msg) {
	if (msg == 'n/a') document.getElementById('searchDetail').innerHTML = '<img src="/extra/weer/img/loader.gif" width="205" height="30" title="" alt="" border="0">';
	else document.getElementById('searchDetail').innerHTML = msg;
}


if (window.addEventListener) window.addEventListener("load", setupWR, false); else if (window.attachEvent) window.attachEvent("onload", setupWR); else if (document.getElementById) window.onload=setupWR;




function addEvent(el,type,listener,useCapture) {
 if(typeof el == 'string') {el = document.getElementById(el);}
 if(!el){return false;}
 if(document.addEventListener) {
  // W3C DOM Level 2 Events - used by Mozilla, Opera and Safari
  if(!useCapture) {useCapture = false;} else {useCapture = true;}
  el.addEventListener(type,listener,useCapture);
 } else {
  // MS implementation - used by Internet Explorer
  el.attachEvent(type, listener);
 }
}


//addEvent('rightCol','keyup', keyevent );


//if (window.addEventListener) window.addEventListener("keyup", keyevent, true); else if (window.attachEvent) window.attachEvent("onkeyup", keyevent); else if (document.getElementById) //window.keyup=keyevent;


//document.onkeyup=keyevent(event);