﻿/// <reference path="jquery-1.2.6-vsdoc.js" />

window.onresize = function() { adjustLegends(); }


function adjustLegends() {
    if ($(".veMap").css("left") != undefined) {
        $("#myControl").css("left", parseInt($(".veMap").position().left + ($(".veMap").width() - $("#myControl").width()) - 5) + 'px');

       $("#myMapLayerControl").css("left", parseInt($(".veMap").position().left + ($(".veMap").width() - $("#myMapLayerControl").width()) -5 ) + 'px');

   }

}

function infoOver() {
    $(".div-legend").css("opacity", "1");
}
function infoOut() {
     $(".div-legend").css("opacity", "0.8");
}

function togglePolygon(areaName, type) {
    var map = getIDMap();
    
    var l = map.GetShapeLayerByIndex(1);
    for (var i = 0; i < l.GetShapeCount(); i++) {
        var shape = l.GetShapeByIndex(i);
        var shapearea = shape.GetTitle();

        if (shapearea == areaName) {

            if (type == 'show') {
                shape.SetLineColor(new VEColor(cntstR, cntstG, cntstB, 1.0));
                shape.SetLineWidth(4);
                shape.SetZIndex(null, 99999);
            } else {
            shape.SetLineColor(new VEColor(lineR, lineG, lineB, 1));
                shape.SetLineWidth(1);
                shape.SetZIndex(null, 101);
            }
            //i = l.GetShapeCount();
            highlightLegend(shape.GetFillColor(), type);
        }
    }

}


function highlightLegend(color, type) {
    var cellid = 'c_' + color.R + color.G + color.B;
    fillColor = null;
    var cell = document.getElementById(cellid);
    if (cell != null) {

        if (type == 'show') {
            $('#' + cellid).addClass("highlight");
        } else {

            $('#' + cellid).removeClass("highlight");
        }

    }

}


var infoControl = null;

function showMe(e) {
    
    if (e.elementID != null) {
        
        var map = getIDMap();

        var shape = map.GetShapeByID(e.elementID);


//        var doit = false;
//        if (shape.GetType() == "Polyline") {
//            var l = shape.GetShapeLayer();
//            var layertitle = l.GetTitle();
//            if (layertitle != "main_polyline") {
//                shape.SetLineColor(new VEColor(cntstR, cntstG, cntstB, 1.0));
//                shape.SetLineWidth(4);
//                shape.SetZIndex(null, 99999);
//            }
//        }

        if (shape.GetType() != "Polyline") {


            var l = shape.GetShapeLayer();
            var HolesCount = 0;
            for (var i = 0; i < l.GetShapeCount(); i++) {
                var shape2 = l.GetShapeByIndex(i);
                if (shape2.GetTitle() == shape.GetTitle()) {
                    shape2.SetLineColor(new VEColor(cntstR, cntstG, cntstB, 1.0));
                    shape2.SetLineWidth(4);
                    HolesCount += 1;
                    shape2.SetZIndex(null, 99999);
                }
            }
            if (HolesCount >= 1)
                shape.SetZIndex(null, 99998);
        

            if ($("#myDataControl").is(":visible") && $("#ShapeID").html() == e.elementID) {
            
            }
            else {
                shape.SetLineColor(new VEColor(cntstR, cntstG, cntstB, 1.0));
                shape.SetLineWidth(4);
                shape.SetZIndex(null, 99999);
            }

            //shape.Redraw(true, true, true, true);
            if ($.getQueryString({ id: "pg" }) != 6240) {
            $("td:contains('" + shape.GetTitle() + "')").parent().addClass("highlight");
            }
            
            highlightLegend(shape.GetFillColor(), 'show');
            
            // Add info control
            if (shape.GetDescription() != '' && $.getQueryString({ id: "pg" }) != 6240) {
                if (infoControl == null) {
                    var veE = $find('veextender');
                    infoControl = document.createElement("div");
                    infoControl.id = "myInfoControl";
                    infoControl.style.top = 2 + "px";

                    if ($('#myMapLayerControl').is(':visible')) {
                        infoControl.style.top = parseInt(infoControl.style.top) + $('#myMapLayerControl').height() + 5 + "px";
                    }

                    if ($('#myControl').is(':visible')) {
                        infoControl.style.top = parseInt(infoControl.style.top) + $('#myControl').height() + 5 + "px";
                        
                    }

                    infoControl.style.left = (veE.get_width() - 195) + "px";

                    map.AddControl(infoControl);
                    $(infoControl).addClass("div-infobox");
                    infoControl.style.visibility = "hidden";
                }
                if (infoControl != null && shape.GetDescription() != '') {
                    infoControl.innerHTML = shape.GetDescription();
                    infoControl.style.visibility = "visible";
                }
            }

            if ($.getQueryString({ id: "pg" }) == 6240) {
                map.HideInfoBox();
                

                    var loc = map.PixelToLatLong(new VEPixel(e.mapX, e.mapY));
                    if (!isNaN(e.mapX) && !isNaN(e.mapY)) {
                        
                        map.ShowInfoBox(shape);
                        if (shape.GetTitle() + "<br />" == shape.GetDescription()) {
                            $(".VE_Pushpin_Popup_Body").css("display", "none");
                        }

                    }

            }

        }

    }

}
function hideMe(e) {
    
    if (e.elementID != null) {

        var map = getIDMap();

        var shape = map.GetShapeByID(e.elementID);

        if (shape.GetType() != "Polyline") {

            var l = shape.GetShapeLayer();
            
            for (var i = 0; i < l.GetShapeCount(); i++) {
                var shape2 = l.GetShapeByIndex(i);
                
                if (shape2.GetTitle() == shape.GetTitle()) {
                    shape2.SetLineColor(new VEColor(lineR, lineG, lineB, 1.0));
                    shape2.SetLineWidth(1);
                    shape2.SetZIndex(null, 101);
                }
            }
        
        
            if ($("#myDataControl").is(":visible") && $("#ShapeID").html() == e.elementID) {

            }
            else {
            shape.SetLineColor(new VEColor(lineR, lineG, lineB, 1.0));
            shape.SetLineWidth(1);
            shape.SetZIndex(null, 101);
            }

            //shape.Redraw(true, true, true, true);
            $("td:contains('" + shape.GetTitle() + "')").parent().removeClass("highlight");
            highlightLegend(shape.GetFillColor(), 'hide');
            if (infoControl != null) {
                infoControl.style.visibility = "hidden";
                infoControl = null;
                $("#myInfoControl").remove();
            }
        }

    }

}

