﻿ILib.Present = true;
ILib.GlobalIdIndex = 0;
ILib.gaHttp = new Array(); // Global array to hold the Http requests currently active

function ILib() {
}

ILib.IsIE = function () {
    return navigator.appName == 'Microsoft Internet Explorer';
}
ILib.IsIE7 = function () {
    return navigator.appName == 'Microsoft Internet Explorer' && (navigator.appVersion.indexOf('MSIE 7') != -1);
}

ILib.GetEvent = function getEvent(ev) {
    if (isIE())
        return window.event;
    else
        return ev;
}
ILib.GetEventElement = function (ev) {
    return (isIE()) ? event.srcElement : (ev.srcElement ? ev.srcElement : ev.currentTarget);
}

ILib.GetCursorPosition = function (e) {
    e = e || window.event;
    var cursor = { x: 0, y: 0 };
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX +
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

ILib.Resource = function (resource) {
    var path = Theme.ThemeBase;
    return path + resource;
}
ILib.LoadResourceFile = function (foldername, filename, filetype) {
    var fileref = null;
    var path = Theme.ThemeBase + foldername + '/';
    if (filetype == "js") { //if filename is a external JavaScript file
        fileref = document.createElement('script');
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", path+filename + '.js');
    }
    else if (filetype == "css") { //if filename is an external CSS file
        fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", path+filename + '.css');
    }
    else {
        throw "Unknow resource filetype for ILib.LoadResourceFile, fileType='" + filetype + "'";
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}

ILib.GetHttpRequest = function () {
    // Allocate a new Http Request item
    // Shamefully knicked from somewhare on the web - but was so long ago (circa 2002)
    // That I've forgotten who to give credit to.
    var xmlHttp = null;
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
        } catch (E) {
            xmlHttp = false
        }
    }
    if (!xmlHttp) {
        try {
            xmlHttp = new XMLHttpRequest();
        }
        catch (e) {
            xmlHttp = false
        }
    }
    return xmlHttp
}

ILib.GetNamedHttpRequest = function (name) {
    var xmlHttp = ILib.GetHttpRequest();
    if (xmlHttp != null)
        ILib.gaHttp[name] = xmlHttp;
    return xmlHttp;
}

ILib.FreeHttpRequest = function (name) {
    delete ILib.gaHttp[name];
}

ILib.FindHttpRequest = function (name) {
    return ILib.gaHttp[name];
}

ILib.DoIn = function (action, time) {
    setTimeout(function () {
        try {
            eval(action);
        }
        catch (e) {
            alert("Failed to excute task ('" + action + "')\n\n" + e.description);
        }
    }, time);
}

ILib.GetDocumentScrollTop = function () {
    return parseInt(document.body.scrollTop) + parseInt(document.documentElement.scrollTop);
}
ILib.GetDocumentScrollLeft = function () {
    return parseInt(document.body.scrollLeft) + parseInt(document.documentElement.scrollLeft);
}

ILib.GetInsideWindowWidth = function () {
    if (typeof (window.innerHeight) == 'number') {
        return window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        return document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        return document.body.clientWidth;
    }
    return parseInt(document.body.clientWidth) + parseInt(document.documentElement.clientWidth);
}

ILib.GetInsideWindowHeight = function () {
    if (typeof (window.innerHeight) == 'number') {
        return window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        return document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        return document.body.clientHeight;
    }
    return parseInt(document.body.clientHeight) + parseInt(document.documentElement.clientHeight);
}

ILib.AppendStyleRule = function (ruleSet) {
    if (document.styleSheets) {
        if (document.styleSheets.length == 0) {
            appendStyleElement();
        }
        if (document.styleSheets.length > 0) {
            if (document.styleSheets[0].insertRule) {
                document.styleSheets[document.styleSheets.length - 1].insertRule(ruleSet, document.styleSheets[document.styleSheets.length - 1].cssRules.length);
            }
            else if (document.styleSheets[0].addRule) {
                var ruleSetPattern = /(.*)({([^}]*)})/;
                var match = ruleSetPattern.exec(ruleSet);
                if (match) {
                    var selector = match[1];
                    var declarations = match[3];
                    document.styleSheets[document.styleSheets.length - 1].addRule(selector, declarations);
                }
            }
        }
    }
}

ILib.AppendStyleElement = function () {
    if (document.createElement) {
        var styleElement = document.createElement('style');
        if (styleElement) {
            styleElement.type = 'text/css';
            var headElement = document.getElementsByTagName('head')[0];
            headElement.appendChild(styleElement);
        }
    }
}

ILib.PopupAction = function (popupComment, resourceUrl, targetId) {
    ILib.PopupBox(popupComment, true);
    try {
        asyncNavigateSilent(resourceUrl, targetId);
    } catch (e) {
        ILib.PopupBox('Failed to perform action : ' + e.description);
    }
    return false;
}

ILib.PopupContent = function (popupTitle, resourceUrl) {
    var targetId = 'ILib$Popup$' + (ILib.GlobalIdIndex++);
    var c = '<div id="' + targetId + '"><center><img src="'+ILib.Resource('gfx/loading.gif')+'"/></center></div>'
    ILib.PopupBox(c, false, false, true, popupTitle);
    try {
        asyncNavigateSilent(resourceUrl, targetId);
    } catch (e) {
        ILib.PopupBox('Failed to load content : ' + e.description + ", " + e.line);
    }
    return false;
}

