﻿
//IE doesn't have Array indexOf.
if (!Array.indexOf) {
    Array.prototype.indexOf = function(o) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == o) return i;
        }
        return -1;
    }
}

//Display Methods
var smActiveX = 1;              //Video-Insight ActiveX Control
var smServerPush = 2;           //Server Push
var smClientPull = 3;           //Client Pull
var smThirdPartyActiveX = 4;    //Third Party ActiveX e.g. Axis SDK Control

//Object vars
var arrSelection = new Array();  //Stores the currently selected cameras
var myTree;
var myLayout;                    //Global reference to layout object        

//Menu arrays
var header_text = new Array("Enterprise Directory", "Facility Directory", "Layout Directory");
var menu_up = new Array("images/ent_up.jpg", "images/fac_up.jpg", "images/lay_up.jpg");
var menu_down = new Array("images/ent_down.jpg", "images/fac_down.jpg", "images/lay_down.jpg");

var is_navigating = false;

//Init vars
var content_w;
var content_h;
var node_clicked = null;

var divNav;
var divContent;
var divTreeview;
var divPtzPnlHdr;
var divPtzPanel;
var divPtzBottom;

//PTZ panel constants
var ptz_panel_active = false;
var ptz_collapsed_offset = Number(navigator.appName.indexOf('Internet Explorer') != -1 ? 19 : 19);
var ptz_expanded_offset = Number(navigator.appName.indexOf('Internet Explorer') != -1 ? 185 : 185);

//Removes duplicates in a JavaScript array
function unique(a) {
    tmp = new Array(0);
    for (i = 0; i < a.length; i++) {
        if (!contains(tmp, a[i])) {
            tmp.length += 1;
            tmp[tmp.length - 1] = a[i];
        }
    }
    return tmp;
}

//Returns true if item 'e' is contained in array 'a'
function contains(a, e) {
    for (j = 0; j < a.length; j++) if (a[j] == e) return true;
    return false;
}

//This section handles the views
function onDirClick(dir) {
    is_navigating = true;
    if (dir != current_dir) {
        document.getElementById("combofield").value = header_text[dir];
        document.getElementById("view0").src = (dir == 0) ? menu_down[0] : menu_up[0];
        document.getElementById("view1").src = (dir == 1) ? menu_down[1] : menu_up[1];
        document.getElementById("view2").src = (dir == 2) ? menu_down[2] : menu_up[2];
    }
    //Update current_dir
    current_dir = dir;
    //Now set the value of the hidden field. The view buttons then trigger a postback.
    document.getElementById("txtView").value = current_dir;
    //Update directory menu
    document.getElementById('iframe_dir_menu').src = 'menu.aspx?type=' + dir;
}

//PTZ Panel Funtions
var allowToggle = true;

function togglePTZ() {
    //Don't allow ptz control if the user has no permission.
    if (!allowToggle) {
        alert('PTZ control not permitted or not available for this camera.');
        return;
    }

    var is_collapsed = divPtzPanel.style.display == 'none';
    if (is_collapsed) {
        expandPTZ();
    } else {
        collapsePTZ();
    }
}

function expandPTZ() {
    divPtzPanel.style.display = 'block';
    divPtzPnlHdr.style.backgroundImage = 'url(images/pnl_hdr_bg.gif)';
    divPtzBottom.style.display = 'block';
    document.getElementById("ptzMinMax").src = "images/minimize.gif"
    onWindowResize();
}

function collapsePTZ() {
    divPtzPanel.style.display = 'none'; // collapse this pane
    divPtzPnlHdr.style.backgroundImage = 'url(images/pnl_collapsed.gif)';
    divPtzBottom.style.display = 'none';
    document.getElementById("ptzMinMax").src = "images/maximize.gif"
    onWindowResize();
    document.getElementById('ptz_pnl_hdr').innerText = 'Pan Tilt Zoom';
}

