﻿// JScript File
// KCGIS Center 6.1.2009
// Contains code for accessing and displaying WSDOT closures
dojo.require("dojo.colors");
var pmSymbol = new esri.symbol.PictureMarkerSymbol('images/dottrafficcam.gif', 16, 16);
//var closureSymbol = new esri.symbol.PictureMarkerSymbol('images/alert_closed.gif', 18, 18);
//var restrictSymbol = new esri.symbol.PictureMarkerSymbol('images/alert_restricted.gif', 18, 18);
//var upcomingSymbol = new esri.symbol.PictureMarkerSymbol('images/alert_future.gif', 18, 18);
var closureSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0]), 1), new dojo.Color("red"));
var restrictSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND, 9, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0]), 1), new dojo.Color("#FF8C00"));
var upcomingSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 9, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0]), 1), new dojo.Color("blue"));

var gsvc = new esri.tasks.GeometryService("http://gismaps.kingcounty.gov/ArcGIS/rest/services/Geometry/GeometryServer");
var outSR = new esri.SpatialReference({ wkid: 2285 });
var wsDOTgraphicLayer = new esri.layers.GraphicsLayer({id:"wsdot"});
var urlSuff = "&action=2"
var wsdotLoaded = false;

function getWSDOTInfoAndMap() {
        esri.request({
          url:"WSDOTfeed.aspx", 
          content:{},
          callbackParamName:"callback",
          load:wsdotRequestComplete, 
          error:esriConfig.defaults.io.errorHandler
        });
}

function wsdotRequestComplete(response, ioArgs){
          console.log("wsdotRequestComplete");
          var featureSet = new esri.tasks.FeatureSet(response);
          console.log("next will call geometry service");
         gsvc.project(featureSet.features, outSR, projectComplete);
}

