﻿// JScript File
var map = null;
var shape = null;
var PlacesShapeLayer = null;
var IEVersion = '';
var OldMapCenter = null;
var ActiveCommunityLayersArray = new Array();
var infoBtnStatus = false;
var selectBtnStatus = false;
var PlaceShapeInfoOnlyLayer = null;
var PlaceShapeInfoOnlyTEMPLayer = null;
var InfoQueryControl = null;
var LoadingControl = null;
var SelectControl = null;
var QueryLatLong = null;


function GetMap()      
{   
    map = new VEMap('myMap');      
    map.LoadMap(new VELatLong(44.500717834, -121.706542), 6, 's',0, VEMapMode.Mode2D, 0);    
    map.EnableShapeDisplayThreshold(false);
    PlaceShapeInfoOnlyLayer = new VEShapeLayer();
    PlaceShapeInfoOnlyLayer.SetTitle("InfoLayer");
    map.AddShapeLayer(PlaceShapeInfoOnlyLayer);
    PlaceShapeInfoOnlyTEMPLayer = new VEShapeLayer();
    PlaceShapeInfoOnlyTEMPLayer.SetTitle("InfoTEMPLayer");
    map.AddShapeLayer(PlaceShapeInfoOnlyTEMPLayer);
    PlacesShapeLayer = new VEShapeLayer();
    PlacesShapeLayer.SetTitle("PlaceShapes");    
    map.AddShapeLayer(PlacesShapeLayer);  
    map.AttachEvent("onclick", onClickHandler);    
    //map.AttachEvent("onmousemove", onMouseMoveHandler);     
    //map.AttachEvent("onmouseout", onMouseOutHandler); 
    
    //loadCensusPlaces();
//    var CensusPlaceSource = new VEShapeSourceSpecification(VEDataType.ImportXML,'http://maps.live.com?http://www.oregonexplorer.info/ExternalContent/KML/SchoolTest.kml', ShapeLayerCensusPlaces);    
//    map.ImportShapeLayerData(CensusPlaceSource, onFeedLoad, 0);   
    IEVersion = msieversion();     
    if (IEVersion < 7)
    {          
    getTopoTiles(1,false, "Census2000", "Counties");     
    getTopoTiles(1,true, "Census2000", "CensusTracts"); 
    getTopoTiles(1,true, "Census2000", "CensusPlaces");
    //getTopoTiles(1,true, "Census2000", "CityLimits");   
    var SliderDivStatewide = $get('sliderStateWideDiv');	
	SliderDivStatewide.style.visibility = 'hidden';	
	window.setTimeout('getHideSliderDivs(false, 0)', 0);
    }
    else
    {
    getTopoTiles(.3,false, "Census2000", "Counties"); 
    getTopoTiles(.4,true, "Census2000", "CensusTracts"); 
    getTopoTiles(.4,true, "Census2000", "CensusPlaces");   
    //getTopoTiles(.4,true, "Census2000", "CityLimits");   	
    }              
    setAccordionDisplay(1);   
    //setAccordionDisplay();   
}

function getTopoTiles(opacity,bVisibility, ServiceName, Layer)
{              
        var TileServerBase = 'http://oregonexplorer.info/TileServer_Rural';              
        var TileServerNum = 1;        
        var layerUrl = TileServerBase +"/@"+ServiceName+"@/$"+Layer+"$/tiles/%4.png";                
        var TileSourceName = ServiceName+Layer;
        var tileSourceSpec = new VETileSourceSpecification(TileSourceName,layerUrl);     
        tileSourceSpec.NumServers = TileServerNum;          
        var bounds = [new VELatLongRectangle(new VELatLong(46.3928, -124.5782),new VELatLong(41.6447, -116.5658))];        
        tileSourceSpec.Bounds= bounds;
        tileSourceSpec.MaxZoomLevel = 21;        
        tileSourceSpec.Opacity = opacity;  
        var zIndex= 20;
        switch (Layer)
            {
            case "CensusPlaces":
            zIndex = 25;
            break;
            case "CityLimits":
            zIndex = 30;
            break;
            default:
            break;
            }
        tileSourceSpec.ZIndex = zIndex;      
        try{
        map.AddTileLayer(tileSourceSpec,bVisibility);            
        }
        catch (ex)
        {
        map.DeleteTileLayer(TileSourceName);
        map.AddTileLayer(tileSourceSpec,bVisibility);
        }
}

