var div = null;
var map = null;
var pinid = 0;
var crunchedLayerManager = null;
var layerList = null;
var slPoints = new VEShapeLayer();
var rect;
var control;

function ToggleLayer2(name, active) {
    try {
        map.DeleteTileLayer('Watersheds')
    }
    catch (err) {
    }
    var trans = document.getElementById('trans').value;
    if (active == true) {
        var bounds = [new VELatLongRectangle(new VELatLong(30.2832672477311, -96.0607490420695), new VELatLong(29.4634013652724, -94.8317468517717))];
        var tileSourceSpec = new VETileSourceSpecification("Watersheds", "http://www.hcoem.org/Layer_Watershed/%4.png");
        tileSourceSpec.NumServers = 1;
        tileSourceSpec.Bounds = bounds;
        tileSourceSpec.MinZoomLevel = 8;
        tileSourceSpec.MaxZoomLevel = 20;
        tileSourceSpec.Opacity = trans;
        tileSourceSpec.ZIndex = 50;
        map.AddTileLayer(tileSourceSpec, true);
    }
    else {
        map.DeleteTileLayer('Watersheds')
    }
}

function GetSize() {
    var myWidth = 0, myHeight = 0;

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    if (myWidth > 0) {
        var result = new Array();
        result[0] = myWidth;
        result[1] = myHeight;
        return result;
    }
    else {
        return null;
    }
}

function SetMapSize() {
    var browserSize = GetSize();

    var div = document.getElementById("myMap");

    if (browserSize != null) {
        div.style.width = (browserSize[0] - 325) + "px";
        div.style.height = (browserSize[1] - 200) + "px";

    }
    else {
        div.style.width = "800px";
        div.style.height = "600px";
    }
    div.style.overflow = "hidden";
    div.style.position = "relative";

}

function MapModeChanged(e) {
    if (map.GetMapMode() == Msn.VE.MapActionMode.Mode3D &&
            window.location.protocol == "file:") {
        alert("MapCruncher tiles will only display in 3D mode " +
                "if your map is hosted on a web server (using http://).\n" +
                "Currently, your tiles are coming from a local machine (file://) " +
                "so only 2D mode is available.");
    }
}

function LoadPage() {
    //if (location.search == '?P=StationMap') {
        SetMapSize();
        window.onresize = SetMapSize;

        map = new VEMap('myMap');
        map.SetDashboardSize(VEDashboardSize.Small);
        map.LoadMap(new VELatLong(29.85, -95.44), 9, 'r', false);
        crunchedLayerManager = new VE.MapCruncher.CrunchedLayerManager(map);
        map.AddShapeLayer(slPoints);
        //map.SetClientToken(token);
        map.AttachEvent("oninitmode", MapModeChanged);
        //map.AttachEvent("onendzoom", ShowCurrent);
        ShowCurrent();
        var count = slPoints.GetShapeCount();
        if (count > 0) {
            rect = slPoints.GetBoundingRectangle();
            map.SetMapView(rect);
        }
    //}
}

function HideAll() {
    map.DeleteAllShapes();
}

function ShowAllCh() {
    map.DeleteAllShapes();
    if (document.aspnetForm.MainCk.checked == true) {
        ShowAll();
        for (i = 0; i < document.aspnetForm.chkW.length; i++) {
            if (document.aspnetForm.chkW[i].checked == false) {
                document.aspnetForm.chkW[i].checked = true;
            }
        }
    }
    else {
        for (i = 0; i < document.aspnetForm.chkW.length; i++) {
            if (document.aspnetForm.chkW[i].checked == true) {
                document.aspnetForm.chkW[i].checked = false;
            }
        }
    }
}

function ShowGauge(id, mylat, mylong, mylib, myadd) {
    try {
        var url = this.location.search;
        var strIco = "images/021.gif";
        var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(mylat, mylong));
        map.ClearInfoBoxStyles();
        var mZoom = map.GetZoomLevel();
        var font = "10";
        var CurzIndex = 1200;
        if (mZoom < 11) {
            font = "8";
        }
        if (mZoom > 8) {
            strIco = "<div style='font-size:" + font + "px;font-weight:bold;border:solid 2px Black;padding:0px 1px 0px 1px;background-color:#FFFFFF;text-align:center;'>" + id + "</div>";
        }
        if (id == 'NR') {
            strIco = "<div style='font-size:8px;border:solid 1px Grey;padding:0px 1px 0px 1px;background-color:#FFFFFF;text-align:center;'>NR</div>";
            CurzIndex = 998;
        }
        if (url == '?P=StationMap&G=Wind') {
            if (id.indexOf('~') > -1) {
                var SplitResult = id.split("~");
                strIco = "<div style='font-size:" + font + "px;font-weight:bold;border:solid 2px Black;padding:0px 1px 0px 1px;background-color:#FFFFFF;text-align:center;'>" + SplitResult[0] + "<br /><img src='images/wind/" + SplitResult[1] + ".png' height='15' /></div>";
            }
        }
        pin.SetCustomIcon(strIco);
        pin.SetTitle(mylib);
        pin.SetDescription(myadd);
        pin.SetZIndex(CurzIndex, null);
        slPoints.AddShape(pin);
        pinid++;
    }
    catch (err) {
        alert(err.description)
        map.DeleteShape(pin);
    }
}

