// site wide Javascript functions
;jQuery(function($) {
    // specify metadata to extract from 'data' attribute
    $.metadata && $.metadata.setType('attr', 'data');

    // fix legend element by wrapping with <span/>
    $("legend").wrap("<span class='legendWrap'></span>");
    
    // nicer checkboxes
    $("form.niceCheckboxes input[type=checkbox]").live('click', updateCheckboxWrappers).each(updateCheckboxWrappers);
    function updateCheckboxWrappers() {
        var $div = $(this).closest(".checkbox");
        if (this.checked) {
            $div.addClass('ui-state-checked');
        } else {
            $div.removeClass('ui-state-checked');
        }
    }
    
    // make flash message hideable
    $("#flashMessage").click(function() {
            $(this).slideUp("normal", function() {
                    $(this).remove();
            });
    });
    
    // hide/show ajax loader
    $(document).ajaxStart(function(e, ui) {
            $("#loading").slideDown();
    });
    $(document).ajaxStop(function() {
            $("#loading").slideUp();
    });
    
    $("a.ajaxDialog").click(function(e) {
            if (this.href) {
                $("#ajaxDialog").load(this.href, null, function(data, status) {
                        var h1 = $("#ajaxDialog h1").text();
                        $("#ajaxDialog h1").remove();
                        $("#ajaxDialog").dialog("option", "title", h1).dialog("open");
                });
                return false;
            }
    });
    
    $("#ajaxDialog").dialog({
        autoOpen : false,
        height : 500,
        width : 640,
        modal : true,
        open : function(e, ui) {
        }
    });
    
    $(".tabs").tabs();
    
    $(".safeSubmit").submit(function() {
            var $submitBtns = $("[type=submit],[type=image]", this).attr("disabled", true).addClass("disabled");
            setTimeout(function() {
                    $submitBtns.attr("disabled", false).removeClass("disabled");
                    $submitBtns = null;
            }, 30000);
    });
    
});

$(window).load(function() {
    // signup popup box
    $("#emailSignup").dialog({
        autoOpen : true,
        title : '',
        modal : true,
        position : ['center', 'center'],
        width : 400,
        height : 275,
        hide : 'fade',
        close : function() {
            $.cookie("Emails[status]", true, { expires : 7 });
        }
    });
    $("#emailSignup a.buttonMaybeLater").click(function() {
        $("#emailSignup").dialog("close");
        return false;
    });
});

if (!window.console) {
    window.console = {
        log : function() {
        }
    }
}


