function createMarkerCity(point, tekst, tekst2, titelke, iconneke)
{
 // Create a lettered icon for this point using our icon class
    var marker = new GMarker(point, iconneke);
    markerListeners.push(
     GEvent.addListener(marker, 'click', 
            function() {
                map.mymap.movedByOther = true;
                map.mymap.zoomByOther = true;
                // marker.openInfoWindowHtml(tekst);
				var infoTabs = [
	          new GInfoWindowTab("Info", tekst), 
			  new GInfoWindowTab("Kortingen", tekst2)];
				marker.openInfoWindowTabsHtml(infoTabs);
            }

        )
 )
 return marker;
}

function createMarkerItem(point, tekst, titelke, iconneke) 
{
 // Create a lettered icon for this point using our icon class
 var marker = new GMarker(point, iconneke);
 markerListeners.push(
     GEvent.addListener(marker, 'click', 
            function() {
                map.mymap.movedByOther = true;
                map.mymap.zoomByOther = true;
                // marker.openInfoWindowHtml(tekst);
				var infoTabs = [
	          new GInfoWindowTab("Info", tekst), 
			  new GInfoWindowTab("Kortingen", tekst)];
				marker.openInfoWindowTabsHtml(infoTabs);
				// new GInfoWindowTab("Foto's", header+fotos+footer), 
            }
     )
 )
 return marker;
}

function GetMarkerData(url, sendData, load_)
{
    setWaitCursor();
    var dat = new Date();
    if(WriteLog)
        GLog.write('start GetMarkerDate - ' + dat.getHours() + ':' + dat.getMinutes() + ':' + dat.getSeconds() + ':' + dat.getMilliseconds());
    if(load_)
    {
        map.mymap.movedByOther = true;
        map.mymap.zoomByOther = true;
    }
    request = GXmlHttp.create();
    request.open('POST', url, true);
    request.send(sendData);

    request.onreadystatechange = function() 
    {
        if (request.readyState == 4) 
        {
            if(request.status == 200)
            {
                var oXmlDoc = request.responseXML;

                var markers = oXmlDoc.documentElement.getElementsByTagName('marker');
                var dat = new Date();
                if(WriteLog)
                    GLog.write('start Received Markers - ' + dat.getHours() + ':' + dat.getMinutes() + ':' + dat.getSeconds() + ':' + dat.getMilliseconds());
                CreateMarkers(markers, load_);                                                         
            }
            else
                alert('the server did not respond. (errorcode ' + request.status + ')');

            ClearCursor();
        }
    };
}

function CreateMarkers(markers, load)
{
    var dat = new Date();
    if(WriteLog)
            GLog.write('start CreateMarkers - ' + dat.getHours() + ':' + dat.getMinutes() + ':' + dat.getSeconds() + ':' + dat.getMilliseconds());
    var bounds = new GLatLngBounds(); 

    for(i=0;i<markers.length;i++)
    {    
        var title, content, lat, lng, imageUrl, icon, zookurl, zookid;                                
        for(j=0;j<markers[i].childNodes.length;j++)
        {                 
            switch(markers[i].childNodes[j].nodeName)
            {
                case 'title':
                    title = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
                case 'content':
                    content = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
				case 'content2':
                    content2 = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
                case 'img':
                    imageUrl = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
                case 'markericon':
                    icon = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
                case 'zookurl':
                    zookurl = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
                case 'lat':
                    lat = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
                case 'lng':
                    lng = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
                case 'zookid':
                    zookid = GetNodeValue(markers[i].childNodes[j].firstChild);
                    break;
            }
        }

        var point = new GLatLng(parseFloat(lat),parseFloat(lng), true);

        bounds.extend(point);

        text = "<table class='infoWindowTable'>";
        text = text + "<tr>";
        text = text + "<td colspan='2'>";
        text = text + "<h2><a target='_blank' href='" + zookurl + "'>" + title + "<\/a><\/h2>";
        text = text + "<\/td>";
        text = text + "<\/tr>";
        text = text + "<tr>";
        text = text + "<td rowspan='2'>";
        text = text + "<a target='_blank' href='" + zookurl + "'><img height='90' width='120' src='" + imageUrl + "' \/><\/a>";
        
        if(typeof(SetLastMinute) != "undefined")
        {
            text = text + SetLastMinute();
            text = text + "<br />";
        }
        text = text + "<\/td>";
        text = text + "<td>" + content + "<\/td>";
        text = text + "<\/tr>";
        text = text + "<tr>";
        text = text + "<td>";
        text = text + "<a target='_blank' href='" + zookurl + "'>" + lookAtAd + "<\/a>";
        text = text + "<\/td>";
        text = text + "<\/tr>";
        text = text + "<\/table>";
		
		text2 = "<table class='infoWindowTable'>";
        text2 = text2 + "<tr>";
        text2 = text2 + "<td>";
        text2 = text2 + "<h2><a target='_blank' href='" + zookurl + "'>" + title + "<\/a><\/h2>";
        text2 = text2 + "<\/td>";
        text2 = text2 + "<\/tr>";
        text2 = text2 + "<tr>";
        text2 = text2 + "<td style='padding-bottom:5px;text-decoration:none;font-family:arial;font-size:12px;color:#fe9900;font-weight:bold;'>" + content2 + "<\/td>";
        text2 = text2 + "<\/tr>";
        text2 = text2 + "<tr>";
        text2 = text2 + "<td>";
        text2 = text2 + "<a target='_blank' href='" + zookurl + "'>" + lookAtAd + "<\/a>";
        text2 = text2 + "<\/td>";
        text2 = text2 + "<\/tr>";
        text2 = text2 + "<\/table>";

        marker = createMarkerCity(point, text, text2, title, eval(icon));
        marker.zookId = zookid;

        clusterer.AddMarker(marker, "<a target='_blank' href='" + zookurl + "'>" + title + "<\/a>");
    }
    if(load)
    {
        if(typeof(setbounds) != "undefined" && typeof(NewBounds) != undefined)
        {
            NewBounds();
        }
        else
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
        
        //Hidden fields opvullen met coördinaten
        SetMapPoints(map);
    }

    var dat = new Date();
    if(WriteLog)
            GLog.write('end CreateMarkers - ' + dat.getHours() + ':' + dat.getMinutes() + ':' + dat.getSeconds() + ':' + dat.getMilliseconds());
}
function ReloadParent(url)
{
    window.parent.location.href=url;
}
                
