/// <reference path="jquery-1.4.4-vsdoc.js" />

//wrap the $ in a anonymous function to prevent faults with other libraries an run everything in this scope
var application = (function($)/// <param name="$" type="jQuery" />
{
    $ = jQuery;
    //function object instead of annonymous functions (profilers give you the function names => better to debug )
    var app = {

        //document ready initialisation
        initReady: function() {
            app.makeEnterClickable();
            app.setDisabledClasses();
            app.initCollapsiblePanels();
        },

        //window load initialisation
        initLoad: function() {
            app.setFocus();
        },

        //initiate collapsible panels
        initCollapsiblePanels: function() {
            $("#wrapper").undelegate("a.trigger", "click.trigger");
            $("#wrapper").delegate("a.trigger", "click.trigger", function(e) {
                e.preventDefault();
                $(this).toggleClass("expanded");
                $(this).next(".triggerPanel").stop().slideToggle("fast");
            });
        },

        //give focus to the first visible input element on the page for usability
        setFocus: function() {
            $(":input:visible:first").focus();
        },

        //set disabled classes on links
        setDisabledClasses: function() {
            $("a[disabled=disabled]").addClass("disabled");
        },

        //make input elements fire the submit action on enter click and cancel action on esc click
        makeEnterClickable: function() {
            $(".clicker :text,.clicker :password").unbind("keydown");
            $(".clicker :text,.clicker :password").keydown(
                function(evt) {
                    var evt = (evt) ? evt : ((event) ? event : null);
                    //cache the enter button
                    var $enterButton = $(this).parents(".clicker:eq(0)").find(".enterAction");
                    //cache the cancel button
                    var $cancelButton = $(this).parents(".clicker:eq(0)").find(".cancelAction");
                    if (evt.keyCode == 13) {
                        // enter click action
                        if ($enterButton.attr("href")) {
                            eval($enterButton.attr("href").replace("javascript:", ""));
                        } else if ($enterButton.attr("onclick")) {
                            $enterButton.trigger("click");
                        } else {
                            $enterButton.trigger("click");
                        }
                        return false;
                    } else if ((evt.keyCode == 27) && ($cancelButton.exists())) {
                        //escape click action
                        if ($cancelButton.attr("href")) {
                            eval($cancelButton.attr("href").replace("javascript:", ""));
                        } else if ($cancelButton.attr("onclick")) {
                            $cancelButton.trigger("click");
                        } else {
                            $cancelButton.trigger("click");
                        }
                        return false;
                    }
                });
        }
    };
    $(document).ready(app.initReady);

    //functionality that needs to run after the page has finished loading
    $(window).load(app.initLoad);

    // return the public methods and variables
    return {
        initReady: app.initReady
    }
})(jQuery);

//function to see if an element exists
jQuery.fn.exists = function() { return jQuery(this).length > 0; }

/* solves the firebug console calls in browsers without firebug */
if (!('console' in window) || !('firebug' in console)) {
    var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
    window.console = {};
    for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() { };
}

//for the scriptmanager, needs to be at the end of the script
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
