/**
* Add an event handler function to an object
* 
* @param {Object} The object
* @param {String} The event name to listen for, eg "load", "unload", "mousedown"
* @param {Function} The function to call on the event
*/
function addEventHandler(targetObject, eventType, handlerFunc) {
    if (targetObject.addEventListener) {
        targetObject.addEventListener(eventType, handlerFunc, true);
    }
    else if (targetObject.attachEvent) {
        targetObject.attachEvent('on' + eventType, handlerFunc);
    }
}

/**
* Create rounded corners on highlighted divs
*/
function roundedCorners() {

    var divs = document.getElementsByTagName("div");

    for (var i = 0; i < divs.length; i++) {
        var div = divs[i];

        if (div.className.indexOf("Highlight") != -1) {
            var tl = document.createElement("div");
            tl.className = "TopLeft Corner";
            div.appendChild(tl);

            var tr = document.createElement("div");
            tr.className = "TopRight Corner";
            div.appendChild(tr);

            var br = document.createElement("div");
            br.className = "BottomRight Corner";
            div.appendChild(br);

            var bl = document.createElement("div");
            bl.className = "BottomLeft Corner";
            div.appendChild(bl);
        }
    }
}


addEventHandler(window, "load", roundedCorners);

/**
* Uses the current URL to find the link to the current page and select it
*/
function SetLinks() {
    var links = document.getElementsByTagName("a");
    var currentUrl = new String(window.location).toLowerCase();

    for (var i = 0; i < links.length; i++) {
        var link = links[i];
        var href = link.href.toLowerCase();
        if (href == currentUrl || href.replace("default.aspx", "") == currentUrl)
            link.className += " selected";
    }
}

addEventHandler(window, "load", SetLinks);


var dialogueWindows = new Object();

onunload = closeAllDialogues;

function openDialogue(windowName, windowUrl, windowWidth, windowHeight) {
    var dialogue = dialogueWindows[windowName];
    var windowTop = (screen.height / 2) - (windowHeight / 2);
    var windowLeft = (screen.width / 2) - (windowWidth / 2);
    var features = 'height=' + windowHeight + ',width=' + windowWidth + ',top=' + windowTop + ',left=' + windowLeft + ',scrollbars=no,resizable=yes,status=no';

    if (dialogue && !dialogue.closed)
        dialogue.close();

    dialogueWindows[windowName] = window.open(windowUrl, windowName, features);
}

function closeDialogue() {
    if (window.opener && !window.opener.closed) {
        window.opener.focus();
    }
    window.close();
}

function closeAllDialogues() {
    for (windowName in dialogueWindows) {
        var dialogue = dialogueWindows[windowName];

        if (dialogue && !dialogue.closed) {
            dialogue.close();
        }
    }
}

function completeDialogue() {
    if (window.opener && !window.opener.closed) {
        window.opener.location.reload();
    }
    closeDialogue();
}

function parseGetVars() {
    var args = new Array();
    var query = window.location.search.substring(1);
    if (query) {
        var strList = query.split('&');
        for (str in strList) {
            var parts = strList[str].split('=');
            args[unescape(parts[0])] = unescape(parts[1]);
        }
    }
    return args;
}


function fitImage() {
    var offsetWidth = 12;
    var offsetHeight = 30;

    var windowWidth = document.images[0].width + offsetWidth;
    var windowHeight = document.images[0].height + offsetHeight;

    var windowTop = (screen.height / 2) - (windowHeight / 2);
    var windowLeft = (screen.width / 2) - (windowWidth / 2);

    window.moveTo(windowLeft, windowTop);
    window.resizeTo(windowWidth, windowHeight);

    self.focus();
}
