﻿
// Shows a popup
function LoadPopup(path, width, height, rootEl, text)
{
    var win=window.open(path,'','width=' + width + ',height='+ height + ', menubar=no, resizable=no, titlebar=no');
    var el = win.document.getElementById(rootEl);
    el.innerHTML = text;
    win.document.close();
}

// Calc the distance between two locations
function distCosineLaw(lat1, lon1, lat2, lon2) 
{
  var R = 6371; // earth's mean radius in km
  var d;
  // formula:  http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1
  d = Math.acos(Math.sin(toRad(lat1))*Math.sin(toRad(lat2)) + Math.cos(toRad(lat1))*Math.cos(toRad(lat2))*Math.cos(toRad(lon2-lon1))) * R;
  return d * 0.621371192; // convert to miles
}

function toRad(v) 
{
    return (v * Math.PI / 180);
}

function GetSearchDist(mapView)
{
    var rect = mapView;
    var d;
    if (distCosineLaw != null)
    {
        // find 1/2 the width of the current zoom
        d = Math.min(250, Math.ceil(distCosineLaw(rect.TopLeftLatLong.Latitude, rect.TopLeftLatLong.Longitude, rect.BottomRightLatLong.Latitude, rect.BottomRightLatLong.Longitude) * .5));
    }
    else
    {
        d = 1;
    }
    
    return d;
}

function FailedCallback(error)
{
    try
    {
        Dump("Stack Trace: " +  error.get_stackTrace() + "<br/>" +
        "Service Error: " + error.get_message() + "<br/>" +
        "Status Code: " + error.get_statusCode() + "<br/>" +
        "Exception Type: " + error.get_exceptionType() + "<br/>" +
        "Timedout: " + error.get_timedOut(), true);
    }
    catch (e)
    { }
}

function Dump(t, clear)
{
    var dump = document.getElementById("dump");
    if (dump == null)
    {
        dump = document.createElement('div');
        dump.id = 'dump';
        document.body.appendChild(dump);
    }
    
    if (clear)
    {
        dump.innerHTML = t;
    }
    else
    {
        dump.innerHTML += t;
    }
}

function GetLatLong(e)
{
    var dump = document.getElementById("dump");
    show(dump);
    Dump("<span>?lat=" + e.view.LatLong.Latitude.toFixed(5) + "&lon=" + e.view.LatLong.Longitude.toFixed(5) + "&z=" +  e.view.zoomLevel + "</span>", true);
}

function hideInfoPane()
{
    var el = document.getElementById("entityDetail");
    hide(el);
}

function hide(el)
{
    if (el != null)
        el.style.visibility = "hidden";
}

function show(el)
{
    if (el != null)
        el.style.visibility = "visible";
}

function GetInfopane()
{
//    <div id="entityDetail" class="floatingForm">
//        <div class="title">
//        </div>
//        <div class="details">
//        </div>
//        <div class="footer">
//        </div>
//    </div>

    var el = document.getElementById("entityDetail");
    if (el == null)
    {
        el = CreateElement("entityDetail", "floatingForm");
        var t = CreateElement("", "title");
        var d = CreateElement("", "details");
        var f = CreateElement("", "footer");
        el.appendChild(t);
        el.appendChild(d);
        el.appendChild(f);
        
        document.body.appendChild(el);
    }
    var children = el.getElementsByTagName("div");
    children[1].style.height = "220px"; //220px
    el.style.top = "75px"; //75px
    el.style.height = "310px"; //310px
    
    return el;
}

function GetElement(id, cl, parent)
{
    var result = document.getElementById(id);
    if (result == null)
    {
        result = CreateElement(id, cl);
        if (parent != null)
        {
            parent.appendChild(result);
        }
        else
        {
            document.body.appendChild(result);
        }
    }
    return result;
}

function CreateElement(id, cl)
{
    var result = document.createElement('div');
    result.id = id;
    if (cl != null)
        result.className = cl;
    
    return result;
}