
function getTopMenu (arrMainMenu, arrSingleMenu, dbMenu)
{
	
	this.arrMainMenu = arrMainMenu;
	this.arrSingleMenu = arrSingleMenu;
	this.dbMenu = dbMenu;
	this.active;
	this.browser;
	this.activeNav;

	if ($.browser.opera) 
	{ 
		$(this.browser = 'opera');
	}
	else if ($.browser.safari) 
	{
		$(this.browser = 'safari');
	}

	this.str_replace = function (search, replace, subject)
	{
		return subject.split(search).join(replace);
	}

	this.in_array = function (array, value) 
	{
		for (var j = 0; j < array.length; j++)
			if( array[j] == value )
				return true;
		return false;
	}

	this.getSubmenu = function (id)
	{
		// offene menüs/submenüs schließen
		this.closeSubmenus();
		this.cancelPassiveMenu();
		this.passiveMenu (this.activeNav);
		for (var i = 0; i < this.arrMainMenu.length; i++)
		{
			if ('a_' + this.arrMainMenu[i] != id)
			{
				$("#a_" + this.arrMainMenu[i]).removeClass("active_item");
			}
		}

		// single besitzt keine submenüs
		var sid = this.str_replace ('a_','',id);
		if (this.in_array (this.arrSingleMenu,sid))
		{
			//$("#a_" + sid).removeClass("nav_active");
			$("#a_" + sid).css({ borderBottom: "1px solid #EB8206" });
			return false;
		}
		
		// prüfen auf nav_active - id merken und klasse entfernen
		if (document.getElementById ('a_' + sid).className.indexOf('nav_active') != -1)
		{
			this.activeNav = 'a_' + sid;
			$("#a_" + sid).removeClass("nav_active");
		}
		
		// position des menüpunktes ermitteln und submenü entsprechend positionieren
		if (this.browser == 'safari')
		{
			var xtop = 1;
		}
		else
		{
			var xtop = 0;
		}
		var width = $("#" + id).width();
		var top = $("#" + id).offset().top;
		var left = $("#" + id).offset().left;
		$("#submenu_1").css({ top: (top + 21 + xtop) + "px", left: left + "px", minWidth: width + 6 + 'px' });
		
		// untermenüpunkte ermitteln und einblenden
		var submenu_1 = '';
		var where = new Array('sub_level', '==', '1');
		var and = new Array('parent_id', '==', sid);
		var items = this.dbMenu.selectRow(where, and, false);
		for( var i=0; i < items.length; i++ )
		{
			submenu_1 += '<div class="submenu" id="div_' + items[i].element_id + '"><a href="' + items[i].href + '" target="' + items[i].target + '" id="a_' + items[i].element_id + '" onmouseover="topmenu.cancelPassiveMenu(); topmenu.getSubmenu2(this.id);" onmouseout="topmenu.outMenu(\'a_' + items[i].parent_id + '\')">' + items[i].text + '</a></div>';
		}
		
		$("#" + id).addClass("active_item");
		$('#submenu_1').html(submenu_1);
		$("#submenu_1").show();
		
	}
	
	this.getSubmenu2 = function (id)
	{
		// offenes submenü2 schließen
		$("#submenu_2").empty();
		$("#submenu_2").hide();
		$("#sub2sub").hide();
		
		// position des submenüpunktes ermitteln und submenü2 entsprechend positionieren
		if (this.browser == 'opera')
		{
			var xwidth = 3;
			var xtop = 2;
			var xheight = 22;
		}
		else if (this.browser == 'safari')
		{
			var xwidth = 2;
			var xtop = 1;
			var xheight = 23;
		}
		else
		{
			var xwidth = 2;
			var xtop = 1;
			var xheight = 22;
		}
		var width = $("#submenu_1").width();
		var top = $("#" + id).offset().top;
		var left = $("#" + id).offset().left;
		//$("#submenu_2").css({ top: (top - 1) + "px", left: (left - xwidth + width) + "px" });
		//$("#sub2sub").css({ top: (top) + "px", left: (left - xwidth + width) + "px" });
		$("#submenu_2").css({ top: (top - xtop) + "px", left: (left - xwidth + 4 + width) + "px" });
		$("#sub2sub").css({ top: (top - xtop) + "px", left: (left - xwidth + width) + "px", height: xheight + 'px' });
		
		// untermenüpunkte ermitteln und einblenden
		var submenu_2 = '';
		var where = new Array('sub_level', '==', '2');
		var and = new Array('parent_id', '==', this.str_replace('a_','',id));
		var items = this.dbMenu.selectRow(where, and, false);
		for( var i=0; i < items.length; i++ )
		{
			submenu_2 += '<div class="submenu" id="div_' + items[i].element_id + '"><a href="' + items[i].href + '" target="' + items[i].target + '" id="a_' + items[i].element_id + '" onmouseover="topmenu.cancelPassiveMenu();" onmouseout="topmenu.outMenu(\'a_' + items[i].parent_id.substr(0,1) + '\')">' + items[i].text + '</a></div>';
		}

		if (submenu_2 != '')
		{
			$('#submenu_2').html(submenu_2);
			$("#sub2sub").show();
			$("#submenu_2").show();
		}
	}
	
	this.outMenu = function (id)
	{
		// single besitzt keine submenüs
		var sid =  this.str_replace ('a_','',id);
		if (this.in_array (this.arrSingleMenu,sid))
		{
			if (document.getElementById ('a_' + sid).className.indexOf('nav_active') == -1)
			{
				$("#a_" + sid).css({ borderBottom: "none" });
			}
			return false;
		}

		this.active = eval ("window.setTimeout(\"topmenu.passiveMenu('" + id + "');\", 300);");
	}
	
	this.passiveMenu = function (id)
	{
		// offene submenüs schließen
		this.closeSubmenus();
		
		// klasse entfernen
		$("#" + id).removeClass("active_item");

		// prüfen auf nav_active - klasse hinzufügen
		if (this.activeNav == id)
		{
			this.activeNav = '';
			$("#" + id).addClass("nav_active");
		}
		
	}
	
	this.cancelPassiveMenu = function ()
	{
		window.clearTimeout(this.active);
	}
	
	this.closeSubmenus = function ()
	{
		$("#submenu_1").empty();
		$("#submenu_2").empty();
		$("#submenu_1").hide();
		$("#submenu_2").hide();
		$("#sub2sub").hide();
	}
	
	// db initialisieren
	this.init = function ()
	{
		
		var el = document.getElementById('initTopmenu').getElementsByTagName('*');
		for (var i = 0; i < el.length; i++)
		{
			if (el[i].tagName.toLowerCase() == 'a')
			{
				if (el[i].id.substr(2).length > 3) 
				{
					var sub_level = 2;
					var parent_id = el[i].id.substr(2,3);
				}
				else
				{
					var sub_level = 1;
					var parent_id = el[i].id.substr(2,1);
				}
				var data = new Array(el[i].id.substr(2), sub_level, parent_id, el[i].firstChild.data, el[i].href, el[i].className, el[i].target);
				this.dbMenu.insertRow(data);
			}
		}
		$("#initTopmenu").empty();
	}
	
}