function msieversion()
   {
      var ua = window.navigator.userAgent
      var msie = ua.indexOf ( "MSIE " )
      if ( msie > 0 )      // If Internet Explorer, return version number
         return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
      else                 // If another browser, return 0
         return 10 
   }
   
 function txtBoxSubmit(e)
  {   
    if (e && e.keyCode == 13)
    {
        if (e.srcElement.value != "")
        {           
         map.Find(null, e.srcElement.value);
         toggleTabs('selectplace');
        }
        return false;
                }
                }  
                
function ClearSearchBox(searchBox)
{
    if (infoBtnStatus)
        {
            ToggleInfoFunction();
        }
        if (selectBtnStatus)
        {
            ToggleSelectFunction();
        }
    switch (searchBox)
    {
      case 'counties':
        var hdfSearch = $get('hdfSearchBoxCounties');
        if (hdfSearch.value == 'first')
        {
        var SearchBox = $get('tbCounties');
        SearchBox.value = '';
        SearchBox.style.color = 'black';
        }    
        hdfSearch.value = 'notFirst';     
        break;
      case 'communities':
        var hdfSearch = $get('hdfSearchBoxCommunities');
        if (hdfSearch.value == 'first')
        {
        var SearchBox = $get('tbCommunities');
        SearchBox.value = '';
        SearchBox.style.color = 'black';
        }    
        hdfSearch.value = 'notFirst';     
        break;        
    }
}
 function toggleLegend()
 {
    var rdBtnCounties = $get('rbtnCounties');
    var rdBtnCommunities = $get('rbtnCommunities');
    var divCommunityLegend = $get('divCommunityLegend');
    var divCountyLegend = $get('divCountyLegend');
    var activeLayerinLayerType = $get('hdfActivePlaceType'); 
    var divQueryResults = $get('divQueryResults');   
    //var divInfoQuery = $get('divInfoQuery');
    if (rdBtnCounties.checked)
    {
        divCountyLegend.style.display = 'block';
        divCommunityLegend.style.display = 'none';                
        activeLayerinLayerType.value = 'counties';
        toggleSearchBox('counties');
        divQueryResults.style.display = "none";
        //divInfoQuery.style.visibility = "hidden";
        deleteInfoLayers();
    }
    else
    {
        divCountyLegend.style.display = 'none';
        divCommunityLegend.style.display = 'block';        
        activeLayerinLayerType.value = 'communities';
        toggleSearchBox('communities');        
        divQueryResults.style.display = "none";
        deleteInfoLayers();           
    }
    changeActiveLayerDisplay();
 }
 function ToggleInfoBtn(onOff)
 {
    var imgInfoBtn = $get('imgInfoBtn');
    switch (onOff)
    {
        case 'on':
        if (infoBtnStatus)
        {
        imgInfoBtn.src = 'img/infoBtn_active.gif';
        }        
        break;
        case 'off':
        if (!infoBtnStatus)
        {
        imgInfoBtn.src = 'img/infoBtn.gif';
        }        
        break;
        default:
        break;            
    }
 }
 
 function ToggleSelectBtn(onOff)
 {
    var imgSelectBtn = $get('imgSelectBtn');
    switch (onOff)
    {
        case 'on':
        if (selectBtnStatus)
        {
        imgSelectBtn.src = 'img/selectBtn_active.gif';
        }
        break;
        case 'off':
        if (!selectBtnStatus)
        {
        imgSelectBtn.src = 'img/selectBtn.gif';
        }
        break;
        default:
        break;            
    }
 }
 
 function ToggleInfoFunction()
 {
    var imgInfoBtn = $get('imgInfoBtn');
    if (infoBtnStatus)//turn mode off
    {
    infoBtnStatus = false;
    imgInfoBtn.src = 'img/infoBtn.gif';
    map.DetachEvent("onmousemove", MouseMoveHelp);  
    var hdfActiveLayers = $get('hdfActivePlaceType');
    if (hdfActiveLayers.value == "communities")
    {  
    //ToggleInfoQueryCheckBoxes('off');
    deleteInfoLayers();                
    PlaceShapeInfoOnlyTEMPLayer.DeleteAllShapes();
    QueryResultPlaceBinArray.clear();
    var divQueryResultsDisplay = $get('divQueryResultsDisplay');
    divQueryResultsDisplay.innerHTML = "";
    divQueryResultsScript = "";    
    var divQueryResults = $get('divQueryResults');
    divQueryResults.style.display = 'none';
    //RemoveMyControl('InfoQuery');
    }    
    }
    else//turn mode on
    {
     infoBtnStatus = true;
     if (selectBtnStatus)
     {
     ToggleSelectFunction()     
     }
     imgInfoBtn.src = 'img/infoBtn_active.gif';
     map.AttachEvent("onmousemove", MouseMoveHelp);    
     var hdfActiveLayers = $get('hdfActivePlaceType');
//    if (hdfActiveLayers.value == "communities")
//    {
//    ToggleInfoQueryCheckBoxes('on');  
//     }     
    }
 }
 
 function ToggleSelectFunction()
 {
    var imgSelectBtn = $get('imgSelectBtn');
    if (selectBtnStatus)//turn mode off
    {
    selectBtnStatus = false;
    imgSelectBtn.src = 'img/selectBtn.gif';
    map.DetachEvent("onmousemove", MouseMoveSelect);
    var hdfActiveLayers = $get('hdfActivePlaceType');
//    if (hdfActiveLayers.value == "communities")
//    {
//    ToggleInfoQueryCheckBoxes('off');   
//    }
    }
    else//turn mode on
    {
    selectBtnStatus = true;
    if (infoBtnStatus)//turn mode off
    {
    ToggleInfoFunction()
    }
    imgSelectBtn.src = 'img/selectBtn_active.gif';
    map.AttachEvent("onmousemove", MouseMoveSelect);
    var hdfActiveLayers = $get('hdfActivePlaceType');
//    if (hdfActiveLayers.value == "communities")
//    {
//    ToggleInfoQueryCheckBoxes('on');  
//    }    
    } 
 }
 
 function ToggleInfoQueryCheckBoxes(onOff)
 {
    var divInfoQuery = $get('divInfoQuery');
    if (onOff == 'on')
    {   
        divInfoQuery.style.visibility = 'visible';
    }
    else
    {
     divInfoQuery.style.visibility = 'hidden';
    }
 }
 
 function MouseMoveHelp(e)
{
    var x = e.mapX;
    var y = e.mapY;
    pixel = new VEPixel(x, y);
    var LL = map.PixelToLatLong(pixel);
    document.getElementById("myMap").style.cursor='help';    
}

 function MouseMoveSelect(e)
{
    var x = e.mapX;
    var y = e.mapY;
    pixel = new VEPixel(x, y);
    var LL = map.PixelToLatLong(pixel);
    document.getElementById("myMap").style.cursor= 'img/NORMAL08.cur';    
}
 function changeActiveLayerDisplay()
{   
   var opacity = '';
   if (IEVersion < 7)
    {  
    opacity = '1';    
    }
    else
    {   
    opacity = $get('Slider_statewide').value;
   }
   var activeLayerinLayerType = $get('hdfActivePlaceType').value;      
   map.DeleteAllTileLayers();   
   if (activeLayerinLayerType == 'communities')
   {       
    setActiveCommunityLayers();    
    for (var i=0;i<ActiveCommunityLayersArray.length;i++)
    {
    getTopoTiles(opacity,true, "Census2000", ActiveCommunityLayersArray[i]); 
    }        
   }
   else
   {
      var cbCounties = $get('cbShowCounties');
      if (cbCounties.checked == true)
      {
      getTopoTiles(opacity,true, "Census2000", "Counties");        
      }
   }   
}     
   
