﻿function assignSideMenuSubmenu(menuId, submenuId) {
    $("#" + menuId).mouseover(function(event) {
        // determine submenu position
        var currentMenuItem = event.currentTarget;
        var submenuLeft = getOffsetLeftInContainer(currentMenuItem, "outerContainer") - 6;
        
        var submenu = $("#" + submenuId);
        submenu.css("left", submenuLeft + "px");
        
        // must explicitly set width of <a> elements in submenu to make it work in IE 6 and 7
        var submenuWidth = document.getElementById(submenuId).offsetWidth + "px";
        var submenuAnchors = $("#" + submenuId + " a");
        for (i = 0; i < submenuAnchors.length; i++) {
            $(submenuAnchors[i]).css("width", submenuWidth);
        }
                
        submenu.css("visibility", "visible");
    });
    
    $("#" + menuId).mouseout(function(event) {
        if (!isMouseOverElement(event.pageX, event.pageY, document.getElementById(submenuId))) {
            $("#" + submenuId).css("visibility", "hidden");
        }
        
    });
        
    $("#" + submenuId).mouseout(function(event) {
        if (!isMouseOverElement(event.pageX, event.pageY, document.getElementById(submenuId)) && !isMouseOverElement(event.pageX, event.pageY, document.getElementById(menuId))) {
            $("#" + submenuId).css("visibility", "hidden");
        }
    });
}

function assignMainMenuSubmenu(menuId, submenuId) {
    var shouldHideSubmenu = false;

    $("#" + menuId).mouseover(function(event) {
        var currentMenuItem = event.currentTarget;
        
        $("#" + menuId).addClass("hovered");
        
        if (submenuId != "") {
            $("#" + submenuId).css("display", "block");
            
            var submenuRight = getOffsetRightInContainer(currentMenuItem, "outerContainer");
            $("#" + submenuId).css("right", submenuRight + "px");
        }
        shouldHideSubmenu = false;
        showGlass();
    });
    
    $("#" + menuId).mouseout(function(event) {
        var currentMenuItem = event.currentTarget;
        
        shouldHideSubmenu = true;
        hideSubmenu();
        
        hideGlass();
    });
    
    if (submenuId != "") {
        $("#" + submenuId).mouseover(function(event) {
            $("#" + menuId).addClass("hovered");
            $("#" + submenuId).css("display", "block");
            shouldHideSubmenu = false;
            showGlass();
        });
        
        $("#" + submenuId).mouseout(function(event) {
            shouldHideSubmenu = true;
            hideSubmenu();
            hideGlass();
        });
    }
        
    function hideSubmenu() {
        // submenu hidden with a delay (as short as possible)
        // otherwise it was fliking when moving mouse cursor over it in IE
        setTimeout(hideSubmenuDelayed, 1);
    }
    
    function hideSubmenuDelayed() {
        if (shouldHideSubmenu) {
            $("#" + menuId).removeClass("hovered");
            if (submenuId != "") {
                $("#" + submenuId).css("display", "none");
            }
        }
    }
}

function showGlass() {
	showGlassInternal("#mainMenuGlass");
}

function hideGlass() {
	hideGlassInternal("#mainMenuGlass");
}

function showGlassInternal(glassPanelId) {
	// glass is contained in outer container, which has position: relative
	// due to that if we set left=0 in glass, it will be left-aligned with outer container
	// we need to shift if to the left to be aligned with document
	var outerContainerOffset = document.getElementById("outerContainer").offsetLeft;
	
	var bodyHeight = $(document).height();
	var bodyWidth = $(document).width();
	
	var glassPanel = $(glassPanelId);
	glassPanel.css("left", (-1 * outerContainerOffset) + "px");
	glassPanel.css("width", bodyWidth + "px");
	glassPanel.css("height", bodyHeight + "px");
	//glassPanel.css("display", "block");
	glassPanel.clearQueue();
	glassPanel.stop();
	glassPanel.fadeTo("fast", 0.3);
}

function hideGlassInternal(glassPanelId) {
	var glassPanel = $(glassPanelId);
	//glassPanel.css("display", "none");
	glassPanel.clearQueue();
	glassPanel.stop();
	glassPanel.fadeOut("fast");
}

