﻿var map
var mini
var loadingMessage
var theDistrict
var theCounty
var theCity

var imgNode

var rcEmpty=false;
var markerCount=0;
var lineCount=0;
var closedLineCount=0;

var isIntranet=false;

var geoXml; 


function browser(){
    if (window.XMLHttpRequest && document.all){
        return "ie7";
    }
    else if (window.Iterator){
        return "firefox 2+";
    }
    else if (document.compatMode && document.all){
        return "ie6+";
    }
    else if (Array.every){
        return "firefox 1.5+";
    }
    else if (window.opera){
        return "any opera";
    }
    else if (window.createPopup){
        return "if5.5+";
    }
    else if (window.attachEvent){
        return "ie5+";
    }
    else if (document.all){
        return "ie4+";
    }
    else if (window.getComputedStyle){
        return "safari";
    }
    else {
        return "other";
    }
}

function createMap(){
    document.getElementById('map1').innerHTML="";
    map = new GMap2(document.getElementById('map1'));
  }

function startup(){
    
    	/* 
	 * ADDED BY STUART HARLAN 11/16/2009 TO
	 * PREVENT REDRAWING OF POLYLINES ON ZOOMING
	 */
	GEvent.addListener(map, "zoomend", function() {
        //textMessage.innerHTML='Zooming...';
        //textMessage.style.visibility='visible';
        contextmenu.style.visibility='hidden';
        prevZoomLevel=map.getZoom();
        prevMapCenter=map.getCenter();
        removePointsAndLines();
        removePolygons();
        if (bRadar){hideRadar()}
	});
	
    if (GBrowserIsCompatible()) {
    
        var querystring=window.location.search;
        querystring=querystring.toUpperCase();
        
        if(querystring.indexOf("SHOWWZ=Y")>-1){
            document.getElementById("chkWorkzones").checked=true;
        }
        if(querystring.indexOf("SHOWWZ_FUTURE=Y")>-1){
            document.getElementById("chkWorkzones_Future").checked=true;
        }
        if(querystring.indexOf("SHOWRC=Y")>-1){
            document.getElementById("chkConditions").checked=true;
        }
        if(querystring.indexOf("SHOWINCIDENTS=Y")>-1){
            document.getElementById("chkIncidents").checked=true;
        }
        if(querystring.indexOf("SHOWWZ=N")>-1){
            document.getElementById("chkWorkzones").checked=false;
        }
                if(querystring.indexOf("SHOWWZ_FUTURE=N")>-1){
            document.getElementById("chkWorkzones_Future").checked=false;
        }
        if(querystring.indexOf("SHOWRC=N")>-1){
            document.getElementById("chkConditions").checked=false;
        }
        if(querystring.indexOf("SHOWINCIDENTS=N")>-1){
            document.getElementById("chkIncidents").checked=false;
        }
        if(querystring.indexOf("SHOWINCIDENTS=Y")>-1){
            document.getElementById("chkIncidents").checked=true;
        }
        
        if(querystring.indexOf("DB=")>-1){
            var holddb=querystring.substring(querystring.indexOf("DB=")+3);
            var mydb
            
            if(holddb.indexOf("&")>-1){
                mydb=holddb.substr(0,holddb.indexOf("&"));
            }
            else {
                mydb=holddb.substr(0,holddb.length);
            }
            
            document.getElementById("lnkTextAll").href=document.getElementById("lnkTextAll").href+"?db="+mydb;
            document.getElementById("lnkTextRC").href=document.getElementById("lnkTextRC").href+"&db="+mydb;
        }
    
        if (browser()!='safari'){
    	    if(document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#SVG","1.1")){ 
                _mSvgEnabled = _mSvgForced = true;
            }
        }
        
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        
        var ovMap=new GOverviewMapControl();
        map.addControl(ovMap);
        mini=ovMap.getOverviewMap();
        ovMap.hide();
        
        //map.addControl(new ZoomToStateButton());
        createMarkers('incidents');
        createMarkers('flood');
        createMarkers('workzones');
        createMarkers('conditions');
      createMarkers('workzones_future');
        textMessage=document.createElement("div");
        textMessage.style.color="red";
        textMessage.style.visibility='hidden';
        textMessage.style.fontFamily="Arial";
        textMessage.style.fontSize="12pt";
        textMessage.style.fontWeight="bold";
        textMessage.style.backgroundColor="white";
        textMessage.style.borderRight="black thin solid"; 
        textMessage.style.borderTop="black thin solid";
        textMessage.style.borderLeft="black thin solid";
        textMessage.style.borderBottom="black thin solid";
        textMessage.style.paddingRight="5px";
        textMessage.style.paddingTop="5px";
        textMessage.style.paddingBottom="5px";
        textMessage.style.paddingLeft="5px";
        textMessage.innerHTML="Zooming..."


        
        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(75,25));  
        pos.apply(textMessage);
        map.getContainer().appendChild(textMessage);
        
        contextmenu = document.getElementById('divContext');
        contextmenu.innerHTML+="<a href='javascript:zoomIn();'>Zoom In</a><br />";
        contextmenu.innerHTML+="<a href='javascript:zoomOut();'>Zoom Out</a><br />";
        contextmenu.innerHTML+="<a href='javascript:zoomToPoint();'>Zoom Here</a><br />";
        contextmenu.innerHTML+="<a href='javascript:zoomPrevious();'>Zoom to Previous Extent</a><br />";
        contextmenu.innerHTML+="<a href='javascript:zoomToState();'>Zoom to State</a><br />";
        map.getContainer().appendChild(contextmenu);
        
        toolTip2=document.getElementById('divToolTip2');
        toolTip2.innerHTML="<br/>"
        map.getContainer().appendChild(toolTip2);
        
        var elem = document.getElementsByTagName("div");
        var images = document.getElementsByTagName("img");
        
        for(var i = 0; i<images.length; i++){
        
            var source=images[i].getAttribute("src");
            
            if (source){
                if (source.indexOf("slider.png")>=0){
                    if (document.all){
                        if (window.XMLHttpRequest){
                            GEvent.addDomListener(images[i].parentNode,"mousedown",function(){
                                textMessage.innerHTML='Zooming...';
                                textMessage.style.visibility='visible';
                                contextmenu.style.visibility='hidden';
                                prevZoomLevel=map.getZoom();
                                prevMapCenter=map.getCenter();
                                removePointsAndLines();
                                removePolygons();
                                if (bRadar){hideRadar()}
                            });
                            
                            GEvent.addDomListener(images[i], "mousedown", function(){
                                textMessage.innerHTML='Zooming...';
                                textMessage.style.visibility='visible';
                                contextmenu.style.visibility='hidden';
                                prevZoomLevel=map.getZoom();
                                prevMapCenter=map.getCenter();
                                removePointsAndLines();removePolygons();if (bRadar){hideRadar()}
                                //setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()}",10);
                            });
                        }
                        else {
                            GEvent.addDomListener(images[i].parentNode.parentNode,"mousedown",function(){
                                textMessage.innerHTML='Zooming...';
                                textMessage.style.visibility='visible';
                                contextmenu.style.visibility='hidden';
                                prevZoomLevel=map.getZoom();
                                prevMapCenter=map.getCenter();
                                removePointsAndLines();
                                removePolygons();
                                if (bRadar){hideRadar()}
                            });
                            
                            GEvent.addDomListener(images[i].parentNode, "mousedown", function(){
                                textMessage.innerHTML='Zooming...';
                                textMessage.style.visibility='visible';
                                contextmenu.style.visibility='hidden';
                                prevZoomLevel=map.getZoom();
                                prevMapCenter=map.getCenter();
                                removePointsAndLines();removePolygons();if (bRadar){hideRadar()}
                                //setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()}",10);
                            });
                        }
                    }
                    else {
                        GEvent.addDomListener(images[i].parentNode.parentNode,"mousedown",function(){
//                            textMessage.innerHTML='Zooming...';
//                            textMessage.style.visibility='visible';
//                            contextmenu.style.visibility='hidden';
//                            prevZoomLevel=map.getZoom();
//                            prevMapCenter=map.getCenter();
//                            removePointsAndLines();
//                            removePolygons();
//                            if (bRadar){hideRadar()}
                        });
                        
                        GEvent.addDomListener(images[i], "mousedown", function(){
                            textMessage.innerHTML='Zooming...';
                            textMessage.style.visibility='visible';
                            contextmenu.style.visibility='hidden';
                            prevZoomLevel=map.getZoom();
                            prevMapCenter=map.getCenter();
                            removePointsAndLines();removePolygons();if (bRadar){hideRadar()}
                            //setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()}",10);
                        });
                    }
                }
            }
        }
        
        for(var i = 0; i<elem.length; i++){
        
            var title = elem[i].getAttribute("title");

            if(title=="Zoom In"){
                GEvent.addDomListener(elem[i], "mousedown", function(){
                    textMessage.innerHTML='Zooming...';
                    textMessage.style.visibility='visible';
                    contextmenu.style.visibility='hidden';
                    prevZoomLevel=map.getZoom();
                    prevMapCenter=map.getCenter();
                    if (document.all){
                        if (window.XMLHttpRequest){
                            removePointsAndLines();
                            removePolygons();
                            if (bRadar){hideRadar()}
                        }
                        else {
                            setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()}",10)
                        }
                    }
                    else {
                        removePointsAndLines();
                        removePolygons();
                        if (bRadar){hideRadar()}
                    }
                });
            }
            
            if(title=="Zoom Out"){
                GEvent.addDomListener(elem[i], "mousedown", function(){
                    textMessage.innerHTML='Zooming...';
                    textMessage.style.visibility='visible';
                    contextmenu.style.visibility="hidden";
                    prevZoomLevel=map.getZoom();
                    prevMapCenter=map.getCenter();
                    if (document.all){
                        if (window.XMLHttpRequest){
                            removePointsAndLines();
                            removePolygons();
                            if (bRadar){hideRadar()}
                        }
                        else {
                            setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()}",10)
                        }
                    }
                    else {
                        removePointsAndLines();
                        removePolygons();
                        if (bRadar){hideRadar()}
                    }
                });
            }
            
            if(title=="Return to the last result"){
                GEvent.addDomListener(elem[i], "mousedown", function(){
                    textMessage.innerHTML='Zooming...';
                    textMessage.style.visibility='visible';
                    contextmenu.style.visibility="hidden";
                    prevZoomLevel=map.getZoom();
                    prevMapCenter=map.getCenter();
                    if (document.all){
                        if (window.XMLHttpRequest){
                            removePointsAndLines();
                            removePolygons();
                            if (bRadar){hideRadar()}
                        }
                        else {
                            setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()}",10)
                        }
                    }
                    else {
                        removePointsAndLines();
                        removePolygons();
                        if (bRadar){hideRadar()}
                    }
                });
            }
        }
        
        GEvent.addListener(map,"click",function(overlay,point){
            contextmenu.style.visibility="hidden";
            
            if(overlay){
                if(overlay.point){
                    //overlay.openInfoWindowHtml('<div style="font: Verdana; font-size: 12; white-space:nowrap;"><img src=' + overlay.image.path + ' height=' + overlay.image.height + ' width=' + overlay.image.width + '/><br/><font face="Verdana">' + overlay.html + '</font></div>');
                    //overlay.openInfoWindowHtml('<div style="font: Verdana; font-size: 12; white-space:nowrap;"><font face="Verdana">' + overlay.html + '</font></div>');
                    toolTip2.style.visibility='hidden';
                    textMessage.innerHTML='Zooming...';
                    textMessage.style.visibility='visible';
                    
                    prevZoomLevel=map.getZoom();
                    prevMapCenter=map.getCenter();

			if(overlay.specialEnvelope != null)
			{
				var newZoom=map.getBoundsZoomLevel(
					new GLatLngBounds(
						new GLatLng(
							overlay.specialEnvelope.ymin,
							overlay.specialEnvelope.xmin),
						new GLatLng(
							overlay.specialEnvelope.ymax,
							overlay.specialEnvelope.xmax)));
        			setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()};" +
					"map.setCenter(new GLatLng(" + ((overlay.specialEnvelope.ymin + overlay.specialEnvelope.ymax) / 2) + 
					"," + ((overlay.specialEnvelope.xmin + overlay.specialEnvelope.xmax) / 2) + ")," + newZoom + ");",10);
			}
			else
			{
				var newZoom=map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(overlay.minY,overlay.minX),new GLatLng(overlay.maxY,overlay.maxX)));
        			setTimeout("removePointsAndLines();removePolygons();if (bRadar){hideRadar()};map.setCenter(new GLatLng(" + overlay.point.y + "," + overlay.point.x + ")," + newZoom + ");",10);
			}
                } else if (point) {
                    //alert('point');
                }
            }
        });
        
        GEvent.addListener(map,"movestart",function(){
            contextmenu.style.visibility="hidden";
            prevZoomLevel=map.getZoom();
            prevMapCenter=map.getCenter();
            removePointsAndLines();
            removePolygons();
            if (bRadar){hideRadar()}
        });
               
        GEvent.addListener(map,"moveend",function(){
        
            if (map.getZoom()<11){
                if (document.getElementById('chkConditions').checked){
                    createLines('state_conditions');
                }
                else {
                    createLines('state_conditions','closed');
                }
            }
            else {
                if (document.getElementById('chkConditions').checked){
                    createLines('conditions');
                }
                else {
                    createLines('conditions','closed');
                }
            }
            
            if (map.getZoom()<11){
                if (document.getElementById('chkWorkzones').checked){
                    addPointsToMap('workzones');
                }
if (document.getElementById('chkWorkzones_Future').checked){
                    addPointsToMap('workzones_future');
                }                
                if (document.getElementById('chkIncidents').checked){
                    addPointsToMap('incidents');
                }
            }
            else {
                if (document.getElementById('chkWorkzones').checked){
                    addPointsToMap('workzones');
                    createLines('workzones');
                }
                if (document.getElementById('chkWorkzones_Future').checked){
                    addPointsToMap('workzones_future');
                    createLines('workzones_future');
                }
                
                if (document.getElementById('chkIncidents').checked){
                    addPointsToMap('incidents');
                    createLines('incidents');
                }
                else {
                    createLines('incidents','closed');
                }
                createLines('flood');
            }
            
            for (c=0;c<closedMarkers.length;c++){
                map.addOverlay(closedMarkers[c]);
            }
            
            if (bRadar){showRadar()}
            drawLines();
            
            if (map.getZoom()==7)addMoPolygon();
            
            textMessage.style.visibility='hidden';
            toolTip2.style.visibility="hidden";
            
            map.checkResize();
        });
        
        GEvent.addListener(map,"singlerightclick",function(point,source,overlay){
            clickPoint=point;
            
            if(overlay){
                if(overlay.point){
                    var holdID='';
                    if (overlay.twfid){
                        holdID=overlay.twfid;
                    }
                    appendZoomLink(overlay.point.x,overlay.point.y,overlay.minX,overlay.maxX,overlay.minY,overlay.maxY);
                    appendFeatureLink(holdID);
                }
                else {
                    appendZoomLink();
                    appendFeatureLink();
                }
            }
            else {
                appendZoomLink();
                appendFeatureLink();
            }
            
            var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(point.x,point.y));  
            pos.apply(contextmenu);
            contextmenu.style.visibility = "visible";                    
        });
        
        if (location.search.indexOf("dist")!=-1||location.search.indexOf("county")!=-1||location.search.indexOf("city")!=-1){
            var theURLquery=location.search.split("&");
            for (c=0;c<theURLquery.length;c++){
                if (theURLquery[c].indexOf("dist")!=-1){
                    theDistrict=theURLquery[c].substring(theURLquery[c].indexOf("=")+1,theURLquery[c].length)
                    getEncodedPolygon('district',theDistrict);
                }
                else if (theURLquery[c].indexOf("county")!=-1){
                    theCounty=theURLquery[c].substring(theURLquery[c].indexOf("=")+1,theURLquery[c].length)
                    getEncodedPolygon('county',theCounty);
                }
                else if (theURLquery[c].indexOf("city")!=-1){
                    theCity=theURLquery[c].substring(theURLquery[c].indexOf("=")+1,theURLquery[c].length)
                    getEncodedPolygon('city',theCity);
                }
            }
        }
        else {
            map.setCenter(new GLatLng(38.4, -92.4), 7);
        }
        
        setInterval("refreshTimer()",600000);
        window.resizeBy(-1,-1);
        
        if(querystring.indexOf("SHOWRADAR=Y")>-1){
            if (querystring.indexOf("LOOP=Y")>-1){
                bAnimate=true;
                //setTimeout("createRadarSeries();addRadarSeries();",50);
                setTimeout("createRadarSeries();",50);
                //createWarningSeries();
            }
            else {
                createRadarOverlays2();
                //createWarningOverlays();
                addRadar();
            }
        }
    }
	// check and see if auto refresh is already checked
	autoRefreshChange();
	
	// Added by Stuart Harlan, 03/22/2010
	var regExpPattern = new RegExp("STATEZOOM=([0-9]{1,2})","i");
	if(querystring.match(regExpPattern) != null)
	{
		map.setZoom(parseInt(RegExp.$1));
	}
}

