﻿
//cameraGIcon variable(s) are located in javascripts/Googlemaps/MapInitializer
//One marker group for each entity with each entity having its own marker group for direction
var cameraEMarkerGroup;
var cameraWMarkerGroup;
var cameraNMarkerGroup;
var cameraSMarkerGroup;

var signEMarkerGroup;
var signWMarkerGroup;
var signNMarkerGroup;
var signSMarkerGroup;

var signEBlankMarkerGroup;
var signWBlankMarkerGroup;
var signNBlankMarkerGroup;
var signSBlankMarkerGroup;

var incidentMarkerGroup;
var constructionMarkerGroup;
var plannedEventMarkerGroup;

var tatsFlashingMarkerGroup;
var tatsNonFlashingMarkerGroup;
var tatsGrayNoReadingMarkerGroup;

function AddTatsEntities(tatsEntities) {

    try {

        //If update has called AddTatsEntities, then get rid of the obsolete data first before re-initializing the marker objects again..
        if ((tatsFlashingMarkerGroup != null && tatsFlashingMarkerGroup.getMrksVisible())) {
            TgTATS();
        }

        //Dispose to see new objects in the horizon..
        if (tatsFlashingMarkerGroup != null) {
            tatsFlashingMarkerGroup.Dispose(true); tatsNonFlashingMarkerGroup.Dispose(true);
            tatsGrayNoReadingMarkerGroup.Dispose(true);
        }

        tatsFlashingMarkerGroup = new MarkerGroup(googleMap, tatsFlashingGIcon);
        tatsNonFlashingMarkerGroup = new MarkerGroup(googleMap, tatsNonFlashingGIcon);
        tatsGrayNoReadingMarkerGroup = new MarkerGroup(googleMap, tatsNoReadingGIcon);

        var group;
        
        for (var i = 0; i < tatsEntities.length; i++) {

            group = eval("tats" + tatsEntities[i].Type + "MarkerGroup");
            group.createMrk(tatsEntities[i].Type, tatsEntities[i].Longitude, tatsEntities[i].Latitude, tatsEntities[i].Restriction, '', '',
                tatsEntities[i].EntryTimeText, tatsEntities[i].Location, tatsEntities[i].RoadCondition, 
                tatsEntities[i].Weather, tatsEntities[i].CrewsWorking, 7);
        }

        group = null;

    }
    catch (e) { alert(e.message + " -- AddTatsEntities"); }
}

function TgTATS() {

    try {
        if (typeof (tatsFlashingMarkerGroup) != "undefined")
            tatsFlashingMarkerGroup.tgMrksVisible();

        if (typeof (tatsNonFlashingMarkerGroup) != "undefined")
            tatsNonFlashingMarkerGroup.tgMrksVisible();

        if (typeof (tatsGrayNoReadingMarkerGroup) != "undefined")
            tatsGrayNoReadingMarkerGroup.tgMrksVisible();

    }
    catch (e) {
        alert(e.message + " - TgTATS");
    }
}


function AddCameraEntities(vcsEntities) {

    try { 
        
        //If update has called AddCamera, then get rid of the obsolete data first before re-initializing the marker objects again..
        if((cameraEMarkerGroup != null && cameraEMarkerGroup.getMrksVisible()) ||
           (cameraWMarkerGroup != null && cameraWMarkerGroup.getMrksVisible()) ||
           (cameraNMarkerGroup != null && cameraNMarkerGroup.getMrksVisible()) ||
           (cameraSMarkerGroup != null && cameraSMarkerGroup.getMrksVisible()) ){
            TgCCTV();
        }

        //Dispose to see new objects in the horizon..
        if (cameraEMarkerGroup != null) {
            cameraEMarkerGroup.Dispose(true); cameraWMarkerGroup.Dispose(true);
            cameraNMarkerGroup.Dispose(true); cameraSMarkerGroup.Dispose(true);
        }

        cameraEMarkerGroup = new MarkerGroup(googleMap, cameraGEIcon);
        cameraWMarkerGroup = new MarkerGroup(googleMap, cameraGWIcon);
        cameraNMarkerGroup = new MarkerGroup(googleMap, cameraGNIcon);
        cameraSMarkerGroup = new MarkerGroup(googleMap, cameraGSIcon);

        for (var i = 0; i < vcsEntities.length; i++) {

            direction = vcsEntities[i].Direction;
            group = eval("camera" + direction + "MarkerGroup");
            group.createMrk('Camera', vcsEntities[i].Longitude, vcsEntities[i].Latitude, '', '', '', '',
                    vcsEntities[i].Name, vcsEntities[i].EntityId, vcsEntities[i].Direction, vcsEntities[i].Url, vcsEntities[i].ZoomLevel);
        }

   }
  catch(e){alert(e.message + " -- AddCameraEntities");}
}

