﻿// JScript File
// this helper function is used only so that properties of the parent to get can be set
// if properties are specified, it will check them to see if they match the node before returning it
function getParentNode(node, props) {
    if (!node)
        return null;

    var result = node.parentNode;

    while (result != null) {
        if (result.nodeType == 1 && (!props || checkNodeHasProperties(result, props)))
            break;

        result = result.parentNode;
    }

    return result;
}

// equivalent of firstChild but crossbrowser (since FF considers spaces and new lines as child nodes)
// if properties are specified, it will check them to see if they match the node before returning it
function getFirstChild(node, props) {
    if (!node)
        return null;

    var result = node.firstChild;

    while (result != null) {
        if (result.nodeType == 1 && (!props || checkNodeHasProperties(result, props)))
            break;

        result = result.nextSibling;
    }

    return result;
}

// equivalent of lastChild but crossbrowser (since FF considers spaces and new lines as child nodes)
// if properties are specified, it will check them to see if they match the node before returning it
function getLastChild(node, props) {
    if (!node)
        return null;

    var result = node.lastChild;

    while (result != null) {
        if (result.nodeType == 1 && (!props || checkNodeHasProperties(result, props)))
            break;

        result = result.previousSibling;
    }

    return result;
}

// equivalent of firstChild but crossbrowser (since FF considers spaces and new lines as child nodes)
// if properties are specified, it will check them to see if they match the node before returning it
function getNextSibling(node, props) {
    if (!node)
        return null;

    var result = node.nextSibling;

    while (result != null) {
        if (result.nodeType == 1 && (!props || checkNodeHasProperties(result, props)))
            break;

        result = result.nextSibling;
    }

    return result;
}

// equivalent of firstChild but crossbrowser (since FF considers spaces and new lines as child nodes)
// if properties are specified, it will check them to see if they match the node before returning it
function getPreviousSibling(node, props) {
    if (!node)
        return null;

    var result = node.previousSibling;

    while (result != null) {
        if (result.nodeType == 1 && (!props || checkNodeHasProperties(result, props)))
            break;

        result = result.previousSibling;
    }

    return result;
}

// this will check the node against the properties specified as an associative array
// if the property is an object, it will check the reference
// else it will check the lowercase value of the properties to see if they match
function checkNodeHasProperties(node, props) {
    if (!node)
        return false;

    for (var property in props)
        if (
                (typeof node[property] == "undefined" ||
                (typeof props[property] == "object" && node[property] != props[property]) ||
                (typeof node[property].toString == "undefined" || typeof node[property].toString().toLowerCase == "undefined" || node[property].toString().toLowerCase() != props[property].toString().toLowerCase()))
                &&
                (node.getAttribute(property) == null ||
                (typeof props[property] == "object" && node[property] != props[property]) ||
                (typeof node.getAttribute(property).toString == "undefined" || typeof node.getAttribute(property).toString().toLowerCase == "undefined" || node.getAttribute(property).toString().toLowerCase() != props[property].toString().toLowerCase()))
            )
        return false;

    return true;
}

// get the actual style of the element
// which includes any properties set inline or in a css file
function getStyle(oElm, strCssRule) {
    if (typeof oElm == "undefined" || oElm == null)
        return null;

    var strValue = "";

    if (document.defaultView && document.defaultView.getComputedStyle)
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    else if (oElm.currentStyle) {
        strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) { return p1.toUpperCase(); });
        strValue = oElm.currentStyle[strCssRule];
    }

    return strValue;
}

// this function will ensure that the best method for adding an event to a control is used based on the browser specifics
function CrossBrowserAddEventHandler(target, eventName, handlerName) {
    // if an ID passed in, get the element
    if (typeof (target) == "string")
        target = document.getElementById(element);

    // make sure the event name is not like 'on'something because of the addEventListener way of dealing with event names
    eventName = eventName.toLowerCase().replace(/^on/, "");

    // mouse wheel event uses a different name in FF
    if (eventName == 'mousewheel' && isFF)
        eventName = 'DOMMouseScroll';

    // if browser supports addEventListener, use it
    if (target.addEventListener)
        target.addEventListener(eventName, handlerName, false);
    // if browser supports attachEvent, use it
    else if (target.attachEvent)
        target.attachEvent("on" + eventName, handlerName);
    // else, use the basic way of attaching an event handler
    else {
        // get the existing handler for this event
        var originalHandler = target["on" + eventName];
        // if something exists
        if (originalHandler)
        // add a function to call the original handler and then our own handler
            target["on" + eventName] = function(e) { originalHandler(e); handlerName(e); };
        else
        // else just set our own handler as the handler for thing event
            target["on" + eventName] = handlerName;
    }
}

// add indexof method to arrays in the browser that don't have support for it
if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}


// function that formats a number of seconds like (HH):MM:SS
function FormatSecondsText(seconds) {
    function stringPadLeft(str, length, chr) {
        for (var i = 0; i < length - str.toString().length; i++)
            str = chr + str;

        return str;
    }

    var hours = parseInt(seconds / (60 * 60));
    seconds -= (hours * 60 * 60);
    var minutes = parseInt(seconds / 60);
    seconds -= minutes * 60;

    return (hours > 0 ? stringPadLeft(hours, 2, "0") + ":" : "") + stringPadLeft(minutes, 2, "0") + ":" + stringPadLeft(seconds, 2, "0");
}


// this function ensures that the item is visible inside the container
function EnsureItemIsVisible(item, container) {
    if ((container.offsetWidth + container.scrollLeft) < (item.offsetLeft + item.offsetWidth)) {
        container.scrollLeft = item.offsetLeft + item.offsetWidth - container.offsetWidth;
    }
    else if (container.scrollLeft > item.offsetLeft) {
        container.scrollLeft = item.offsetLeft;
    }
}