timerID = '';

$(document).ready(function () {
    if(BrowserDetect.browser == "Firefox") {
        $("div.navigation-dropdowns").appendTo("#container");
    }
    navOffset = $("#navigation").offset();
    navWidth = 745;
    /*$("div.navigation").each(function () {
        navWidth += $(this).outerWidth();
    });*/
    
    $("div.navigation li[@id] a").bind("mouseover", function () {
        thisId = $(this).parent().attr("id");
        $("div.navigation-dropdowns").show().find("div.dropdown").hide();
        clearTimeout(timerID);
        if($("div.navigation-dropdowns div." + thisId).size() != 0) {
            btnOffset = $("li#" + thisId).offset();
            mod = btnOffset.left - navOffset.left - 245;
            if(mod + $("div.navigation-dropdowns div." + thisId).outerWidth() > navWidth) {
                mod = navWidth - $("div.navigation-dropdowns div." + thisId).outerWidth();
            }
            if($("div#videoplayer").size() != 0) {
                embeddedPlayerManager.getPlayer().hidePlayer();
            }
            $("div.navigation-dropdowns div." + thisId).css("margin-left", mod).show();
        }
    }).bind("mouseout", function () {
        timerID = setTimeout(function () {
            $("div.navigation-dropdowns").hide();
            isVisible = true;
            $("div#videoplayer").parents().each(function () {
                if($(this).is(":visible") == false) {
                    isVisible = false;
                }
            });
            if($("div#videoplayer").size() != 0 && isVisible) {
                embeddedPlayerManager.getPlayer().unHidePlayer();
            }
        },"250");
    });
    
    $("div.navigation-dropdowns div.dropdown a").bind("mouseover", function () {
        clearTimeout(timerID);
    }).bind("mouseout", function () {
        timerID = setTimeout(function () {
            $("div.navigation-dropdowns").hide();
            isVisible = true;
            $("div#videoplayer").parents().each(function () {
                if($(this).is(":visible") == false) {
                    isVisible = false;
                }
            });
            if($("div#videoplayer").size() != 0 && isVisible) {
                embeddedPlayerManager.getPlayer().unHidePlayer();
            }
        },"250");
    });
    
    $("#navigation a, .navigation-dropdowns a").not($("a[@href*=html]")).each(function () {
        hrefMod = "__source=nav";
        queryStringMod = "/?";
        if(this.href.indexOf("?") != -1) {
            queryStringMod = "&";
        }
        this.href = this.href + queryStringMod + hrefMod;
    });
    
    $("div.navigation label").each(function () {
        forName = $(this).attr("for");
        labelText = $(this).text();
        if(forName != undefined) {
	    if($("#" + forName).attr("type") == "text") {
		$("#" + forName).attr("value",$(this).text());
		$(this).remove();
	    }
        }
    });
    
    $("div.navigation input").bind("focus", function () {
        this.value = "";
        $(this).css("color","black");
    }).bind("blur",function () {
        $(this).css("color","#B5B5B5");
    }).css("color","#B5B5B5");
    
    $("div.navigation input.button").each(function () {
        htmlFrag = $("<div class='button'><a href='#'><span>" + $(this).attr("value") + "</span></a></div>");
	$(this).css({width:"1px", height:"1px", position:"absolute", top:"-1000px"}).after(htmlFrag);
    });
    
    $("div.navigation form").bind("submit", function () {
        query = $(this).find("input").attr("value");
        re = new RegExp(" ", "g");
        query = query.replace(re, "+");
        switch(locationDetails.cmsLoc) {
            case 'ah-dev.nbcuni.ge.com':
                newURL = "searchresults.shtml?q=" + query;
            break;
            case '202.54.219.122:8080':
                newURL = "/search/?q=" + query;
            break;
            case 'qa-www.accesshollywood.com':
                newURL = "/search/?q=" + query;
            break;
            case 'localhost':
                newURL = "/search/?q=" + query;
            break;
            default:
                newURL = "/search/?q=" + query;
            break;
        }
        window.location = newURL;
        return false;
    });
    
    $("div.navigation div.button a").bind("click", function () {
        $(this).parents("div.navigation").find("form").submit();
        return false;
    });
    
    $("a[@href*=/topic/movies]").attr("href","/showcase/movies");
    
    $("li#hot a").bind("mouseover", function () {
        $(this).parent().css("background-image", "url(http://accesshollywood.nbcunifiles.com/ah/images/external/bg_org_01.gif)");
    }).bind("mouseout", function () {
        $(this).parent().css("background-image", "url(http://accesshollywood.nbcunifiles.com/ah/images/external/bg_blue_01.gif)");
    });
});