﻿var IdahoContact =
{
    toggleState: 1,
    map: null,
    container: null,
    button: null,
    cancel: null,
    textBox: null,
    toggle: null,
    marker: null,
    latLng: null,
    parkingLatLng: null,
    dirPanel: null,
    siteMessage: null,

    Init: function() {

        this.PreInit();

        var map_canvas = document.getElementById("map-canvas");

        if (map_canvas) {

            //$(map_canvas).height(200).width(462).css("margin-bottom", "10px");

            map = new GMap2(map_canvas);
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());

            var latitude = document.getElementById("latitude");
            var longitude = document.getElementById("longitude");
            var pLatitude = document.getElementById("parkingLat");
            var pLongitude = document.getElementById("parkingLng");

            latLng = new GLatLng(latitude.value, longitude.value);
            parkingLatLng = new GLatLng(pLatitude.value, pLongitude.value);

            var icon = new GIcon(G_DEFAULT_ICON);
            icon.image = "/resources/img/marker.png";

            marker = new GMarker(latLng, { icon: icon, clickable: false });            
            
            map.addOverlay(marker);
            map.addControl(new IdahoContact.ToggleDirectionsMarker());

            map.setCenter(latLng, 13);

            //Create Directions Window Panel
            dirPanel = document.createElement("div");
            dirPanel.className = "directions-panel";

            $(map_canvas).parent().after(dirPanel);

            directions = new GDirections(map, dirPanel);

            //Build Modal Window Content
            container = document.createElement("div");
            container.className = "modal-container";

            var bg = document.createElement("div");
            bg.className = "modal-bg";

            var htmlBlock = document.createElement("div");
            htmlBlock.className = "modal-content";

            var header = document.createElement("img");
            header.className = "modal-header";
            header.src = "/resources/img/header/get_directions_header.gif";

            siteMessage = document.createElement("span");
            siteMessage.className = "modal-site-message";
            siteMessage.innerHTML = "Postcode not found";

            var label = document.createElement("label");
            label.setAttribute("for", "postcode-textbox");
            label.className = "modal-label";
            label.innerHTML = "Enter your postcode:";

            textBox = document.createElement("input");
            textBox.setAttribute("id", "postcode-textbox");
            textBox.className = "modal-postcode-textbox";
            textBox.type = "text";

            button = document.createElement("input");
            button.type = "image";
            button.className = "modal-submit";
            button.src = "/resources/img/buttons/submit-location.gif";
            button.value = "Get Directions";

            cancel = document.createElement("input");
            cancel.type = "image";
            cancel.className = "modal-close";
            cancel.src = "/resources/img/buttons/close-modal.gif";
            cancel.value = "Cancel";


            $(htmlBlock).append(header);
            $(htmlBlock).append(siteMessage);
            $(htmlBlock).append(label);
            $(htmlBlock).append(textBox);
            $(htmlBlock).append(button);
            $(htmlBlock).append(cancel);

            $(container).append(bg);
            $(container).append(htmlBlock);
        }
    },

    ToggleDirectionsMarker: function() {
    },

    PreInit: function() {

        this.ToggleDirectionsMarker.prototype = new GControl();

        this.ToggleDirectionsMarker.prototype.initialize = function(map) {

            toggle = document.createElement("img");
            toggle.alt = "Toggle Directions";
            toggle.src = "/resources/img/buttons/directions.gif";

            $(toggle).click(function() {

                IdahoContact.PerformAction();

            });

            map.getContainer().appendChild(toggle);

            return toggle;
        }

        this.ToggleDirectionsMarker.prototype.getDefaultPosition = function() {
            return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(213, 7));
        }
    },

    PerformAction: function() {
        if (IdahoContact.toggleState == 1) {

            $(button).click(function(event) {

                event.preventDefault();

                $.ajax({
                    async: false,
                    type: "POST",
                    url: "/resources/services/LocationServices.asmx/GetLatLongFromPostcode",
                    data: "{'postcode': '" + textBox.value + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        if (msg.d.Latitude != 0 && msg.d.Longitude != 0) {
                            $(container).remove();
                            $(map.getContainer()).animate({ height: 400 }, 1000, "swing", function() {
                                directions.load("from: " + msg.d.Latitude + ", " + msg.d.Longitude + " to: " + parkingLatLng.lat() + ", " + parkingLatLng.lng());
                            });
                            $(siteMessage).css("display", "none");
                        }
                        else {
                            $(siteMessage).css("display", "block");
                        }
                    },
                    error: function() {
                        $(siteMessage).css("display", "block");
                    }
                });

                GEvent.addListener(directions, "load", function() {

                    $(dirPanel).css("display", "block");
                    $(dirPanel).animate(1000, "swing");
                    map.checkResize();

                    map.closeInfoWindow();
                    map.removeOverlay(marker);

                    toggle.src = "/resources/img/buttons/reset.gif";
                    IdahoContact.toggleState = 0;

                });

                GEvent.addListener(directions, "error", function() {
                    $(siteMessage).css("display", "block");
                });
            });

            $(cancel).click(function() {

                $(container).remove();
                toggle.src = "/resources/img/buttons/directions.gif";
                IdahoContact.toggleState = 1;

            });

            $(map.getContainer()).parent().append(container);

            $(textBox).focus();

        }
        else {

            map.closeInfoWindow();

            $(dirPanel).css("display", "none");

            directions.clear();

            map.addOverlay(marker);

            toggle.src = "/resources/img/buttons/directions.gif";
            IdahoContact.toggleState = 1;

            $(map.getContainer()).animate({ height: 200 }, 1000, "swing", function() {

                map.checkResize();
                map.setCenter(latLng, 13);

            });
        }
    }
}

$(document).ready(function() {
    IdahoContact.Init();
});