var dataControl = null;

function showData(e) {
    if (panned == 0) {
        if ($.getQuerystring({ id: "pg" }) == "6133") {
 
            if (e.elementID != null) {

                var map = getIDMap();

                var shape = map.GetShapeByID(e.elementID);
                closeDataInfo();
                if (shape.GetType() != "Polyline") {

                    shape.SetLineColor(new VEColor(cntstR, cntstG, cntstB, 0.7));
                    shape.SetLineWidth(6);
                    shape.SetZIndex(null, 99999);
                    //shape.Redraw(true, true, true, true);
                    $("td:contains('" + shape.GetTitle() + "')").parent().addClass("highlight");
                    highlightLegend(shape.GetFillColor(), 'show');

                    // Add info control
                    
                    if (shape.GetDescription() != '') {
                        if (dataControl == null) {
                            var veE = $find('veextender');
                            dataControl = document.createElement("div");
                            dataControl.id = "myDataControl";
                            dataControl.style.top = 172 + "px";

                            dataControl.style.left = 2 + "px";

                            map.AddControl(dataControl);
                            $(dataControl).addClass("div-infobox");

                            //                    $(dataControl).mouseover(function() {
                            //                        $(this).css("opacity", 1);
                            //                    });
                            //                    $(dataControl).mouseout(function() {
                            //                        $(this).css("opacity", 0.8);
                            //                    });

                        }

                        if (dataControl != null && shape.GetDescription() != '') {
                            $(dataControl).show();
                            dataControl.innerHTML = "<span class=\"span-legend-title close\">Employment by Industry <img src=\"app_themes/default/images/close-white.gif\" alt=\"Close\" onclick=\" closeDataInfo()\;\" /></span>";
                            dataControl.innerHTML += "<div id=\"ShapeID\" style=\"visibility:hidden;\">" + e.elementID + "</div>";
                            dataControl.innerHTML += "<span class=\"span-destzone-num\">Destination zone: " + shape.GetTitle() + "</span>";
                            dataControl.innerHTML += "<img id=\"imgWaitIndData\" src=\"App_themes/Default/images/loading.gif\" />";

                            // Fetch the Industry data for this DZN
                            
                            var tpolyinfo = $('#table-polyinfo').find("tr:eq(0)");
                            var dnz = $(tpolyinfo[0]).find("td:eq(1)")[0].innerHTML;

                            var standard = $('#ctl00_ContentPlaceHolder1_rbIndustryYear_0');
                            if (standard[0].checked)
                                standard = "ANZSIC93";
                            else
                                standard = "ANZSIC06";

                            var clientid = $.getQuerystring({ id: "id" });

                            
                            if (dnz > 0) {
                                var service = new AJAXService();
                                service.GetIndustryDataByZone(dnz, standard, clientid, onGetIndustryDataByZoneSuccess, onGetIndustryDataByZoneFailure, null);
                            }
                        }
                    }


                }

            }
        }
    }
    panned = 0;
}
function onGetIndustryDataByZoneSuccess(e) {
    $("#imgWaitIndData").hide();
    $("#myDataControl table").remove();
    dataControl.innerHTML += e;
    $("#myDataControl table").addClass("tbl-main-destzone");
    $("#myDataControl table").append("<tfoot></tfoot>");
    $("#myDataControl td:contains('Total industries')").parent().appendTo('#myDataControl tfoot');
     
    if ($.browser.msie) {
        $("#myDataControl").append("<div class=\"div-tbl-body\"><table id=\"tbl-body\" class=\"tbl-data table-legend\"></table></div>");
        
        $("#myDataControl").append("<table id=\"tblFooter\" class=\"tbl-data table-legend\"></table>");
        $("#myDataControl .tbl-main-destzone tfoot tr").appendTo("#tblFooter");
        $("#myDataControl .tbl-main-destzone tbody").appendTo("#tbl-body");
    }

    rowRollover();
}
function onGetIndustryDataByZoneFailure(e) {
    alert("GetIndustryDataByZone Failed");
}

