$(document).ready(function() {
    renderMenu();
    if ($("#searchForm .search-box").val() !== "") {
        $("#searchForm label").hide();
    }
    $("#searchForm label").click(function() {
        $(this).fadeOut();
        $("#searchForm .search-box").focus();
    });
    $("#searchForm .search-box").focus(function() {
        $("#searchForm label").fadeOut();
    });
    $("#searchForm .search-box").blur(function() {
        if ($("#searchForm .search-box").val() === "") {
            $("#searchForm label").fadeIn();
        }
    });
    $("#searchForm .go").mousedown(function() {
        if ($("#searchForm .search-box").val() === "") {
            $("#searchForm .search-box").focus();
            return false;
        }
    });
    $("#searchForm .go").click(function() {
        if ($("#searchForm .search-box").val() === "") {
            return false;
        }
    });
});


function renderMenu(e) {

    siteWidth = $("div.site-wrapper").width();
    parentOffsetLeft = $('#floor_nav').offset().left;

    $(".sub-floor-menus").each(function() {
        $(this).css("width", $(this).width()); //For IE6
    });
	$(".menu").removeClass("css-menu"); //remove css nav functionality
    $("#floor_nav ul.floors > li").each(function(i) {
        offset = $(this).offset(); //calculate the left placement for the menus based on the bottom left corner of the matching top floor buttons
        menu = $('#sub_' + this.id);
		menu.hide().css("width","").css("width",menu.width());//this prevents the wrapping which occours in the css version of the nav
        leftPosition = offset.left - parentOffsetLeft; //find the sub menu and reposition relative to the parent

        if (leftPosition < 0) {
            leftPosition = offset.left - parentOffsetLeft;
        }

        //Check if the sub menu is too wide to fit in the site wrapper
        if (menu.width() + leftPosition > siteWidth) {
            leftPosition = offset.left - parentOffsetLeft + $(this).width() - menu.width() + parseInt($(this).find(".sub-menu-wrapper-right").css("padding-right")); //find the sub menu and reposition relative to the parent

            if (leftPosition < 0) {
                leftPosition = offset.left - parentOffsetLeft + ($(this).width() / 2) - (menu.width() / 2); //center
            }
        }
        menu.css("left", leftPosition + "px");
        menu.bgiframe(); //fix for ie6
        $(this).dropdown({ menu: menu, type: 'slide', speedIn: 300, speedOut: 1 });
    });
}

(function($) {
    $.fn.dropdown = function(opt) {

        var defaults = {
            speedIn: 200,
            speedOut: 200,
            delay: 50,
            type: 'slide',
            transIn: { opacity: 'show' },
            transOut: { opacity: 'hide' }
        };

        var options = $.extend(defaults, opt);

        var menu = $(options.menu);
        var speedIn = options.speedIn;
        var speedOut = options.speedOut;
        var delay = options.delay;
        var type = options.type;
        var transIn = options.transIn;
        var transOut = options.transOut;

        var menuOver = false;
        var buttonOver = false;

        //find top level anchor
        var topLevelAnchor = $(this).find('a')[0];

        var topNavItem = "";

        var topNavConfig = {
            sensitivity: 20, // number = sensitivity threshold (must be 1 or higher)    
            interval: 200, // number = milliseconds for onMouseOver polling interval    
            over: topNavOver, // function = onMouseOver callback (REQUIRED)    
            timeout: 400, // number = milliseconds delay before onMouseOut    
            out: topNavOut // function = onMouseOut callback (REQUIRED)    
        };

        $(menu).hide();
        $(this).hoverIntent(topNavConfig);

		/* Basic version for touch screens */
		$(this).bind("touchstart",function(){
			$(menu).show();
		});
		$(this).bind("touchend",function(){
			setTimeout(function(){
				$(menu).hide();
			},3000);
		});

        function topNavOver() {
            if (menu.is(':animated')) { return };
            switch (type) {
                case 'slide':
                    $(menu).slideDown(speedIn);
                    break;
                case 'fade':
                    $(menu).fadeIn(speedIn);
                    break;
                case 'simple':
                    $(menu).show();
                    break;
                case 'blind':
                    $(menu).show("blind", { direction: "vertical" }, 200);
                    break;
                case 'custom':
                    $(menu).animate(transIn, speedIn);
            }
            buttonOver = true;
            $(topLevelAnchor).addClass("active");
        }

        function topNavOut() {
            buttonOver = false;
            setTimeout(function() {
                if (menuOver == false && buttonOver == false) {
                    switch (type) {
                        case 'slide':
                            $(menu).slideUp(speedOut);
                            break;
                        case 'fade':
                            $(menu).fadeOut(speedOut);
                            break;
                        case 'simple':
                            $(menu).hide();
                            break;
                        case 'blind':
                            $(menu).hide("blind", { direction: "vertical" }, 200);
                            break;
                        case 'custom':
                            $(menu).animate(transOut, speedOut)
                    }
                    $(topLevelAnchor).removeClass("active");
                }
            }, delay);

        }
    }
})

(jQuery);