// Debug-Modus?
debug = false;

// Redirect bei Ajax-Bookmarks
// (z.B. "portrait/personen/leitung#portrait-personen-fachbereiche___eintrag-fachbereich-1-92"
// auf "portrait/reputationen/fachbereiche#eintrag-fachbereich-1-92")

if ( location.hash && location.hash.search(/(.*___.*)|(\/)/) > -1 )
{

	oldloc = "http://" + location.hostname + location.pathname;
	//$.lia.log("oldloc = "+oldloc);
	
	// z.B. "portrait-personen-fachbereiche___eintrag-fachbereich-1-92"
	oldHash = location.hash;
	//$.lia.log("oldHash = "+oldHash);

	// wir trennen die m?gliche neue URL und den Anker
	var hashParts = [];
	hashParts = location.hash.split('___');

	// die neue URL ("portrait-personen-fachbereiche")
	newloc = hashParts[0];

	// aus "#portrait_personen_fachbereiche" wird "portrait_personen_fachbereiche"
	newloc = newloc.slice(1);

	// aus "portrait_personen_fachbereiche" wird "portrait/personen/fachbereiche"
	newloc = newloc.replace(/_/g,'/');

	// $_base hinzu, also "http://wwww.hfm-detmold.de/portrait/personen/fachbereiche"
	newloc = _base + newloc;
	//$.lia.log("newloc = "+newloc);

	if ( newloc != oldloc )
	{
		//newloc = 'foo';
		
		// per Ajax pr?fen, ob die neue URL keinen 404 erzeugen w?rde 
		$.ajax(
		{
			type: "GET",
			url: newloc,
			async: false,
			dataType: "html",
			cache: false,
			success: function(data)
			{
				// den Anker/Hash wieder hinzuf?gen
				// (falls er vorhanden war)
				if ( hashParts[1] )
				{
					newloc = newloc+oldHash;
				}
				$.lia.log("newloc = "+newloc);
				
				// neue URL aufrufen... go, go, go!
				location.href = newloc;
	
				return false;
			},
			error: function(msg)
			{
	  			//console.log(msg);
			}
		});
	}
}


// SCROLL
header_height = 264;
scroll_delay = 250;
fixed_item_height = 30;
scroll_after_px_invisible = fixed_item_height;
naviStatus = "normal";

function doAttachEvent() {
	try {
		window.attachEvent("onscroll", fixedMenu);
	} catch(err) {}
}

