var gmap = null;
var buildingsService = null;
var trafficAlertQueryTask = null;
var trafficAlertQuery = null;
var constructionAreaQueryTask = null;
var constructionAreaQuery = null;
var mapExtension = null;
var bounds = null;
var gOverlays = null;

function initialize() {

       //alert(GBrowserIsCompatible());
	gmap = new GMap2(document.getElementById("mapDiv"));
       var centerat = new GLatLng(40.764031, -111.838503);
	gmap.addMapType(G_PHYSICAL_MAP);
	
       gmap.addControl(new GLargeMapControl());
       gmap.addControl(new GMapTypeControl());
       gmap.setCenter(centerat, 15);
       gmap.enableScrollWheelZoom();
	
       //Create MapExtension utility class
       mapExtension = new esri.arcgis.gmaps.MapExtension(gmap);

	// Query Tasks
	trafficAlertQueryTask = new esri.arcgis.gmaps.QueryTask("http://gisweb1.it.utah.edu/ArcGIS/rest/services/mapservices/construction_alerts/MapServer/0");
	//constructionAreaQueryTask = new esri.arcgis.gmaps.QueryTask("http://gisweb1.it.utah.edu/ArcGIS/rest/services/mapservices/construction_areas/MapServer/0");
	constructionAreaQueryTask = new esri.arcgis.gmaps.QueryTask("http://gisweb1.it.utah.edu/ArcGIS/rest/services/mapservices/construction_areas_public_facing/MapServer/0");
	
	// Querys
    	trafficAlertQuery = new esri.arcgis.gmaps.Query();
	trafficAlertQuery.outFields = ['Title', 'Description', 'BeginDate', 'EndDate'];
	
	constructionAreaQuery = new esri.arcgis.gmaps.Query();
	constructionAreaQuery.outFields = ['Project','Description', 'beginDate', 'endDate'];
	
	executeTrafficAlertQuery();
	executeConstructionAreaQuery();
		
} 

function executeTrafficAlertQuery() {
	//bounds = gmap.getBounds();

	// clear map overlays and event listeners using MapExtension removeFromMap
	//mapExtension.removeFromMap(gOverlays);

	// set query parameters
	//trafficAlertQuery.queryGeometry = bounds;
	trafficAlertQuery.where = 'objectid is not null';
	trafficAlertQuery.returnGeometry = true;

	// execute query task
	trafficAlertQueryTask.execute(trafficAlertQuery, false, trafficAlertQueryCallback);

}

function executeConstructionAreaQuery() {
	//bounds = gmap.getBounds();

	// set query parameters
	//constructionAreaQuery.queryGeometry = bounds;
	constructionAreaQuery.where = 'objectid is not null';
	constructionAreaQuery.returnGeometry = true;

	// execute query task
	constructionAreaQueryTask.execute(constructionAreaQuery, false, constructionAreaQueryCallback);

}

function trafficAlertQueryCallback(fset) {
    //format UTC dates for display
    var beginDate, endDate, beginDateString, endDateString;
    var beginDay, beginMonth, beginMonth, endDay, endMonth, endYear;
    for(var i=0;i<fset.features.length;i++){
        //begin date
        beginDate = new Date(fset.features[i].attributes.BeginDate);
    	 beginDay = beginDate.getUTCDate();
        beginMonth = beginDate.getUTCMonth() + 1;
        beginYear = beginDate.getUTCFullYear();
        beginDateString = beginMonth + "/" + beginDay + "/" + beginYear;
        fset.features[i].attributes.BeginDate = beginDateString;

        //end date
        endDate= new Date(fset.features[i].attributes.EndDate);
    	 endDay = endDate.getUTCDate();
        endMonth = endDate.getUTCMonth() + 1;
        endYear = endDate.getUTCFullYear();
        endDateString = endMonth + "/" + endDay + "/" + endYear;
        fset.features[i].attributes.EndDate = endDateString;

    }
    
    // create a custom icon
    var taIcon = new GIcon();
    taIcon.image = "./images/i_route.png";
    taIcon.iconSize = new GSize(40, 40);
    taIcon.iconAnchor = new GPoint(20, 20);
    taIcon.infoWindowAnchor = new GPoint(20, 20);
        
    var overlayOptions = {
      markerOptions: {
        icon: taIcon 
      }
    };

	
	var infoWindowOptions = {
      content: "<span style='font-weight: bold;'>Title:</span><br/><span style='color: #000099;'>{Title}</span><br/><br/><span style='font-weight: bold;'>Description: </span><br/><span style='color: #000099;'>{Description}</span><br/><br/><span style='font-weight:bold;'>Begin Date: </span><span style='color: #000099;'>{BeginDate}</span><br/><br/><span style='font-weight:bold;'>End Date: </span><span style='color: #000099;'>&nbsp;&nbsp;&nbsp;{EndDate}</span>",
	  maxWidth: 500
    };
	
	mapExtension.addToMap(fset, overlayOptions, infoWindowOptions);
}

function constructionAreaQueryCallback(fset) {
    //format UTC dates for display
    var beginDate, endDate, beginDateString, endDateString;
    var beginDay, beginMonth, beginMonth, endDay, endMonth, endYear;
    for(var i=0;i<fset.features.length;i++){
        //begin date
        beginDate = new Date(fset.features[i].attributes.beginDate);
    	 beginDay = beginDate.getUTCDate();
        beginMonth = beginDate.getUTCMonth() + 1;
        beginYear = beginDate.getUTCFullYear();
        beginDateString = beginMonth + "/" + beginDay + "/" + beginYear;
        fset.features[i].attributes.beginDate = beginDateString;

        //end date
        endDate= new Date(fset.features[i].attributes.endDate);
    	 endDay = endDate.getUTCDate();
        endMonth = endDate.getUTCMonth() + 1;
        endYear = endDate.getUTCFullYear();
        endDateString = endMonth + "/" + endDay + "/" + endYear;
        fset.features[i].attributes.endDate = endDateString;

    }

	 //overlay symbology     
        var overlayOptions = {
    	 strokeColor:"#FF0000",
        strokeWeight:6,
        strokeOpacity:0.75,
        fillColor:"#FF0000",
        fillOpacity:0.65
    };
	
	var infoWindowOptions = {
      content: "<span style='font-weight: bold;'>Project:</span><br><span style='color: #000099;'>{PROJECT}</span><br/><br/><span style='font-weight: bold;'>Description: </span><br/><span style='color: #000099;'>{description}</span><br/><br/><span style='font-weight: bold;'>Begin Date: </span><span style='color: #000099;'>{beginDate}</span><br/><br/><span style='font-weight: bold;'>End Date: </span><span style='color: #000099;'>&nbsp;&nbsp;&nbsp;{endDate}</span>",
	  maxWidth: 500
    };
	
	mapExtension.addToMap(fset, overlayOptions, infoWindowOptions);
}

function convertUTCHour(hour){
    var convertedHour = hour;

    //check for 12:00 AM
    if(hour == 0){
        convertedHour = 12;
        return convertedHour;
    } 
    //handle the pm hours  
    if(hour > 12){
        convertedHour = hour - 12;
        return convertedHour;
    }
    //if the hour is am and not 12 am, keep the value and return it to caller
    return convertedHour;
}

function UTCAmOrPm(hour){
    if(hour <=11){
        return 'AM';
    }
    return 'PM';
}



