(function(jQuery) {

    /**
     * Constructor
     */
    function HS() {
        jQuery(document).ready(jQuery.proxy(this.initialise, this));
    }

    /**
     * Initialises the HS Web Application
     */
    HS.prototype.initialise = function() {

        HS.debug("HS: Initialising");

        var vacancyForm = jQuery("#vacancySearchForm");

        if (vacancyForm.length) {

            var jVacancies = jQuery("#vacancies");

            jVacancies.ajaxStart(function() {
                jQuery(this).addClass("loading");
            });

            jVacancies.ajaxComplete(function() {
                jQuery(this).removeClass("loading");
            });

            // update the vacancies immediately upon loading the page
            //this.updateVacanciesDisplay();

            // ensure that all input items cause the vacancies to update
            vacancyForm.delegate('input', 'click', jQuery.proxy(this.updateVacanciesDisplay, this));

        }

        // handles all clicks on the vacancies
        jQuery("body").delegate('a.job', 'click', jQuery.proxy(this.displayJob, this));

        jQuery(".bookmark").bind('click', this.bookmarkThisPage);

    };

    /**
     * Bookmarks the current page
     */
    HS.prototype.bookmarkThisPage = function() {

        var title = document.title, location = document.location.href;

        if (window.sidebar) { // For Mozilla
            window.sidebar.addPanel(title, location, "")
        } else if (window.external) { // For IE
            window.external.AddFavorite(url, title);
        } else {
            alert("Please press CTRL+D to bookmark this page");
        }

    };

    /**
     * Updates the vacancies on display
     */
    HS.prototype.updateVacanciesDisplay = function() {

        jQuery('#vacancies').empty();

        var sectors = [];

        jQuery('.jobtype:checked').each(
                function () {
                    sectors.push(jQuery(this).val());
                }
                );

        var duration = jQuery('input:radio[name=duration]:checked').val();
        var hours = jQuery('input:radio[name=hours]:checked').val();

        var data = {
            "sector": sectors,
            "duration": duration,
            "hours": hours
        };

        HS.debug("Loading vacancies", data);

        jQuery('#vacancies').load('ajax/vacancies.jsp', data);

    };


    /**
     * Displays a job in a dialog
     */
    HS.prototype.displayJob = function(event) {

        var jLink = jQuery(event.currentTarget),
                jobId = jLink.attr("data-jobid"),
                jobTitle = jLink.text(),
                jDialog = jQuery("<div></div>"),
                inShortList = jLink.attr("data-inshortlist") === "true";

        HS.debug("Displaying vacancy " + jobTitle);

        // add it to the DOM
        jQuery("body").append(jDialog);

        // load the content inside the dialog using the ajax version of the file
        jDialog.load("ajax/" + jLink.attr("href"));

        var buttons = {};
        buttons["Close"] = function() {
            $(this).dialog("close");
        };

        if (inShortList) {
            buttons["Remove from Shortlist"] = function() {
                $(this).dialog("close");
                self.location.href = 'shortlist.jsp?action=remove&jobref=' + jobId
            };
        } else {
            buttons["Add to Shortlist"] = function() {
                $(this).dialog("close");
                self.location.href = 'shortlist.jsp?action=add&jobref=' + jobId
            };
        }

        jDialog.dialog({
            title: jobTitle,
            closeOnEscape: true,
            width: 700,
            height: 400,
            resizable: false,
            buttons: buttons,
            close: function(event, ui) {
                jDialog.empty();
                jDialog.remove();
            }
        });

        event.preventDefault();

    };

    /**
     * Prints out the arguments to the console
     */
    HS.debug = function() {
        try {
            if (window.console && window.console.log) {
                for (var i = 0; i < arguments.length; i++) {
                    window.console.log(arguments[i])
                }
            }
        } catch (error) {
        }
    };

    new HS();

})(jQuery);
