﻿//--------------------------------------------------------------------------------
// Copyright 	2000-2001, A. Aten. All rights reserved.
// Name			bbConversion
// Description	Contains all routines needed to fill the Omzettingen form,
//			and calculate the conversion between different units.
// Referenced by		omzettingen.htm
// References		<None>
// Version			1.05
// History
//	Date		Author	Reason
//	---------------	-------	--------------------------------------------------
//  28-jun-2008 A. Aten Creation of GOverviewMapControl improved
//	30-apr-2003	A. Aten	Changed reference to form
//	14-Feb-2001	A. Aten	Created
//--------------------------------------------------------------------------------
// Global variables
var map; 
// array to hold copies of the markers to support show/hide of categories
var gmarkers = [];
// bounds is global declared to support multiple countries
var bounds = new GLatLngBounds();

////////////////////////////////////////////////////////////////////////////////
// Load data, set markers, calculate zoom area and center map
////////////////////////////////////////////////////////////////////////////////    
    function GInitialize(country, type, element) {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById( element ));
            map.addControl(new GMapTypeControl());  
            map.addControl(new GLargeMapControl());
//            alert('Controls created');
            // It is necessary to make a setCenter call of some description before adding markers
            // At this point we dont know the real values
            map.setCenter(new GLatLng(0, 0), 0);
            // Create an empty GLatLngBounds() object
            // var bounds = new GLatLngBounds();

            // Read the data from xml
            var request = GXmlHttp.create();
            var url = "/App_GMap/gmap.aspx";
            request.open("GET", url + "?c=" + country + "&t=" + type, true);
//            alert(url + "?c=" + country + "&t=" + type);
            request.onreadystatechange = function() {
//                alert(request.readyState);
                if (request.readyState == 4) {
//                alert(request.responseText);
                    var xmlDoc = GXml.parse(request.responseText);
                    // obtain the array of markers and loop through it
                    var markers = xmlDoc.documentElement.getElementsByTagName("name");
              
                    for (var i = 0; i < markers.length; i++) {
                        // obtain the attribues of each marker
                        //found = false;
                        var lat = parseFloat(markers[i].getAttribute("lat"));
                        var lng = parseFloat(markers[i].getAttribute("lng"));
                        var point = new GLatLng(lat, lng);
                        // create the marker
                        if (markers[i].getAttribute("type_id") == "773946191") {
                            image = "brewery.gif";
                            found = true;
                        }
                        else if (markers[i].getAttribute("type_id") == "-1098385486") {
                            image = "museum.gif";
                            found = true;
                        }
                        else if (markers[i].getAttribute("type_id") == "-1184127260") {
                            image = "beershop.gif";
                            found = true;
                        }
                        else if (markers[i].getAttribute("type_id") == "-1797444075") {
                            image = "supplier.gif";
                            found = true;
                        }
                        if (found) {
                            // Each time a point is added, extent the bounds to include it
                            bounds.extend(point);
                            var address = "";
                            if ( markers[i].getAttribute("address") != "" ) {
                                address += markers[i].getAttribute("address");
                            }
                            if ( markers[i].getAttribute("city") != "" ) {
                                if ( address != "" ) {
                                    address += "<br/>"
                                }
                                address += markers[i].getAttribute("city");
                            }
                            map.addOverlay(createMarker(point
                                               , markers[i].firstChild.nodeValue
                                               , address
                                               , markers[i].getAttribute("url")
                                               , image
                                               , markers[i].getAttribute("type_id")
                                           )
                            );  
                        } // if (found) {
                    } // for (var i = 0; i < markers.length; i++) {
                    // When all the points have been processed, the zoom level can be set to fit the points. 
                    map.setZoom(map.getBoundsZoomLevel(bounds));
                    //The centre can be obtained by using the bounds.getCenter() method 
                    map.setCenter(bounds.getCenter());
                    map.addControl(new GOverviewMapControl());
                } // if (request.readyState == 4) {
            } // request.onreadystatechange = function() {
            request.send(null);
        } else {
            alert("Sorry, de Google Maps API kan niet gebruikt worden met deze browser");
        } // if (GBrowserIsCompatible()) {
    } // function GInitialize(country, type) {

////////////////////////////////////////////////////////////////////////////////
// Creates a marker at the given point with the given number label
////////////////////////////////////////////////////////////////////////////////
function createMarker( point, description, address, url, image, type ) {  

    var marker = new GMarker( point, { title:description, icon:createIcon(image) } );  
    
    GEvent.addListener( marker, "click", function() {   
        var info =  "<b>" + description + "</b>";
        if ( address != "" ) {
            info += "<br/>" + address;
        }
        if ( url != "" ) {
            info += "<br/>" + "<a target='_blank' href='http://" + url + "/'>" + url + "</a>";
        }
        marker.openInfoWindowHtml( info );  
    });  
    marker.myCategory = type;
    marker.myName = description;
    gmarkers.push( marker );

    return marker;
}
////////////////////////////////////////////////////////////////////////////////
// Creates the given icon with the shadow
////////////////////////////////////////////////////////////////////////////////
function createIcon(image) {
    
    var icon = new GIcon();
    var dir = "/App_Media/images/gmap/";
    
    icon.image = dir + image;
    icon.shadow = dir + "shadow.png";
    icon.iconSize = new GSize(25, 25);
    icon.shadowSize = new GSize(38, 25);
    icon.iconAnchor = new GPoint(0, 12);
    icon.infoWindowAnchor = new GPoint(12, 12);

    return icon;
    
}

////////////////////////////////////////////////////////////////////////////////
// Shows all markers of a particular category, and ensures the checkbox is checked
////////////////////////////////////////////////////////////////////////////////
function show(category) {
    for (var i=0; i<gmarkers.length; i++) {
      if (gmarkers[i].myCategory == category) {
        gmarkers[i].show();
      }
    }
    // Check the checkbox 
    //document.getElementById(category).checked = true;
}

////////////////////////////////////////////////////////////////////////////////
// Hides all markers of a particular category, and ensures the checkbox is cleared
////////////////////////////////////////////////////////////////////////////////
function hide(category) {
    for (var i=0; i<gmarkers.length; i++) {
      if (gmarkers[i].myCategory == category) {
        gmarkers[i].hide();
      }
    }
    // Clear the checkbox
    //document.getElementById(category).checked = false;
    // Close the info window, in case its open on a marker that we just hide
    map.closeInfoWindow();
    // Rebuild the side bar
    makeSidebar();
}
////////////////////////////////////////////////////////////////////////////////
// A checkbox has been clicked
////////////////////////////////////////////////////////////////////////////////
function boxclick(box, category) {
    if (box.checked) {
      show(category);
    } else {
      hide(category);
    }
// Rebuild the side bar
    makeSidebar();
}
////////////////////////////////////////////////////////////////////////////////
// If clicked on link in sitebar, display info window and corresponding marker
////////////////////////////////////////////////////////////////////////////////
function myClick(i) {
    GEvent.trigger(gmarkers[i], "click");
}
////////////////////////////////////////////////////////////////////////////////
// Rebuilds the sidebar to match the markers currently displayed
////////////////////////////////////////////////////////////////////////////////
function makeSidebar() {
    var html = "";
    for (var i=0; i<gmarkers.length; i++) {
      if (!gmarkers[i].isHidden()) {
        html += '<a href="javascript:myClick(' + i + ')">' + gmarkers[i].myName + '</a><br>';
      }
    }
    //document.getElementById("sideBar").innerHTML = html;
}
//-->