var menusShown = new Array();
var timer = null;
var menuLevel = 0;
var lastMenuLevel = 0;
var parentMenu = null;
var displayWinow = null;

function displayInNewWindow(path, width, height) {
	if( displayWinow != null ) { /*TODO: Close window */ }
	displayWinow = window.open(path, "","scrollbars=1,width="+width+",height="+height+",");
}

function showMenu(menu) {
    if( menusShown.length > 0 && menusShown[0] == menu ) {
        if( timer != null ) { clearTimeout(timer); timer = null; } 
        return;
    } else {
	if( timer != null ) { clearTimeout(timer); timer = null; } 
	hideMenus();
    }
    $("#"+menu).addClass("selected_menu");
    $("#"+menu).mouseout( mouseExit );

    var menuOffset = $("#"+menu).offset();
    var menuHeight = $("#"+menu).outerHeight({ margin: true });
    var menuWidth = $("#"+menu).width();
    var windowHeight = $(window).height();

    $("#"+menu+"_menuitems").each( function(i) {
	$(this).css({top: eval(menuOffset.top + menuHeight - 12)+"px", left: eval(menuOffset.left + 5)+"px" });
	$(this).width(menuWidth - 10);
        
	if( $(this).outerHeight({ margin: true }) > windowHeight ) { $(this).height(windowHeight - 15); }
		$(this).css({visibility: "visible"});
	$(this).mouseout( mouseExit );
	$(this).mouseover( mouseEnter );
    });

    $("#"+menu+"_menuitems div").each( function(i) {
	$(this).mouseover( mouseEnterSubMenu );
	$(this).mouseout( mouseExitSubMenu );
        $(this).attr("menulevel", new String(menuLevel) );
	var children = $(this).children("a");
        if( children.attr("href") != null ) {
            $(this).attr("link", children.attr("href"));
            $(this).mousedown( linkedCellClicked );
        } else {
	     $(this).mousedown( unlinkedCellClicked );
	}
    });
   
    menusShown.push( menu );
    menuLevel=menuLevel+1;
}

var gParent = null; var gMenu = null;
function showSubMenu(parent, menu) {
    gParent = parent;
    gMenu = menu;
    $("#"+menu+"_menuitems").each( function(i) {
        if( $(this).attr("dynamic") != null && $(this).attr("dynamic_loaded") == null ) {
            $("#"+menu+"_menuitems").load("/GetSubMenus", {listId:menu+"_menuitems"}, setupSubMenu );
            $(this).attr("dynamic_loaded", "true");
        } else {
	    setupSubMenu()
	}
    });
}

function setupSubMenu() {
    var parent = gParent; gParent = null;
    var menu = gMenu; gMenu = null;
    
    var menuHeight = $("#"+parent).outerHeight({ margin: true });
    var menuOffset = $("#"+parent).offset();
    var menuWidth = $("#"+parent).outerWidth({ margin: true });
    var windowHeight = $(window).height();
    
     $("#"+menu+"_menuitems").each( function(i) {
	$(this).css({top: eval(menuOffset.top +2)+"px", left: eval(menuOffset.left + menuWidth)+"px" });
	$(this).width(menuWidth);
        
        if( ((menuOffset.top+2) + $(this).outerHeight({ margin: true })) > windowHeight ) { $(this).height(windowHeight - menuOffset.top - 5); }
		$(this).css({visibility: "visible"});
        
        $(this).mouseout( mouseExit );
	$(this).mouseover( mouseEnter );
    });
    
    $("#"+menu+"_menuitems div").each( function(i) {
	$(this).mouseover( mouseEnterSubMenu );
	$(this).mouseout( mouseExitSubMenu );
        $(this).attr("menulevel", new String(menuLevel) );
	var children = $(this).children("a");
        if( children.attr("href") != null ) {
            $(this).attr("link", children.attr("href"));
            $(this).mousedown( linkedCellClicked );
        }
    });
    
    menusShown.push( menu );
    menuLevel=menuLevel+1;
}

function hideMenus() {
    if( timer != null ) { clearTimeout(timer); timer = null; }
    for(var pos = 0, size = menusShown.length; pos < size; pos++ ) {
        $("#"+menusShown[pos]+"_menuitems").each( function(i) {
            $(this).css({visibility: "hidden"});
            $(this).unbind('mouseover').unbind('mouseout').unbind('mousedown');
        }); 
    }
    $("#"+menusShown[0]).removeClass("selected_menu");
    menusShown.length = 0;
    menuLevel = 0;
    parentMenu = null;
}

function hideMenusOverLevel(level) {
     for(var pos = menusShown.length-1; pos > level; pos-- ) {
        //alert("Hiding: "+menusShown[pos]);
        $("#"+menusShown[pos]+"_menuitems").each( function(i) {
            $(this).css({visibility: "hidden"});
            $(this).unbind('mouseover').unbind('mouseout').unbind('mousedown');
        }); 
        menuLevel = menuLevel - 1;
        menusShown.pop();
    }
    if(menusShown.length == 0 ) { parentMenu = null; }
}

function mouseExit() {
    if( timer != null ) { clearTimeout(timer); }
    timer = setTimeout( hideMenus, 800);
}

function mouseEnter() {
    if( timer != null ) { clearTimeout(timer); timer = null; } 
}

function linkedCellClicked() {
    hideMenus();
    document.location.href = $(this).attr("link");
}

function unlinkedCellClicked() {
    if( timer != null ) { clearTimeout(timer); }
    timer = setTimeout( hideMenus, 125);
}

function mouseEnterSubMenu() {
    var thisLevel = $(this).attr("menulevel");
    if( thisLevel < menuLevel) {
        hideMenusOverLevel( thisLevel );
    }
    if( thisLevel == menuLevel ) {
        hideMenusOverLevel( thisLevel );
    }
    this.style.background="#EEE";

    //TODO: This always returns an object - learn how to detect an empty set
    if( $("#"+$(this).attr("id")+"_menuitems") != null ) {
       parentMenu = $(this).attr("id");
       showSubMenu($(this).attr("id"), $(this).attr("id"));
    }
}

function mouseExitSubMenu() {
    this.style.background="#CCC";
}

var showDialog = function(h) {
    h.w.height($(window).height()-100);
    h.w.css({ left: eval( ($(window).width() - 800)/2 )+"px" });
    h.w.css('opacity',0.92).slideDown("fast");
}

window.onresize=function() {
    $("#header-outer").css({ left: eval( ($(window).width() - 900)/2 )+"px" });
}

function prepareRequestBrochure(formData, jqForm, options) { 	
    var okToSubmit = true;
    formData.length = 0;

    $("#requestBrochure input[@type=hidden]").each( function(i) {
	formData.push( { name: $(this).attr("name"), value:  $(this).val() } );
    });

    $("#requestBrochure input[@type=text]").each( function(i) {
	if($(this).val() != this.defaultValue) {
	    formData.push( { name: $(this).attr("name"), value:  $(this).val() } );
	}
    });

    $("#requestBrochure input[@type=checkbox][@checked]").each( function(i) {
	formData.push( { name: $(this).attr("name"), value:  $(this).val() } );
    });

    return okToSubmit; // here we could return false to prevent the form from being submitted; 
}

function go(name) {
	window.location.href = $("#"+name).val();
}	