﻿/// <reference assembly="ESRI.ArcGIS.ADF.Web.UI.WebControls" name="ESRI.ArcGIS.ADF.Web.UI.WebControls.Runtime.JavaScript.references.js"/>

var HeaderVisible = true;

function LeftPanelTab(Button) {
    if (Button == "clear") {
        hidePanel("EDCFloatingPanel");
        hidePanel("OverviewMap_Panel");
        hidePanel("Toc_Panel");
        hidePanel("Tasks_Menu_Panel");
        hidePanel("GettingStartedPanel");
        hidePanel("Results");
        hidePanel("TaskManager1_FriscoFindAddressTask1");
        hidePanel("TaskManager1_FriscoSearchAttributesTask1");
        hidePanel("QueryBuilder1_QueryBuilderPanel");
        hidePanel("TaskManager1_PrintTask");
        hidePanel("Measure1_MeasureToolbar");
    }
    else if (Button == "all") {
        showPanel("EDCFloatingPanel");
        showPanel("OverviewMap_Panel");
        showPanel("Toc_Panel");
        showPanel("Tasks_Menu_Panel");
        showPanel("GettingStartedPanel");
        hidePanel("Results");
        hidePanel("TaskManager1_FriscoFindAddressTask1");
        hidePanel("TaskManager1_FriscoSearchAttributesTask1");
        hidePanel("QueryBuilder1_QueryBuilderPanel");
        hidePanel("TaskManager1_PrintTask");
        hidePanel("Measure1_MeasureToolbar");
    }
    else if (Button == "edc") {
        showPanel("EDCFloatingPanel");
        showPanel("OverviewMap_Panel");
        hidePanel("Toc_Panel");
        hidePanel("Tasks_Menu_Panel");
        hidePanel("GettingStartedPanel");
        hidePanel("Results");
        hidePanel("TaskManager1_FriscoFindAddressTask1");
        hidePanel("TaskManager1_FriscoSearchAttributesTask1");
        hidePanel("QueryBuilder1_QueryBuilderPanel");
        hidePanel("TaskManager1_PrintTask");
        hidePanel("Measure1_MeasureToolbar");
    }
    else if (Button == "toc") {
        hidePanel("EDCFloatingPanel");
        hidePanel("OverviewMap_Panel");
        showPanel("Toc_Panel");
        hidePanel("Tasks_Menu_Panel");
        hidePanel("GettingStartedPanel");
        hidePanel("Results");
        hidePanel("TaskManager1_FriscoFindAddressTask1");
        hidePanel("TaskManager1_FriscoSearchAttributesTask1");
        hidePanel("QueryBuilder1_QueryBuilderPanel");
        hidePanel("TaskManager1_PrintTask");
        hidePanel("Measure1_MeasureToolbar");

    }
    else if (Button == "task") {
        hidePanel("EDCFloatingPanel");
        hidePanel("OverviewMap_Panel");
        hidePanel("Toc_Panel");
        showPanel("Tasks_Menu_Panel");
        hidePanel("GettingStartedPanel");
        hidePanel("Results");
        // Hide all the seperate Task windows
        hidePanel("TaskManager1_FriscoFindAddressTask1");
        hidePanel("TaskManager1_FriscoSearchAttributesTask1");
        hidePanel("QueryBuilder1_QueryBuilderPanel");
        hidePanel("TaskManager1_PrintTask");
        hidePanel("Measure1_MeasureToolbar");
        // Dock PrintTask, Won't do it by default
        var PT;
        PT = $find('TaskManager1_PrintTask');
        PT.dock();

    }
    else if (Button == "help") {
        hidePanel("EDCFloatingPanel");
        hidePanel("OverviewMap_Panel");
        hidePanel("Toc_Panel");
        hidePanel("Tasks_Menu_Panel");
        showPanel("GettingStartedPanel");
        hidePanel("Results");
        hidePanel("TaskManager1_FriscoFindAddressTask1");
        hidePanel("TaskManager1_FriscoSearchAttributesTask1");
        hidePanel("QueryBuilder1_QueryBuilderPanel");
        hidePanel("TaskManager1_PrintTask");
        hidePanel("Measure1_MeasureToolbar");
    }
    
}

// My Hide Floating Panel. Test visibility before making request. Saves time.
function hidePanel(name) {
    var obj = $find(name);
    if (obj == null) return;
    var test = obj.get_element().style.display;
    if (test != 'none') hideFloatingPanel(name);
}

