﻿(function($) {  
    // Run-once policy
    if (!$.loaded) $.loaded = {};
    if ($.loaded["site-header"]) return;
    $.loaded["site-header"] = true;
    
    $.fn.syncInputs = function() {
        var these = this;
        function changed(e) {
            these.not($(this)).val($(this).val());
        }
        these.each(function() {
            $(this).change(changed);
            $(this).focus(changed); // catches drag-drop text edits
            $(this).keyup(changed);
        });
    }
    
    $(document).ready(function() {
        // MF: Quick hack to get IE6,7 to not display #content-wrapper on top of #dropdown
        // JS turned off obviously disables this :(
        if (window.ActiveXObject && (parseInt($.browser.version, 10) <= 7)) {
            $("#content-wrapper").after($("#menu"));
            $("#menu").css({
                "position": "absolute",
                "top": "154px"         // Magic number...
            });
            
            function updateMenuLayout() {
                $("#menu").css({
                    "left": $("#header-content").offset().left +"px"
                });
            };
            
            $(window).resize(updateMenuLayout);
            updateMenuLayout();
        }

        // Synchronise the multiple instances of the search forms
        $("input[rel=header_search]").syncInputs();
        $("[id$='_ddlSearchCat']").syncInputs();
        
        /* Activate autocomplete */
        $("input[rel=header_search]").each(function() {
            var txtThis = $(this);
            // "this" is from each()
            (function (txtThis) {
                txtThis.autocomplete({
                    serviceUrl: '/ajax/get_search_hints.aspx',
                    onSelect: function(value, data) {
                        txtThis.closest("fieldset").find("input[type=submit]").get(0).click();
                    }
                });
            })(txtThis);
        });
        
        // Browser bug => browser-sniffing OK
        var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
        if (isChrome) {
            $("input[rel=header_search]").each(function() {
                var txtBox = $(this);
                var elInvisiblePane = txtBox.parent().find(".js-chrome-mouseover-fix-child");
                
                (function (elInvisiblePane, txtBox) {
                    elInvisiblePane.css({"display": "block"});
                    txtBox.focus(function() {
                        elInvisiblePane.hide();
                    });
                    txtBox.blur(function() {
                        elInvisiblePane.show();
                    });
                    elInvisiblePane.mousedown(function(){
                        txtBox.focus();
                        return false;
                    });
                    
                    if (document.activeElement == txtBox.get(0))
                        txtBox.focus();
                    
                })(elInvisiblePane, txtBox);
            });
        }
    });    
})(jQuery);
