﻿/////////////////////
// Load the maplet //
/////////////////////
function jWDLoad() {
    if (GBrowserIsCompatible()) {
        map = new google.maps.Map2(document.getElementById("map"));
        gdir = new GDirections(map, document.getElementById("directions"));

        // Add controls
        map.addControl(new google.maps.LargeMapControl());
        map.addControl(new google.maps.MapTypeControl());

        // Set error handler
        google.maps.Event.addListener(gdir, "error", jWDHandleErrors);

        // Set center
        jWDGoToSite(document.getElementById("ddlAddress"), false);
    }
} // jWDLoad()

//////////////////
// Set location //
//////////////////
function jWDGoToSite(sender, hideDirections) {
    // Make sure we have a valid object
    if (!sender) sender = document.getElementById("ddlAddress");

    if (sender) {
        if (hideDirections) jWDHideDirections();

        map.setCenter(new google.maps.LatLng(locations[sender.selectedIndex][0], locations[sender.selectedIndex][1]), 14);

        var point = new GLatLng(locations[sender.selectedIndex][0],
                                  locations[sender.selectedIndex][1]);

        map.clearOverlays();
        map.addOverlay(new GMarker(point));
        map.openInfoWindow(map.getCenter(), locations[sender.selectedIndex][7]);

        jWDSetAddressDetails(locations[sender.selectedIndex]);
    }
} // jWDGoToSite()

/////////////////////////
// Set address details //
/////////////////////////
function jWDSetAddressDetails(address) {

    document.getElementById("addressValue").firstChild.nodeValue = address[2];
    document.getElementById("streetValue").firstChild.nodeValue = address[3];
    document.getElementById("zipCityValue").firstChild.nodeValue = address[4];
    document.getElementById("phoneValue").firstChild.nodeValue = address[5];
    document.getElementById("faxValue").firstChild.nodeValue = address[6];

} // jWDSetAddressDetails()

//////////////////////////
// Populate info bubble //
//////////////////////////
function jWDCreateInfoContent(address1, address2, address3, src) {

    var panel = document.createElement("div");
    var classAttribute = document.createAttribute("class");

    classAttribute.nodeValue = "Textnormal";
    panel.setAttributeNode(classAttribute);

    var imageNode = document.createElement("img");
    var srcAttribute = document.createAttribute("src");

    srcAttribute.nodeValue = src;
    imageNode.setAttributeNode(srcAttribute);

    panel.appendChild(imageNode);
    panel.appendChild(document.createElement("br"));
    panel.appendChild(document.createTextNode(address1));
    panel.appendChild(document.createElement("br"));
    panel.appendChild(document.createTextNode(address2));
    panel.appendChild(document.createElement("br"));
    panel.appendChild(document.createTextNode(address3));
    return panel;
} // jWDCreateInfoContent()

/////////////////////////////
// Handle directions error //
/////////////////////////////
function jWDHandleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
        jWDHideDirections();
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_BAD_KEY) {
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    }
    else {
        alert("An unknown error occurred.");
    }
} // jWDHandleErrors()

////////////////////
// Set directions //
////////////////////
function jWDSetDirections() {

    var startAddress = "";
    var startZip = document.getElementById("txtStartZip").value;
    var startCity = document.getElementById("txtStartCity").value;
    var startStreet = document.getElementById("txtStartStreet").value;
    var selIndex = document.getElementsByName("ddlAddress")[0].selectedIndex;
    var toAddress = locations[selIndex][3] + ", " + locations[selIndex][4] + ", CH";

    // Make sure the user provided some data
    if (startZip == "" && startCity == "" && startStreet == "") return;

    if (startStreet.length > 0) startAddress += (startStreet + ", ");
    if (startZip.length > 0) startAddress += (startZip + " ");
    if (startCity.length > 0) startAddress += startCity;

    gdir.load("from: " + startAddress + " to: " + toAddress, { "locale": "DE_ch" });

    jWDShowDirections();
} // jWDSetDirections()

////////////////////////////
// Display directions bar //
////////////////////////////
function jWDShowDirections() {
    window.resizeTo(820, 821);
    document.getElementById("directions").style.display = "block";
    document.getElementById("mapView").style.width = "720px";
}

/////////////////////////
// Hide directions bar //
/////////////////////////
function jWDHideDirections() {
    document.getElementById("directions").style.display = "none";
    document.getElementById("mapView").style.width = "500px";
    window.resizeTo(550, 821);
}

///////////////////////////
// Print window contents //
///////////////////////////
function jWDPrint() {
    window.print();
}

//////////////////
// Close pop-up //
//////////////////
function jWDClose() {
    window.close();
}