// My Show Floating Panel. Test visibility before making request.
function showPanel(name) {
    var obj = $find(name);
    if (obj == null) return;
    var test = obj.get_element().style.display;
    if (test == 'none') showFloatingPanel(name);
}

function ShowMaptip() {// Doesn't work unless feature has already been initialized
    var map = $find('Map1');
    map._graphicFeatures[0].get_mapTips().get_callout().show();
}

// ******************************** Tool busy indicator *****************************************
function SetupToolBusy() {
    var map = $find('Map1');
    var TB1 = $find('Toolbar1');
    var TB2 = $find('Toolbar2');
    map.add_mouseUp(ShowToolBusy);
    TB1.add_onToolSelected(CloseToolBusy);
    TB2.add_onToolSelected(CloseToolBusy);
}

function ShowToolBusy(sender, args) {

    if (currentToolbarTool = "point") {
        var coords = args.coordinate;
        var BusyImage = document.getElementById("ToolBusy");
        var X, Y;
        if (coords != null && !isNaN(coords.get_x()) && !isNaN(coords.get_y())) {
            // ignore null or non-numeric input
            // var ScreenCoords = toScreenPoint(coords);
            X = args.offsetX;
            Y = args.offsetY;
            //X = coords.get_x();
            //Y = coords.get_y();
            BusyImage.style.left = X - 8 + "px";
            BusyImage.style.top = Y - 8 + "px";
            BusyImage.style.visibility = "visible";
        }
    }
}

function HideToolBusy() {
    var BusyImage = document.getElementById("ToolBusy");
    // Park image out of the way
    BusyImage.style.left = "0px";
    BusyImage.style.top = "0px";
    BusyImage.style.visibility = "hidden";
}

function CloseToolBusy(sender, args) {
    var map = $find('Map1');
    var TB1 = $find('Toolbar1');
    var TB2 = $find('Toolbar2');

    map.remove_mouseUp(ShowToolBusy);
    TB1.remove_onToolSelected(CloseToolBusy);
    TB2.remove_onToolSelected(CloseToolBusy);
}

//********************************** End Tool Busy indicator ******************


// Open a new window for Virtual Earth. Might modify to expand usage (i.e. pass width, height)
function PictureResults(URL) {
    var PictureWin;
    var windowOptions = "toolbar=no,width=420,height=430,top=50,left=50,scrollbars=no,resizable=yes";
    if (!PictureWin || PictureWin.closed) {
        PictureWin = window.open(URL, "printWindow", windowOptions);
        //PictureWin.moveTo(0, 0);
        PictureWin.focus();
    }
}


// function to handle Do Not Show again checkbox on Getting Started form
function DoNotShow_onclick() {
    var today = new Date();
    var thisMonth = today.getMonth();
    var thisYear = today.getFullYear();
    var expiration = new Date(thisYear + 1, thisMonth, 1);
    var DoNotShow = document.getElementById("DoNotShow");
    if (DoNotShow.checked == true)
    { document.cookie = "FriscoMapsGettingStarted=false; expires=" + expiration; }
    else
    { document.cookie = "FriscoMapsGettingStarted=true; expires=" + expiration; }

}


// Get the value of the cookie with the specified name.
function GetCookie(sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }

    // a cookie with the requested name does not exist
    return null;
}

function showBusyIndicator(sender) {
    showLayer("BusyIndicator");
}

function hideBusyIndicator(sender) {
    hideLayer("BusyIndicator");
}

function setupProgressIndicator() {   // Set the location of the progress bar         
    var map = $find('Map1');
    
    map.set_progressBarAlignment(ESRI.ADF.System.ContentAlignment.MiddleCenter);
}



// Borrowed from 9.3 code
function MapCoordsMouseMove(sender, args) {
    var coords = args.coordinate;
    var roundFactor = Math.pow(10, 2); //sjf
    // window.status = (Math.round(coords.get_x() * roundFactor) / roundFactor) + ", " + (Math.round(coords.get_y() * roundFactor) / roundFactor);
    window.status = convertSP(coords.get_x(), coords.get_y());
}

