TfeMenu = function(){
    this.timer = null;
    this.activeMenu;
    this.activeMenuClass;
    this.activeDropdown;
	this.activeDropdownLink;
	this.activeDropdownLinkClass;
    this.activeDropdownSub;
}

TfeMenu.prototype.OpenMenu = function(menu, dropdownId, direction, offsetX, offsetY){
    this.StopTimer();
    this.CloseMenu();
    
    this.activeMenu = menu;
    this.activeDropdown = this.FindDropdown(dropdownId);

    this.activeMenuClass = this.activeMenu.className;
    this.activeMenu.className = 'active' + dropdownId;
    
    offsetX += this.GetOffsetX(menu);
    offsetY += this.GetOffsetY(menu);
    
   	if(direction == 'up') {
	   	offsetY -= this.activeMenu.offsetHeight;
    	offsetY -= this.activeDropdown.offsetHeight;
	}
    
    this.ShowDropdown(offsetX, offsetY);
    
    this.SetEvents();
}

TfeMenu.prototype.OpenSubMenu = function(menu, dropdownId, offsetX, offsetY) {
	this.StopTimer();
	this.CloseSubMenu();
	
	this.activeDropdownLink = menu;
	this.activeDropdownSub = this.FindDropdown(dropdownId);
	
	this.activeDropdownLinkClass = this.activeDropdownLink.className;
	this.activeDropdownLink.className = 'active' + dropdownId;
	
	offsetX += this.GetSubMenuOffsetX(menu);
	offsetY += this.GetSubMenuOffsetY(menu);
	
	this.ShowDropdownSub(offsetX, offsetY);
	
	this.SetEvents();
}

TfeMenu.prototype.ShowDropdown = function(offsetX, offsetY){
    if(this.activeDropdown != null){
        this.activeDropdown.style.top = offsetY + this.activeMenu.offsetHeight + 'px';
        this.activeDropdown.style.left = offsetX + 'px';
        this.activeDropdown.style.visibility = 'visible';
    }
}

TfeMenu.prototype.ShowDropdownSub = function(offsetX, offsetY) {
	if(this.activeDropdownSub != null) {
		this.activeDropdownSub.style.left = offsetX + 'px';
		this.activeDropdownSub.style.top = offsetY + 'px';
		this.activeDropdownSub.style.visibility = 'visible';
	}
}

TfeMenu.prototype.CloseMenu = function(){
    if(this.activeDropdown != null){
        this.activeDropdown.style.visibility = 'hidden';
    }
    if(this.activeMenu != null){
        this.activeMenu.className = this.activeMenuClass;
    }
    this.CloseSubMenu();
}

TfeMenu.prototype.CloseSubMenu = function() {
	if(this.activeDropdownSub != null) {
	    this.activeDropdownSub.style.visibility = 'hidden';
		this.activeDropdownSub = null;
	}
	if(this.activeDropdownLink != null) {
		this.activeDropdownLink.className = this.activeDropdownLinkClass;
		this.activeDropdownLink = null;
	}
}

TfeMenu.prototype.SetEvents = function(){
	if(this.activeMenu != null) {
		this.activeMenu.onmouseout = startTimer;
	}
	if(this.activeDropdown != null) {
		this.activeDropdown.onmouseout = startTimer;
		this.activeDropdown.onmouseover = stopTimer;
	}
	if(this.activeDropdownSub != null) {
		this.activeDropdownSub.onmouseout = startTimer
		this.activeDropdownSub.onmouseover = stopTimer;
	}
}

TfeMenu.prototype.FindDropdown = function(dropdownId){
    return document.getElementById(dropdownId);
}

TfeMenu.prototype.StartTimer = function(){
    this.timer = setTimeout('closeMenu()',500);
}

TfeMenu.prototype.StopTimer = function(){
    if(this.timer != null){
        clearTimeout(this.timer);
    }
}

TfeMenu.prototype.GetOffsetX = function(node){
    var offsetX = this.activeMenu.offsetLeft;
    
    var parentOffsetNode = node.offsetParent;
    while(parentOffsetNode != null){
        if(parentOffsetNode.offsetLeft != null){
            offsetX += parentOffsetNode.offsetLeft;
        }
        parentOffsetNode = parentOffsetNode.offsetParent
    }
    
    return offsetX;
}

TfeMenu.prototype.GetOffsetY = function(node){
    var offsetY = this.activeMenu.offsetTop;
    
    var parentOffsetNode = node.offsetParent;
    while(parentOffsetNode != null){
        if(parentOffsetNode.offsetTop != null){
            offsetY += parentOffsetNode.offsetTop;
        }
        parentOffsetNode = parentOffsetNode.offsetParent
    }
    
    return offsetY;
}

TfeMenu.prototype.GetSubMenuOffsetX = function(node) {
	if(this.activeDropdown != null) {
		var offsetX = this.activeDropdown.style.left;
		if(isNaN(offsetX) && offsetX.indexOf('px') > -1) {
			offsetX = parseInt(offsetX.substring(0, offsetX.indexOf('px')));
		}
		
		if(this.activeDropdown != null) {
			var activeDropdownWidth = this.activeDropdown.offsetWidth;
			if(isNaN(activeDropdownWidth) && activeDropdownWidth.indexOf('px') > -1) {
				activeDropdownWidth = parseInt(activeDropdownWidth.substring(0, activeDropdownWidth.indexOf('px')));
			}
			offsetX += activeDropdownWidth;
		}
		
		return offsetX;
	} else {
		return 0;
	}
}

TfeMenu.prototype.GetSubMenuOffsetY = function(node) {
	if(this.activeDropdown != null) {
		var offsetY = this.activeDropdown.style.top;
		if(isNaN(offsetY) && offsetY.indexOf('px') > -1) {
			offsetY = parseInt(offsetY.substring(0, offsetY.indexOf('px')));
		}
		
		if(this.activeDropdownLink != null) {
			offsetY += this.activeDropdownLink.offsetTop;
		}
		
		return offsetY;
	} else {
		return 0;
	}
}

var oTfeMenu = new TfeMenu();

function openMenu(menu, dropdownId, direction, offsetX, offsetY){
	oTfeMenu.OpenMenu(menu, dropdownId, direction, offsetX, offsetY);
}

function openSubMenu(menu, dropdownId, offsetX, offsetY) {
	oTfeMenu.OpenSubMenu(menu, dropdownId, offsetX, offsetY);
}

function closeMenu(){
	oTfeMenu.CloseMenu();
}

function closeSubMenu() {
	oTfeMenu.CloseSubMenu();
}

function startTimer(){
	oTfeMenu.StartTimer();
}

function stopTimer(){
	oTfeMenu.StopTimer();
}