function closeDataInfo() {
    if (dataControl != null) {
        var map = getIDMap();

        var shape = map.GetShapeByID($("#ShapeID").html());
        
        if (shape.GetType() != "Polyline") {
            shape.SetLineColor(new VEColor(lineR, lineG, lineB, 1.0));
            shape.SetLineWidth(1);
            shape.SetZIndex(null, 101);
            }
            $(dataControl).fadeOut("fast");
                dataControl = null;
                $("#myDataControl").remove();
           
        
    }
}

function showMiniMap() {
    //Show minimap
    
    miniMapExtender = new VEToolkit.MiniMapExtender(getIDMap());
    miniMapExtender.SetVerticalAlignment(VEToolkit.MiniMapVerticalAlignment.Top);
    miniMapExtender.SetHorizontalAlignment(VEToolkit.MiniMapHorizontalAlignment.Left);
    miniMapExtender.SetMiniMapSize(VEMiniMapSize.Small);
    miniMapExtender.SetVerticalOffset(24);
    miniMapExtender.SetHorizontalOffset(28);
    



    $('#chkToggleMiniMap').attr("checked", "checked");
    miniMapExtender.Show();


    
}
function hideMiniMap() {
    miniMapExtender = new VEToolkit.MiniMapExtender(getIDMap());
    miniMapExtender.Hide();
}

function showLegend() {

    $("#myControl").css("visibility", "visible");
    $("#myMapLayerControl").css("visibility", "visible");
}
function hideLegend() {
    $("#myControl").css("visibility", "hidden");
    $("#myMapLayerControl").css("visibility", "hidden");
}

function dimPolys() {
    var map = getIDMap();
    var l = map.GetShapeLayerByIndex(1);

    for (var i = 0; i < l.GetShapeCount(); i++) {
        var shape = l.GetShapeByIndex(i);
        shape.SetLineColor(new VEColor(lineR, lineG, lineB, 1));
        shape.SetLineWidth(1);
        shape.SetZIndex(null, 101);
        $("td:contains('" + shape.GetTitle() + "')").parent().removeClass("highlight");
    }

    if (infoControl != null) {
        infoControl.style.visibility = "hidden";
    }


}

function bigMap() {
    var mapdivs = $(".veMap");
    var width = "";
    var height = "";

    width = $(window).width();
    height = $(window).height();

    width += "px";
    height += "px";

    var mapdiv = mapdivs[0];
    mapdiv.style.position = 'absolute';
    mapdiv.style.top = '40px';
    mapdiv.style.left = '10px';
    mapdiv.style.zIndex = 999999;
    //mapdiv.style.width = width;
    //mapdiv.style.height = height;
    var map = getIDMap();

    map.Resize($(window).width() - 20, $(window).height() - 50);
    map.SetZoomLevel(map.GetZoomLevel() + 2);

}

