/*#######################################################################################
# Deze software is intellectueel eigendom van Knooppunt en is beschermd op grond van de #
# Nederlandse auteurswet, internationale verdragsbepalingen en de toepasselijke wetten  #
# van het land waarin de software gebruikt wordt. U verbindt zich ertoe de software     #
# niet te zullen veranderen, aanpassen of te vertalen, tenzij uitdrukkelijk anders      #
# overeengekomen. U verkrijgt uitsluitend het gebruiksrecht van de software. Knooppunt  #
# is in in geen geval aansprakelijk voor directe of indirecte schade, gevolgschade of   #
# enige vorm van incidentele of bijzondere schade, inclusief gederfde winst of bespa-   #
# ringen, en evenmin voor vorderingen van derden, door het gebruik van de software.     #
#                                                                                       #
#     (c) Knooppunt internetdiensten        http://www.knoop.nl        E. Toering       #
#                                                                                       #
#######################################################################################*/
// if the URL contains 'test' GPS positions are shown
var map = new Array();
map['locations'] = new Array();
map['tabs']      = new Array('address','route');//,'address','test');
map['tab_titles']= new Array('Adres','Route');//,'Adres','Test');
map['tab_size']  = 'width:300px;height:80px';
map['zoomlevel_min'] = 10;
map['map_type'] = 'G_NORMAL_MAP'; // G_NORMAL_MAP (line) G_SATELLITE_MAP (satellite photo's) G_HYBRID_MAP (both)

//fill the positions:
pos = build_location(52.94370845847476 // lat // 52.9369566060647
                    ,6.038516163825989 // long//  5.9516221119822
                    ,'Schoterlandseweg 46, 8455JH Katlijk', // adress: street nr, pc city
             new Hash('address', '<b>Jansma\'s Hof</b><br>Schoterlandseweg 46<br>8455 JH Katlijk (Friesland)<br>Tel.(0513)541845<br>Fax (0513)542355'
//   id (in map[tabs])-.  content -. (do not forget to add the keys to 'map['tabs']'
//                  ,'test','testinhoud<br>'
                     )
                    );
// what to do onload
extra_onload    ="load("+pos+");resize_Gmap();";
// processed in the load funtion
extra_onunload  ="GUnload()";
// what to do on a browser resize
extra_resize_cmd = 'setTimeout("';
extra_resize_cmd+= "map[\'GMap2\'].checkResize();";
extra_resize_cmd+= "re_center_zoom();";
//extra_resize_cmd+= "map[\'GMap2\'].setCenter(map[\'bounds\'].getCenter());";
extra_resize_cmd+= '",250);'; //map.setZoom(map.getBoundsZoomLevel(bounds)) multi-pos?

// the way to initialize:
if (typeof(window.onload) != 'undefined' ) { window.onload = function() {window.onload;eval(extra_onload)} }
else                                       { window.onload = function() {              eval(extra_onload)} }

//                            posX posY     width              height
//map['map_small'] = new Array(250,155,'breedte-250-16','hoogte-155-35-hoogte*.02'); => example dynamic width/height
// positions & size map
map['map_small'] = new Array(4,-11,673,'if(hoogte>580){hoogte-275}else{310}');
map['map_large'] = new Array('if(breedte>1000){-(breedte-440)/2}else{-272}',-237,'if(breedte>1000){breedte}else{1000}','if(hoogte>580){hoogte}else{580}');
// positions & size fullscreen button
map['fullscreen_small'] = new Array(map['map_small'][0]+'+65',map['map_small'][1]+'+7',80,19);
map['fullscreen_large'] = new Array(map['map_large'][0]+'+65',map['map_large'][1]+'+7',80,19);
// fullscreen buttons
map['fullscreen_src']   = 'img/fullscreen.png';
map['fullscreen_on_src']= 'img/fullscreen-on.png';
// which divs need to be hidden at fullscreen? (check with MSIE6 => doesn't follow z-index rules quite well..)
map['fullscreen_div_hide'] = new Array();
// show the lat/long in the doc.title?
if (location.search.indexOf('test') > 0 ) { setInterval("show_pos()",500); }

map['tab_route_text']  = '<b>Postcode</b> of <b>adres</b> (straat, plaats) van uw vertrekpunt:';
map['tab_route_button']= 'toon route &gt;';

/* uncomment to use personal icon:
map['icon'] = new GIcon();
map['icon'].image           ="../dealers/logo-janusk.png";
map['icon'].iconSize        =new GSize(32,40);
map['icon'].shadow          ="../dealers/logo-janusk-shadow.png";
map['icon'].shadowSize      =new GSize(52,40);
map['icon'].iconAnchor      =new GPoint(16,20);
map['icon'].infoWindowAnchor=new GPoint(27,18);
*/