function LoadEvac() {
    try {
        var bounds = [new VELatLongRectangle(new VELatLong(30.1947986242631, -95.9163040763784), new VELatLong(28.8087910065505, -94.2941667289584))];
        var tileSourceSpec = new VETileSourceSpecification("Routes", "http://www.hcoem.org/Layer_Routes/%4.png");
        tileSourceSpec.NumServers = 1;
        tileSourceSpec.Bounds = bounds;
        tileSourceSpec.MinZoomLevel = 8;
        tileSourceSpec.MaxZoomLevel = 20;
        tileSourceSpec.Opacity = .7;
        tileSourceSpec.ZIndex = 50;
        map.AddTileLayer(tileSourceSpec, true);
        map.SetCenterAndZoom(new VELatLong(29.32, -95.36), 9);
        //layerList = crunchedLayerManager.ImportLayersFromAnchorHRef("CrunchedLayers2");
        //crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Routes', .7);
        //crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Zone A', .5);
        //crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Zone B', .5);
        //crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Zone C', .5);
        //crunchedLayerManager.SetCenterAndZoomForLayer(map, layerList, 'Zone C');
    }
    catch (err) {
    }
}

function LoadWatershed() {
    var trans = 0.8;
    //document.getElementById('trans').value;
    //var bounds = [new VELatLongRectangle(new VELatLong(29.4634013652724, -96.0607490420695), new VELatLong(30.2832672477311, -94.8317468517717))];
    var bounds = [new VELatLongRectangle(new VELatLong(30.2832672477311, -96.0607490420695), new VELatLong(29.4634013652724, -94.8317468517717))];
    var tileSourceSpec = new VETileSourceSpecification("Watersheds", "http://www.hcoem.org/Layer_Watershed/%4.png");
    tileSourceSpec.NumServers = 1;
    tileSourceSpec.Bounds = bounds;
    tileSourceSpec.MinZoomLevel = 8;
    tileSourceSpec.MaxZoomLevel = 20;
    tileSourceSpec.Opacity = trans;
    tileSourceSpec.ZIndex = 50;
    map.AddTileLayer(tileSourceSpec, true);
    var url = this.location.search;
    if (url == '?P=StationMap&G=Wind' || url == '?P=StationMap&G=Temperature' || url == '?P=StationMap&G=Road' || url == '?P=StationMap&G=Other') {
        AddMapLogo();
    }
    
    //catch(err)
    //{
    //crunchedLayerManager.DeactivateAlphaLayer(map, 'Watersheds');
    //crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Watersheds', trans);
    //}
}

function FindLoc() {
    try {
        var strAddress = ''
        if (document.getElementById('txtWhere1').value > '') {
            strAddress = strAddress + document.getElementById('txtWhere1').value
        }
        if (document.getElementById('txtWhere2').value > '') {
            strAddress = strAddress + ', ' + document.getElementById('txtWhere2').value
        }
        if (document.getElementById('txtWhere3').value > '') {
            strAddress = strAddress + ' ' + document.getElementById('txtWhere3').value
        }
        //alert(strAddress);        
        map.Find(null, strAddress);
    }
    catch (err) {
        alert(err.description)
    }
}

function onFoundResults(e) {
    var results = "Matching location(s):<br>";
    for (x = 0; x < e.length; x++) {
        results += "<a href='javascript:map.FindLocation(\"" + e[x].ID + "\");'>" + e[x].ID + "</a><br>";
    }
    document.getElementById('resultDiv').innerHTML = results;
    var pin = new VEPushpin(pinID, map.GetCenter(), null, 'Location', document.getElementById('txtWhere1').value + ', ' + document.getElementById('txtWhere2').value + ' ' + document.getElementById('txtWhere3').value);
    map.AddPushpin(pin);
    pinID++;
    map.ZoomOut();
}

function mZoomIn() {
    map.ZoomIn();
}

function mZoomOut() {
    map.ZoomOut();
}


function ChangeTrans(trans) {
    try {
        ToggleLayer2
    }
    catch (err) {
        //alert(err.description)
    }
}

function AddMapLogo() {
    if (control == null) {
        control = document.createElement("div");
        control.id = "TXDOTOEM";
        var width = document.getElementById('myMap').style.width;
        width = width.replace(/px/, "");
        width = width - 140;
        //var height = document.getElementById('myMap').style.height;
        //height = height.replace(/px/, "");
        //height = height - 100;
        control.style.top = "15px";
        control.style.width = "122px";
        //control.style.border = "solid 1px Blue";
        control.style.left = width + "px";
        control.style.opacity = ".2"
        control.innerHTML = '<table cellspacing="0" cellpadding="3" align="center"><tr><td height="5"></td></tr><td><img src="images/TxDOTTrans.gif" /></td><tr><td height="5"></td></tr></table>';
        map.AddControl(control);
        //addShim2(control);
    }
}

