// JavaScript part of the Searchbox view class

// This is a jQuery plugin, but - due to the ID selectors - there can
// only be one searchbox on a page.

(function($) {

    var adv_on = function (event) {
        event.preventDefault();

        jQuery("#jix_searchbox_advanced").slideDown(400, function() {
            jQuery("#jix_searchbox_advanced_off").hide();
            jQuery("#jix_searchbox_advanced_on").show();
        });

        jQuery("input[name=adv]").val(1);
    };

    var adv_off = function (event) {
        event.preventDefault();

        jQuery("#jix_searchbox_advanced").slideUp(400, function() {
            jQuery("#jix_searchbox_advanced_off").show();
            jQuery("#jix_searchbox_advanced_on").hide();
        });

        jQuery("input[name=adv]").val('');
    };

    $.fn.searchbox = function () {
        // Default selection
        if (!jQuery(".jix_checklist_jobage input:checked").length) {
            jQuery(".jix_checklist_jobage input:first").click();
        }
        
        jQuery("#jix_searchbox_advanced_on, #jix_searchbox_advanced_on a").click(adv_off);
        jQuery("#jix_searchbox_advanced_off, #jix_searchbox_advanced_off a").click(adv_on);
        
        var block_opts = {
            message: null,
            overlayCSS:  {
                backgroundColor: '#666',
                opacity: 0.15,
                cursor: 'default'
            },
            applyPlatformOpacityRules: false,
            fadeIn: 0
        };
        
        function regions_block_update() {
            if (jQuery("#jix_searchbox_zipcodes").val()) {
                if (!jQuery("#jix_searchbox_region_container .blockUI").length) {
                    jQuery("#jix_searchbox_region_container .jix_dropdownbox_border_outer").block(block_opts);
                }
            } else {
                jQuery("#jix_searchbox_region_container .jix_dropdownbox_border_outer").unblock({ fadeOut: 0 });
            }
        }
        
        function archive_selected() {
            return jQuery(".jix_dropdownbox_jobage:has(input[name=archive]:checked)").length > 0;
        }
        
        jQuery("#jix_searchbox_zipcodes.error").focus(function () { jQuery(this).removeClass("error") });
        
        jQuery("#jix_searchbox_zipcodes").keyup(function () { regions_block_update() });
        regions_block_update();
        
        // Disable company_q when companyid is in the url
        var url = window.location.href;
        if (url != url.replace(/.*\?.*(companyid=[0-9]+).*/, "$1")) {
            jQuery(".jix_searchbox_border_outer:has(#jix_searchbox_company_q)").block(block_opts);
        } else {
            jQuery("#jix_searchbox input[name=companyid]").remove();
        }
        
        // Disable default input during submission, to avoid empty GET parameters
        // Always keep a query string, though.
        jQuery("form[name=jobsearch]").submit(function () {
            var elements = jQuery("input[value=][name!=q][name!=qs]", this);
        
            if (!archive_selected()) {
        
                elements = elements.add("input[name=mindate],input[name=maxdate]");
            }
            elements.attr('disabled', true);
            var form = this;
            setTimeout(function () { form.submit(); elements.attr('disabled', false); }, 1);
        
            return false;
        });
        
        // Display archive options when archive is selected
        jQuery(".jix_dropdownbox_jobage").bind('newstate', function () {
            var archive_options = jQuery("#jix_searchbox_archive");
            if (archive_selected()) {
                archive_options.slideDown();
            } else {
                archive_options.slideUp();
            }
        });
        
        jQuery("#jix_searchbox_mindate, #jix_searchbox_maxdate").datepicker({
            dateFormat: "yymmdd",
            altFormat: "yymmdd",
            maxDate: 0
        });
        
        jQuery("input[name=qs]:first").focus();
  };

})(jQuery);

