﻿var ENTER = String.fromCharCode(13);
var TAB = String.fromCharCode(9);
var DL = String.fromCharCode(27);
var map;

var baseIcon = new GIcon(G_DEFAULT_ICON); 
 // change the properties that are different. 
 baseIcon.iconSize=new GSize(12,12); 
 baseIcon.shadowSize=new GSize(0,0); 
 baseIcon.iconAnchor=new GPoint(12,12); 
 baseIcon.infoWindowAnchor=new GPoint(16,0); 
 baseIcon.image = 'images/punt.gif';


// icon1.image = 'logo.png';
// icon1.iconSize = new GSize(32, 32);

var notfound = '';
var adressen = '';

function MaakURLUniek()
{
	var now = new Date();
	return now.getYear() + '' + now.getMonth() + '' + now.getDay() + '' + now.getHours() + '' + now.getMinutes() + '' + now.getSeconds() + '';
}

// function showAddress(map, geocoder, geoadres, label, address, zip, city, domein, email, url, image, tel)

function showMultipleAddress(map, adresblok)
{
    if (adresblok == '') { return false; }
    
    var a_adres = adresblok.split('@@@');
    var gpscoord = a_adres[a_adres.length - 1];
    // alert(gpscoord);
   	var point = new GLatLng(gpscoord.split(',')[0], gpscoord.split(',')[1]);
    if (point != null)
    {
        var label = '';
        var adres;
        for (var i = 0; i < a_adres.length - 1; i++)
        {
            adres = a_adres[i].split(TAB);
            if (label.length > 0) { label += ', '; }
            label += adres[0];
        }

		var marker = new GMarker(point, {icon: baseIcon, title: label } )

	    var html = '<div class="popup">';
	    html += '<div align=left><a href="http://www.muis.nl" target=_new><img src="images/imuis-vista-icon-24.gif" border=0 width=24></a></div>';
    	
	    for (var i=0; i < a_adres.length - 1; i++)
	    {
            adres = a_adres[i].split(TAB);

            html += "<table cellspacing=0 cellpadding=0 border=0>";
            html += '<col width=325 valign=top>';
            if (adres[7] != '') { html += '<col width=250>'; }
            html += '<tr><td>';
        	
            html += "<table width=100% cellspacing=2 cellpadding=2 border=0>";
            html += '<col width=* align=left valign=top>';
            html += '<tr><td style="white-space: nowrap;">';
            html += '<font size=2><b>' + adres[0] + '</b></font></td></tr>';
        	
            if (adres[1] != '') { html += '<tr><td>' + adres[1] + ', ' + adres[3] + ', Nederland</td></tr>'; }
            if (adres[8] != '') { html += '<tr><td>Tel: ' + adres[8] + '</td></tr>'; }
            if (adres[5] != '') { html += '<tr><td>' + adres[5] + '</td></tr>'; }
            if (adres[6] != '') { html += '<tr><td>' + adres[6] + '</td></tr>'; }
            html += '</table>';
        	
            html += '</td>';
            if (adres[7] != '') { html += '<td>' + adres[7] + '</td>'; } else { html += '&nbsp;'; }
        	
            html += '</tr></table>';
	    }
    	
    	// showAddress(map, adres[10], adres[1] + ', ' + adres[3] + ', Nederland', adres[0], adres[1], adres[2], adres[3], adres[4], adres[5], adres[6], adres[7], adres[8]);

	    html += '</div>';
		GEvent.addListener(marker, "click", function()
		{
    	    marker.openInfoWindowHtml(html, {maxWidth:250 });
		})
		map.addOverlay(marker);
    } 
}

function showAddress(map, gpscoord, geoadres, label, address, zip, city, domein, email, url, image, tel)
{
	var point = new GLatLng(gpscoord.split(',')[0], gpscoord.split(',')[1]);
    if (point != null)
    {
		var marker = new GMarker(point, {icon: baseIcon, title: label + ' ' + city } )
        /*
		if ((gpscoord.split(',')[0] == '52.7057623') && (gpscoord.split(',')[1] == '5.1485839'))
		{ alert('52'); alert(marker); }
		else
		{
		    return false;
		}
		*/
		GEvent.addListener(marker, "click", function()
		{
			marker.openInfoWindowHtml(formatWindow(0, label,address, zip, city, domein, email, url, image, tel),  {maxWidth:250 });
		})
		map.addOverlay(marker);
    }
}    

