﻿
var url = document.URL;
var map;
var gdir;
var geocoder = null;
var addressMarker;
var companyLat;
var companyLng;
var fromAddress;
var companyID = '1';
var companyName = 'HBP Group';
var companyAddress = 'Enterprise House,<br>Woodhouse Road,<br>Scunthorpe,<br>DN16 1BD';
var companyPhone = '01724 400 300';
var companyEmail = 'email@hbp-group.co.uk';
var companyWebsite = 'www.hbpsystems.co.uk';
var companyGLatLng = "53.5744,-0.6231"
companyGLatLng = companyGLatLng.split(",")


function handleErrors() {
    processing();
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
        alert("We couldn't find your exact address \"" + fromAddress + "\". This may be because the address you specified is relatively new, incorrect, or you may have to be more specific.\n\nPlease try again.\n\nIf this problem persists, please contact HBP on 01724 400 300");
        resetMap();
        return;
    }
    if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
        alert("Your directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code + "\n\nPlease try again, putting either postcode or street and town in to the directions box");
        resetMap();
        return;
    }
    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);
        resetMap();
        return;
    }
    if (gdir.getStatus().code == G_GEO_BAD_KEY) {
        alert("We could not process your query at this present time.\n\n Please contact HBP on 01724 400 300 with the following error:\n\n Error code: " + gdir.getStatus().code + " - Map Key Invalid");
        resetMap();
        return;
    }
    if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
        resetMap();
        return;
    }
    else {
        alert("Please check your address and try again.");
        resetMap();
        return;
    }

}

function onGDirectionsLoad() {
    var directionsContainer = document.getElementById("directions")
    directionsContainer.style.display = "block";
}
function processing() {

}

function showPrinterFriendlyDirections() {
    window.open("printer_friendly_directions.aspx", "printerwindow", "status=0, toolbar=0, location=0, menubar=0, directories=0, scrollbars=yes, resizable=0, height=550, width=650");
}

function SetupDefaultMap() {
    if (GBrowserIsCompatible()) {

        companyLat = parseFloat(companyGLatLng[0]);
        companyLng = parseFloat(companyGLatLng[1]);
        var center = new GLatLng(companyLat, companyLng);
        map = new GMap2(document.getElementById("map"));
        gdir = new GDirections(map, document.getElementById("directions"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GOverviewMapControl(new GSize(100, 100)));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();
        map.setCenter(center, 13);
        map.setMapType(G_NORMAL_MAP);

        var infoBubble = "<div id='map_marker'><img src='../images/logos/" + companyName.replace(/ /g, "-") + "-logo.jpg' class='map_logo'/><h2 class='map_title'>" + companyName + "</h1>" + companyAddress.replace(/<br>/g, " ") + "<br /><br /><span class='map_links'><a href='mailto:" + companyEmail + "' title='Send Email to " + companyName + " Now' class='map_text_link'>" + companyEmail + "</a><br /><a href='http://" + companyWebsite + "' title='" + companyName + " Website' class='map_text_link'>" + companyWebsite + "</a></span></span></div>"

        var markerOptions = { title: companyName };
        var locationMarker = new GMarker(center, markerOptions);
        map.addOverlay(locationMarker);

        GEvent.addListener(locationMarker, "click", function() {
            this.openInfoWindowHtml(infoBubble);
        }
        )
    }

    else {
        document.getElementById("google_map_container").style.display = "none";
        document.getElementById("map_image").style.display = "block";
    }
}

function resetMap() {
    SetupDefaultMap();
}

function submitDirections(e, directionsInput) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    if (keycode == 13) {
        fromAddress = directionsInput.value;
        setDirections(fromAddress)
        return true;
    }
    else {
        return true;
    }
}

function setDirections(fromAddress) {
    processing();

    //var fromAddress = document.getElementById("fromAddress").value;
    if (fromAddress == "") {
        alert("Please enter either a postcode, or a street and town and try again.");
        resetMap();
        return;
    }
    var toAddress = companyName + ", " + companyAddress.replace(/<br>/g, " ") + " @ " + companyLat + ", " + companyLng;
    var locale = "en_UK";

    map.clearOverlays();
    document.getElementById("directions").innerHTML = "";
    gdir.load("from: " + fromAddress + " to: " + toAddress);
}
