/**
 * @author Theo Bakker, WHELP.nl
 * @title 31
 */

//load Google Map

var map;
var gmarkers = [];


// === This function picks up the click and opens the corresponding info window ===
function myclick(i) {
	
	//GEvent.trigger(gmarkers[i], "click");
}

// ======= This function handles selections from the select box ====
// === If the dummy entry is selected, the info window is closed ==
function handleSelected(opt) {
var i = opt.selectedIndex - 1; 
if (i > -1) {
    GEvent.trigger(gmarkers[i],"click");
}
else {
    map.closeInfoWindow();
}
}


function load(vHeader, googleCenter, vNewsID, vCMS, vMaand, vJaar, vGID, vPostcode) 
{

	var i=0;
	var side_bar_html = "";
	var labelContainer;
	
	var select_html2;
	select_html2 =    '<select onChange="handleSelected(this)">';
	select_html2 += '<option selected> - Toon een lokatie - </option>';
	
	if (GBrowserIsCompatible()) 
	{
		
		
		// START LABELCONTROL 
		// ZET CODE: map.addControl(new LabelControl());
		// === Create a custom Control ===
		function LabelControl() {  }
			LabelControl.prototype = new GControl();

			LabelControl.prototype.initialize = function(map) {
			labelContainer = document.createElement("div");
			labelContainer.style.overflow="auto";
			labelContainer.style.backgroundColor = "#ffffcc";
			labelContainer.style.border = "1px solid black";
			labelContainer.style.height="200px";
			labelContainer.style.width="120px";
			labelContainer.style.paddingLeft="5px";

			map.getContainer().appendChild(labelContainer);
			return labelContainer;
		}

		LabelControl.prototype.getDefaultPosition = function() {
			return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 33));
		}
		// EIND LABELCONTROL 




		
	
		var loc =new String(googleCenter);
		var locArr = loc.split(",")
		for (var j = 0; j < locArr.length; j++) 
		{
			lat = locArr[0];
			lon = locArr[1];
			
		}
		
		var centerpoint = new GLatLng(lat, lon);
		
		
  		map = new GMap2(document.getElementById("map"));
  		//map.addControl(new LabelControl());
  		map.addControl(new GLargeMapControl());
  		map.addControl(new GOverviewMapControl());

  		map.setCenter(centerpoint, 12);
	  	
	  	
	  	//START ZET ICONEN
	  	var iconhome = new GIcon();
		iconhome.image = "../googlemap/icons/icon_home.png";
		iconhome.iconSize = new GSize(32, 32);
		iconhome.iconAnchor=new GPoint(16,32);
		iconhome.infoWindowAnchor = new GPoint(5, 1);
		iconhome.shadowSize=new GSize(56,32);
		//EIND ZET ICONEN
		

  		var marker = new GMarker(centerpoint, iconhome);
		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(vHeader,{maxUrl:"gemeentehuis.html"});
			});
		if (vCMS) 
		{
			// Recenter Map and add Coords by clicking the map
			GEvent.addListener(map, 'click', function(overlay, point) {
						
						document.getElementById("postcode4").value=point.y + ',' + point.x;        
			});
		}else
		{
			//GEvent.addListener(map,"infowindowclose", function() {
			//	document.getElementById(lastlinkid).style.background="#ffffff";
			//});
		}
		//create randomnumber and retrieve xml file
		var randomnumber=Math.floor(Math.random()*11111)
		
		if (vCMS) 
		{
			xmlfile = "../bekendmakingen/cmbekendmaking.asp?random=" + randomnumber + "&newsid=" + vNewsID
		}else
		{
			xmlfile = "../bekendmakingen/cmbekendmaking.asp?random=" + randomnumber + "&newsid=" + vNewsID + "&vmaand=" + vMaand + "&vjaar=" + vJaar + "&vgid=" + vGID + "&vpostcode=" + vPostcode
		}
		
		document.getElementById('xmltest').value =xmlfile;
		//xmlfile = "../bekendmakingen/google.xml"
		
		GDownloadUrl(xmlfile , 
			function(data, responseCode) {
				var iconsource
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("NIEUWS");
				
				var select_html;
				select_html = '';
				for (var i = 0; i < markers.length; i++) 
				{
					html = '';
					try
					{
						xid = xml.getElementsByTagName("NEWSID")[i].childNodes[0].nodeValue;
						xloc = xml.getElementsByTagName("GEOLOC")[i].childNodes[0].nodeValue;
						ddatum = xml.getElementsByTagName("NIEUWSDATUM")[i].childNodes[0].nodeValue;
						pcode = xml.getElementsByTagName("PCODE")[i].childNodes[0].nodeValue;
						naam = xml.getElementsByTagName("NEWSHEADER")[i].childNodes[0].nodeValue;
						gid = xml.getElementsByTagName("NIEUWSGID")[i].childNodes[0].nodeValue;
						bekendid = xml.getElementsByTagName("EBEKENDID")[i].childNodes[0].nodeValue;
						ebekendmaking = xml.getElementsByTagName("EBEKENDMAKING")[i].childNodes[0].nodeValue;
						//html += '<table border=0 cellpadding=0 cellspacing=0 id="googlemapbal">';
						//html += '<tr><td colspan=2><b>' + naam + '</b></td></tr>';
						//html += '<tr><td><font size=1><i>soort: ' + ebekendmaking + '</i></td></tr>';
						//html += '<tr><td>postcode: ' + pcode + '</td></tr>';
						//html += '<tr><td><a href ="../sitemap/page.asp?action=cnewsdet&nid=' + xid + '">bekijk de bekendmaking</a></font></td></tr>';
						//html += '</table>';
						
						html += '<div style="width:275px; height:60px; overflow:auto; line-height:14px;">';
						html += '<div><b>' + naam + '</b></div><div><font size=1><i>soort: ' + ebekendmaking + '</i></div><div>postcode: ' + pcode + '</div><div>klik <a href ="../sitemap/page.asp?action=cnewsdet&nid=' + xid + '">hier</a> voor de bekendmaking</font></div>';
						html += '</div>';

						
						select_html += '<option>' + pcode + ' - ' + ebekendmaking + '</option>';
						iconsource = "../googlemap/icons/icon_" + bekendid + ".png"; 
						
						//switch(parseFloat(bekendid))
						//{
						//case 8:
						//	iconsource = "../googlemap/icons/icon_1.png";   
						//	break;
						//case 10:
						//	iconsource = "../googlemap/icons/icon_2.png";
						//	break;
						//case 11:
						//	iconsource = "../googlemap/icons/icon_3.png";
						//	break;
						//case 16:
						//	iconsource = "../googlemap/icons/icon_4.png";
						//	break;
						//default:						
						//	iconsource = "../googlemap/icons/icon_1.png";
						//}

						var icon = new GIcon();
						icon.image = iconsource;
						icon.iconSize = new GSize(20, 34);
						icon.iconAnchor = new GPoint(0, 20);
						icon.infoWindowAnchor = new GPoint(5, 1);
						
						var loc =new String(xloc);
						var locArr = loc.split(",")
						
						for (var j = 0; j < locArr.length; j++) 
						{
							lat = locArr[0];
							lon = locArr[1];
							
						}
						var point = new GLatLng(lat,lon);
						map.addControl(new GMapTypeControl());
						//var marker = new GMarker(point);	
						
						
						var marker = createMarker(point,html, icon, pcode, ddatum,xid);
						map.addOverlay(marker);
						
						//labelContainer.innerHTML = side_bar_html;	
						//document.getElementById("side_bar").innerHTML = side_bar_html;		
					}
					catch (e) {}
				}
				select_html2 += select_html;
				select_html2 += '</select>';
				
				document.getElementById("selection").innerHTML = select_html2;	
			}	
		);
		
		
		
		
		
		
		//Create marker and set up event window
		function createMarker(point,html,icon, postcode, datum, xid){
				//var linkid = "link"+i;
				var marker = new GMarker(point,icon);
				GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
				//document.getElementById(linkid).style.background="#ffff00";
				
				//lastlinkid=linkid;
				});

				// save the info we need to use later for the side_bar
				gmarkers[i] = marker;
				// add a line to the side_bar html
				//side_bar_html += '<div id="'+linkid+'"><a href="javascript:myclick(' + i + ')">' + datum + ' - ' + postcode + '</a><br>a</div>';
				
				
				i++;

				return marker;
		}
		
		
  } //close GBrowserIsCompatible
} //close load

function splitmaand(vValue)
{
	var loc =new String(vValue);
	var locArr = loc.split(" ")
	
	document.getElementById('bekendmaand').value = locArr[0];
	document.getElementById('bekendjaar').value = locArr[1];
	googleform.submit();
}