function setActiveCommunityLayers()
{
    var activelayers = '';
    var cbPlace = $get('cbShowCensusPlaces');
    var cbTract = $get('cbShowCensusTracts');
    //var cbCity = $get('cbShowCityLimits');
    ActiveCommunityLayersArray.clear();
    if (cbTract.checked == true)
    {   
       ActiveCommunityLayersArray.push('CensusTracts');
    }   
    if (cbPlace.checked == true)
    {
     ActiveCommunityLayersArray.push('CensusPlaces');
    }
//    if (cbCity.checked == true)
//    {
//    ActiveCommunityLayersArray.push('CityLimits');
//    }        
}


function toggleLayers(layer)
{
    var activeLayerinLayerType = $get('hdfActivePlaceType');    
    switch (layer)
    {
        case 'CensusPlaces':   
            activeLayerinLayerType.value = "communities";
            break;
        case 'CensusTracts':
            activeLayerinLayerType.value = "communities";
            break;
        case 'CityLimits':
            activeLayerinLayerType.value = "communities";
            break;
        case 'Counties':
            activeLayerinLayerType.value = "counties";
            break;
   }
   changeActiveLayerDisplay();
} 
  
   
function loadCensusPlaces()
{
    var ParamActiveLayers = {"Layers": 'CensusTracts'};
    var ParamService = {"Service": 'Census2000'};                
    WebServices.GetFeatureWebService.GetFeature(ParamActiveLayers.Layers, ParamService.Service, OnSucceeded, OnFailed);     
}

