function cycleColors () {
	//$.lia.log("cycleColors()");

	// TABELLEN & LISTEN mit Klassen versehen

	// ausser im studientool und in der Lehrenden-Liste
	if (
		$('body.studium_portal-studienbewerber_studienberater').length < 1
		&& $('body.studium_portal-studierende_studienberater').length < 1
		&& $('body.portrait_personen_lehrende').length < 1
	) {
		cycleListColors();
		cycleTableColors();
	}

}

function cycleListColors() {
	//$.lia.log("cycleListColors()");

	$("div#content ul").each(function() {
		$(this).children("li").each(function(c) {
			if (c % 2) {
				$(this).addClass('even');
			}
			else {
				$(this).addClass('odd');
			}
			if (c === 0) {
				$(this).addClass('first');
			}
			else {
				if (c == $(this).parent("ul").children("li").length-1) {
					$(this).addClass('last');
				}
			}
		});
	});

}

function cycleTableColors()
{
	//$.lia.log("cycleTableColors()");

	$("div#content table").each(function() {
		$(this).children("tr").each(function(c) {
			if (c % 2) {
				$(this).addClass('even');

			}
			else {
				$(this).addClass('odd');
			}
			if (c === 0) {
				$(this).addClass('first');
			}
			else {
				if (c == $(this).parent("table").children("tr").length-1) {
					$(this).addClass('last');
				}
			}
		});
	});
}


function startMeUp(lehrende, clean)
{
	//$.lia.log("startMeUp()", "group");
	/* 	console.time("foo"); */

	//$.lia.log(typeof(lehrende));

	if (typeof(lehrende) === 'undefined') {
		lehrende = false;
	}

	if ( ! lehrende )
	{
		$.lia.mark_external_links();

		// Anker-Links
		hashLinks();

		// Dummy-Links
		$.lia.dummy_links();

		// Popup-Links
		$.lia.popup_links();

		// Elemente verstecken
		$.lia.hide_items();

		// Listen und Tabellen einf�rben
		cycleColors();

		// Autoren-Mouseover
		textAutor();
	}
	else {
		$.lia.external_links();
	}

	//$.lia.log(typeof(clean));

	if (typeof(clean) === 'undefined')
	{
		clean = false;
	}

	if (clean)
	{
		//$.lia.muh();
		$("#loading").hide();
		//$("*").css("cursor","default !important");
	}

/* 	console.timeEnd("foo"); */
	//$.lia.log("", "groupEnd");
}

function ajaxGet(trigger)
{
	$.ajax({
		type: "GET",
		url: trigger,
		async: false,
		data: "ajax=true",
		dataType: "html",
		cache: true,
		success: function(data) {
			$("#main").prepend(data);

			//$.lia.log("is_scrolled = "+is_scrolled);

			if (is_scrolled === true) {
				// Fenster scrollen und somit Navi auf fixed setzen
				window.scrollTo(0,264);
				delete is_scrolled;
				scrolled = $(window).scrollTop();
				if (scrolled < 264) {
					fixedMenu();
					window.scrollTo(0,0);
				}
			}

			// Flash stoppen
			try {
				window.document.flashheadswf.SetVariable('_root.stopMe','yes');
				$("#flashheadswf")[0].SetVariable('_root.stopMe','yes');
			} catch(err) {}

			return false;
		},
		error: function(msg) {
  			$("#content").load('404',
  				{ajax: true}
			);
		}
	});

	//$.lia.log(trigger.indexOf("portrait/personen/lehrende"));

	if (trigger.indexOf("portrait/personen/lehrende") != -1) {
		startMeUp(true, true);
	}
	else {
		startMeUp(false, true);
	}

	return(false);
}

function textAutor()
{
	$('div.entry div.text div.info').css('visibility', 'hidden');

	$('div.entry div.text').mouseover(function() {
		$(this).children('div.info').css('visibility', 'visible');
	});

	$('div.entry div.text').mouseout(function() {
		$(this).children('div.info').css('visibility', 'hidden');
	});

}

// STUDIEN-TOOLS

// Form f�r Ajax-Benutzung vorbereiten
function makeFormFunky()
{
	// Legends & inputs verstecken
	$('form#matrix legend').hide();
	$('form#matrix input').hide();
	$("form#matrix td.checkbox").hide();

	// Fieldsets absolut ausrichten, �bereinander legen
	$('form#matrix fieldset').removeClass('statisch').addClass('funky');

	// Tab-Navigation anzeigen
	$('ul#addTabs').toggle();

	// Buttons verstecken
	// $('form#matrix input.choose, form#matrix input.reset').toggle();

	// Einstieg in anderes Tab?
	// z.B. ?studienberater_start=Abschlussgrad
	if (typeof(studienberater_start) != "undefined") {
		tabAktiv = studienberater_start;
	}
	else {
		tabAktiv = "Instrument";
	}

	$("div.wrapper").each(function() {

		wrapperName = $(this).attr("id").substring(8);

		if(wrapperName != tabAktiv)	{
			$(this).hide();
		}
	});

	// Tab aktiv setzen
	$('ul#addTabs a').removeClass('tab_aktiv');
	//$("ul#addTabs a#tab-"+tabAktiv).addClass('tab_aktiv');

}

// NAVIGATION

function higlightNavigation()
{
	//$.lia.log(naviStatus);

	if (naviStatus === 'normal') {
		naviStatus = 'hl';
		$('div#navi span#navitems a.item').addClass("white");

	}
	else {
		naviStatus = 'normal';
		$('div#navi span#navitems a.item').removeClass("white");
	}
}


// SUCHE
function toggleSearch()
{
  	//$.lia.log("toggleSearch()");
	$('div#navi form#search input#searchstring').toggle().focus();
}


// SUB-SUBNAVIGATION
function showSubSubSub()
{
	if (subSubSubStatus == 'closed')
	{
		$(this).children('ul').show();
		$('div#content').css({opacity:0.2});

		subSubSubStatus = 'open';
		if (debug) {
			$.lia.log("SubSubSub " + subSubSubStatus);
		}
		if (debug) {
			$.lia.log("showing sub-sub");
		}
	}
}

function hideSubSubSub()
{
	if (subSubSubStatus == 'open')
	{
		$(this).children('ul').hide();
		$('div#content').css({opacity:1});

		subSubSubStatus = 'closed';
	}
}

// SCROLL
function fixedMenu()
{
	scrolled = $(window).scrollTop();

	var fix_when_scrolled_more_than = (header_height + scroll_after_px_invisible);
	fix_when_scrolled_more_than = header_height;
	var fixed = false;
	var navi_height = $("#subnavi").height();
	var navi_height_total = navi_height + 100;/*  310 ist die Höhe des Rests unterhalb der Navi */
	var content_height = $("#content").height();
//	console.log(navi_height);
	
	if (scrolled > fix_when_scrolled_more_than && $(window).height() > navi_height_total && content_height >= navi_height_total)
	{
		if (fixed === false) {
			//log("Menu is now fixed");
			$('div#navi').addClass('fixed');
			$('div#left').addClass('fixed');

			$('div#content_fade').show();

		}
	}
	else
	{
		if (scrolled <= header_height && typeof(ajax) == 'undefined')
		{
			//log("Menu is now unfixed");
			$('div#navi').removeClass('fixed');
			$('div#left').removeClass('fixed');

			$('div#content_fade').hide();
		}
	}
	return false;
}

// Anker-Links
function hashLinks()
{
	//log("hashLinks()");
	$("div#toc a,div.back_to_top a").click(function() {
		location.hash = $(this).attr('href').replace(/([^#]+)#(.*)/,'$2');
		return false;
	});
}
