// Baseret på kode fra TV2's hjemmeside


t_freeze = false; // If true, a drop down menu does not time out; only used in the search menu
t_closetimer = null; // Timer for removal of drop down menu
t_open_menu = null;  // Currently open menu
IE = /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent); // Is this Internet Explorer?


// Called when the mouse is over an menu line item.
// Opens a drop down menu.

function t_dropdown(menitem, dropdownId) {
    t_hidemenu(t_open_menu);
    t_showmenu(menitem, dropdownId);
    t_freeze = false;
}


// Called when the mouse is over a drop down menu.
// Prevents the menu from disappearing.
function t_over() {
    if (t_closetimer!=null) {
        clearTimeout(t_closetimer);
        t_closetimer = null;
    }
}


// Called when the mouse leaves a drop down menu.
// Causes the menu to disappear in 0.5 seconds
function t_leave() {
	if (!t_freeze) {
        // Clear an outstanding timer before starting a new one
        if (t_closetimer!=null)
            clearTimeout(t_closetimer);
            
		t_closetimer = setTimeout("t_hidemenu()",500);
    }
}


// Find the position of an object relative to the object ("main" or
// "container", depending on browser) used when position an object.
function findPos(obj) {
	var curleft = curtop = 0;

    relativeTo = IE ? "main" : "container";

    for (o=obj; o.id!=relativeTo; o=o.offsetParent) {
        curleft += o.offsetLeft;
        curtop += o.offsetTop;
    }
    return [curleft,curtop];
}

// Displays a drop down menu
function t_showmenu(menuitem, dropdownId) {
    var topmPos = findPos(menuitem);
    var dropdown = document.getElementById(dropdownId);
    dropdown.style.left = topmPos[0];
	dropdown.style.top = topmPos[1] + 25;
	dropdown.style.visibility = "visible";
    t_open_menu = dropdown;
}


// Closes the open drop down menu
function t_hidemenu() {
    if (t_open_menu==null) // No menu is being shown
        return;

    if (t_closetimer!=null) {
        clearTimeout(t_closetimer);
        t_closetimer = null;
    }
    t_open_menu.style.visibility = "hidden";
    t_open_menu = null;
}