// Convert Stateplane coords to Lat/Long from Laura Wilson
function convertSP(uX, uY) {
    // From ESRI ArcGIS Desktop layer properties
    // Projected Coordinate System:	NAD_1983_StatePlane_Texas_North_Central_FIPS_4202_Feet
    // Projection:	Lambert_Conformal_Conic
    // False_Easting:	1968500.00000000
    // False_Northing:	6561666.66666667
    // Central_Meridian:	-98.50000000
    // Standard_Parallel_1:	32.13333333
    // Standard_Parallel_2:	33.96666667
    // Latitude_Of_Origin:	31.66666667
    // Linear Unit: 	Foot_US
    var a = 20925604.48; //major radius of ellipsoid, map units (NAD 83) 
    var ec = 0.08181905782; //eccentricity of ellipsoid (NAD 83) 
    var angRad = 0.01745329252; //number of radians in a degree 
    var pi4 = 3.141592653582 / 4; //Pi / 4
    var p0 = 31.66666667 * angRad; //latitude of origin
    var p1 = 32.13333333 * angRad; //latitude of first standard parallel
    var p2 = 33.96666667 * angRad; //latitude of second standard parallel
    var m0 = -98.50000000 * angRad; //central meridian
    var x0 = 1968500.00000000; //False easting of central meridian, map units
    var y0 = 6561666.66666667;   // SJF: False northing
    // Calculate the coordinate system constants. 
    with (Math) {
        m1 = cos(p1) / sqrt(1 - (pow(ec, 2)) * pow(sin(p1), 2));
        m2 = cos(p2) / sqrt(1 - (pow(ec, 2)) * pow(sin(p2), 2));
        t0 = tan(pi4 - (p0 / 2));
        t1 = tan(pi4 - (p1 / 2));
        t2 = tan(pi4 - (p2 / 2));
        t0 = t0 / pow(((1 - (ec * (sin(p0)))) / (1 + (ec * (sin(p0))))), ec / 2);
        t1 = t1 / pow(((1 - (ec * (sin(p1)))) / (1 + (ec * (sin(p1))))), ec / 2);
        t2 = t2 / pow(((1 - (ec * (sin(p2)))) / (1 + (ec * (sin(p2))))), ec / 2);
        n = log(m1 / m2) / log(t1 / t2);
        f = m1 / (n * pow(t1, n));
        rho0 = a * f * pow(t0, n);

        // Convert the coordinate to Latitude/Longitude. 

        // Calculate the Longitude.
        uX = uX - x0;
        uY = uY - y0;    // sjf added
        pi2 = pi4 * 2;

        rho = sqrt(pow(uX, 2) + pow((rho0 - uY), 2));
        theta = atan(uX / (rho0 - uY));
        txy = pow((rho / (a * f)), (1 / n));
        lon = (theta / n) + m0;
        uX = uX + x0;
        uY = uY + y0;    // sjf

        // Estimate the Latitude 
        lat0 = pi2 - (2 * atan(txy));

        // Substitute the estimate into the iterative calculation that 
        // converges on the correct Latitude value. 
        part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
        lat1 = pi2 - (2 * atan(txy * pow(part1, (ec / 2))));

        while ((abs(lat1 - lat0)) > 0.000000002) {
            lat0 = lat1;
            part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
            lat1 = pi2 - (2 * atan(txy * pow(part1, (ec / 2))));
        }

        // Convert from radians to degrees. 
        Lat = lat1 / angRad;
        Lon = lon / angRad;

        //alert(lat); 
        //alert(lon); 

        //Round the latitude and longitude 
        //lat = (CLng(lat * 100000)) / 100000; 
        //lon = (CLng(lon * 100000)) / 100000; 

        //Lat = lat.toPrecision(7); 
        //Lon = lon.toPrecision(8); 

        return Degrees2DMS(Lon) + "W, " + Degrees2DMS(Lat) + "N";

    }
}

//Convert decimal degress to DMS
function Degrees2DMS(degrees) {
    var deg //As Integer
    var min //As Integer
    var sec //As Decimal
    var totalSecs //As Double
    var response // As String

    totalSecs = Math.abs(degrees) * 3600;
    deg = Math.floor(Math.abs(degrees));
    totalSecs -= (deg * 3600);
    min = Math.floor(totalSecs / 60.0);
    sec = Math.floor(totalSecs - (min * 60) + 0.5);

    response = deg + "° " + min + "' " + sec + "''";

    return response;

}

// New website suggestion
function btnnewnEbsiteyes_onclick() {
    window.location = "http://BetaEDCmaps.friscotexas.gov";
    return true;
}

function btnnewwebsiteno_onclick() {
    element = document.getElementById("NewWebsiteRedirect");
    element.style.left = "-350px";
    hidePanel("NewWebsiteRedirect");
}     
