;(function($) {
    if (!window.wcm_editing) {
        var d = document.domain.split(".");
        if (d.length >= 2) document.domain = d[d.length-2] + "." + d[d.length-1];
    } 
    $.fn.quickFinder = function(settings) {
        var config = {
            proxyHeightFn: null,
            expandSpeed: 250,
            collapseSpeed: 150,
            tabHoverTimeout: 250,
            tabHoverTimeout2: 1000,
            bumpDistance: 5
        };
        var tabHoverTimeoutID = null;
        
        if (settings) $.extend(config, settings);
 
        this.each(function() {
            var noIframe = true;
            if (config.proxyHeightFn == null) {
                try {
                    if (parent._RU_QuickFinder_Set_Height) {
                        config.proxyHeightFn = parent._RU_QuickFinder_Set_Height;
                        noIframe = false;
                    }
                    else config.proxyHeightFn = function() {};
                } catch(ex) {
                    config.proxyHeightFn = function() {};
                }
            }
            var qf = $(this);
            var originalHeight = qf.outerHeight();
            var bumpHeight = originalHeight + config.bumpDistance;
            qf.data("originalHeight", originalHeight);
            var f = $(".quick-finder-flyout", qf);
            var qfo = $(".quick-finder-other", f);
            var searchText = "Search Radford University";
            var trim = $.trim;
            var qs = $("form.quick-finder-search", f);
            qs
                .find(":text")
                    .focus(function() {
                        if (trim($(this).val().toLowerCase()) == searchText.toLowerCase()) $(this).val("");
                    })
                    .blur(function() {
                        if (trim($(this).val()) == "") $(this).val(searchText);
                    })
                    .end()
                .find(":submit")
                    .hover(
                        function() {
                            var t = trim($(":text", qs).val()).toLowerCase();
                            if (t && t != searchText.toLowerCase()) $(this).addClass("submit-hover");
                        },
                        function() {
                            $(this).removeClass("submit-hover");
                        }
                    )
                    .click(function() {
                        $(this).blur();
                        var t = trim($(":text", qs).val()).toLowerCase();
                        return t && t != searchText.toLowerCase();
                    });
            
            var fT = f.css("top");
            var fL = f.css("left");
            f.css({
                /*top: -9999,
                left: -9999,*/
                display: "block"
            });
            var qfoW = qfo.innerWidth();
            var qfoH = qfo.innerHeight();
            var oL = $(".quick-finder-other-links", qfo);
            var cL = $(".quick-finder-common-links", qfo);
            var oLoL = oL.position().left;
            var olW = qfoW - oLoL + 1;
            
            oL.add(cL).css("width", olW);

            var maxHeight = 0;
            oL.each(function(idx) {
                $(this).show();
                $(this).css("width", olW);
                var oLh = $(this).height();
                if (oLh > maxHeight) maxHeight = oLh;
                $(this).hide();
            });

            var cLh = cL.innerHeight();
            if (cLh > maxHeight) maxHeight = cLh;
            
        
            var qfoIH = qfo.height();
            var qfoPadding = qfoH - qfoIH;
            
            if (maxHeight > qfoH) {
                qfo.css("height", maxHeight - qfoPadding);
            } else {
                maxHeight = qfoH;
            }
            
            oL.add(cL).css(
                    {
                        height: maxHeight,
                        display: "none"
                    }
                );
            var expandedHeight = originalHeight + maxHeight;
            cL.show();
            f.css({
                top: fT,
                left: fL,
                display: "none"
            });
            $("ul.quick-finder-global li.expand,ul.main li.quick-finder-global a", qf)
                .hover(
                    function() {
                        $(this).addClass("hover");
                        if (noIframe && !$(this).hasClass("collapse")) {
                            f.show();
                            qf.stop().animate(
                                { height: bumpHeight },
                                100,
                                "swing"
                            );
                        }
                    },
                    function() {
                        $(this).removeClass("hover");
                        if (noIframe && !$(this).hasClass("collapse")) {
                            qf.stop().animate(
                                { height: originalHeight },
                                200,
                                "swing"
                            );
                        }
                    }
                )
                .click(function() {
                    var li = $(this).closest("li");
                    var h = qf.data("originalHeight");
                    if (li.hasClass("collapse")) {
                        //f.hide();
                        if (tabHoverTimeoutID) window.clearTimeout(tabHoverTimeoutID);
                        $("ul.quick-finder-other-tabs li", qfo).removeClass("active");
                        $(".quick-finder-other-links", qfo).hide();
                    } else {
                        f.show();
                        h += f.outerHeight();
                    }
                    li.toggleClass("collapse");
                    if (noIframe) {
                        qf.stop().animate(
                            { height: h },
                            li.hasClass("collapse") ? config.collapseSpeed : config.expandSpeed,
                            "swing"
                        );
                    } else {
                        qf.css("height", h);
                        config.proxyHeightFn(h);
                    }
                    return false;
                });
            $("ul.quick-finder-other-tabs li a", qfo)
                .hover(
                    function() {
                        if (tabHoverTimeoutID) window.clearTimeout(tabHoverTimeoutID);
                        $(this)
                            .parent()
                                .siblings(".active")
                                    .removeClass("active")
                                    .end()
                                .addClass("active");
                        var href = $(this).attr("href");
                        $(".quick-finder-other-links:visible", qfo).hide();
                        $("#" + href, qfo).show();
                    },
                    function() {
                        var tabLink = $(this);
                        tabHoverTimeoutID = window.setTimeout(function() {
                            tabLink.parent().removeClass("active")
                            $(".quick-finder-other-links:visible", qfo).hide();
                        }, config.tabHoverTimeout); 
                    }
                ).click(function() { return false; });
            oL.hover(
                function() {
                    if (tabHoverTimeoutID) window.clearTimeout(tabHoverTimeoutID);
                },
                function() {
                    tabHoverTimeoutID = window.setTimeout(function() {
                        $("ul.quick-finder-other-tabs li.active", qfo).removeClass("active");
                        $(".quick-finder-other-links:visible", qfo).hide();
                    }, config.tabHoverTimeout2);
                }
            );
        });
 
        return this;
    };
    
    $("#quick-finder").quickFinder();
})(jQuery);