function getHideSliderDivs(Show, SliderNum)
{
      
    var DivElementsSliders = getElementsByClassName(document, "div", "ajax__slider_h_rail");
        if (!Show)
        {   
        if (IEVersion > 6.99 && DivElementsSliders[SliderNum] != undefined)
        {            
        DivElementsSliders[SliderNum].style.visibility = "hidden";     
        DivElementsSliders[SliderNum].parentElement.style.visibility = 'hidden'   
        }
        }
        else
        {  
       if (IEVersion > 6.99 && DivElementsSliders[SliderNum] != undefined)   
        {       
        DivElementsSliders[SliderNum].style.visibility = 'visible';      
        DivElementsSliders[SliderNum].parentElement.style.visibility = 'visible';          
        }       
     }
}
//function OnSucceeded(result)
//{
//if (result != null && result.text != "failed")
//{
//var polyFeatures = result.getElementsByTagName("FEATURE").length;
//for (var i=0; i < polyFeatures; i++)
//{
//    var Feature = result.getElementsByTagName("FEATURE")[i];
//    var polyRings = Feature.getElementsByTagName("RING").length;
//    for (var h=0; h < polyRings; h++)
//    {    
//    var PolyCoords = Feature.getElementsByTagName("RING")[h].text;
//    var Points = PolyCoords.split(/\;/);
//    var polyPoints = new Array();
//    var LatLong = new Array();
//    for (var j=0; j<Points.length; j++)
//    {
//    LatLong = Points[j].split(/\ /);
//    LatLong[1] = parseFloat(LatLong[1]);
//    LatLong[0] = parseFloat(LatLong[0]);
//    polyPoints.push(new VELatLong(LatLong[1],LatLong[0]));  
//    }
//    }
//    var title = 'test';
//    var sDescription = 'test';
//    AddFeaturePoly(polyPoints, title, sDescription, ''); 
//    }    
//}
//}

//function AddFeaturePoly(polyPoints, title, sDescription, isCityCounty)
//           {             
//            shape = new VEShape(VEShapeType.Polygon, polyPoints);
//            //shapeID ++;            
//            //var icon = new VECustomIconSpecification();
//            //var layer = null;         
//            shape.SetCustomIcon("<div>&nbsp;</div>");          
//            shape.SetTitle(title);
//            shape.SetDescription(sDescription);
//            //var Anchor = shape.GetIconAnchor();           
//            //shape.ShowDetailOnMouseOver = false;   
//            //shape.SetLineWidth(3);   
//            //shape.SetZIndex(2000);       
//            //shape.SetLineColor(new VEColor(0,150,100,1.0));
//            //shape.SetFillColor(new VEColor(0,150,100,0.5));                                
//            //map.AddShape(shape);
//            ShapeLayerCensusPlaces.AddShape(shape);           
//         }

