/**
 * @fileoverview
 * Important site initialisation functions and workarounds for the usual array
 * of IE 6 rendering problems.
 */

$(function() {
    initialiseDropdownMenus();
    initialiseOtherSitesDropdowns();

    if ($.browser.msie && $.browser.version == "6.0") {
        jQuery(window).resize(fixMainWidth);
        fixMainWidth();
    }
    else if ($.browser.msie && $.browser.version == "7.0") {
        jQuery(window).resize(fixIe7FooterCenter);
    }
});


function fixIe7FooterCenter() {
    var pageBodyWidth = jQuery('body').width();
    var footerLeftMargin = (pageBodyWidth / 2) -270;
    jQuery('#footer_nav').css("left", footerLeftMargin);
}



/**
 * A function for IE 6 that will fix the width of #main and attempt to stop the
 * worst of IE's background image flickering issues. It's possible this could
 * now be done with only the BackgroundImageCache call and a CSS expression,
 * but hell, it works, and this is easier to debug than CSS expressions.
 */
function fixMainWidth() {
    try {
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch (err) {}


    var body = $("body");
    var width = $(window).width();
    try {
    if (body.hasClass("left_background") && body.hasClass("right_background")) {
        var width = width - 550;
        if (productsPageWidthFix && width < 600) {
            width = 600;
            jQuery('body').css("width", "1155");
        }
        else if (productsPageWidthFix && width > 600) {
            width = 600;
            jQuery('body').css("width", $(window).width());
        }
        else if (width < 515) {
            width = 515;
        }
        else if (width > 600) {
            width = 600;
        }
        $("#main").width(width);
    }
    }
    catch (e) {}
}

/**
 * Initialises the header and product navigation dropdown menus with the jQuery
 * hover intent plugin where appropriate.
 */
function initialiseDropdownMenus() {
    /* Use hover intent for the product navigation to allow for a more
     * forgiving user experience when using the second level navigation. */
    var options = {
        over: function() { $(this).addClass("over"); $("ul", this).bgiframe(); },
        out: function() { $(this).removeClass("over"); },
        timeout: 125
    };
    $("#product_nav .dropdown_menu li").hoverIntent(options);

    /* The top header just gets regular hover behaviour, per internal
     * discussions during development, since it doesn't have second level
     * navigation. */
    $("#header .dropdown_menu li").hover(function() { $(this).addClass("over"); $("ul", this).bgiframe(); }, function() { $(this).removeClass("over"); });
}

/**
 * Initialises the dropdown behaviour of the other sites navigation. Because
 * three of the elements there have login forms that appear on hover, we need
 * to stop them from hiding for a few seconds after mouseout if the input
 * fields are still being used.
 *
 * My understanding of how this works is that we have a state hash within this
 * function's scope, which includes a flag for whether the dropdown should be
 * "locked" on mouseout. Shockingly, this flag is called locked. This is
 * controlled by focus/blur event handlers on the input fields within the other
 * sites navigation, which set the flag appropriately, and the flag is then
 * looked at on mouse out to determine whether the dropdown should disappear
 * immediately or wait a few seconds.
 *
 * The background image handling for the inputs is also handled in here, since
 * it's part of dealing with the same set of elements.
 */
function initialiseOtherSitesDropdowns() {
    // Top menu tooltip functionality
    var lock = {'target' : null, 'locked': false, 'lostFocus' : false };
    $("#other_sites li").unbind();
    $("#other_sites li").unbind();
    $("#other_sites li").hover(function(e) { 
        if (lock.target !== null) {
            $(lock.target).removeClass("over"); 
            lock.target = null;
            lock.locked = false;
        }
        $(this).addClass("over"); 
        $("ul", this).bgiframe(); 
        lock.target = this; 

        // Clear timer
        if (lock.lostFocus !== false) {
            clearTimeout(lock.lostFocus);
            lock.lostFocus = false;
        }
    }, 
    function(e) { 
        if (!lock.locked) {
            $(this).removeClass("over"); 
            lock.target = null;
            lock.locked = false;
        }
        else {
            lock.lostFocus = setTimeout(function() {
                if (lock.lostFocus !== false) {
                    $(lock.target).removeClass("over"); 
                    lock.target = null;
                    lock.locked = false;
                    lock.lostFocus = false;
                }
            }, 3000);
        }
    });

    // Initialise username/password field background behaviour.
    $("#header .username, #header .password, #main .username, #main .password, #search_q")
        .each(function() { this.value || $(this).addClass("initial"); })
        .blur(function() { this.value || $(this).addClass("initial"); })
        .focus(function() { $(this).removeClass("initial"); });

    // Set special focus rules for login forms
    var focus_list = ["username", "password"];
    for (var i in focus_list) {
        var target = "#header .dropdown_menu li ." + focus_list[i];
        target = $(target);
        target.focus(function() { if (lock.target !== null) lock.locked = true; });
        target.blur(function() { if (lock.target !== null) lock.locked = false; });
    }
}

// vim: set et ts=4 sw=4 cin nopaste:
