﻿
addEvent(window,'unload',myGUnload,false);

function addEvent(elm, evType, fn, useCapture) { 
    if (elm.addEventListener) { 
        elm.addEventListener(evType, fn, useCapture); 
        return true; 
    } else if (elm.attachEvent) { 
        var r = elm.attachEvent('on' + evType, fn); 
        return r; 
    } else { 
        elm['on' + evType] = fn; 
    } 
}

function myGUnload() {
    try {
        GUnload();
    } catch (ex) {
        // ignore
    }
}

function MyZoomControl() {
}

MyZoomControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
MyZoomControl.prototype.initialize = function(map) {
  return document.getElementById("ControlDiv");
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
MyZoomControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

function MyMapTypeControl() {
}

MyMapTypeControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
MyMapTypeControl.prototype.initialize = function(map) {
  return document.getElementById("KaartTypeDiv");
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
MyMapTypeControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

function MyLegendaControl() {
}

MyLegendaControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
MyLegendaControl.prototype.initialize = function(map) {
  return document.getElementById("LegendaSpan");
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
MyLegendaControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 37));
}

/*Map2*/

function addDraggableMarker(obj) {
    if( blnFree ){
        var center = obj.getCenter();
        var marker = new GMarker(center, {draggable: true, title: 'Click to remove'});
        
        GEvent.addListener(marker, "dragend", function() { 
            document.getElementById("txtLat").value = marker.getPoint().lat();
            document.getElementById("txtLong").value = marker.getPoint().lng();
            marker.openInfoWindowHtml("Lat = " + marker.getPoint().lat() + "<br>Lng = " + marker.getPoint().lng());
        });
		GEvent.addListener(marker, "mouseover", function(marker, point) {  
		//alert('hello');
		});
		GEvent.addListener(obj, "click", function(marker, point) {  
			if (marker){obj.removeOverlay(marker);  }
			try{
				document.form1.txtLong.value = '';
				document.form1.txtLat.value = '';
				document.form1.txtBeschrijving.value = '';
				document.form1.txtFileUpload.value = '';
			}catch(e){}
		});
		//GEvent.addListener(marker, "click", function() { 
		//	marker.openInfoWindowHtml("Lat = " + marker.getPoint().lat() + "<br>Lng = " + marker.getPoint().lng());
		//});
        GEvent.addListener(marker, "dragstart", function() {
          obj.closeInfoWindow();
        });
        obj.addOverlay(marker);
        document.getElementById("txtLat").value = marker.getPoint().lat();
        document.getElementById("txtLong").value = marker.getPoint().lng();
        //blnFree = false;
    }
}
function TogggleProjects(obj, gmarkersId){
	if(obj.checked == true){
		//alert(gmarkersId);
	}else{
	
	}
}
function resetOverlays(obj){
    document.getElementById("txtLat").value = "";
    document.getElementById("txtLong").value = "";
    obj.clearOverlays()
    blnFree = true;
}
function checkGetPercelen(obj){
	if(intPropZoomLevel!=0){
		var zoom = obj.getZoom();
		var bounds = obj.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		if(zoom >= intPropZoomLevel){
			//alert(zoom);alert(bounds);
			getData(southWest.lng(), southWest.lat(), northEast.lng(), northEast.lat(), '2')
		}
	}
}

function getData(swclong, swclat, neclong, neclat, regio){
	// ===== Start with an empty GLatLngBounds object =====     
	var bounds = new GLatLngBounds();
	// Read the data from example.xml
	var request = GXmlHttp.create();
	request.open("GET", "services/get_markers.asmx/GetPercelen?swclong="+swclong+"&swclat="+swclat+"&neclong="+neclong+"&neclat="+neclat+"&regio="+regio, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = GXml.parse(request.responseText);
			// obtain the array of markers and loop through it
			var markers = xmlDoc.documentElement.getElementsByTagName("DataTablePercelen");
			//alert(markers.length);
			if(markers.length >0){
				mapGMapControl.clearOverlays();
				mapGMapControl.addOverlays(arrayPointsGMapControl);
				for (var i = 0; i < markers.length; i++) {
						var lat = parseFloat(markers[i].getElementsByTagName("PER_LAT")[0].firstChild.nodeValue);
						var lng = parseFloat(markers[i].getElementsByTagName("PER_LONG")[0].firstChild.nodeValue);
						var  html = '<div style="width: 210px; height:50px; background-color:#FFF;">';
						html += markers[i].getElementsByTagName("STR_Naam")[0].firstChild.nodeValue + '&nbsp;:&nbsp;';
						html += markers[i].getElementsByTagName("Aantal")[0].firstChild.nodeValue + '&nbsp;woningen';
						html += '</div>';
						var point = new GLatLng(lat,lng);
						var marker = createMarkerToo(point,"",html);
						mapGMapControl.addOverlay(marker);
						bounds.extend(point);
				}
			}
			// ===== determine the zoom level from the bounds =====
			//mapGMapControl.setZoom(mapGMapControl.getBoundsZoomLevel(bounds));
			// ===== determine the centre from the bounds ======
			//mapGMapControl.setCenter(bounds.getCenter());
		}
	}
	request.send(null);
}