function refreshTimer(){
    //callback to update data;
}

//function ZoomToStateButton() { }
//ZoomToStateButton.prototype = new GControl();
//ZoomToStateButton.prototype.initialize = function() {
//    var divButton=document.createElement("div");
//    divButton=document.createElement("div");
//    divButton.style.color="black";
//    //divButton.style.visibility='hidden';
//    divButton.style.fontFamily="Arial";
//    divButton.style.fontSize="12px";
//    //divButton.style.fontWeight="";
//    divButton.style.backgroundColor="white";
//    divButton.style.borderRight="black 1px solid"; 
//    divButton.style.borderTop="black 1px solid";
//    divButton.style.borderLeft="black 1px solid";
//    divButton.style.borderBottom="black 1px solid";
//    divButton.style.paddingRight="1px";
//    divButton.style.paddingTop="1px";
//    divButton.style.paddingBottom="1px";
//    divButton.style.paddingLeft="1px";
//    divButton.style.cursor="hand";
//    divButton.innerHTML="Statewide View"
//    
//    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(200,25));  
//    pos.apply(divButton);
//    map.getContainer().appendChild(divButton);
//    
//    GEvent.addDomListener(divButton,"click",function(){
//        alert('zoom to state');
//    });
//    return divButton;
//}

//ZoomToStateButton.prototype.getDefaultPosition = function() {
//     return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(200, 25));
//}

var timerId = null;

function triggerRefresh()
{
	refresh();
	var e = document.getElementById("_lastRefresh");
	var d = new Date();
	e.innerHTML = "Last AutoRefresh: " + d.toString();
}

function autoRefreshChange()
{
	var e = document.getElementById("_autoRefresh");
	var l = document.getElementById("_autoRefreshStatus");
	if(e != null)
	{
		if((e.checked == true) && (timerId == null))
		{
			timerId = window.setInterval(triggerRefresh, 300000);
			l.innerHTML = "Is ON";
		}
		else if(timerId != null)
		{
			window.clearInterval(timerId);
			timerId = null;
			l.innerHTML = "Is OFF";
		} else {
			l.innerHTML = "Is OFF";
		}
	}
	else if(timerId != null)
	{
		clearInterval(timerId);
		timerId = null;
		l.innerHTML = "Is OFF";
	}
}




