
$j = $; //jQuery.noConflict();

function do_logout() {
	var data= {};
	
	$.post("/login/logout.php", data, function(ret){
		if(ret.ret == "ok") {
			document.location.href=document.location.href;
		} else {
			alert("Logout fehlgeschlagen!\n");
		}
	}, "json");
}

function newCalculation() {
	var data= {"do":"newCalculation"};
	
	$.post("/calc.php", data, function(ret){
		if(ret.ret == "ok") {
			document.location.href=document.location.href;
		} else {
			alert("Neue Kalkulation fehlgeschlagen!\n");
		}
	}, "json");
}

$j(document).ready(function() {	
	// disable old events
	/*
	$j('li.Level0Item > ol').hide();

	$j('li.Level0Item').mouseover(function(){
		console.log('hover');
		$j('> ol', this).slideDown();
	}).mouseout(function(){
		$j('> ol', this).slideUp();
	});
	*/

	if($j("#Main-2").outerHeight({margin:true})<($j(window).innerHeight())) {
		$j("#Main-2").css("height",($j(window).innerHeight())+"px");
	}
	
	if($j("#PageOutput").length>0) {
		$j("#BottomMenu").appendTo("#PageOutput");
		
		//wenn fenster kleiner ist als output --> keine absolute pos für BottomMenu
		if(($j("#PageContainer").outerHeight()+$j("#BottomMenu").outerHeight()+123-$j("#BottomMenuLine").outerHeight()+10)>=$j(window).height()) {
			
			$j("#BottomMenu").css({
				"bottom": "0px"
			});
			
			
			$j("#BottomMenuLine").css("top",($j("#PageOutput").outerHeight()+$j("#BottomMenu").outerHeight()-$j("#BottomMenuLine").outerHeight()+123+7)+"px");
			$j("#BottomMenu").css("position", "relative");
		} else {
			
			//Output verlängern, wenn das Fenster zu groß ist
			if(($j("#PageOutput").outerHeight()+$j("#BottomMenuLine").outerHeight())<$j(window).height()) {
				$j("#PageOutput").css("height",($j("#Main-2").outerHeight()-$j("#BottomMenuLine").outerHeight()-123)+"px");
			} else {
			}
			$j("#BottomMenu").css("bottom","0px");
			$j("#BottomMenuLine").css("bottom","0px");
			
			//$j("#BottomMenuLine").css("top",($j("#Main-2").outerHeight()-$j("#BottomMenuLine").outerHeight())+"px");
		}
		
	} else {
		//Wenn fenster zu klein
		if((637+$j("#BottomMenu").outerHeight())>$j(window).height()) {
			$j("#PageContent").css("height",(637)+"px");
			$j("#BottomMenu").appendTo("#PageContainer");
			
			$j("#BottomMenuLine").css("top",($j("#PageContent").outerHeight()+$j("#BottomMenu").outerHeight()-$j("#BottomMenuLine").outerHeight())+"px");
		} else {
			$j("#PageContent").css("height",$j(window).height()+"px");
			$j("#BottomMenu").appendTo("#PageContent");
			$j("#BottomMenu").css("bottom","0px");
			$j("#BottomMenuLine").css("top",($j("#PageContent").outerHeight()-$j("#BottomMenuLine").outerHeight())+"px");
		}		
	}
	
	/*border für die ersten td setzen. beim letzten soll kein border sein*/
	var count_border = $j("#BottomMenu td").length;
	var i = 1;
	
	$j("#BottomMenu td").each(function() {
		
		if(i<count_border)
			$(this).css("border-right", "1px solid #c3c3c3");
		
		i++;
	});
	
	if (TransMenu.isSupported() && 0)
	{
		function init_transmenu_subitems(transmenu, subitems)
		{
			subitems.each(function(i){
				var link = $j('> a', this);
				var item = transmenu.addItem(link.text(), link.attr('href'), link.attr('target'));

				var subitems = $j('> ol > li', this);
				if (subitems.length)
				{
					var subtransmenu = transmenu.addMenu(transmenu.items[i], i);
					init_transmenu_subitems(subtransmenu, subitems);
				}
			});
		}

		function hide_transmenu()
		{
			TransMenuSet.registry[0].hide();
		}

		var menuitems = $j('li.Level0Item').add('li.TopMenuItem').add('#MoilHeader');

		var ms = new TransMenuSet(TransMenu.direction.down, 1, 24, TransMenu.reference.topLeft);

		// generate transmenu from html
		menuitems.each(function(){
			var subitems = $j('> ol > li', this);
			if (!subitems.length)
			{
				this.onmouseover = hide_transmenu;
			}
			else
			{
				var link = $j('> a', this);
				var transmenu = ms.addMenu(link[0], 0);

				init_transmenu_subitems(transmenu, subitems);
			}
		});

		// remove menu from html
		menuitems.find('> ol').remove();

		/*
		menu.onactivate = function() {};
		menu.ondeactivate = function() {};
		*/

		// init transmenu
        var aMenuHtml = [];
        for (var i = 0, menu = null; menu = TransMenu.registry[i]; i++) {
                aMenuHtml[i] = menu.toString();
        }
        $j('body').append(aMenuHtml.join(""));

		TransMenu.initialize();
	}
});

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
