// TfeMenu
var NODE_TYPE_ELEMENT = 1;
var NODE_TYPE_TEXT = 3;
TfeMenu = function() {
    this.timer = null;
    this.activeMenu;
    this.activeMenuClass;
	this.activeMenuImageSrc;
    this.activeDropdown;
	this.activeDropdownLink;
	this.activeDropdownLinkClass;
}
TfeMenu.prototype.OpenMenu = function(menu) {
    this.StopTimer();
    this.CloseMenu();
    
    this.activeMenu = menu;
	
	this.activeDropdown = menu.nextSibling;
	while (this.activeDropdown.nextSibling && this.activeDropdown.nodeType && this.activeDropdown.nodeType != NODE_TYPE_ELEMENT) {
		this.activeDropdown=this.activeDropdown.nextSibling;
	}
    // If a menu cannot be found; set the activeDropdown to 'null'
    if (this.activeDropdown.nodeType == NODE_TYPE_TEXT) {
	    this.activeDropdown = null;
	}

    this.activeMenuClass = this.activeMenu.className;
    this.activeMenu.className = "active";
	
	//Switch the image
	this.activeMenuSrc = menuImageSrc = this.activeMenu.childNodes[0].src;
	// If the menu image is a png image, then in IE6 the src attribute is replaced by /path-to-image/img_blank.gif
	// therefore the background should be re-applied with the hover image
	
	if (this.activeMenuSrc.indexOf("img_blank.gif") > -1) {
	    if (this.activeMenu.childNodes[0].runtimeStyle.filter.indexOf("_a.png") == -1) {
	        // Do not switch image if current menu is the current menu
	        this.activeMenu.childNodes[0].runtimeStyle.filter = this.activeMenu.childNodes[0].runtimeStyle.filter.replace(".png", "_h.png");
	    }
	}
	else if (this.activeMenuSrc.indexOf("_a.png") == -1) {
        // Do not switch image if current menu is the current menu
	    this.activeMenu.childNodes[0].src = this.activeMenuSrc.substr(0, this.activeMenuSrc.length - 4) + '_h.' + this.activeMenuSrc.substr(this.activeMenuSrc.length - 3);
	}
    
    this.ShowDropdown();
    
    this.SetEvents();
}
TfeMenu.prototype.ShowDropdown = function() {
    if (this.activeDropdown != null) {
        this.activeDropdown.style.display = "block";
    }
}
TfeMenu.prototype.CloseMenu = function() {
    if (this.activeDropdown != null) {
        this.activeDropdown.style.display = "none";
    }
    if (this.activeMenu != null) {
        this.activeMenu.className = this.activeMenuClass;
		
		//Restore the image
	    if (this.activeMenuSrc.indexOf("img_blank.gif") > -1) {
	        if (this.activeMenu.childNodes[0].runtimeStyle.filter.indexOf("_a.png") == -1) {
	            // Do not restore image if current menu is the current menu
	            this.activeMenu.childNodes[0].runtimeStyle.filter = this.activeMenu.childNodes[0].runtimeStyle.filter.replace("_h.png", ".png");
            }
	    }
	    else if (this.activeMenuSrc.indexOf("_a.png") == -1) {
	        // Do not restore image if current menu is the current menu
	        this.activeMenu.childNodes[0].src = this.activeMenuSrc;
	    }
    }
}
TfeMenu.prototype.SetEvents = function() {
	if (this.activeMenu != null) {
		this.activeMenu.onmouseout = startTimer;
	}
	if (this.activeDropdown != null) {
		this.activeDropdown.onmouseout = startTimer;
		this.activeDropdown.onmouseover = stopTimer;
	}
}
TfeMenu.prototype.StartTimer = function() {
    this.timer = setTimeout("closeMenu()", 500);
}
TfeMenu.prototype.StopTimer = function() {
    if (this.timer != null) {
        clearTimeout(this.timer);
    }
}
var oTfeMenu = new TfeMenu();
function openMenu(menu) {
	oTfeMenu.OpenMenu(menu);
}
function closeMenu() {
	oTfeMenu.CloseMenu();
}
function startTimer() {
	oTfeMenu.StartTimer();
}
function stopTimer() {
	oTfeMenu.StopTimer();
}