function projectComplete(features) {
console.log("projectComplete...");
if (features.length > 0) {wsdotLoaded = true;} 
console.log("wsdotLoaded = " + wsdotLoaded);
         var now = new Date();
                    
          for (var i = 0, il = features.length; i < il; i++) {
              //Get the current feature from the featureSet.
              //Feature is a graphic
              var graphic = features[i];
            //  graphic.attributes.IncidentEndTime = eval(new Date(parseFloat(features[i].attributes.IncidentEndTime.substring(6, features[i].attributes.IncidentEndTime.length - 2))));
              graphic.attributes.IncidentStartTime = eval(new Date(parseFloat(features[i].attributes.IncidentStartTime.substring(6, features[i].attributes.IncidentStartTime.length - 2))));
              graphic.attributes.IncidentLastUpdate = eval(new Date(parseFloat(features[i].attributes.IncidentLastUpdate.substring(6, features[i].attributes.IncidentLastUpdate.length - 2))));
              //var infoTemplate = new esri.InfoTemplate("${strtext}", "${*}");
              var infoTemplate = new esri.InfoTemplate();
//              var thewsdotURL = "<a href='http://www.wsdot.wa.gov/traffic/trafficalerts/default.aspx?refnum=" + graphic.attributes.Refrence + urlSuff + "' target='_blank'>More info (external link) &gt;&gt;</a><br/>";
			var thewsdotURL = "<span class='wsdotLink'>This Travel Alert is provided courtesy of the Washington State Department of Transportation (WSDOT). <br/><br/> For more information about this alert, please visit the <a href='http://www.wsdot.wa.gov/traffic/trafficalerts/default.aspx?refnum=" + graphic.attributes.Refrence + urlSuff + "' target='_blank'>WSDOT Web site.</a> (external link)</span><br/>";


// and now for some formatting
              var wsdotContent = ""
              var statusCSS1 = "";
              var closureTitle = graphic.attributes.Road + "&nbsp;&nbsp;" + graphic.attributes.Direction;
              var summary = stCap(graphic.attributes['Summary']);
              var lastUpdate = graphic.attributes['IncidentLastUpdate'];
              var startDate = graphic.attributes['IncidentStartTime'];
			  var tb=new Date(startDate); 
			  var priorityText = graphic.attributes['PriorityText'];
           //   var endDate = graphic.attributes['IncidentEndTime'];
              var mapimage = graphic.attributes['mapimage'];
			  var strText = stCap(graphic.attributes['strtext']);
              var rdStatus = (mapimage.split("-"))[0];
                console.log(rdStatus);
              switch(rdStatus){
                case "Closer", "Closure":
                    statusCSS1 = "wsdotClosed";
                    rdStatus = "Closed";
                    graphic.setSymbol(closureSymbol);
                    break;
                case "Open":
                    statusCSS1 = "bubbleStatusOpen";
                    graphic.setSymbol(restrictSymbol);
                    break;                            
                default:
                    statusCSS1 = "wsdotRestricted";
                    graphic.setSymbol(restrictSymbol); 
             }
			 
			 if (tb > now){
			 	    statusCSS1 = "wsdotUpcoming";
                    graphic.setSymbol(upcomingSymbol);
			 }   


             wsdotContent = "<strong>" + closureTitle + "</strong>";

             wsdotContent +="<br/><span class='bubbleUpdated'>Last updated: " + dateFormat(lastUpdate, dateMask) + "</span><br/><br/>";
			 wsdotContent += "<span class='" +statusCSS1+"'>" + strText + "</span><br/>";
			 wsdotContent +="<span class='wsDOTPriorityText'>" + priorityText +"</span><br/><br/>"
			 //wsdotContent += "<span class='" +statusCSS1+"'>" + rdStatus + "</span><br/>";
			 //wsdotContent +="Due to:&nbsp;" + strText + "<br/><br/>";
			 wsdotContent +="Effective:&nbsp;" + dateFormat(startDate, dateMask) + "<br/><br/>";
			 //wsdotContent +="Anticipated end:&nbsp;" + dateFormat(endDate, dateMask) + "<br/>";			 
             wsdotContent +="<span class='wsdotSummary'>"+ summary +"</span><br/>";
			 wsdotContent += "<br/><br/>" + thewsdotURL;
           
               var titleContent = "<div class='trafficcamTitle'><img hspace='4' src='images/dottrafficcam.gif' /></div>WSDOT Travel Alert&nbsp;";
//var titleContent = "" ;
// titleContent= "<div class='trafficcamTitle'><img hspace='4' src='images/dottrafficcam.gif' /></div>"
//titleContent+= "<div><img src='images/dottrafficcam.gif' />WSDOT Travel Alert&nbsp;<table  width='100%'><tr><td align='right'>" + priorityText +"</td></tr></table></div>";


titleContent= '<div class="titleWrapper"><span class="titleLeftText"><img src="images/dottrafficcam.gif" alt="WSDOT" border="0" align="top">&nbsp;&nbsp;WSDOT Travel Alert</span></div>';
              infoTemplate.setTitle(titleContent);
			  //infoTemplate.setTitle("<div class='trafficcamTitle'><img src='images/dottrafficcam.gif' /></div> WSDOT Travel Alert");
              infoTemplate.setContent(wsdotContent);
              graphic.setInfoTemplate(infoTemplate);

              //Add graphic to the map graphics layer.
              //map.graphics.add(graphic);
              wsDOTgraphicLayer.add(graphic);
              console.log("add graphic " + i + "...");
          }
        if (wsdotLoaded){ 
          dojo.byId("chkWSDOT").checked=true;
          dojo.byId("chkWSDOT").disabled=false;

             graphicsonMouseOver_handler = dojo.connect(wsDOTgraphicLayer, "onMouseOver", function(evt) {
                changeCursor();
                });
             
              graphicsonMouseOut_handler = dojo.connect(wsDOTgraphicLayer, "onMouseOut", function(evt) {
	                changeCursor();
                 });
				 
			    dojo.connect(wsDOTgraphicLayer, "onClick", function(evt) {
	                map.infoWindow.resize(350,350);
					console.log("Graphic clicked");
					//dojo.stopEvent(evt);
                 });	 

              map.addLayer(wsDOTgraphicLayer);	
			  // wsDOTgraphicLayer.hide();
        }
}

function toggleWSDOT(){
console.log("toggleWSDOT");
    if (dojo.byId("chkWSDOT").checked===true){
        wsDOTgraphicLayer.show();
    }else{
        wsDOTgraphicLayer.hide();
    }   
}
function stCap(strObj){
	return(strObj.charAt(0).toUpperCase()+strObj.substr(1));
}

