﻿
var SnapshotPath = "mapsubsystem/snapshots/";           // Path to live camera images.
var WhichWayPath = "images/SnapshotCCTV/direction/"    // Path to the thumbnail images used to indicate direction.


var popCCTVID = '';
var popCCTVDirection = '';
var popCCTVLocTxt = '';
var popCCTVTrafficLandId = ' ';
var popIsVideoStreamAvailable = ' ';

var popDMSID = '';
var popDMSStreetID = '';
var popDMSPhase1Message = '';
var popDMSPhase2Message = '';

var img1dir;            //
var img2dir;            //
var img1direction;      //
var img2direction;      //

function alertCCTV(thisID, thisDirection, thisLocTxt, thistrafficLandId, thisIsVideoStreamAvailable) {
    
    popCCTVID = thisID;
    popCCTVDirection = thisDirection;
    popCCTVLocTxt = thisLocTxt;
    popCCTVTrafficLandId = thistrafficLandId;
    popIsVideoStreamAvailable = thisIsVideoStreamAvailable;

    pop_alertCCTV();
}

function alertDMS(thisDeviceID, thisStreetID, thisPhase1Message, thisPhase2Message) {
    popDMSID = thisDeviceID;
    popDMSStreetID = thisStreetID;
    popDMSPhase1Message = thisPhase1Message;
    popDMSPhase2Message = thisPhase2Message;

    pop_alertDMS(false);
}

