/**
Ideally this is a place where we can put javascript that doesn't need to be at the top of the page.
Currently javscript in here is wishpot-only (not facebook)
**/

jQuery(document).ready(function () {
    // Internet Explorer 6 PNG Fix - not yet in use, we use a filter for now
    // if ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined)) {
    //     DD_belatedPNG.fix('.png');
    // }
    if (isFacebookConnectReload()) return;

    // Fluid Lists (items, collections, and users)
    initFluidLists();

    //run any functions who had registered
    if (typeof (WishpotOnDomReadyFunctions) != "undefined") {
        for (x in WishpotOnDomReadyFunctions) {
            WishpotOnDomReadyFunctions[x]();
        }
    }

    // Toggle Content
    $('dl.toggle dt a').click(function () {
        $(this).parents('dl.toggle:first').find('dd').toggle('fast');
    });
    $('ol.toggle > li, ul.toggle > li').find('a:first').click(function () {
        $(this).nextAll().toggle('fast');
    });

    // Add events to buttons
    $('button').click(function () {
        if ($(this).hasClass('clicked')) {
            return false;
        }
        //only mark submit buttons, otherwise clicked will never be cleared
        if ($(this).attr('type') == 'submit') {
            $(this).addClass('clicked');
        }
    });

    // Buttons Spinners
    $('.b-spinready').click(function () {
        //only mark submit buttons, otherwise clicked will never be cleared
        if ($(this).attr('type') == 'submit') {
            $(this).removeClass('b-spinready').addClass('b-spinner');
        }
    });

    //see if we were instructed to highlight anything
    if (document.location.hash != null && document.location.hash.indexOf("h=") >= 0) {
        var highlightObjName = document.location.hash.substring(document.location.hash.indexOf("h=") + 2);
        if (document.getElementById(highlightObjName) != null)
            WithJQueryUi(function () {
                $("#" + highlightObjName).effect("highlight", {}, 5000);
            });
    }

});

jQuery(window).load(function () {
    WP_DBG("window loaded: " + document.location.href);
    if (isFacebookConnectReload()) return;

    // Prevent links with href="#" from jumping
    $('a[href="#"]').click(function () {
        return false;
    });

    correctErroredImages();
});

function isFacebookConnectReload() {
    return (window.location.search.indexOf('fb_xd_bust') >= 0);
}