//function OnFailed(result)
//{
//alert(result);
//}

//function MapResize()
//{
//try
//{
//OldMapCenter = map.GetCenter();
////Find Browser Window Size
//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;
//}
//myWidth = parseInt(myWidth*.75);
//myHeight = parseInt(myHeight*.65);
//map.Resize(myWidth, myHeight);
//map.PanToLatLong(OldMapCenter);
//}
//catch(e)
//{
//}
//}


Array.prototype.clear = function () {
    this.length = 0;
};

Array.prototype.find = function(searchStr) {
  var returnArray = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    } else {
      if (this[i]===searchStr) {
        if (!returnArray) { returnArray = [] }
        returnArray.push(i);
      }
    }
  }
  return returnArray;
}

Array.prototype.clear=function()
  {
      this.length = 0;
  };
  
function AddMyControl(ControlType)
 {
    switch (ControlType)
    {
    case "InfoQuery":  
    case "Select":  
    var hdfActiveLayers = $get('hdfActivePlaceType');
    if (hdfActiveLayers.value == "communities")
    {
    if (InfoQueryControl == null)
    {
       InfoQueryControl = document.createElement("div"); 
       InfoQueryControl.id = "myInfoQueryControl";
       InfoQueryControl.style.top ="2px"; 
       InfoQueryControl.style.position = "relative";
       InfoQueryControl.style.left = "630px"; 
       InfoQueryControl.className = "InfoQueryControl"; 
       //<img src=img/infoBtn.gif/>
       InfoQueryControl.innerHTML = '<div style="font-size:9px"><input type="checkbox" id=cbQueryCity checked="checked"/> City<input id=cbQueryPlace type="checkbox"/>Place<input id=cbQueryTract type="checkbox"/>Tract</div>';  
       map.AddControl(InfoQueryControl);
       addShim(InfoQueryControl, "myInfoControl");
    }
    }
    break;
    case "Loading":
    if (LoadingControl == null)
    {
       LoadingControl = document.createElement("div"); 
       LoadingControl.id = "myControl";
       LoadingControl.style.top ="185px"; 
       LoadingControl.style.left = "200px"; 
       LoadingControl.className = "LoadingDivControl"; 
       LoadingControl.innerHTML = '<img src="img/indicator_mozilla_blu.gif" /><strong> Loading layer</strong>';  
       map.AddControl(LoadingControl);
       addShim(LoadingControl, "myLoadingControl");
    }
    break;
//    case "Select":
//    if (SelectControl == null)
//    {
//       SelectControl = document.createElement("div"); 
//       SelectControl.id = "myControl";
//       SelectControl.style.top ="185px"; 
//       SelectControl.style.left = "200px"; 
//       SelectControl.className = "LoadingDivControl"; 
//       SelectControl.innerHTML = '<img src="img/indicator_mozilla_blu.gif" /><strong> Loading layer</strong>';  
//       map.AddControl(SelectControl);
//       addShim(SelectControl, "mySelectControl");
//    }
//    break;
   }
    
 }
 function addShim(el, id)
 {
    var shim = document.createElement("iframe");
    shim.id = id;
    shim.frameBorder = "0";
    shim.style.position = "absolute";
    shim.style.zIndex = "1";
    shim.style.top  = el.offsetTop;
    shim.style.left = el.offsetLeft;
    shim.width  = el.offsetWidth;
    shim.height = el.offsetHeight;
    el.shimElement = shim;
    el.parentNode.insertBefore(shim, el);
 }
 function RemoveMyControl(control)
 {
    switch (control)
    {
        case "InfoQuery":
        case "Select":
            if (InfoQueryControl != null)
            {
            map.DeleteControl(InfoQueryControl);
            InfoQueryControl = null;
            }
        break;
//        
//            if (SelectControl != null)
//            {
//            map.DeleteControl(SelectControl);
//            SelectControl = null;
//            }
//        break;
        case "Loading":
            if (LoadingControl != null)
            {
            map.DeleteControl(LoadingControl);
            LoadingControl = null;
            }
        break;
        default:
        break;
    }    
   
 }