// A function to create the marker and set up the event window
function createMarkerToo(point,name,html) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

function clearRollOver(){
	document.getElementById('ToolTip').style.display = 'none';
}

function rollOver(map, marker, sText){
	var sClass="extern";
	var sSrc="img";
	var sUrl=""
	var arrTemp;
	clearRollOver();
	if(sText.indexOf("|") != -1){
		arrTemp = sText.split('|');
		if(arrTemp.length > 2){
			sText = arrTemp[4];
			sUrl = arrTemp[5];
			//alert(sText + '-' + arrTemp.length);
		}else{
			sText = arrTemp[0];
			sUrl = arrTemp[1];
		}
	}else{
	
	}

	if(sUrl!=''&&sText!=''){

		var sHTML = '<table cellspacing="0" cellpadding="0" height="25" class="' + sClass + '"><tr>'
		sHTML += '<td class="tooltiplinks"><img src="/images/blank.gif" width="25" height="23" /></td>'
		sHTML += '<td class="tooltipmidden"><div id="ToolTipText" class="ToolTipText"><a href="' + sUrl + '" target="_parent">' + sText + '</a></div></td>'
		sHTML += '<td class="tooltiprechts"><a href="" title="sluit" onclick="CheckMouseOut();return false;" style="width:10;height:23;">X</a></td>'
		sHTML += '</tr></table>'

		var oMarkerImage = ( event.srcElement ? event.srcElement : event.target )
		var oMarkerDiv = oMarkerImage.parentNode.parentNode
		var oKaartDiv = oMarkerDiv.parentNode.parentNode
		
		//var sSrc = oMarkerImage.src.replace('.gif', '-h.gif')
		//alert(oMarkerImage.offsetLeft);
		//alert(oMarkerImage.offsetTop);
		//alert(oMarkerDiv.offsetLeft);
		//alert(oKaartDiv.offsetLeft);

		x = oMarkerImage.offsetLeft + oMarkerDiv.offsetLeft + oKaartDiv.offsetLeft + 35;
		y = oMarkerImage.offsetTop + oMarkerDiv.offsetTop + oKaartDiv.offsetTop + 30;
		y = map.getSize().height - y;

		document.getElementById('ToolTip').innerHTML = sHTML
		document.getElementById('ToolTip').style.left = '-4000px'
		document.getElementById('ToolTip').style.display = 'block'
		
		br = document.getElementById('ToolTip').clientWidth + 20

		document.getElementById('ToolTipText').style.width = '0px'
		document.getElementById('ToolTip').style.left = x + 'px'
		document.getElementById('ToolTip').style.bottom = y + 'px'
		
		Verbreed(0, br, 0)
	}
}
function Verbreed(stap, br, nHover){
		stappen = 12;
		if ( navigator.userAgent.indexOf('Firefox') > -1 ) stappen -= 6;
		stap ++;
		document.getElementById('ToolTipText').style.width = (br * FactorVersnel(stap / stappen)) + 'px';
		if (stap < stappen)
		{
			setTimeout('Verbreed(' + stap + ',' + br + ',' + nHover + ')', 15);
		}
}
function FactorVersnel(f){
	return (1-Math.cos(f * Math.PI / 2));
}
function CheckMouseOut(){
	document.getElementById('ToolTip').innerHTML = '';
	document.getElementById('ToolTip').style.display = 'none';
}
function UnHover(){
	//setTimeout('CheckMouseOut()', 1000);
}
function HoverTooltip(){
	document.getElementById('ToolTip').style.display = 'block';
}