/*
			map.addOverlay(marker);
          // marker.openInfoWindowHtml(address);
*/

function ToonGPSOpKaart(loc, label)
{
    point = new GLatLng(loc.split(',')[0], loc.split(',')[1]);
    var marker = new GMarker(point, {icon: baseIcon, title: label } )
    map.addOverlay(marker);
	map.setCenter(point, 16, G_NORMAL_MAP);
	marker.openInfoWindowHtml(label,  {maxUrl:'', maxWidth:250 });
    
    return false;
}

function initialize()
{
  if (GBrowserIsCompatible())
  {
    map = new GMap2(document.getElementById("map_canvas"));
	// map_canvas.style.width = document.body.clientWidth - 20;
	// map_canvas.style.height = document.body.clientHeight - 20;
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.enableScrollWheelZoom();
	map.enableDoubleClickZoom();
	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(52.2,5.3), 8, G_SATELLITE_MAP);


    if (REF == 'IMUISONLINE')
    {
        var _LOCATIONS = LOCATIONS.split('@@@');
        
        var gmaps_plaats;
        var point;
        var label;
        var city;
        
        for (var i=0; i < _LOCATIONS.length; i++)
        {
            gmaps_plaats = _LOCATIONS[i].split(';');
            point = new GLatLng(gmaps_plaats[0].split(',')[0], gmaps_plaats[0].split(',')[1]);
            label = gmaps_plaats[1];
            city = gmaps_plaats[2];
            
            /*
            alert(point);
            alert(label);
            alert(city);
            */
            
            var marker = new GMarker(point, {icon: baseIcon, title: label + ' ' + city } )

            // alert(marker);
            map.addOverlay(marker);
        }
        return false;
    }

    if (REF == 'I_RADIUS')
    {
        var _LOCATIONS = LOCATIONS.split('@@@');
        
        var gmaps_plaats;
        var point;
        var label;
        var city;
        var tel;
        var pointlabel;
        
        d_adressen.innerHTML = '';
        
        for (var i=0; i < _LOCATIONS.length; i++)
        {
            gmaps_plaats = _LOCATIONS[i].split(';');
            point = new GLatLng(gmaps_plaats[0].split(',')[0], gmaps_plaats[0].split(',')[1]);
            label = gmaps_plaats[1];
            city = gmaps_plaats[2];
            tel = gmaps_plaats[3];

            pointlabel = '<br>' + label + ', ' + city + '<br>Tel:' + tel;
		    var marker = new GMarker(point, {icon: baseIcon, title: label + ', ' + city + ' Tel:' + tel } );
		    /*
		    GEvent.addListener(marker, "click", function()
		    {
			    marker.openInfoWindowHtml(pointlabel, {maxUrl:'', maxWidth:250 });
		    })
		    */
		    map.addOverlay(marker);

            if (i == 0) { ToonGPSOpKaart(gmaps_plaats[0], pointlabel); }
            d_adressen.innerHTML += '- <a href="" onclick="ToonGPSOpKaart(\'' + gmaps_plaats[0] + '\', \'' + pointlabel + '\'); return false;">' + label + ', ' + city + '</a><br>';
        }

        return false;
    }

	var geocoder = new GClientGeocoder();

	var HTTPURL = 'index.aspx?L=' + REF + '&RANDOMCODE=' + RANDOMCODE;
	var antwoord = PostRequest(HTTPURL);
	// alert(HTTPURL);
	// alert(antwoord);
	if (antwoord == '') { return false; }
	if (antwoord.indexOf(ENTER))
	{
		antwoord = antwoord.split(ENTER);
		var adres = '';
		for (var i=0; i < antwoord.length; i++)
		{
		    showMultipleAddress(map, antwoord[i]);
		    
		    /*
			adres = antwoord[i].split(TAB);
			if (adres != '')
			{
			alert(adres);
			showAddress(map, adres[10], adres[1] + ', ' + adres[3] + ', Nederland', adres[0], adres[1], adres[2], adres[3], adres[4], adres[5], adres[6], adres[7], adres[8]);
			// showAddress(map, geocoder, 'tekst' + ', ' + 'tekst', 'tekst', 'tekst', 'tekst', 'tekst', 'tekst', 'tekst', 'tekst', 'tekst', 'tekst');
			// alert(adres);
			// aantaltotaal += 1;
			// adressen += '\n' + adres;
			}
			*/
		}
	}

/*
    var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        for (var i = 0; i < 10; i++) {
          var latlng = new GLatLng(southWest.lat() + latSpan * Math.random(),
                                  southWest.lng() + lngSpan * Math.random());
          map.addOverlay(new GMarker(latlng));
        }
*/
        
	// copyclipboard(adressen);
	
	if (notfound != '') { alert(notfound); }
  }
}