/*
* For browsers other than IE, we need to inject a hidden trafficland div into the home page.
Trafficland javascript injects an IFrame into the div container; this injection works fine with
internet explorer. But in FF, chrome, and Safari, this injection confuses the DOM structure of the page
and the website starts doing a postback on actual trafficland javascript, which is not feasible. 

The dynamic injection of iframe is done via embedded javascript. This function does not need to 
be called during run time - only load time is allowed. 
*/
function InsertEmptyIFrameHolderFF() {

    //Get the underlying HTML diff
    var divCameraContent = document.getElementById("DivCameraContent");
    
    //If trafficland srcipt has not been injected on the client side yet, will only happen when the page gets loaded for the first
    //time and user has clicked on the camera icon-- DO NOT CHNAGE THE DIV NAME - Traffic land code would not work
    if (document.getElementById("trafficland_singlecam_start") == null  && BrowserDetect.browser != "Explorer") {

        var divTrafficLand = document.createElement('div');
        divTrafficLand.id = "trafficland_singlecam_start"; //DO NOT CHNAGE DIV ID - Traffic land code would not work
        
        divTrafficLand.innerHTML = ""; 

        var script = document.createElement('script');

        script.language = 'Javascript';
        script.src = 'http://embed.trafficland.com/kcscout/trafficland_singlecam.tljs?webid=' + 8696 + '&key=ea2668e93e5834843e420dce4151bff1';
        script.type = 'text/javascript';

        divTrafficLand.appendChild(script);

        setCookie("TrafficLandId", 8696);
        
        divCameraContent.appendChild(divTrafficLand);
    }
}
function pop_alertCCTV() {

    //old functionality ..in case something breaks
    //    eval('winCCTV = window.open("SnapshotCCTV.aspx?camID=' + parent.popCCTVID + '&cameraTxt=' + parent.popCCTVLocTxt + '" ,"winCCTV","status=no,width=385,height=540,scrollbars=no")');
    //    winCCTV.focus(); 

    try {
        //Get the underlying HTML diff
        var divCameraContent = document.getElementById("DivCameraContent");

        //User must have clicked on the motion camera icon on the google map
        //Empty trafficlandid shows that the web page is loading for the first time
        if ( (popCCTVTrafficLandId == -1 || popCCTVTrafficLandId == " " )) {

                        
            HideOrShowDivs(false); //IF the page is loading for the first time, then the TrafficLand divs are hidden via this function call

            divCameraContent.style.textAlign = "center";

            if (popCCTVID == "") {
                popCCTVDirection = getCookie("popCCTVDirection") ? getCookie("popCCTVDirection") : "N";
                popCCTVID        = getCookie("popCCTVID")        ? getCookie("popCCTVID")        : "M035SBC-09";
                popCCTVLocTxt    = getCookie("popCCTVLocTxt")    ? getCookie("popCCTVLocTxt")    : "I35 S @ LEVEE RD";
            }
                
            var divCameraName = document.createElement("div");
            divCameraName.id  = "DivCameraName";
            divCameraName.appendChild(document.createTextNode(popCCTVID));
            divCameraName.style.textAlign = "center";

            var imageCapture = document.createElement("img");
            imageCapture.border = "0";
            imageCapture.id = "ImgCapture";

            //Create the URL on the fly concatenated by Math.Random(), so that we can tell the browser to get
            //a new image everytime the default page gets refreshed
            imageCapture.src = SnapshotPath + popCCTVID + ".jpg" + '?rand=' + Math.random().toString();
            
            imageCapture.style.textAlign = "center";

            if (BrowserDetect.browser != "Explorer") {
                imageCapture.style.paddingLeft = "7px";
            }

            divCameraName.appendChild(imageCapture);

            divCameraContent.appendChild(divCameraName);

            var divIntersectionName = document.createElement("div");
            divIntersectionName.id  = "DivIntersectionName";
                
            divIntersectionName.appendChild(document.createTextNode(popCCTVLocTxt));
            divIntersectionName.style.textAlign = "center";

            divCameraContent.appendChild(divIntersectionName);

            setCookie("popCCTVDirection", popCCTVDirection);
            setCookie("popCCTVID", popCCTVID);
            setCookie("popCCTVLocTxt", popCCTVLocTxt);

            
        } else {

        var divTrafficLand;

        //check for null, it means page is loading for the first time, assign a default value
        if (getCookie("TrafficLandId") && popCCTVTrafficLandId == " ") {

            popCCTVTrafficLandId = getCookie("TrafficLandId") ? getCookie("TrafficLandId") : 8696; //Should be populating from Cookie
            popCCTVDirection     = getCookie("TrafficLandDirection") ? getCookie("TrafficLandDirection") : "W";
            popCCTVID            = getCookie("TrafficLandCCTVID") ? getCookie("TrafficLandCCTVID") : "M470WBC-06";
        }

        //At this point,we shall already assume that trafficland javascript has been injected into the website
        //Consider this if condition as a worst case scenario
        
        //If trafficland srcipt has not been injected on the client side yet, will only happen when the page gets loaded for the first
        //time and user has clicked on the camera icon-- DO NOT CHNAGE DIV ID - Traffic land code would not work
        if (document.getElementById("trafficland_singlecam_start") == null) {

            divTrafficLand = document.createElement('div');
            divTrafficLand.id = "trafficland_singlecam_start";
            divTrafficLand.style.display = "block";
            divTrafficLand.innerHTML = ""; //very important - otherwise with every click new divs will get added keeping the existing ones intact

            var script = document.createElement('script');

            script.language = 'Javascript';
            script.src = 'http://embed.trafficland.com/kcscout/trafficland_singlecam.tljs?webid=' + popCCTVTrafficLandId + '&key=ea2668e93e5834843e420dce4151bff1';
            script.type = 'text/javascript';

            divTrafficLand.appendChild(script);


            //for the second clickage, just replace the existing Iframe, with new traffic land id
        } else {

            divTrafficLand = document.getElementById("trafficland_singlecam_start");
            divTrafficLand.innerHTML = divTrafficLand.innerHTML.replace("webid=" + getCookie("TrafficLandId"), "webid=" + popCCTVTrafficLandId);
            divTrafficLand.innerHTML = divTrafficLand.innerHTML.replace("webid=" + getCookie("TrafficLandId"), "webid=" + popCCTVTrafficLandId);
        }

        setCookie("TrafficLandId", popCCTVTrafficLandId);
        setCookie("TrafficLandDirection", popCCTVDirection);
        setCookie("TrafficLandCCTVID", popCCTVID);

        divCameraContent.appendChild(divTrafficLand);

        HideOrShowDivs(true);

        }

        DetermineDirection(); //cache important variables, which will be used to show the correct images in ToolTip functionality
        
        if(timeoutid != -1)
            self.clearTimeout(timeoutid);

        timeoutid = self.setTimeout(pop_alertCCTV, 60000); //Timeout every 1 minute

    }
    catch (e) { alert(e); }
}

var timeoutid = -1;

/*
To show trafficland specific divs, we must toggle between the underlying toggle state, whereas, to show snapshot specidfic divs,
we simply delete the existing and add the new ones. Adding new trafficland divs work with IE , but do not work with FF/Chorome since IFrame
support is not consistent among these browsers.
*/
function HideOrShowDivs(showtrafficLandDiv) {

    try {

        if (document.getElementById("DivCameraName") != null) {

            var divCameraContent = document.getElementById("DivCameraContent")
            divCameraContent.removeChild(document.getElementById("DivCameraName"));
            divCameraContent.removeChild(document.getElementById("DivIntersectionName"));
        }
        
        document.getElementById("trafficland_singlecam_start").style.display = showtrafficLandDiv ? "block" : "none";
    }
    catch (e) {

    }

}