function clearSelection() {
    //Uncheck all checked nodes and clear the arrSelection array;
    var all_nodes = myTree.get_allNodes();
    //Add nodes to the array
    for (var i = 0; i < all_nodes.length; i++) all_nodes[i].uncheck();
    arrSelection = new Array();
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function getAssociatedCameraNodes(camera_csl) {
    //returns array of nodes based on camera_list
    var arrCameraList = camera_csl.split(',');
    var arrTempCameras = new Array();
    for (var i = 0; i < arrCameraList.length; i++) {
        var camera_id = arrCameraList[i];
        if (camera_id.indexOf('-') >= 0) {
            camera_id = getIdFromDelimitedString(camera_id, '-', 1);
        } else {
            camera_id = parseInt(camera_id);
        }
        var myNode = myTree.findNodeByValue(camera_id);
        if (myNode) {
            arrTempCameras.push(myNode);
        }
    }
    return arrTempCameras;
}

function getIdFromDelimitedString(id_string, delimiter, position) {
    //returns the ID based on delimiter and position in the string
    var arr_temp = id_string.split(delimiter);
    var id = null;
    if (arr_temp.length >= position) {
        id = Number(arr_temp[position]);
    }
    return id;
}

function getLayoutDimensions() {
    var layoutWidth = content_w;    //content_w - 10;   //10px for right border
    var layoutHeight = content_h - 30;  //30px for content header
    return new Array(layoutWidth, layoutHeight);
}

function selectNode(node_value) {
    //Select the specified node
    var node = myTree.findNodeByValue(String(node_value));
    if (node) {
        node.set_selected(true);
        onClientNodeClicked(null, null, node); //perform the default action for the selected node        
    }
}

var hlp_timeout_id;
function showHelp() {
    setHelpTimeout();
    hideAdmin();
    var menu = document.getElementById('iframe_help');
    if (menu) {
        menu.style.display = 'block';
    }
}

function hideHelp() {
    //clear timeout if needed
    cancelHelpTimeout();
    var menu = document.getElementById('iframe_help');
    if (menu) {
        menu.style.display = 'none';
    }
}

function setHelpTimeout() {
    hlp_timeout_id = setTimeout('hideHelp()', 3000);    //time out after three seconds
}

function cancelHelpTimeout() {
    if (hlp_timeout_id != 0) {
        clearTimeout(hlp_timeout_id);
        hlp_timeout_id = 0;
    }
}

var adm_timeout_id;
function showAdmin() {
    setAdminTimeout();
    hideHelp();
    var menu = document.getElementById('iframe_admin');
    if (menu) {
        menu.style.display = 'block';
    }
}

function hideAdmin() {
    //clear timeout if needed
    cancelAdminTimeout();
    var menu = document.getElementById('iframe_admin');
    if (menu) {
        menu.style.display = 'none';
    }
}

function setAdminTimeout() {
    adm_timeout_id = setTimeout('hideAdmin()', 3000);    //time out after three seconds
}

function cancelAdminTimeout() {
    if (adm_timeout_id != 0) {
        clearTimeout(adm_timeout_id);
        adm_timeout_id = 0;
    }
}

function onDirComboClick() {
    //toggle directory filter
    var cboDirBtn = document.getElementById('cboDirBtn');
    var iframeDirMenu = document.getElementById('iframe_dir_menu');
    var isMenuCollapsed = iframeDirMenu.style.visibility == 'hidden';

    iframeDirMenu.style.visibility = isMenuCollapsed ? 'visible' : 'hidden';
    cboDirBtn.src = isMenuCollapsed ? 'images/directory_btn_dn.gif' : 'images/directory_btn_up.gif';

}

function onDirComboMouseOut() {
    //toggle directory filter
    var cboDirBtn = document.getElementById('cboDirBtn');
    var iframeDirMenu = document.getElementById('iframe_dir_menu');

    iframeDirMenu.style.visibility = 'hidden';
    cboDirBtn.src = 'images/directory_btn_up.gif';

}

var stream_timer = 0;
var stream_timeout_msg = '<span style="font: 12px Arial,Verdana; color:white;"><center><br><br>Your video session has been closed in order to conserve bandwidth.<br><br>Please refresh the page or click <a href="default.aspx">here</a> to continue viewing.</span></center>';
function enableStreamTimeout(ms) {
    if (stream_timer != 0) {
        clearTimeout(stream_timer);
    }
    stream_timer = setTimeout('streamTimeout()', ms);
}

function streamTimeout() {
    //Stop video streaming
    var c = document.getElementById('content');
    if (c) {
        var ax = document.getElementById('VILivefeed');
        if (ax) {
            try {
                ax.Close();
            } catch (e) {
                //alert(e);
            }
        }
        c.innerHTML = stream_timeout_msg;
    }
    if (is_maximized) {
        var divFS = document.getElementById('div_fullscreen');
        divFS.innerHTML = stream_timeout_msg;
    }
}