function addShim2(el) {
    var shim2 = document.createElement("iframe");
    shim2.id = "KeyShim";
    shim2.frameBorder = "0";
    shim2.style.position = "absolute";
    shim2.style.zIndex = "1";
    shim2.style.top = el.offsetTop;
    shim2.style.left = el.offsetLeft;
    shim2.width = el.offsetWidth;
    shim2.height = el.offsetHeight;
    shim2.style.opacity = ".2";
    el.shimElement = shim2;
    el.parentNode.insertBefore(shim2, el);
}


/****************************
var map = null;
var pinid = 0;
var div = null;
var layerList = null;
        
function GetMap()
{
map = new VEMap('myMap');
//map.LoadMap(new VELatLong(29.8, -95.35), 9, 'r', false);
map.LoadMap();
map.AttachEvent("onendzoom",ShowCurrent);
map.SetMapStyle('r');
crunchedLayerManager = new VE.MapCruncher.CrunchedLayerManager(map);
var div = document.getElementById("myMap");

div.style.width = "450px";
div.style.height = "380px";
div.style.overflow = "hidden";
div.style.position = "relative";

if (crunchedLayerManager!=null)
{
crunchedLayerManager.controlManager.UpdateControlLayout();
}
crunchedLayerManager.ImportLayersFromAnchorHRef("CrunchedLayers2");
var layer = crunchedLayerManager.layerList.find('Watersheds');
layer.Activate(map);
layer.SetDefaultView(map);
try
{
ShowCurrent();
}
catch(err)
{
map.DeleteAllShapes();                
}
}   

function HideAll() 
{
map.DeleteAllShapes();                
}  

function ShowAllCh()
{
map.DeleteAllShapes();
if (document.aspnetForm.MainCk.checked==true){ 
ShowAll();
for (i=0;i<document.aspnetForm.chkW.length;i++) {
if (document.aspnetForm.chkW[i].checked==false) {
document.aspnetForm.chkW[i].checked = true;
}
}
}
else {
for (i=0;i<document.aspnetForm.chkW.length;i++) {
if (document.aspnetForm.chkW[i].checked==true) {
document.aspnetForm.chkW[i].checked = false;
}
}
}
}

function ShowGauge(id, mylat, mylong, mylib, myadd) { 
try
{    
var strIco = "images/021.gif";
var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(mylat, mylong));   
map.ClearInfoBoxStyles();  
var mZoom = map.GetZoomLevel();
if (mZoom>9)
{
strIco = "<div style='font: small-caption;'>"+id+"</div>";
}
pin.SetCustomIcon(strIco);
pin.SetTitle(mylib);         
pin.SetDescription(myadd);     
map.AddShape(pin);
pinid++;
}
catch(err)
{
map.DeleteAllShapes();      

}
}  

function ToggleLayer(name, active)
{
if (active == true)
{
crunchedLayerManager.layerList.find(name).Activate(map);
}
else
{
crunchedLayerManager.layerList.find(name).Deactivate(map);
}
}

function ToggleLayer2(name, active)
{
if (active == true)
{
crunchedLayerManager.layerList.find(name).Activate(map);
}
else
{
crunchedLayerManager.layerList.find(name).Deactivate(map);
}
}

function LoadEvac()
{
layerList = crunchedLayerManager.ImportLayersFromAnchorHRef("CrunchedLayers");
crunchedLayerManager.StartAutomaticLegends(map, layerList);
crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Routes', .7);
crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Zone A', .5);
crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Zone B', .5);
crunchedLayerManager.ActivateAlphaLayer(map, layerList, 'Zone C', .5);
crunchedLayerManager.SetCenterAndZoomForLayer(map, layerList, 'Zone C');
document.getElementById('resultDiv').innerHTML='Evacuation Routes';      

}

function LoadWatershed()
{
alert('start')
alert('step 1')
}

function FindLoc()         
{            
map.FindLocation(document.getElementById('txtWhere1').value+', '+document.getElementById('txtWhere2').value+' '+document.getElementById('txtWhere3').value, onFoundResults);         
}         

function onFoundResults(e)         
{            
var results="Matching location(s):<br>";            
for (x=0; x<e.length; x++)            
{               
results+="<a href='javascript:map.FindLocation(\""+e[x].ID+"\");'>"+e[x].ID+"</a><br>";
}            
document.getElementById('resultDiv').innerHTML=results;   
var pin = new VEPushpin(pinid, map.GetCenter(), null, 'Location', document.getElementById('txtWhere1').value+', '+document.getElementById('txtWhere2').value+' '+document.getElementById('txtWhere3').value);
map.AddPushpin(pin);
pinid++;
map.ZoomOut();
}

function mZoomIn()
{
map.ZoomIn();
}
    
function mZoomOut()
{
map.ZoomOut();
}
*/

function DisposeMap() {
    if (map != null) {
        map.Dispose();
    }
}