function pop_alertDMS(firstTime) {

    //If this function is being called from the HTML and not from a DMS Google icon
    //And if a cookie has already been saved, start assigning our variables the values from cookie
    if (firstTime && getCookie("DMSId")) {
        
        popDMSID            = getCookie("DMSId");
        popDMSPhase1Message = getCookie("DMSPhase1Message");
        
        if(getCookie("DMSPhase2Message")){
            popDMSPhase2Message = getCookie("DMSPhase2Message");
        }
        
        popDMSStreetID      = getCookie("DMSStreetID");
    }
    
    var dmsDivSignContent = document.getElementById("DMSDivSignContent");

    if (document.getElementById("DMSDivSignName") != null) {

        dmsDivSignContent.removeChild(document.getElementById("DMSDivSignName"));
        dmsDivSignContent.removeChild(document.getElementById("DMSDivSignMessage"));
        dmsDivSignContent.removeChild(document.getElementById("DMSDivIntersectionName"));
    }

    var dmsDivSignName = document.createElement("div");
    dmsDivSignName.id  = "DMSDivSignName";
    dmsDivSignName.appendChild(document.createTextNode(popDMSID));

    dmsDivSignContent.appendChild(dmsDivSignName);

    var dmsDivSignMessage = document.createElement("div");
    dmsDivSignMessage.id  = "DMSDivSignMessage";
    dmsDivSignMessage.innerHTML = popDMSPhase1Message;

    dmsDivSignContent.appendChild(dmsDivSignMessage);

    var dmsDivIntersectionName = document.createElement("div");
    dmsDivIntersectionName.id  = "DMSDivIntersectionName";
    dmsDivIntersectionName.appendChild(document.createTextNode(popDMSStreetID));

    dmsDivSignContent.appendChild(dmsDivIntersectionName);

    //Save everything for the next run
    setCookie("DMSId",            popDMSID);
    setCookie("DMSPhase1Message", popDMSPhase1Message);

    if (popDMSPhase2Message != "") {
        setCookie("DMSPhase2Message", popDMSPhase2Message);
    }
   
    setCookie("DMSStreetID",      popDMSStreetID);
    
    _msg1 = popDMSPhase1Message;
    _msg2 = popDMSPhase2Message;

    if (_msg1 != _msg2 && _msg2 != "") {
        IniSgnTm();
    }
}

// ===========================================================
// Determin the direction
// ===========================================================
function DetermineDirection(){

    if ((popCCTVDirection == 'E') || (popCCTVDirection == 'W')) {
        img1dir = 'W';
        img2dir = 'E';
        img1direction = 'West';
        img2direction = 'East';

    }
    else {
        img1dir = 'N';
        img2dir = 'S';
        img1direction = 'North';
        img2direction = 'South';
    }
}



/*
An Awful way of creating a table, but the approach worked splendidly.
*/
function ShowToolTipSnapShot() {

    var table = "<table id=MainTable width=325px>" + 
                 "<tr>" + 
                    "<td >" + 
                        img1direction + 
                    "</td>" + 
                    "<td >" +
                        img2direction +
                    "</td>" + 
                 "</tr>" + 
                 
                 "<tr>"  +
                    "<td >" +
                        '<img src="' + WhichWayPath + popCCTVID + img1dir + '.jpg" alt="" name="ImgDirection1" width="150" height="113" />' +
                    "</td>" +
                    "<td >" +
                        '<img src="' + WhichWayPath + popCCTVID + img2dir + '.jpg" alt="" name="ImgDirection1" width="150" height="113" />' +
                    "</td>" +
                 "</tr>" +
             "</table>";

        showtip(table);    
}

function HideToolTipSnapShot() {
    hidetip();
}


var _tmId1;
var _fl1 = 0;
var _msg1;
var _msg2;
var _lyr1;
var _tmS2 = 1000; //this could change, sometimes the first show tip is too slow(or fast)
//start at _tmS2 since have delay before start timer from initial/first showtip
//this way it seems to be phasing the same from beginning
var _tmS3 = _tmS2;
function IniSgnTm() {
    try {
        _fl1 = 0;
        _lyr1 = document.getElementById('DMSDivSignMessage');
        StpSgnTm();
        StrSgnTm();
    }
    catch (e) {
        //alert(e.message);
    }
}
function StpSgnTm() {
    clearTimeout(_tmId1);
}
function StrSgnTm() {
    if (!_fl1) {
        _fl1 = 1;
        if (_lyr1 != null && _lyr1 != "undefined") {
            _lyr1.innerHTML = _msg1;
        }
        clearTimeout(_tmId1);
        _tmId1 = self.setTimeout("StrSgnTm()", _tmS3);
    }
    else if (_fl1) {
        _fl1 = 0;
        _lyr1.innerHTML = _msg2;
        clearTimeout(_tmId1);
        _tmId1 = self.setTimeout("StrSgnTm()", _tmS3);
    }
    _tmS3 = 2000; //sign time refresh in millisecs
}