function load(pos) {
	if (GBrowserIsCompatible())	{
    if (typeof(window.onunload)!='undefined' ) { window.onunload = function() {window.onunload;eval(extra_onunload)} }
    else                                       { window.onunload = function() {                eval(extra_onunload)} }
    preloader('',map['fullscreen_src'],map['fullscreen_on_src']);

		GMap2.prototype.wheelZoom = function(event) {
                	if(event.cancelable)    event.preventDefault();
                	(event.detail || -event.wheelDelta) < 0 ? MapApp.mapObj.zoomIn():
			MapApp.mapObj.zoomOut();
                	return false;
        	}
		map['GMap2'] = new GMap2(document.getElementById("map"));
		map['GMap2'].addControl(new GLargeMapControl());
		map['GMap2'].addControl(new GMapTypeControl());
		map['GMap2'].addControl(new GScaleControl());
		map['GMap2'].enableDoubleClickZoom(true);
		map['GMap2'].enableContinuousZoom(true);
		map['geocoder'] = new GClientGeocoder();
    map['bounds']   = new GLatLngBounds();
   	map['center']   = new GLatLng(0, 0);
//    map['GMap2'].setCenter(new GLatLng(0, 0), 13); // there needs to be a center to prevent errors..
    map['GMap2'].setMapType(eval(map['map_type']));
//    alert('load');
		GEvent.addDomListener(document.getElementById("map"), "DOMMouseScroll", wheelZoom); // Firefox
		GEvent.addDomListener(document.getElementById("map"), "mousewheel",     wheelZoom); // MSIE

  	for (pos=0;pos<map['locations'].length;pos++) { place_location(pos); }
	}
}

// showAddress('parallelweg 7, Heerenveen, NL')
function showAddress(address) {
	if (map['geocoder']) {
    map['geocoder'].getLatLng(address,
    function(point) {
	      if (!point) {
	        alert(address + " niet gevonden.");
	      }
	      else {
	        map['GMap2'].setCenter(point, 13);
	        var marker = new GMarker(point);
	        map['GMap2'].addOverlay(marker);
//	     		marker.openInfoWindowHtml(address + '<br/><br/>');
	        map['GMap2'].setZoom(15);
	      }
      }
    )
	}
}

function build_location(lat,long,name,hash) {
  pos = map['locations'].length;
  map['locations'][pos] = new Array();
  if(typeof(hash) == 'object') {
    for (var i in hash.items) {
//alert(i+':'+hash.items[i]);
      map['locations'][pos][i] = hash.items[i];
    }
  }
  map['locations'][pos]['lat'] = lat;
  map['locations'][pos]['long']= long;
  map['locations'][pos]['name']= name;
  return pos;
}
function place_location(pos) {
//	alert(map['lokatie_lat']+"\n"+lokatie_long);
	if (typeof(map['locations'][pos]['lat']) =='number'
	 && typeof(map['locations'][pos]['long'])=='number' ) {
		map['locations'][pos]['point'] = new GLatLng(map['locations'][pos]['lat'], map['locations'][pos]['long']);
    place_point(pos);
	} else if (map['geocoder']) {
    map['geocoder'].getLatLng(map['locations'][pos]['name']+', NL',
    	function(point) {
	      if (!point) {
	        alert(map['locations'][pos]['name'] + " niet gevonden.");
	        return;
	      }
	      else {
          map['locations'][pos]['point']= point;
          map['locations'][pos]['lat']  = map['locations'][pos]['point'].lat();
          map['locations'][pos]['long'] = map['locations'][pos]['point'].lng();
          place_point(pos);
	      }
      }
    )
	} else {
    return;
	}
}
function place_point(pos) {
//  alert('place_point '+map['locations'][pos]['point']);
  map['bounds'].extend(map['locations'][pos]['point']);
  map['center'] = map['bounds'].getCenter();
  map['GMap2'].addOverlay( createMarker(pos,'','') );
  re_center_zoom();
//  map['GMap2'].setCenter(map['bounds'].getCenter());
//  map['GMap2'].setCenter(map['locations'][pos]['point'], 13);
//marker.openInfoWindowHtml(address + '<br/><br/>');
//  map['GMap2'].setZoom(15);
}

function re_center_zoom() {
	zoomlevel = map['GMap2'].getBoundsZoomLevel(map['bounds']);
	if (typeof(map['zoomlevel_min']) != 'undefined' && zoomlevel > map['zoomlevel_min']) { zoomlevel = map['zoomlevel_min'] }
//	alert(map['GMap2'].getBoundsZoomLevel(map['bounds'])+' wordt '+zoomlevel);
	map['GMap2'].setCenter(map['center'], zoomlevel);
//  map['GMap2'].setZoom(zoomlevel);
}
function wheelZoom(a) {
  if(typeof(a) == 'undefined' || !a) {return}
  if (a.detail) { // Firefox
    if (a.detail < 0)      { map['GMap2'].zoomIn(); }
    else if (a.detail > 0) { map['GMap2'].zoomOut(); }
  }
  else if (a.wheelDelta) { // MSIE
    if (a.wheelDelta > 0)      { map['GMap2'].zoomIn(); }
    else if (a.wheelDelta < 0) { map['GMap2'].zoomOut(); }
  }
}

