var map;
id2marker=[];
id2point_center=[];
idlijst=[];

$(function(){
		   
	$("a,area,map").bind("focus",function(){if($(this).blur)$(this).blur();});	// links blurren
	   
	//if (!GBrowserIsCompatible()) { alert ('De browser ondersteund geen googlemaps'); return false; }

	$(".printbutton").click(function()
	{ 
		this_id=$(this).attr("id").substr(12);
		titel = $("#wijziging_"+this_id+" h1").html();
		contents = $("#wijziging_"+this_id+" .contentwrap").html();
		$("#print").remove();
		$("body").append('<div id="print"><h1>'+titel+'</h1><span>'+contents+'</span><img src="files/wijzigingen/'+this_id+'_groot.jpg" width="650"/></div>');
		window.print();
		
	});
	
	/*if ($("#wrapper").hasClass("home"))
	{
		krh=$("#kolom_rechts .contents").height()-289;
		$("#kolom_links .contents").css('height',krh+'px');
	}*/

	$(".fb").fancybox();

	
	
});	


function switchm(id,actie)
{
	for (i=0;i<idlijst.length;i++){if (idlijst[i]==id) { cur_pos=i; break; }}
	
	if (actie==1){target_id = idlijst[cur_pos+2]; if (!target_id) { target_id=idlijst[0]; }	}
	if (actie==-1){target_id = idlijst[cur_pos-1]; if (!target_id) { target_id=idlijst[idlijst.length-1]; }	}	
		
	//map.setCenter(); 
	map.panTo(id2point_center[target_id]);

	GEvent.trigger(id2marker[target_id],'click'); 
}
	
function switchm_popup(id,actie)
{
	for (i=0;i<idlijst.length;i++){if (idlijst[i]==id) { cur_pos=i; break; }}

	if (actie==1){target_id = idlijst[cur_pos+1]; if (!target_id) { target_id=idlijst[0]; }	}
	if (actie==-1){target_id = idlijst[cur_pos-1]; if (!target_id) { target_id=idlijst[idlijst.length-1]; }	}	
	
		
	map.setCenter(id2point_center[target_id]); 
	GEvent.trigger(id2marker[target_id],'click'); 
	poi_leesmeer(target_id);

}

function poi_leesmeer(id)
{
	$("#overlay").show();	
	$("#poi_groot").show();
	$("#poi_groot_content").html($("#popups_groot_"+id).html());
	
}

function sluit_overlay()
{
	$("#overlay").hide(); 
	$("#poi_groot").hide(); 	
}

	
abs_url1 = document.domain; abs_url = "http://"+abs_url1.split('//')[0]+'/';



function initkaart()
{
	route_ida=$('#route_id').html();
	//google map ///////////////////////////////////////////////////////////////////////////////////	
	var geoXml = new GGeoXml(abs_url+"kml/"+route_ida+".kml");


	id2marker=[];
		   
	//if (!GBrowserIsCompatible()) { alert ('De browser ondersteund geen googlemaps'); return false; }
	
	map = new GMap2(document.getElementById("kaart")); 
	map.setUIToDefault();
	map.setCenter(new GLatLng(52.26815737376817, 6.734619140625), 1); 
	map.addOverlay(geoXml);
	geoXml.gotoDefaultViewport(map);
	map.setMapType(G_NORMAL_MAP);
	
	$("#even_geduld").hide();
	
	groenestip = new GIcon();
	groenestip.image = abs_url+"img/poi/stip.png";
	groenestip.shadow = abs_url+"img/googlemaps/schaduw.png";
	groenestip.iconSize = new GSize(31, 31);
	groenestip.shadowSize = new GSize(39, 39);
	groenestip.iconAnchor = new GPoint(10, 10);
	groenestip.infoWindowAnchor = new GPoint(9, 14);
	groenestip.infoShadowAnchor = new GPoint(18, 25);
	
	idlijst=[];
	$("#marker_list li").each(function()
	{ 
		lon=$(this).attr("class").split(',')[1];
		lat=$(this).attr("class").split(',')[0];
		content=$(this).html();	
		tooltip=$(this).attr("title");
		id=$(this).attr("id").substr(1);
		var point = new GLatLng(lon,lat);
		var marker = createMarker(point,groenestip,tooltip,content);
		marker.id=id;
		id2point_center[id]=new GLatLng(lon,lat, 12);   
		id2marker[id]=marker;
		idlijst.push(id);
		map.addOverlay(marker);
	});
	
	function createMarker(point,icon,tooltip,html) 
	{
		var marker = new GMarker(point,{title:tooltip,icon:icon});
	
		GEvent.addListener(marker, "click", function() 
		{
			marker.openInfoWindowHtml(html);
		});	
	
		
		return marker;
	}	
}