$().ready(function()
{
	// Focus-Funktion bei Fehlern nach dem Versand des Kontaktformulars
	if (($('body.kontakt') && $('body.kontakt').length) >= 1 && ($("div#error") && $("div#error").length !== 0)) {
		scroll(0,$("h2").offset().top);
		$("div.ef_alert:first > :input").focus();
	}

	// Men? bei Scroll auf position:fixed setzen
	$('body').attr('onscroll', 'fixedMenu()');

	// Funktionen initialisieren
	// (nicht in der Lehrenden-Liste)

	//$.lia.log($('body.portrait_personen_lehrende').length);
	//$.lia.log($('body.portrait_personen_lehrende-nach-faechern').length);
	if (
		$('body.portrait_personen_lehrende').length == 1
		|| $('body.portrait_personen_lehrende-nach-faechern').length == 1
	) {
		startMeUp(true);
	}
	else {
		startMeUp();
	}

/*
	console.time("foo");
	startMeUp();
	console.timeEnd("foo");
*/

	// NAVI-Hover (nicht auf Startseite)
	$('div#navi').hover(higlightNavigation, higlightNavigation);

	// SUCHE

	$('div#navi form#search input#searchstring').toggle();
	$('form#search').hover(toggleSearch, toggleSearch);

	// SUBNAVI

	// Mouseover f?r Sub-Sub-Navigation mit Sub-Sub-Sub-Men?
	subSubSubStatus = 'closed';
	$('div#subnavi ul.sub-subnavi li.got-sub-sub').bind("mouseover", {}, showSubSubSub);
	$('div#subnavi ul.sub-subnavi li.got-sub-sub').bind("mouseout", {}, hideSubSubSub);

	// subnavi foldout
	$('div#subnavi ul.level1 > li > a').click(function() {
		
		$('div#subnavi ul > li').removeClass('active');
		$('div#subnavi ul > li').removeClass('visible');
		
		if ($(this).siblings('ul').is(':hidden')) {
			
			//$(this).siblings('ul').css('display', 'block');
			$(this).parent().addClass('active');
			$(this).parent().addClass('visible');
			$(this).siblings('ul').hide();
			$(this).siblings('ul').slideDown("slow", function () {
				slide_up_sub_sub_navis();
			});
		}
		else {
			$(this).parent().addClass('visible');
			$(this).parent().addClass('active');
			slide_up_sub_sub_navis();
		}
		
		return false;

	});
	
	// AJAX F?R SUBNAVI
	$("div#subnavi ul.level1 > li > a:not(.external), div#subnavi ul.level2 > li > a:not(.external)").click(function() {
		
		//$.lia.log($(this).parent('li').siblings('li').removeClass('active'));
		$(this).parent('li').siblings('li').removeClass('active');
		$(this).parent().addClass('active');
		
		var verwaltung_login_form = $('#verwaltung_login');
		
		if ($(this).attr('href').match(/service/)) {
			verwaltung_login_form.removeClass('hidden');
		}
		else {
			verwaltung_login_form.addClass('hidden');
		}
		
		// 02.10.2008 dw: links mit "noajax=true" in der subnavi nicht per ajax nachladen (studienberater)
		// 28.05.2009 dw: das ?noajax und den #... part rausschneiden damit firefox neu laedt
		if ($(this).attr('href').match(/noajax=[true|1]/)) {
			var loc = _base + $(this).attr('href').replace(/\?noajax=true/, "").replace(/(#.*)/, '');
				
			//console.log(location.href);
			//console.log(loc);
					
			if (loc != location.href)
			{
				window.location.href = loc;
				
				//console.log("RELOCATE");
			}
			else
			{
				location.reload();
				
				//console.log("RELOAD");
			}

			return false;
		}

		is_scrolled = false;
		//$.lia.log($(window).scrollTop()+" > "+header_height);
		//console.log(typeof(scrollTop()));
		try
		{
			var navi_height = $("#subnavi").height();
			var navi_height_total = navi_height + 100;/*  310 ist die Höhe des Rests unterhalb der Navi */
			
			if ($(window).scrollTop() > header_height && $(window).height() > navi_height_total && content_height >= navi_height_total)
//			if ($(window).scrollTop() > header_height)
			{

				//$.lia.log("Fenster wurde gescrollt, Navigation ist fixed");
				scrolled = $(window).scrollTop();
				is_scrolled = true;
			}
		}
		catch(err)
		{
			
		}

		$("#main").prepend('<img id="loading" src="'+_base_root+'images/loading.gif" alt="" />');

		$("#content").remove();

		// m?ssen wir den Hash ver?ndern?
		foo = "http://"+location.hostname+location.pathname;
		//$.lia.log(foo);
		oldHash = foo.substring(_base.length);
		//$.lia.log("oldHash = "+oldHash);
		//newHash = $(this).attr('href').substring(_base.length);
		newHash = $(this).attr('href');
		//$.lia.log(oldHash+" == "+newHash);
		//alert(oldHash+" == "+newHash);

		if (oldHash != newHash) {
			// Hash neu setzen
			location.hash = newHash;
		} else {
			// Hash zur?cksetzen
			location.hash = '#';
		}

		// Seite nach 500ms per Ajax laden >>
		setTimeout("ajaxGet('"+this+"')", 500);

		return false;
	});

	// FLASH
	if (userHasFlash)
	{ // Wir k?nnen flashen
		// alle seiten

		if ($('body.startseite').length < 1)
		{
			if ($('body').attr('class') == 'portrait'
				|| $('body').attr('class') == 'studium'
				|| $('body').attr('class') == 'konzerte'
				|| $('body').attr('class') == 'service')
			{
				
				// Fallback-Grafik verstecken
				$("div#flashheader img.flash-fallback").hide();

				$('#flashheader').flash(
				{
					src: _base_root+'swf/headeranimation_hauptmenuepunkte.swf',
					width: 950,
					height: 264,
					id: 'flashheadswf',
					name: 'flashheadswf',
					swLiveConnect: true,
					allowScriptAccess: 'sameDomain',
					wmode: 'transparent',
					flashvars: { i18n: _lang }
				},
				{
        			update:true,
          			version: '8'
					/*expressInstall: true,
					update:true
					version: '6.0.65',
					update: false*/
				},
				null,
				function(htmlOptions) // Player zu alt
				{ 
					// Fallback-Grafik wieder anzeigen
					$("div#flashheader img.flash-fallback").show();

					// Player-Tooltip anzeigen
					if( ! readFlashFallbackCookie() )
					{
						$('div.flash-fallback-tooltip').show();
					}
				});
			}
			else
			{
				$("div#flashheader img.flash-fallback").removeClass('hidden');
			}

		}
		else
		{ // Startseite

			// Fallback-Grafik verstecken
			$("div#flashheader img.flash-fallback").hide();

			$('#flashheader').flash(
			{
				src: _base_root+'swf/headeranimation_hauptmenuepunkte.swf',
				width: 950,
				height: 264,
				id: 'flashheadswf',
				name: 'flashheadswf',
				swLiveConnect: true,
				allowScriptAccess: 'sameDomain',
				wmode: 'transparent',
				flashvars: { i18n: _lang }
			},
			{
    			update:true,
      			version: '8'
				/*expressInstall: true,
				update:true
				version: '6.0.65',
				update: false*/
			},
			null,
			function(htmlOptions) // Player kleiner als 8
			{
				//alert('flash < 8');
				$('#flashheader').flash(
				{
					src: _base_root+'swf/headeranimation_hauptmenuepunkte_flash7.swf',
					width: 950,
					height: 264,
					id: 'flashheadswf',
					name: 'flashheadswf',
					swLiveConnect: true,
					allowScriptAccess: 'sameDomain',
					wmode: 'transparent',
					flashvars: { i18n: _lang }
				},
				{
	    			update:true,
	      			version: '7'
					/*expressInstall: true,
					update:true
					version: '6.0.65',
					update: false*/
				},
				null,
				function(htmlOptions) // Player kleiner als 7
				{
					//alert('flash < 7');
					$("div#flashheader img.flash-fallback").show();
					// Player-Tooltip anzeigen
					if( ! readFlashFallbackCookie() )
					{
						$('div.flash-fallback-tooltip').show();
					}
				});
			});
		}

	}
	else
	{ // Flash-Fallback
		$("div#flashheader img.flash-fallback").show();
		
		// Player-Tooltip anzeigen
		if( ! readFlashFallbackCookie())
		{
			$('div.flash-fallback-tooltip').show();
		}
	}

	$('div#navi').append('<div id="content_fade"></div>');
	$('div#content_fade').hide();

	// THICKBOX IMAGES ZOOM ICON
	if ($('div#content a.thickbox')) {
   	$('div#content a.thickbox').each(function() {
   		// 02.10.2008 dw: links mit class="nozoom" kein [+] verpassen
   		if (!$(this).hasClass('nozoom')) {
		   	$(this).append('<span class="zoom"></span>');
	   	}
   	});
   }

});

function changeColor(id)
{
	$('div#'+id).css('color', '#E31B1C');
}

function slide_up_sub_sub_navis ()
{
		// S?mtliche Sub-Sub-Navis durchlaufen
		$('ul.level1 > li ul').each(function(){
			
			if ($(this).parent().is(':not(.active)')) {
				$(this).slideUp();
			}
			else {

			}
			
		});
		
		return false;
}