ILib.PopupIFrame = function (popupTitle, resourceUrl, height, width) {
    var targetId = 'ILib$Popup$' + (ILib.GlobalIdIndex++);
    var c = '<div id="' + targetId + '">';
    c += '<iframe src="' + resourceUrl + '" class="popupIFrame" style="height:' + height + 'px;width:' + width + 'px" allowtransparency></iframe>';
    c += '</div>';
    ILib.PopupBox(c, false, false, true, popupTitle);
}

ILib.PopupImage = function (popupTitle, imageUrl) {
    ILib.PopupBox('<img alt="' + popupTitle + '" title="' + popupTitle + '" src="' + imageUrl + '"/>', false, false, false, popupTitle);
}

ILib.PopupYouTube = function (popupTitle, video) {

    try {
        var youTube = '<object width="512" height="428" type="application/x-shockwave-flash" data="http://www.youtube.com/v/' + video + '&autoplay=1">';
        youTube += '<param name="movie" value="http://www.youtube.com/v/' + video + '"/>';
        youTube += '<param name=\"wmode\" value=\"transparent\" />';
        youTube += '<em><strong>ERROR:</strong> If you can see this, then <a href="http://www.youtube.com/">YouTube</a> is down or you do not have Flash installed.</em>';
        youTube += '</object>';

        ILib.PopupBox(youTube, false, false, true, popupTitle);
    }
    catch (e) {
        alert("Failed to open YouTube viewing popup\n\n" + e.description);
    }
}

ILib.PopupBox = function popupBox(message) // [showloading,href-okbutton,hideokbutton,popuptitle]
{
    try {

        var popupTitle = 'Status';
        var popupId = 'Ilib$Popup' + (ILib.GlobalIdIndex++);
        var titleId = popupId + '$Title';

        if (arguments[4]) popupTitle = arguments[4];

        var optionDiv = getElement('popupHolder');
        if (optionDiv) {

            var c = '<p>' + message + '</p>';
            if (message.toString().indexOf('<div') != -1) c = message;
            if (message.toString().indexOf('<iframe') != -1) c = message;
            if (arguments[1])
                c += '<center><img src="'+ILib.Resource('gfx/loading.gif')+'" /><br /><br /></center>';
            if (message.toString().indexOf('<iframe') == -1) {
                c += "<center>";
                if (arguments[2])
                    c += '<a href="' + arguments[2] + '"><img class="hotButton" title="Close" src="'+ILib.Resource('gfx/buttons/button_ok.png')+'"/></a>';
                else if (!arguments[3])
                    c += '<img class="hotButton" title="Close" src="'+ILib.Resource('gfx/buttons/button_ok.png')+'" onclick="return closeDiv(\'popupHolder\')"/>';

                c += '</center>';
                c += '<div id="popupResponse"></div>';
            }

            var l = '<div style="z-index:51;" class="_panel iwDragTarget" id="popup" onSelectStart="return false" >';
            l += '<div class="_panelwrappercompact">';
            l += '<div class="_pml">';
            l += '<div class="_pmr"><div>';
            /* fudge the popup width to compensate for long titles */
            l += '<p id="' + titleId + '" class="_pdummy">' + popupTitle + '</p>';
            l += '<div class="_pc" id="' + popupId + '"';
            if (message.toString().indexOf('<div') == -1) l += ' style="padding:8px">';
            else if (!arguments[3]) l += ' style="padding-bottom:8px">';
            else l += '>';
            l += c;
            l += '</div></div>';
            l += '<div class="_ptc iwDrag">';
            l += '<div class="_ptl"></div>';
            l += '<div class="_ptr _ptr-1"><div class="_ptclose" onclick="return closeDiv(\'popupHolder\')"/></div></div>';
            l += '<div class="_ptm">';
            l += '<p class="left">' + popupTitle + '</p>';
            l += '</div>'; // _ptm
            l += '</div>'; // _ptc
            l += '</div>'; // _pmr
            l == '</div>'; // _pml

            l += '<div class="_pbc"><div class="_pbl"></div><div class="_pbm"></div><div class="_pbr"></div></div>';
            l += '</div>'; // _panelwrappercompact
            l += '</div>'; // _Panel

            optionDiv.style.position = "absolute";
            optionDiv.zIndex = "999";

            // need to work this out from the current window dimensions
            optionDiv.style.top = (130 + ILib.GetDocumentScrollTop()) + "px";
            optionDiv.style.left = "170px";
            optionDiv.style.display = 'block';
            optionDiv.innerHTML = l;
            fixupBehavior(optionDiv);

            /* fudge the popup width to compensate for long titles */
            getElement(popupId).style.minWidth = getElementWidth(titleId) + "px";
            getElement(titleId).style.display = 'none';

        }
        else
            alert(message);
    } catch (e) {
        alert(message);
    }
}

ILib.OpenWindow = function (title, resource) {

}
