﻿// JScript File
var identifyTask = new esri.tasks.IdentifyTask(dynamicAlertsURL);
var identifyParams = new esri.tasks.IdentifyParameters();
var cipURL = "";
var camURLBase = "";
var textcamsURL = "";
var allcamsURL = "";
var refreshrate=10;           //SECONDS BETWEEN REFRESH
var camIMGURL;
var selectedFeatureQuery = ""; // used for capturing selected Feature for building URL arguments in createURLForCurrentExtent()
var closureResults;
var dateMask = "mmm d, yyyy 'at' h:MM tt";


function initIdentify(){
console.log("initIdentify begins");
    identifyParams.tolerance = 6;
    identifyParams.returnGeometry = true;
    identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
    map.infoWindow.setTitle("");
    map.infoWindow.resize(250,250);
console.log("initIdentify");
console.log(identifyTask);     
}
 
function doIdentify(evt) {
console.log("doIdentify");
    selectedFeatureQuery = "";
    //identifyParams.layerIds = [0,1]; 
    identifyParams.geometry = evt.mapPoint;
    identifyParams.mapExtent = map.extent;
    identifyTask.execute(identifyParams, function(idResults) { addToMap(idResults, evt.screenPoint); });
    dojo.stopEvent(evt);

}

function addToMap(idResults, screenPoint) {

	try{


    //layerresults = {displayFieldName:null,features:[]};
    closureResults = {displayFieldName:null,features:[]};  
     var content = "";
     map.infoWindow.setContent(content);
     map.infoWindow.setTitle("");
     var infoWindowOK = false;
console.log("Result length: " + idResults.length);
    
    
    if (idResults.length > 0) {
        for (var i=0, il=idResults.length; i<il; i++) {
            var idResult = idResults[i];
console.log("LayerID: " + idResult.layerId);
            //layerId == 0 is road closures

            if (((idResult.layerId == 0) || (idResult.layerId == 1)) && (content==="")) {
                closureResults.features.push(idResult.feature);                
                if (!closureResults.displayFieldName) {closureResults.displayFieldName = idResult.displayFieldName};
                map.infoWindow.setTitle("CLOSURES & CONDITIONS");
                // loop through and show the results
                //content = "";
                selectedFeatureQuery = "clid=" + closureResults.features[0].attributes['GlobalID'];
console.log("selectedFeatureQuery: " + selectedFeatureQuery);
                for (var j=0, jl=closureResults.features.length; j<jl; j++) {
                    //get all the values we need
                    var roadStatus = closureResults.features[j].attributes['Road Status'];
                    var closureName = closureResults.features[j].attributes['Closure Name'];
                    var location = closureResults.features[j].attributes['Location and Limits'];
                    var timeClosed = closureResults.features[j].attributes['Planned Closure Date'];
					var plannedOpen =  closureResults.features[j].attributes['Anticipated Opening Date'];
                    var changeDate = closureResults.features[j].attributes['Change Date'];
                    var closureReason = closureResults.features[j].attributes['Closure Reason'];
					var onsiteContact = closureResults.features[j].attributes['Onsite Contact'];
					var applicant = closureResults.features[j].attributes['Applicant'];
                    var projectNo = closureResults.features[j].attributes['Project No.'];
					var moreInfo = closureResults.features[j].attributes['More Information Link'];
					var detourInfo = closureResults.features[j].attributes['Detour Link'];
                    var now = new Date();
                    var tb=new Date(timeClosed);                      
console.log(closureResults.features[j]);
                    
                    // now some logic

                    content += "<div>"
                   
      
					content += "<strong>" + closureName + ":</strong> " + location;
                    
                    if ((timeClosed == undefined) || (timeClosed == "Null") || (timeClosed == "") || (tb > now)) {
                        var statText;
                         switch (roadStatus){
                            case "Full Closure":
                                statText = "closure";
                                break;
                            case "Open":
                                statText = "opening";
                                break;                            
                            case "Partial Closure":
                                statText = "partial closure";
                                break;
							case "Sidewalk Closure":
                                statText = "sidewalk closure";
                                break;
							case "Right-of-Way Work":
                                statText = "right-of-way work";
                                break;
							case "Detour":
                                statText = "detour";
                                break;
                        } 
                        content +="<br/><br/><span style='font-weight: bold; color: blue;'>Upcoming " + statText + "</span>";
                    }else{
                        var statusCSS;
                        switch (roadStatus){
                            case "Full Closure":
                                statusCSS = "font-weight: bold; color: Red;";
                                break;
                            case "Open":
                                statusCSS = "font-weight: bold; color: Green;";
                                break;                            
                            case "Partial Closure":
                                statusCSS = "font-weight: bold; color: Orange;";
                                break;
							case "Right-of-way Work":
                                statusCSS = "font-weight: bold; color: Orange;";
                                break;
							case "Sidewalk Closure":
                                statusCSS = "font-weight: bold; color: blue;";
                                break;
							case "Detour":
                                statusCSS = "font-weight: bold; color: green;";
                                break;
                        }        
                        content += "<br/><br/><span style='" +statusCSS+"'>" +roadStatus + "</span>";
                    }
console.log(content);
                    if (roadStatus !== "Open"){
                            content +="<br/>Due to: " + closureReason;  
                    }
					
                    
                    if (roadStatus == "Partial Closure"){ 
                        content +="<br/><br/>Effective: " + dateFormat(timeClosed, dateMask);
						content +="<br/>Until: " + dateFormat(plannedOpen, dateMask);
                    }else{           
                        if ((timeClosed !== undefined) && (timeClosed !== "Null")&& (tb <= now)) {
                            if (roadStatus == "Open"){
                                //var theReason;
                                //theReason = closureReason;  
                                //content +="<br/><br/>Closed: " + dateFormat(timeClosed, dateMask) + ", " + theReason;    
                            }else{
                                content +="<br/><br/>Closed: " + dateFormat(timeClosed, dateMask);
								content +="<br/>Until: " + dateFormat(plannedOpen, dateMask);
                            }
                        }else{
                            	content +="<br/><br/>Effective: " + dateFormat(timeClosed, dateMask);
								content +="<br/>Until: " + dateFormat(plannedOpen, dateMask);
                        }    
                    }
                     
                     
console.log("projectNo: " + projectNo);   
					if ((projectNo !== undefined) && (projectNo !== "Null") && (projectNo !== "")) {
                        content +="<br/>Project No.: " + projectNo;
                    } 
                    if ((moreInfo !== undefined) && (moreInfo !== "Null") && (moreInfo !== "")) {
                        content +="<br/><a href='" + moreInfo + "' target='_blank'>More information &gt;&gt;</a>";
                    }    
					if ((detourInfo !== undefined) && (detourInfo !== "Null") && (detourInfo !== "")) {
                        content +="<br/><a href='" + detourInfo + "' target='_blank'>Detour Information &gt;&gt;</a>";
                    }   

                    
					content += "<hr style='margin-top: 7px; margin-bottom:0;color: #cccccc; background-color: #cccccc;'>"
					if ((changeDate !== undefined) || (changeDate !== "Null") || (changeDate !== "")) {
                        content +="<span style='font-size: 85%; color:#a9a9a9;'>Last updated: " + dateFormat(changeDate, dateMask) + "</span>";
                    }
                    content +="</div>";
                }
            
                infoWindowOK = true;
                map.infoWindow.resize(275,200);
            }
        }
        console.log("Content: " + content);
        map.infoWindow.setContent(content);
        if (infoWindowOK === true){
            map.infoWindow.show(screenPoint, map.getInfoWindowAnchor(screenPoint));
        }
    }
	
	}
	catch(err)
  {
  txt="There was an error on this page.\n\n";
  txt+="Error description: " + err.description + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
  }
}