function copyclipboard(waarde)
{
	if (window.clipboardData)
	{
		window.clipboardData.setData('Text', waarde);
	}
}

function formatWindow(volgnr, label, address, zip, city, domein, email, url, image, tel)
{
	var html = '<div class="popup">';
	html += '<div align=left><a href="http://www.muis.nl" target=_new><img src="images/imuis-vista-icon-24.gif" border=0 width=24></a></div>';
	
	for (var i=0; i < label.split('@@@'); i++)
	{
	    if (label.split('@@@')[i] != '')
	    {
	        html += "<table cellspacing=0 cellpadding=0 border=0>";
	        html += '<col width=325 valign=top>';
	        if (image.split('@@@')[i] != '') { html += '<col width=250>'; }
	        html += '<tr><td>';
        	
	        html += "<table width=100% cellspacing=2 cellpadding=2 border=0>";
	        html += '<col width=* align=left valign=top>';
	        html += '<tr><td style="white-space: nowrap;">';
	        html += '<font size=2><b>' + label.split('@@@')[i] + '</b></font></td></tr>';
        	
	        if (address.split('@@@')[i] != '') { html += '<tr><td>' + address.split('@@@')[i] + '</td></tr>'; }
	        if ((zip.split('@@@')[i] != '') && (city.split('@@@')[i] != '')) { html += '<tr><td>' + zip.split('@@@')[i] + '&nbsp;&nbsp;<b>' + city.split('@@@')[i] + '</b></td></tr>'; }
	        if (tel.split('@@@')[i] != '') { html += '<tr><td>Tel: ' + tel.split('@@@')[i] + '</td></tr>'; }
	        if (email.split('@@@')[i] != '') { html += '<tr><td>' + email.split('@@@')[i] + '</td></tr>'; }
	        if (url.split('@@@')[i] != '') { html += '<tr><td>' + url.split('@@@')[i] + '</td></tr>'; }
	        /*
	        html += '<tr><td>Inlognaam:</td><td><input type="text" name="username" class="inputborder" onfocus="this.className = \'geel\';" onblur="this.className = \'wit\';" onkeydown="if (window.event.keyCode == 13) { password.focus(); password.value += \'\'; }" style="width: 195px; height: 15px; border: solid 1px #C0C0C0;"></td></tr>';
	        html += '<tr><td>Wachtwoord:</td><td><input type="password" name="password" class="inputborder" onfocus="this.className = \'geel\';" onblur="this.className = \'wit\';" onkeydown="if (window.event.keyCode == 13) { knop_inloggen.click(); }" style="width: 195px; height: 15px; border: solid 1px #C0C0C0; font-family: Arial, Helvetica, sans-serif;"></td></tr>';
	        html += '<tr><td>&nbsp;</td><td><input type="button" name="knop_inloggen" onclick="Inloggen(\'' + domein + '\', username.value, password.value);" value="Inloggen iMUIS Online" style="background-image=url(\'knop_200_17.gif\'); width: 200px; height: 17px; background-repeat: no-repeat;"></td></tr>';
	        */
	        html += '</table>';
        	
	        html += '</td>';
	        if (image.split('@@@')[i] != '') { html += '<td>' + image.split('@@@')[i] + '</td>'; } else { html += '&nbsp;'; }
        	
	        html += '</tr></table>';
	    }
	}
	
	html += '</div>';
	return html;
}