function initializeNewsTicker(newsBoxId, interval) {
    var itemBeingDisplayed = 0;
    var newsItems = $("#" + newsBoxId + " .ListItem");
    
    // determine height of tallest news item
    var tallestHeight = 0;
    for (i = 0; i < newsItems.length; i++) {
        if (newsItems[i].clientHeight > tallestHeight) {
            tallestHeight = newsItems[i].clientHeight;
        }
        var item = $(newsItems[i]);
        item.css("visibility", "visible");
        if (i > 0) {
            item.css("display", "none");
        }
    }
    
    // set news box height to contain longest element
    $("#" + newsBoxId + " .Content").css("height", tallestHeight + "px");
    
    // hide unscripted element
    $("#" + newsBoxId + " .UnscriptedItem").css("display", "none");
    
    // ready for ticker, start ticking
    if (newsItems.length > 1) {
        // tick only if there are several messages
        setInterval(newsTickerTick, interval);
    }
    
    function newsTickerTick() {
        // fade-out current element
        $(newsItems[itemBeingDisplayed]).fadeOut("fast");
        // increase index
        itemBeingDisplayed++;
        if (itemBeingDisplayed >= newsItems.length) {
            itemBeingDisplayed = 0;
        }
        // fade-in new element
        $(newsItems[itemBeingDisplayed]).fadeIn("fast");
    }
}

function initializeInputHandler(inputElementId, defaultText) {
    var firstFocusHandled = false;
    
    var element = $("#" + inputElementId);
    
    
    if (element.val() == defaultText) {
        element.addClass("grayed");

        element.focus(function(event) {
            if (!firstFocusHandled) {
                firstFocusHandled = true;
                element.removeClass("grayed");
                element.val("");
                element.unbind("focus");
            }
        });
    }
}

function initializeSearchBox(searchBoxId, searchButtonId, searchText) {
    var searchLabelText = $("#SearchBoxLabel").text();
    if (searchLabelText == null || searchLabelText == "") {
        searchLabelText = searchText;
    }
    $("#" + searchBoxId).val(searchLabelText);
    $("#" + searchBoxId).keypress(function(event) {
        if (event.which == 13) {
            event.preventDefault();
            
            var searchHref = $("#" + searchButtonId).attr("href")
            //alert("Search href: " + searchHref);
            location.href = searchHref;
        }
    });

    initializeInputHandler(searchBoxId, searchLabelText);
}

function isMouseOverElement(mouseX, mouseY, element) {
    var elementLeft = getOffsetLeftInContainer(element, "");
    var elementTop = getOffsetTopInContainer(element, "");
    var elementRight = elementLeft + element.offsetWidth;
    var elementBottom = elementTop + element.offsetHeight;
    
    //alert(elementLeft + " " + elementRight + " " + elementTop + " " + elementBottom);
    
    return (mouseX >= elementLeft && mouseX < elementRight && mouseY >= elementTop && mouseY < elementBottom);
}

function getOffsetLeftInContainer(element, containerId) {
    var offset = 0;
    while (element != null && element.id != containerId) {
        offset += element.offsetLeft;
        element = element.offsetParent;
    }
    
    return offset;
}

function getOffsetTopInContainer(element, containerId) {
    var offset = 0;
    while (element != null && element.id != containerId) {
        offset += element.offsetTop;
        element = element.offsetParent;
    }
    
    return offset;
}

function getOffsetRightInContainer(element, containerId) {
    var offset = element.offsetWidth;
    while (element != null && element.id != containerId) {
        offset += element.offsetLeft;
        element = element.offsetParent;
    }
    
    var container = document.getElementById(containerId);
    return container.offsetWidth - offset;
}

function showFbBox() {
	var fbMarker = $("#fbPositionMarker");
	var fbBox = $("#fbContentsBox");
	
	fbBox.css("top", fbMarker.offset().top + "px");
	
	fbBox.clearQueue();
	fbBox.stop();
	fbBox.animate( {height : "500px"}, "fast");
}

function hideFbBox() {
	var fbBox = $("#fbContentsBox");
	
	fbBox.clearQueue();
	fbBox.stop();
	fbBox.animate( {height : "0"}, "fast");
}

$(document).ready(function() {
	$("#fbSwitchButton").mouseover(function(evt) {
		showFbBox();
	});
	
	$("#fbSwitchButton").add("#fbContentsBox").mouseout(function(evt) {
	    var fbButton = document.getElementById("fbSwitchButton");
	    var fbBox = document.getElementById("fbContentsBox");
	    if (!isMouseOverElement(evt.pageX, evt.pageY, fbButton) && !isMouseOverElement(evt.pageX, evt.pageY, fbBox)) {
		    hideFbBox();
		}
	});
});