function initkaart2()
{
	route_ida=$('#route_id').html();

	//google map ///////////////////////////////////////////////////////////////////////////////////	
	
	//var geoXml = new GGeoXml();
	var zoom=11;
	id2marker=[];
	idlist=[];
	map = new GMap2(document.getElementById("kaart")); 
	map.setMapType(G_HYBRID_MAP); 
	map.addControl(new GLargeMapControl3D());
	map.addControl(new GMapTypeControl());
	map.enableScrollWheelZoom();
	map.setCenter(new GLatLng(52.33995406943698, 7.0058441162109375),zoom); 
	
	G_HYBRID_MAP.getMinimumResolution = function(){return 11};
	G_HYBRID_MAP.getMaximumResolution = function(){return 15}; 
	G_SATELLITE_MAP.getMinimumResolution = function(){return 11};
	G_SATELLITE_MAP.getMaximumResolution = function(){return 15}; 
	G_NORMAL_MAP.getMinimumResolution = function(){return 11};
	G_NORMAL_MAP.getMaximumResolution = function(){return 15}; 
	
	GEvent.addListener(map, "zoomend", function(oldzoom,newzoom) { zoom=newzoom; add_overlay();  }); 
	
	//geoXml.gotoDefaultViewport(map);
	var geoXml;
	var geoCallback = function()
	{
		map.addOverlay(geoXml,$("#even_geduld").hide());
		geoXml.gotoDefaultViewport(map);
		//setTimeout('$("#even_geduld").hide()',300);
	}
	$("#even_geduld").hide();
	//geoXml = new GGeoXml(abs_url+"kml/kmldata.kml?id="+route_ida, geoCallback);
	
	groenestip = new GIcon();
	groenestip.image = abs_url+"img/poi/stip.png";
	groenestip.shadow = abs_url+"img/googlemaps/schaduw.png";
	groenestip.iconSize = new GSize(31, 31);
	groenestip.shadowSize = new GSize(39, 39);
	groenestip.iconAnchor = new GPoint(10, 10);
	groenestip.infoWindowAnchor = new GPoint(9, 14);
	groenestip.infoShadowAnchor = new GPoint(18, 25);
	
	$("#marker_list li").each(function()
	{ 
		lon=$(this).attr("class").split(',')[1];
		lat=$(this).attr("class").split(',')[0];
		content=$(this).html();	
		tooltip=$(this).attr("title");
		id=$(this).attr("id").substr(1);
		var point = new GLatLng(lon,lat);
		
		var marker = createMarker(point,groenestip,tooltip,content);
		marker.id=id;
		id2point_center[id]=new GLatLng(lon,lat, 12);   
		
		id2marker[id]=marker;
		idlijst.push(id);
		map.addOverlay(marker);
	});
	
	function createMarker(point,icon,tooltip,html) 
	{
		var marker = new GMarker(point,{title:tooltip,icon:icon});
	
		GEvent.addListener(marker, "click", function() 
		{
			marker.openInfoWindowHtml(html);
		});	
	
		
		return marker;
	}	

	current_zoomfile='';
	groundOverlay='';
	
	function add_overlay() 
	{
		pad='reggepad';
		
		if (zoom==11) { routezoom='1'; }
		if (zoom==12) { routezoom='08'; }		
		if (zoom==13) { routezoom='06'; }				
		if (zoom==14) { routezoom='04'; }				
		if (zoom==15) { routezoom='02'; }						

		//zoomfile = abs_url+'img/dinkelpad_route'+routezoom+'.gif';
		zoomfile = abs_url+'img/routes/'+pad+routezoom+'.gif';
		//alert(zoomfile);
//		console.log(zoomfile);
		//if (zoomfile==current_zoomfile) { return; }
		if (groundOverlay) { map.removeOverlay(groundOverlay); }
		
		var pointSW = new GLatLng(52.178458251586754, 6.391897201538086);
		var pointNE = new GLatLng(52.512773473953324, 6.602697372436523);
		groundOverlay = new ProjectedOverlay (zoomfile, new GLatLngBounds(pointSW,pointNE));
		current_zoomfile = zoomfile; 

		map.addOverlay(groundOverlay);
		//groundOverlay.setOpacity(50);
		
	}

	add_overlay();

}

//noordwest = 52.512773473953324, 6.391897201538086
//zuidoost (52.178458251586754, 6.602697372436523)