// function to simulate the Identify function but can call from code instead of a click

function doIdentifyProg(mapPoint) {
    var screenPoint;
    screenPoint = esri.geometry.toScreenPoint(map.extent, map.width, map.height, mapPoint);
   // map.graphics.clear();
    identifyParams.geometry = mapPoint;
    identifyParams.mapExtent = map.extent;
    identifyTask.execute(identifyParams, function(idResults) { addToMap(idResults, screenPoint); }, function(err){alert(err.message);});
}


//Traffic cam auto-refresh

function refreshTrafficCam(url){
        theDate = new Date();
        url += '?dummy=' + theDate.getTime().toString(10);
        // The dummy above enforces a bypass of the browser image cache
        if ((document.imgTrafficCam !== undefined) && (document.imgTrafficCam !== null) &&(map.infoWindow.isShowing)) {
            document.imgTrafficCam.src = url;
            camTimer = setTimeout("refreshTrafficCam(camIMGURL)", refreshrate*1000);
            //console.log(url);
            //console.log("refreshTrafficCam " + camTimer);
        }    
}    
function stopTrafficCam(){
//console.log("stopTrafficCam " + camTimer);
  if (camTimer !== null) {
    clearTimeout(camTimer);
    camTimer = 0;
  }
//console.log("stopTrafficCam ()" + camTimer);  
} 
