var dialogueWindows = new Object();

onunload = closeAllDialogues;

function openDialogue(windowName, windowUrl, windowWidth, windowHeight, scrollbars) {
    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 + ',resizable=yes,status=yes';

    if (scrollbars)
        features += ",scrollbars=yes";

    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() {
    // Only modify window size if this is a popup window - TB 08/10/2007
    if (window.opener && !window.opener.closed) {

        var browserName = navigator.appName;

        if (browserName == "Microsoft Internet Explorer") {
            var offsetWidth = 12;
            var offsetHeight = 62;
        }
        else {
            var offsetWidth = 12;
            var offsetHeight = 30;
        }

        if (window.outerWidth)
            offsetWidth = window.innerWidth - window.outerWidth;
        if (window.outerHeight)
            offsetHeight = window.outerHeight - window.innerHeight;

        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();
}

onload = SetLinks;

// Uses the url to find the link to the current page and select it
function SetLinks() {
    var links = document.getElementsByTagName("a");

    for (var i = 0; i < links.length; i++) {
        var link = links[i];

        //if( link.href == window.location || link.href.replace( "default.aspx", "" ) == window.location )
        if (new String(window.location).indexOf(link.href) == 0) {
            link.className += " Active";
        }
    }

    $('li.SlideFunction:has(a.Active:first-child)').each(
        function () {

            if ($(this).children().first().hasClass('Active')) {

                var linkHref = $('a.Active:first-child', this).attr('href');
                var windowHref = new String(window.location);
                
                if (windowHref.indexOf(linkHref) == (windowHref.length - linkHref.length)) {
                    //                    $('ul.SlideAction', this).slideDown({
                    //                    duration: 3000,
                    //                    easing: "easeOutQuint"
                } else {
                    $('ul.SlideAction', this).css('display', 'block');
                }
            }
        }
    );
}

function AnimateLinks() {
    //$('li.SlideFunction:has(a.Active:first-child)').each(function () {

    $('li.SlideFunction').each(function () {

        if ($(this).children().first().hasClass('Active')) {

            var linkHref = $('a.Active:first-child', this).attr('href');
            var windowHref = new String(window.location);

            if (windowHref.indexOf(linkHref) == (windowHref.length - linkHref.length)) {
                $('ul.SlideAction', this).slideDown({
                    duration: 3000,
                    easing: "easeOutQuint"
                });
            }
            //             else {
            //                $('ul.SlideAction', this).css('display', 'block');
            //            }
        }
    });
}

function addEventHandler(targetObject, eventType, handlerFunc) {
    if (targetObject.addEventListener) {
        targetObject.addEventListener(eventType, handlerFunc, true);
    }
    else if (targetObject.attachEvent) {
        targetObject.attachEvent('on' + eventType, handlerFunc);
    }
}

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 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 closeWelcomeMessage() {
    if (document.getElementById('DontShowAgain').checked == true)
        createCookie('NeedleSportsWelcomeMessage', 'true', 30);
};