function TgCCTV()
{
    try
        {
            if(typeof(cameraEMarkerGroup)!= "undefined")
                cameraEMarkerGroup.tgMrksVisible();

            if (typeof (cameraWMarkerGroup) != "undefined")
                cameraWMarkerGroup.tgMrksVisible();

            if (typeof (cameraNMarkerGroup) != "undefined")
                cameraNMarkerGroup.tgMrksVisible();

            if (typeof (cameraSMarkerGroup) != "undefined")
                cameraSMarkerGroup.tgMrksVisible();
        }
    catch(e)
    {
        alert(e.message + " - TgCCTV");
    }	
}

function AddSignEntities(signEntities) {
    try {

        //If update has called AddCamera, then get rid of the obsolete data first before re-initializing the marker objects again..
        if ((signEMarkerGroup != null && signEMarkerGroup.getMrksVisible()) ||
           (signWMarkerGroup != null && signWMarkerGroup.getMrksVisible()) ||
           (signNMarkerGroup != null && signNMarkerGroup.getMrksVisible()) ||
           (signWMarkerGroup != null && signWMarkerGroup.getMrksVisible()) ||
           (signEBlankMarkerGroup != null && signEBlankMarkerGroup.getMrksVisible()) ||
           (signWBlankMarkerGroup != null && signWBlankMarkerGroup.getMrksVisible()) ||
           (signWBlankMarkerGroup != null && signNBlankMarkerGroup.getMrksVisible()) ||
           (signSBlankMarkerGroup != null && signSBlankMarkerGroup.getMrksVisible())) {

            TgDMS();
        }

        //Dispose to see new objects in the horizon..
        if (signEMarkerGroup != null) {
            signEMarkerGroup.Dispose(true); signWMarkerGroup.Dispose(true);
            signNMarkerGroup.Dispose(true); signSMarkerGroup.Dispose(true);
            signEBlankMarkerGroup.Dispose(true); signNBlankMarkerGroup.Dispose(true);
            signWBlankMarkerGroup.Dispose(true); 
            signSBlankMarkerGroup.Dispose(true);

            signEMarkerGroup = null; signEMarkerGroup = null;
            signNMarkerGroup= null;  signWMarkerGroup = null;
            signEBlankMarkerGroup = null; signWBlankMarkerGroup= null;
            signWBlankMarkerGroup = null; signSBlankMarkerGroup= null;

        } 

        signEMarkerGroup = new MarkerGroup(googleMap, signGEIcon);
        signWMarkerGroup = new MarkerGroup(googleMap, signGWIcon);
        signNMarkerGroup = new MarkerGroup(googleMap, signGNIcon);
        signSMarkerGroup = new MarkerGroup(googleMap, signGSIcon);

        signEBlankMarkerGroup = new MarkerGroup(googleMap, signGEBlankIcon);
        signWBlankMarkerGroup = new MarkerGroup(googleMap, signGWBlankIcon);
        signNBlankMarkerGroup = new MarkerGroup(googleMap, signGNBlankIcon);
        signSBlankMarkerGroup = new MarkerGroup(googleMap, signGSBlankIcon);

        var group, direction, signtype;

        for (var i = 0; i < signEntities.length; i++) {

            try {

                type = signEntities[i].AreBothPhasesEmpty == true ? "Blank" : "";

                direction = signEntities[i].Direction;
                group = eval("sign" + direction + type + "MarkerGroup"); //get the object on the fly
                group.createMrk('Sign', signEntities[i].Longitude, signEntities[i].Latitude, signEntities[i].Phase1Msg, signEntities[i].Phase2Msg, '', '',
                    signEntities[i].Name, signEntities[i].EntityId, '', signEntities[i].IsMultiPhasic, signEntities[i].ZoomLevel);
            }

            catch (e) { continue; }
        }

        group = null;direction = null;signtype = null;

    }
    catch (e) { alert(e.message + " -- AddSignEntities"); }
}


function TgDMS()
{
    try
        {
            if(typeof(signEMarkerGroup)!= "undefined")
                signEMarkerGroup.tgMrksVisible();

            if (typeof (signWMarkerGroup) != "undefined")
                signWMarkerGroup.tgMrksVisible();

            if (typeof (signNMarkerGroup) != "undefined")
                signNMarkerGroup.tgMrksVisible();

            if (typeof (signSMarkerGroup) != "undefined")
                signSMarkerGroup.tgMrksVisible();
                
            
            if (typeof (signEBlankMarkerGroup) != "undefined")
                signEBlankMarkerGroup.tgMrksVisible();

            if (typeof (signWBlankMarkerGroup) != "undefined")
                signWBlankMarkerGroup.tgMrksVisible();

            if (typeof (signNBlankMarkerGroup) != "undefined")
                signNBlankMarkerGroup.tgMrksVisible();

            if (typeof (signSBlankMarkerGroup) != "undefined")
                signSBlankMarkerGroup.tgMrksVisible();
        }
    catch(e)
    {
        alert(e.message + " - TgDMS");
    }	
}

