﻿// JavaScript File
var onloaditems = new Array();
var originalOnErrorFunc = null;

/* simple load handler */
function onLoadHandler()
{
    if(typeof onLoad == "function")
    {
        onLoad();
    }

    for (i in onloaditems) 
    { 
        eval(onloaditems[i]) 
    }
}

function onUnloadHandler()
{
    if(typeof onUnload == "function")
    {
        onUnload();
    }
}

function onResizeHandler()
{
    if(typeof onResize == "function")
    {
        onResize();
    }
}

function FindFeed(tag)
{
    // see if we have this feed        
    var i=0;
    var found = false;
    
    while ( ( i < feeds.length) && !found)
    {
        var feed = feeds[i];
        
        if (feed.ID == tag)
        {
            found = true;
        }
        else
        {
            i++;
        }
    }
    
    if(!found)
    {
        i = -1;
    }
    
    return i;
}

/* feeds */
function UpdateFeeds(checked, tag)
{
    var i = FindFeed(tag);
    
    if (checked)
    {
        // user selected to show layer
        if (i == -1)
        {
            // add new layer to array and map
            var feed = new FeedObject(tag);
            feeds.push(feed);
            
            refreshFeed(feed);
        }
        else
        {
            // re-add existing feed
            feeds[i].active = true;
            refreshFeed(feeds[i]);
        }
    }
    else
    {
        // user wanted to remove the feed
        if (i != -1)
        {
            feeds[i].active = false;
            ResetAndRefreshMapPins();
        }
    }
}

function ResetAndRefreshMapPins()
{
    map.DeleteAllShapeLayers();
    clearTimeout(timerID);
    refreshLayers();
}

function FeedObject(tag)
{
    this.shapelayer = new VEShapeLayer();
    this.shapelayer.ID = tag;
    this.oldshapelayer = null;
    this.layerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, 'georss.aspx?dt='+tag, this.shapelayer);
    this.layerSpec.ID = tag;
    this.layerSpec.Method = 'get';
    this.ID = tag;
    this.active = true;
}

function GetMap()
{
    // create our web map
    map = new VEMap('myMap');
    map.ShowMessageBox = false;
    
    try{
        map.LoadMap();
    }catch(e){
        //Firefox browser does not support SGV or VML
    }
                    
    // Store the original SetBestMapView
    originalOnErrorFunc = map.onerror;
    
    // preserve map styles (road map/ariel/hybrid etc.)
    var map_style = LoadMapStyle();
    if( map_style != null )
    {                    
        map.SetMapStyle( map_style );
    }
    map.AttachEvent('onchangemapstyle', SaveMapStyle);   
    map.AttachEvent('onmousewheel', MouseHandler);             
                  
    resizeMap();
}   

function SaveMapStyle(e)
{
    var map_style = map.GetMapStyle();                
    createCookie('MapStyle', map_style, 1);
}

function LoadMapStyle()
{
    var map_style = readCookie('MapStyle');
    return map_style;
}

function MouseHandler(e)
{
    var msg;
    if (e.eventName == "onmousewheel")
    {
        // if zoomout, unset autozoom.
        if(e.mouseWheelChange < 0)
        {
            if(typeof autozoomcbid != "undefined")
            {
                document.getElementById(autozoomcbid).checked = false;
                autoZoom = false;
            }
        }
    }
}
        
function onFeedLoad(layer)
{
    map.onerror = originalOnErrorFunc;
    
    var i = FindFeed(layer.ID);     
    if((i != -1) && (feeds[i].oldshapelayer != null))
    {   
        map.DeleteShapeLayer(feeds[i].oldshapelayer);
        feeds[i].oldshapelayer = null;
    } 
    else 
    {
        // shouldn't get here... don't know why we do.
        ResetAndRefreshMapPins()
    }
           
    //icons (IconId is set by the <icon> tag in GeoRSS feed)
    var numShapes = layer.GetShapeCount();
    for(var j=0; j < numShapes; ++j)
    {
        var s = layer.GetShapeByIndex(j);
        s.SetCustomIcon("<img src='"+s.IconId+"'/>");
    }
    if(autoZoom)
    {
        SetBestMapView();
    }
}

function SetBestMapView() 
{
     var b=[],e=map.GetShapeLayerCount();
     
     for(var c=0;c<e;++c)
     {
        var d=map.GetShapeLayerByIndex(c);
        
        if(d.GetShapeCount()>0)
        {
            var a=d.GetBoundingBox();
            
            if(a!=null)
            {
                b.push(new VELatLong(a.y1,a.x1));
                b.push(new VELatLong(a.y2,a.x2));
            }
        }
     }     
     
     if(b.length>=2)
     {     
        map.SetMapView(b);
     }
}

function refreshLayers()
{
    // Temporarily override the error function
    map.onerror = function(){};
    
    // iterate feed layers and remove and re-add to effectively refresh the data
    for (var i = 0; i < feeds.length; i++)
    {
        refreshFeed(feeds[i]);
    }

    // reschedule timer        
    timerID = self.setTimeout('refreshLayers()', refreshInterval);
}

function refreshFeed(feed)
{
    if (feed.active)
    {
        // buffer changes
        feed.oldshapelayer = feed.shapelayer;
        
        feed.shapelayer = new VEShapeLayer();
        feed.shapelayer.ID = feed.oldshapelayer.ID;
        feed.layerSpec.Layer = feed.shapelayer;

        map.ImportShapeLayerData(feed.layerSpec, onFeedLoad, false);

    } 
    else 
    {
        map.DeleteShapeLayer(feed.shapelayer);
    }
}
            
    
/* window resize events */
function getWindowHeight() 
{
    if (window.self && self.innerHeight) 
    {
        return self.innerHeight;
    }
    if (document.documentElement && document.documentElement.clientHeight) 
    {
        return document.documentElement.clientHeight;
    }
    return 800;
}

function getWindowWidth() 
{
    if (window.self && self.innerWidth) 
    {
        return self.innerWidth;
    }
    if (document.documentElement && document.documentElement.clientWidth) 
    {
        return document.documentElement.clientWidth;
    }
    return 1260;
}

function resizeMap()
{    
    if(map != null)
    {        
        var newHeight = getWindowHeight() - document.getElementById('myMap').offsetTop;           
        var newWidth = getWindowWidth();
                                        
        map.Resize( newWidth, newHeight );
    }
}


/* cookies */
function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else 
	{
	    var expires = "";
	}
	
	document.cookie = name+"="+value+expires+"; path=/";			
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i=0;i < ca.length;i++) 
	{
		var c = ca[i];
		
		while (c.charAt(0)==' ') 
		{
		    c = c.substring(1,c.length);
		}
		
		if (c.indexOf(nameEQ) == 0) 
		{
		    var value = c.substring(nameEQ.length,c.length)		    
		    return value;
		}
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}


/* popup */
function ShowPopup(url, id)
{
    window.open(url, id, 'width=300,height=200,resizable=yes,status=no,location=no')
}

function ConfigureCheckBox(id, dt)
{    
    var checked = (readCookie( "visibility_" + dt ) == "true");
       
    document.getElementById(id).checked = checked;
    
    if(checked)
    {
        UpdateFeeds(true, dt);
    }
}
