﻿var map = null;

$(document).ready(function() {
    VEShape.prototype.Type = "";
    VEShape.prototype.Postcode = "";
    VEShape.prototype.Options = {};

    map = new VEMap("widemap");

    map.messageShow = function(message) {
        if ($("#widemap .message").size() == 0)
            $("#widemap").append('<div class="message"></div>');
        $("#widemap .message").html(message).show();
    }

    map.messageHide = function() {
        $("#widemap .message").hide();
    }

    //map.SetDashboardSize(VEDashboardSize.Small);
    map.LoadMap(new VELatLong(-28.38173504322308, 134.296875), 4);
    VEMapOptions.EnableBirdseye = false;

    //Hide the birds eye view field
    $('#MSVE_navAction_ObliqueMapView').hide();

    //Change the map to KMs
    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);

    map.messageShow("Loading Panel Data...");

    // Hash of state layers
    map._states = {};
    // Get or create a state layer
    map.getLayer = function(state, type) {
        // Create the state level cache if it doesn't exist
        if (!map._states[state]) {
            map._states[state] = {};
            $("#state").append($('<option value="' + state + '">' + state + '</option>'));
        }
        if (map._states[state][type]) return map._states[state][type];
        var layer = new VEShapeLayer();
        layer.Hide();
        this.AddShapeLayer(layer);
        this._states[state][type] = layer;
        return layer;
    }

    map.update = function() {
        // Set show flags on visible layers
        var t = this.getType();
        var s = this.getState();
        for (state in this._states)
            for (type in this._states[state])
            for (var i = 0; t.length > i; i++)
            if ((s == "" || s == state) && type.indexOf(t.charAt(i)) != -1)
            this._states[state][type].show = true;

        // Show/Hide the layers and delete the show flags.
        for (state in this._states)
            for (type in this._states[state])
            if (this._states[state][type].show) {
            this._states[state][type].Show();
            delete this._states[state][type].show;
        }
        else
            this._states[state][type].Hide();

        this.zoomToVisible();
        this.messageHide();
    }

    map.zoomToVisible = function(zoom) {
        if (!zoom && this.getNoZoom()) return;
        var points = [];
        for (state in this._states)
            for (type in this._states[state]) {
            var layer = this._states[state][type];
            var shapeCount = layer.GetShapeCount();
            if (layer.IsVisible())
                for (var i = 0; i < shapeCount; i++)
                points.push(layer.GetShapeByIndex(i).GetPoints()[0]);
        }
        if (points.length) this.SetMapView(points);
    }

    map.find = function(place) {
        this.Find(null, place, null, null, 0, 1, false, false, true, false, this.findResult);
    }
    map.findResult = function(shapeLayer, findResults, places, moreResults, error) {
        if (error) {
            map.ShowMessage(error);
            return;
        }
        //$("#state").each(function() { this.selectedIndex = 0; });
        $("#zoom").attr("checked", false);
        map.SetCenterAndZoom(places[0].LatLong, 14);
        map.update();
    }

    map.log = function(message) {
        $("#log").append("<p>" + message + "</p>");
    }

    // Hash of custom pins
    map._pins = {};
    // Get or create custom pin
    map.getPin = function(code) {
        if (map._pins[code]) return map._pins[code];
        var pin = new VECustomIconSpecification();
        pin.TextContent = " ";
        pin.Image = "/common/images/pin" + code + ".png";
        map._pins[code] = pin;
        return pin;
    }

    map.getType = function() {
        var type = "";

        if ($("#roadside").attr("checked") && !($("#large").attr("checked") || $("#airport").attr("checked") || $("#poster").attr("checked"))) type += "R";
        if ($("#roadside").attr("checked") && $("#large").attr("checked")) type += "L";
        if ($("#roadside").attr("checked") && $("#airport").attr("checked")) type += "A";
        if ($("#roadside").attr("checked") && $("#poster").attr("checked")) type += "P";

        if ($("#rail").attr("checked") && !($("#surround").attr("checked") || $("#crosstrack").attr("checked"))) type += "T";
        if ($("#rail").attr("checked") && $("#surround").attr("checked")) type += "S";
        if ($("#rail").attr("checked") && $("#crosstrack").attr("checked")) type += "C";

        if ($("#bus").attr("checked")) type += "B";

        return type;
    }

    map.getState = function() {
        return $("#state").val();
    }

    map.getNoZoom = function() {
        return !($("#zoom").attr("checked"));
    }

    map.AttachEvent("onclick", function(mapEvent) {
        var pin = map.GetShapeByID(mapEvent.elementID)
        if (pin == null) return;
        switch (pin.LocationCode) {
            case "R":
                document.location = '/Products/Formats/Detail.aspx?sizecode=' + pin.SizeCode;
                break;
            case "T":
            case "B":
                document.location = '/Products/Formats/Default.aspx?locationcode=' + pin.LocationCode;
                break;
        }
    });

    // Load panel data
    $.getJSON('/Products/PanelDataAll.ashx', function(data) {
        var panel = null;
        var description = null;
        for (state in data)
            for (var i = 0; i < data[state].length; i++) {
            //if (i>10) break;
            panel = data[state][i];
            var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(panel[0], panel[1]))
            pin.LocationCode = panel[2];
            pin.SizeCode = panel[3];
            pin.SetTitle(panel[4]);
            pin.Options = panel[5];
            pin.Type = pin.LocationCode
                    + ((pin.Options.large) ? "L" : "")
                    + ((pin.Options.pos) ? "P" : "")
                    + ((pin.Options.air) ? "A" : "")
                    + ((pin.Options.sur) ? "S" : "")
                    + ((pin.Options.cro) ? "C" : "");
            pin.SetCustomIcon(map.getPin(pin.Type));
            map.getLayer(state, pin.Type).AddShape(pin);
        }

        //map.update();
        map.messageShow("Select a state or search for a postcode/suburb to begin");
    });

    $("#widemapheader input:checkbox").click(function() {
        map.update();
    });
    $("#roadside").click(function() {
        $("#large").attr("disabled", !this.checked);
        $("#poster").attr("disabled", !this.checked);
        $("#airport").attr("disabled", !this.checked);
    });
    $("#rail").click(function() {
        $("#surround").attr("disabled", !this.checked);
        $("#crosstrack").attr("disabled", !this.checked);
    });

    $("#state").change(function() {
        $("#zoom").attr("checked", true);
        map.update();
    }).change();

    $("#go").click(function() {
        var location = map.getState();
        location = ((location != "")?", ":"") + location + ", Australia";
        map.find($("#find").attr("value") + location);
    });
    $("#find").keypress(function(e) {
        if (e.keyCode == 13) {
            $("#go").click();
            return false;
        }
    });
});