function AddIncidentEntities(incidentEntities) {
    try {

        //If update has called AddCamera, then get rid of the obsolete data first before re-initializing the marker objects again..
        if (incidentMarkerGroup != null && incidentMarkerGroup.getMrksVisible()) {
            TgEvent();
        }

        //Dispose to see new objects in the horizon..
        if (incidentMarkerGroup != null) {
            incidentMarkerGroup.Dispose(true);
        }

        incidentMarkerGroup = new MarkerGroup(googleMap, eventGIcon);

        for (var i = 0; i < incidentEntities.length; i++) {
            incidentMarkerGroup.createMrk('Event', incidentEntities[i].Longitude, incidentEntities[i].Latitude, incidentEntities[i].Location,
                incidentEntities[i].LastUpdated, incidentEntities[i].EventDescription, incidentEntities[i].EstimatedClearTime,
                incidentEntities[i].EventTypeDescription, incidentEntities[i].EventId, incidentEntities[i].Direction, incidentEntities[i].ImpactType, 7);
        }
    }
    catch (e) { alert(e.message + " -- AddEventEntities"); }
}

function TgEvent()
{
    try
        {
            if(typeof(incidentMarkerGroup)!= "undefined")
                incidentMarkerGroup.tgMrksVisible();
        }
    catch(e)
    {
        alert(e.message + " - TgEvent");
    }	
}

function AddConstructionEntities(constructionEntities) {

    try {

        //If update has called AddCamera, then get rid of the obsolete data first before re-initializing the marker objects again..
        if (constructionMarkerGroup != null && constructionMarkerGroup.getMrksVisible()) {
            TgConstruction();
        }

        //Dispose to see new objects in the horizon..
        if (constructionMarkerGroup != null) {
            constructionMarkerGroup.Dispose(true);
        }

        constructionMarkerGroup = new MarkerGroup(googleMap, constructionGIcon);

        for (var i = 0; i < constructionEntities.length; i++) {
            constructionMarkerGroup.createMrk('Construction', constructionEntities[i].Longitude, constructionEntities[i].Latitude,
                constructionEntities[i].Location, constructionEntities[i].LastUpdated,
                constructionEntities[i].EventDescription, constructionEntities[i].EstimatedDuration,
                constructionEntities[i].EventTypeDescription, constructionEntities[i].EventId,
                constructionEntities[i].Direction, constructionEntities[i].EstimatedDuration, 7);
        }
    }
    catch (e) { alert(e.message + " -- AddConstructionEntities"); }
   
}

function TgConstruction()
{
    try
        {
            if(typeof(constructionMarkerGroup)!= "undefined")
                constructionMarkerGroup.tgMrksVisible();
        }
    catch(e)
    {
        alert(e.message + " - TgConstruction");
    }	
}

function AddPlannedEventEntities(plannedEventEntities) {

    try {

        //If update has called AddCamera, then get rid of the obsolete data first before re-initializing the marker objects again..
        if (plannedEventMarkerGroup != null && plannedEventMarkerGroup.getMrksVisible()) {
            TgPlannedEvent();
        }

        //Dispose to see new objects in the horizon..
        if (plannedEventMarkerGroup != null) {
            plannedEventMarkerGroup.Dispose(true);
        }

        plannedEventMarkerGroup = new MarkerGroup(googleMap, plannedEventGIcon);

        for (var i = 0; i < plannedEventEntities.length; i++) {
            plannedEventMarkerGroup.createMrk('PlannedEvent', plannedEventEntities[i].Longitude, plannedEventEntities[i].Latitude,
                plannedEventEntities[i].Location,
                plannedEventEntities[i].LastUpdated, plannedEventEntities[i].EventDescription, 
                plannedEventEntities[i].EstimatedDuration,
                plannedEventEntities[i].EventTypeDescription, plannedEventEntities[i].EventId,
                plannedEventEntities[i].Direction, plannedEventEntities[i].EstimatedDuration, 7);
        }

    }
    catch (e) { alert(e.message + " -- AddPlannedEvent()"); } 
}

function TgPlannedEvent() {

    try {
        if (typeof (plannedEventMarkerGroup) != "undefined")
            plannedEventMarkerGroup.tgMrksVisible();
    }
    catch (e) {
        alert(e.message + " - TgPlannnedEvent");
    }
}