function setWaitCursor() {
    document.getElementById('map').className='GmapWait';
    document.body.style.cursor='wait';
    window.parent.document.body.style.cursor='wait';
} 
function ClearCursor() {
    document.getElementById('map').className='';
    document.body.style.cursor='default';
    window.parent.document.body.style.cursor='default';
}
function AbortRequest()
{
    if(typeof(request) == 'object')
        request.abort();
}
function GetNodeValue(node)
{
    if(node != null)
        return node.nodeValue;
    else 
        return '';
}


function GetCenter(gmap)
{
    return gmap.getCenter();
}
// This function adjusts the calculation for the new view point when panning East or West across
// the +/-180 degree line.  It insures the map will pan correctly across this boundary.
function aroundTheWorldLngPan(lng) {
    if (lng < -180) {
        lng = lng + 360;			
    }
    else if (lng > 180) {
        lng = lng - 360;
    }
    return lng;
}
// This function adjusts calculation of the visible longitudinal span when the view crosses the 
// +/-180 degree line.  It insures that the width of the view (in degrees) is calculated correctly.
function aroundTheWorldLngSpan(NE,SW) {
    if (SW > NE) {
        NE = NE + 360;			
    }
    return Math.abs(NE - SW);
}	
// Calculates longitudinal span of the current view
function lngSpan() {
    var bounds = map.getBounds(); // the boundary coordinates for the current view
    var southWest = bounds.getSouthWest(); // the South-West corner of the boundary
    var northEast = bounds.getNorthEast(); // the North-East corner of the boundary
    return aroundTheWorldLngSpan(northEast.lng(), southWest.lng());
}
// This ensures that the map does not scroll beyond +/- 85 degrees latitudinally.
// Using  +/- 90 degrees as a limit creates a problem.  The spherical coordinates
// become so flattened out at the extreme norhtern and southern boundaries
// it takes a lof of N-S movement to change 1 degree.
function limitNS(lat) {
    if (lat > 85) {
        lat = 85;
    }
    else if (lat < -85) {
        lat = -85;
    }
    return lat;
}
// This calculates the latitudinal span of the current view.
function latSpan() {
    var bounds = map.getBounds(); // the boundary coordinates for the current view
    var southWest = bounds.getSouthWest(); // the South-West corner of the boundary
    var northEast = bounds.getNorthEast(); // the North-East corner of the boundary
    return Math.abs(northEast.lat() - southWest.lat());		
}
function NavigateWest()
{
    //panTo = smooth but slowmotion
    map.setCenter(new GLatLng(map.getCenter().lat(), aroundTheWorldLngPan(map.getCenter().lng() - (lngSpan() * panPercentage))), map.getZoom());
}
function NavigateOost()
{
    map.setCenter(new GLatLng(map.getCenter().lat(), aroundTheWorldLngPan(map.getCenter().lng() + (lngSpan() * panPercentage))), map.getZoom());
}
function NavigateNoord()
{
    map.setCenter(new GLatLng(limitNS(map.getCenter().lat() + (latSpan() * panPercentage)), map.getCenter().lng()), map.getZoom());
}
function NavigateZuid()
{
    map.setCenter(new GLatLng(limitNS(map.getCenter().lat() - (latSpan() * panPercentage)), map.getCenter().lng()), map.getZoom());
}
function NavigateNO()
{
    map.setCenter(new GLatLng(limitNS(map.getCenter().lat() + (latSpan() * panPercentage)), aroundTheWorldLngPan(map.getCenter().lng() + (lngSpan() * panPercentage))), map.getZoom());
}
function NavigateNW()
{
    map.setCenter(new GLatLng(limitNS(map.getCenter().lat() + (latSpan() * panPercentage)), aroundTheWorldLngPan(map.getCenter().lng() - (lngSpan() * panPercentage))), map.getZoom());
}
function NavigateZO()
{
    map.setCenter(new GLatLng(limitNS(map.getCenter().lat() - (latSpan() * panPercentage)), aroundTheWorldLngPan(map.getCenter().lng() + (lngSpan() * panPercentage))), map.getZoom());
}
function NavigateZW()
{
    map.setCenter(new GLatLng(limitNS(map.getCenter().lat() - (latSpan() * panPercentage)), aroundTheWorldLngPan(map.getCenter().lng() - (lngSpan() * panPercentage))), map.getZoom());
}
function Zoom(level)
{
    if(level == '+')
    {
        map.zoomIn();
    }
    else if(level == '-')
        map.zoomOut();
}
function MoveTimeout()
{
    SetMapPoints(map);
    window.clearTimeout(SearchTimeoutId);
    SearchTimeoutId = setTimeout('OnMoveEnd()', SearchTimeout)
}

function GetCoordinates()
{
    SetMapPoints(map);
    return GetMapBounds();
}