function show_pos(){
  if (map['GMap2']) {
  	var loc = map['GMap2'].getCenter();
  	document.title = ' long='+loc.x+' lat='+loc.y;
  }
}

map['resize_to'] = '-';
function resize_Gmap() {
	if ( map['resize_to'] == '+' ) {
		map['resize_to'] = '-';
  	for (i=0;i<map['fullscreen_div_hide'].length;i++) {
  		eval(doc2 + '"'+map['fullscreen_div_hide'][i]+'"' + sty).visibility = 'hidden';
    }
		div_plaatsen['map']              = map['map_large'];
		div_plaatsen['button_fullscreen']= map['fullscreen_large'];
		document.getElementById('button_fullscreen_img').src = map['fullscreen_on_src'];
	} else {
		map['resize_to'] = '+';
  	for (i=0;i<map['fullscreen_div_hide'].length;i++) {
  		eval(doc2 + '"'+map['fullscreen_div_hide'][i]+'"' + sty).visibility = 'visible';
    }
		div_plaatsen['map']              = map['map_small'];
		div_plaatsen['button_fullscreen']= map['fullscreen_small'];
		document.getElementById('button_fullscreen_img').src = map['fullscreen_src'];
	}
  map['center'] = map['GMap2'].getCenter();
	Herladen(''); // without the extra_resize_cmd!
  setTimeout('map[\'GMap2\'].checkResize();',100);
 	setTimeout('map[\'GMap2\'].setCenter(map[\'center\']);',100);

//	setTimeout('resize_Gmap();',1500); // onload the map sometimes isn't complete?! 
//	setTimeout('resize_Gmap();',1550); // onload the map sometimes isn't complete?! 
}

//createMarker -> titel ; point (samengestelde geografischepunt); mhtml (tekst: informatie over de kerk); mpic (evt. illustratie)

function createMarker (pos, mhtml, mpic) {
  htmls =  new Array();
  //Aanmaken      Marker met tab-ballon
  // Info window content
 	for (i=0;i<map['tabs'].length;i++) {
 	  if (map['tabs'][i] == 'route'  ) {
      htmls[i] = build_route(pos);
    }
 	  else if (map['tabs'][i] == 'address') {
      htmls[i] = build_address(pos);
    }
 	  else if (typeof(map['locations'][pos][map['tabs'][i]]) != 'undefined' ) {
 	     htmls[i] = map['locations'][pos][map['tabs'][i]];
    }
  }
  //create the marker
  if (typeof(map['icon']) != 'undefined' ) {
    var marker = new GMarker(map['locations'][pos]['point'],map['icon']);
  } else {
    var marker = new GMarker(map['locations'][pos]['point']);
  }
  //link the tab(s)
  GEvent.addListener(marker, "click", function() {
    var tabs = [];
    for (var i=0; i<htmls.length; i++) {
			if (htmls[i].length > 1 ) {
    		htmls[i] = '<div style="'+map['tab_size']+'">' + htmls[i] + '</div>';
      	tabs.push(new GInfoWindowTab(map['tab_titles'][i],htmls[i]));
			}
    }
    marker.openInfoWindowTabsHtml(tabs);
  });
  return marker;
}

function build_route(pos) {
  html = '';
  html+= '<div style="height:5px;width:1px"></div>';
  html+= map['tab_route_text']+'<br>';
  html+= '<div id="contactform" style="position:relative;"><form action="http://maps.google.nl/maps" method="get"" target="_blank">';
  html+= '<input style="width:200px" type="text" SIZE=30 MAXLENGTH=40 name="saddr" id="saddr" value="" /> ';
  html+= '<INPUT style="width:90px" value="'+map['tab_route_button']+'" TYPE="SUBMIT"><input type="hidden" name="daddr" value="'  + map['locations'][pos]['point'].lat() + ',' + map['locations'][pos]['point'].lng() +   " (" + map['locations'][pos]['name'] + ")" + '"/>';
  html+= '</div>';
  return html;
}
function build_address(pos) {
  html = '';
  if (typeof(map['locations'][pos]['address']) != 'undefined' ) {
    html+= map['locations'][pos]['address'];
  } else {
    html+= map['locations'][pos]['name'].replace(/,/g,'<br>');
  }
  return html;
}

function Hash() {
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
}

