/*
Copyright (c) 2007 Ylab, http://www.ylab.nl
*/
var map = null;
var mapLayer = null;
var geocoder = null;
var gLatLong = new Array();
		gLatLong['dm'] = new GLatLng(52.09068, 5.12164);
var gIcons = new Array();
		gIcons['std'] = new GIcon();
		gIcons['std'].image = '../images/mm_20_maroon.png';
		gIcons['std'].shadow = '../images/mm_20_shadow.png';
		gIcons['std'].iconSize = new GSize(12, 20);
		gIcons['std'].shadowSize = new GSize(22, 20);
		gIcons['std'].iconAnchor = new GPoint(6, 20);
		gIcons['std'].infoWindowAnchor = new GPoint(5, 1);

		gIcons['fav'] = new GIcon(gIcons['std']);
		gIcons['fav'].image = '../images/mm_20_gray.png';

		gIcons['dom'] = new GIcon();
		gIcons['dom'].image = '../images/mm_20_dom.png';
		gIcons['dom'].shadow = '../images/mm_20_domshadow.png';
		gIcons['dom'].iconSize = new GSize(10, 28);
		gIcons['dom'].shadowSize = new GSize(23, 28);
		gIcons['dom'].iconAnchor = new GPoint(5, 28);

function initGMap() {
  if (GBrowserIsCompatible() && !map) {
  	mapLayer = document.getElementById("map");
    map = new GMap2(mapLayer);
    var cpos = new GControlPosition(G_ANCHOR_TOP_RIGHT);
    map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT));
		map.setCenter(gLatLong['dm'], 15);

    var marker = new GMarker(gLatLong['dm'], gIcons['dom']);
    map.addOverlay(marker);

    geocoder = new GClientGeocoder();
    geocoder.setBaseCountryCode('NL');
    if(adresses){
      for(var i=0, item; item=adresses[i]; i++){
        showAddress(item.address, item.infohtml, gIcons['std'], i==0)
      }
    }
    if(favorites){
      for(var i=0, item; item=favorites[i]; i++){
        showAddress(item.address, item.infohtml, gIcons['fav'], false)
      }
    }
  }
}

function showAddress(address, infohtml, icon, center) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if(point) {
        	//address found
	        var marker = new GMarker(point, icon);
	        map.addOverlay(marker);
	        attachInfo(marker, infohtml, false);
        	if(center){
						map.panTo(point);
		      	map.setCenter(point, 15);
		      }
		    }
      }
    );
  }
}

function attachInfo(marker, infohtml, bOpen){
	if(marker && infohtml){
	  GEvent.addListener(marker, 'click', function() {
	    marker.openInfoWindowHtml(infohtml);
	  });
	  if(bOpen){
	    marker.openInfoWindowHtml(infohtml);
	  }
	}
}

function toggleMap(bShow){
	if(bShow){
		initGMap();
	}
	if(mapLayer){
		mapLayer.style.display = bShow ? 'block' : 'none';
	}
	document.getElementById('btn-map').className = bShow ? 'pushedbutton' : 'pseudobutton';
	document.getElementById('btn-pho').className = bShow ? 'pseudobutton' : 'pushedbutton';
}

function showMap(){
	toggleMap(true);
}

function hideMap(){
	toggleMap(false);
}


function initPage(){
	document.getElementById('btn-map').onclick = showMap;
	document.getElementById('btn-pho').onclick = hideMap;
	toggleMap(mapByDefault);
}

addLoadFunction(initPage);
addEvent(window, 'onload', initPage);