function getExportUrl() {
    var answer = confirm("Exporting the map may take some time. Click OK to continue, or Cancel to cancel this process");
    if (answer) {
        // get querystring values
        var map = getIDMap();
        var c = map.GetCenter();
        var z = map.GetZoomLevel();
        var ind = $("#ctl00_ContentPlaceHolder1_lboxIndustrySectors").val();
        var legend = $("#chkShowLegend").is(":checked");
        var overmap = $("#chkToggleMiniMap").is(":checked");
        var opac = $("input[id*='txtSlider']").val();
        if (opac == undefined) {
            opac = 7;
        }
        var indclass = $("#ctl00_ContentPlaceHolder1_rbIndustryYear_0").is(":checked");
        var mapstyle = map.GetMapStyle();
        var layers = "";
        $("#myMapLayerControl input:checked").each(function() {
            layers += $(this).attr("id").replace("chk",""); 
            layers += "~";
        });
        if (layers != "") {
            //layers = left(layers, layers.length - 1);
             
        }

        // get the base url
        var url = $("input[id*='txtBaseExportUrl']").val();

        // construct the requesting url
        url = url.replace("[lat]", c.Latitude);
        url = url.replace("[lon]", c.Longitude);
        url = url.replace("[zoom]", z);
        url = url.replace("[industry]", ind);
        url = url.replace("[showlegend]", legend);
        url = url.replace("[showoverview]", overmap);
        url = url.replace("[opac]", opac);
        url = url.replace("[class]", indclass);
        url = url.replace("[mapstyle]", mapstyle);
        url = url.replace("[layers]", layers);
        // assign the url to the form field
        $("input[id*='txtExportUrl']").attr("value", url);


        return true;
        
    }
    else {
        return false;
    }
}

function toggleLayer(e) {
    var layerName;
    
    layerName = $(e).attr("name");
    if ($(e).is(':checked')) {
        FindLayer(layerName).Show();
    }
    else {
        FindLayer(layerName).Hide();
    }
//    if ($("#myMapLayerControl")) {
//        $("#myMapLayerControl input").each(function() {
//            if ($(this).is(":checked")) {
//                FindLayer($(this).attr("name")).Show();
//            }
//            else {
//                FindLayer($(this).attr("name")).Hide();
//            }
//        });
    //    }


}

function FindLayer(title) {
  var map = getIDMap();
  var layer = null;
  for (var i = 0; i < map.GetShapeLayerCount(); i++)
  {
      layer = map.GetShapeLayerByIndex(i);
    if (layer.GetTitle() == title) break;
    layer = null; 
  }
  return layer;
}


function highlightPolys(name) {
    var map = getIDMap();
    var l = map.GetShapeLayerByIndex(1);
    var areaCount = 0
    var numCount = 0

    for (var i = 0; i < l.GetShapeCount(); i++) {
        var shape = l.GetShapeByIndex(i);
        var shapeClr = shape.GetFillColor();
        var matchClr = "c_" + shapeClr.R + shapeClr.G + shapeClr.B
        if (matchClr == name) {
            shape.SetLineColor(new VEColor(cntstR, cntstG, cntstB, 1.0));
            shape.SetLineWidth(4);
            shape.SetZIndex(null, 99999);
            $("td:contains('" + shape.GetTitle() + "')").parent().addClass("highlight");
            areaCount++;
        }
        
    }


    if (infoControl == null) {
        var veE = $find('veextender');
        infoControl = document.createElement("div");
        infoControl.id = "myInfoControl";
        infoControl.style.top = "2px";
        infoControl.style.left = (veE.get_width() - 195) + "px";
        map.AddControl(infoControl);
        $("#myInfoControl").addClass("div-infobox");
        infoControl.style.visibility = "hidden";
    }

//    var high = $(".highlight");
//    var number = 0;
//    var percent = 0;
//    for (var i = 0; i < high.length - 1; i++) {
//        var num = high[i].childNodes[2].innerHTML;
//        var per = high[i].childNodes[3].innerHTML;
//        number += parseInt(num.replace(",", ""));
//        percent += parseFloat(per);
//    }
//    //alert(number.numberFormat("0,0"));
//    //alert(percent.numberFormat("0.0"));
//    
//    var info = "<table cellpadding='3'>"
//    if (infoControl != null) {
//        info += "<tr><td align='left'>Area count:</td><td align='right'>" + areaCount + "</td><tr>";
//        info += "<tr><td align='left'>Total number:</td><td align='right'>" + number.numberFormat("0,0") + "</td></tr>";
//        info += "<tr><td align='left'>Total percent %:</td><td align='right'>" + percent.numberFormat("0.0") + "</td></tr></table>";



//        infoControl.innerHTML = info;
//        infoControl.style.visibility = "visible";
//    }
    
    
    
   
}