$(document).ready(function(){

	// Hide News-Label, when no pagination is active
	if($('.newspagebrowser .tx-ttnews-browsebox').size()<2){
	    $('.newspagebrowser label').hide();
	}
	
						   
	// Folder Lists
	$('.related-folder-list li span').click(function(){
		if( $(this).next('ul').length > 0 ){
			$(this).parent().prevAll('li').removeClass('open');
			$(this).parent().prevAll('li').find('ul').slideUp();
			$(this).parent().nextAll('li').removeClass('open');
			$(this).parent().nextAll('li').find('ul').slideUp();
			$(this).parent().addClass('open');
			$(this).next('ul').slideDown();
		}
	});

	doSplitDate();
	
	// Sitesearch Arrow Toggle
	$('#SelectLeftSearch').change(function(){
		var state = $('#SelectLeftSearch').val();
		//$('#searchFieldLeftSearchForm').val(state);
		if(state == 'SI'){
			$('#submitLeftSearchForm').css('visibility','visible');
		}else{
			$('#submitLeftSearchForm').css('visibility','hidden');
		}
	});
	
	// Calendar Tooltips
	$('td.has-event').live('mouseover', function(){
		doSplitDate();
		var rel = $(this).attr('rel');
		var aInfos = $('#DateList li.' + rel);
		var infos = '';
		for(var i = 0; i <= aInfos.length-1; i++){
			infos += "<p>" + $(aInfos[i]).html() + "</p>\n";
		};
		$('#CalendarTip .content').empty();
		$('#CalendarTip .content').append(infos);
		
		$(this).prepend($('#CalendarTip'));
		$('#CalendarTip').css('margin-top', -($('#CalendarTip').outerHeight() - 5) );
		$('#CalendarTip').show();
		$('#CalendarTip div').show();
		
		if (document.all && !window.opera) {

			var oLinks = $('#CalendarTip div').find('a');
			
			oLinks.each( function () {
				var _self = this;
				$(this).mousedown(function(){
					window.location.href = $(_self).attr('href');	
				});
			});

			$(document).mousemove(function(){
				var e = window.event;
				var target = $(e.srcElement);
				var match = target.closest( '#CalendarTip' );
				var match2 = target.closest('td.has-event');
				
				if ($(match).attr('id') != 'CalendarTip' && !$(match2).hasClass('has-event')) {
					$('#CalendarTip').hide();
				}				
			});

		} else {
			$(this).mouseout(function(){
				$('#CalendarTip').hide();
			});
			$('#CalendarTip').live("mouseout",function(){
				$(this).hide();
			});
		}

	});
	
	$('#CalendarTip .content p a').live('mouseup',function(){
		window.location.href = $(this).attr('href');
	});

	
	// MainNavi Sublist
	$('#MainNavi li, .tx-pxdaacoursemenu-pi1 li').mouseover(function(){
		$(this).find('.list').css('z-index',10);
		$(this).find('.list').show();
		$(this).mouseout(function(){
			$(this).find('.list').hide();
			$(this).find('.list').css('z-index',1);
		});
	});
	
	// START :: Quicksearch Locations
	$('#LocationSearchBox input').live('keyup',function(){
		if($(this).val().length > 0){
			$(this).parent().parent().find('.results').show();
		}
		else{
			$(this).parent().parent().find('.results').hide();
		}
		$(this).blur(function(){
			$(this).parent().parent().find('.results').hide();
		});
	});
	
	/*
	$('ul#LocationResults li').quicksearch({
		attached: '.results',
		position: 'before',
		labelText: 'Finden Sie einen Standort in Ihrer N�he',
		loaderText: '',
		inputText: 'PLZ oder Ort eingeben'
	});
	*/
	// END :: Quicksearch Locations
	
	$('li.lang-switch span.opener').click(function(){
		$('#LangSwitcher').toggle();
		$('#LangSwitcher span.close, #LangSwitcher .tab span').click(function(){
			$('#LangSwitcher').hide();
		});
	});
	
	///
	///Footer Function
	///
	var tFooterTimer = window.setTimeout(function(){},1); 
	var iFooterHoverTiming = 300;
	$('#QuickInfoBox li a').hover(function(){
		var self = this;
		tFooterTimer = window.setTimeout(function(){
			$('#QuickInfoBox li a').removeClass("active");
			$('#QuickInfoActiveText p').removeClass("active");
			var sPElementToShow = $(self).attr("href");
			$(self).addClass("active");
			$(sPElementToShow).addClass("active");
		},iFooterHoverTiming);
	}, function(){
		window.clearTimeout(tFooterTimer);
	});
	
	$('.folder-box .opener').click(function(){
		closeAllOpenSliders(this);
		$(this).toggleClass('open');
		$(this).next('div').slideToggle();
		return false;
	});
	
	
	$('.daaschulungsausgabe_div a').click(function(e){

			splitted = $(this).attr('rel').split('#');
			active_full_text = splitted[0];
			old_active_full_text = active_full_text;
			active_field_of_profession = ( splitted[1].length > 0 )? splitted[1] : false;
			active_topic = ( splitted[3].length > 0 )? splitted[3] : false;
			active_targetgroup = ( splitted[2].length > 0 )? splitted[2] : false;
			sub_active_location = ( splitted[4].length > 0 )? splitted[4] : false;
			sub_active_lon = ( splitted[5].length > 0 )? splitted[5] : false;
			sub_active_lat = ( splitted[6].length > 0 )? splitted[6] : false;
			active_location = false;
			active_lon = false;
			active_lat = false;
			active_is_fundable = false;
			active_course_slider_position_1 = false;
			active_course_slider_position_2 = false
			if( splitted[0].length > 0 ) {
				$('#wrap_search_fulltext').show();
				$('#mainsearch_full_text').val(active_full_text);
			}
			else {
				$('#wrap_search_fulltext').hide();
			}
			$("#mainsearch_overlay").show();
		    $("#mainsearch_frame").fadeIn();
		    $("#mainsearch_overlay").fadeTo(0,0.4);
		    $('html,body').scrollTop(0);
//neu
		    anfangsDatum = 1262757600;
            endDatum = 1269046800;
	        old_course_slider_position_1 = false;
	        old_course_slider_position_2 = false;
	        active_range_slider_position = ( splitted[7].length > 0 )? splitted[7] : false;
	        course_slider_maxWert = 24;
	        range_slider_min = 0;
	        range_slider_max = 50;
	        $("#mainsearch_location").val("Ort oder PLZ eingeben");
	        $("#mainsearch_body").html(' ');
	        $("#wrap_count_result .count").html('0');
	        $("#mainsearch_is_fundable").attr("checked","");
	        if(sub_active_location != false)
	        {
	        	active_location = sub_active_location;
				active_lon = sub_active_lon;
				active_lat = sub_active_lat;
				$("#mainsearch_location").val(active_location);
	        }
//neu
		    callRebuildMainSearch();
		    return false;
		
	});
	
	///
	///	Linkes suchformular
	///
	$('#searchFieldLeftSearchForm').keypress(function(e){
		if(e.keyCode == 13)
		{
			if($('#SelectLeftSearch').val() == 'BA')
			{
			    return false;
			}
			else
			{
				$('#LeftSearchForm').submit();
			}
		}
		
	});
	
	
	
	$('#submitLeftSearchForm').click(function(){
		if($('#SelectLeftSearch').val() == 'BA')
		{

		}
		else
		{
			$('#LeftSearchForm').submit();
		}
	});
	
	$('#searchFieldLeftSearchForm').keyup(function(){
		if($('#SelectLeftSearch').val() == 'BA')
		{
			var tmp_active_full_text = $("#searchFieldLeftSearchForm").val();
			if(tmp_active_full_text.length >= 3)
			{
				active_full_text = tmp_active_full_text;
				callRebuildFullTextMiniSearch('#full_text_list_mini',active_full_text);
			}
			else
	    		$('#full_text_list_mini').hide();
			
		}
	});
	
	$(document).click(function(){ $('#full_text_list_mini, #full_text_list_mini2,#location_list_mini').hide(); });
	
	
	$('#CourseMenuLink').click(function(){
//neu		
		anfangsDatum = 1262757600;
        endDatum = 1269046800;
        active_field_of_profession = false;
        active_topic = false;
        active_targetgroup = false;
        active_location = false;
        active_lon = false;
        active_lat = false;
        active_is_fundable = false;
        active_course_slider_position_1 = false;
        active_course_slider_position_2 = false;
        old_course_slider_position_1 = false;
        old_course_slider_position_2 = false;
        active_range_slider_position = false;
        course_slider_maxWert = 24;
        range_slider_min = 0;
        range_slider_max = 50;
        $("#mainsearch_location").val("Ort oder PLZ eingeben");
        $("#mainsearch_body").html(' ');
        $("#wrap_count_result .count").html('0');
        active_full_text = false;
        $("#mainsearch_full_text").val('');
        $("#mainsearch_is_fundable").attr("checked","");
        if(sub_active_location != false)
        {
        	active_location = sub_active_location;
			active_lon = sub_active_lon;
			active_lat = sub_active_lat;
			$("#mainsearch_location").val(active_location);
        }
//neu	
		searchIt = this.rel.split('|');
		searchIt2 = searchIt[1].split('^');

		if(searchIt[0] == 'fachbereich')
		{
			active_field_of_profession = searchIt2[0]
		}
		else
		{
			active_targetgroup = searchIt2[0]
		}
		if(searchIt2[1])
			active_lat = searchIt2[1];
		if(searchIt2[2])
			active_lon = searchIt2[2];
		if(searchIt2[3])
			active_range_slider_position = searchIt2[3];
		if(searchIt2[4])
		{
			$('#mainsearch_location').val(searchIt2[4]);
			active_location = searchIt2[4];
		}
		$('#wrap_search_fulltext').hide();
		active_full_text = false;
		$('#mainsearch_full_text').val(active_full_text);
		$("#mainsearch_overlay").show();
	    $("#mainsearch_frame").fadeIn();
	    $("#mainsearch_overlay").fadeTo(0,0.4);
	    $('html,body').scrollTop(0);
	    callRebuildMainSearch();
	});
	
	
	$('a.courseLeftMenuDetail').click(function(){
		$('#wrap_search_fulltext').hide();
	    $("#mainsearch_overlay").show();
	    $("#mainsearch_frame").fadeIn();
	    $("#mainsearch_overlay").fadeTo(0,0.4);
	    $('html,body').scrollTop(0);
	    //callRebuildMainSearch(true);

		$('div.wrap_search_interface').slideUp();
		$('div.search_backlink').fadeIn();
		$('html,body').scrollTop(0);
		$('#mainsearch_body').scrollTop(0);
		callShowCourseDetails(this.rel);
	});
	
	
	$('li.show-all a.btn-standard-blu').click(function(){
//neu		
		anfangsDatum = 1262757600;
        endDatum = 1269046800;
        active_field_of_profession = false;
        active_topic = false;
        active_targetgroup = false;
        active_location = false;
        active_lon = false;
        active_lat = false;
        active_is_fundable = false;
        active_course_slider_position_1 = false;
        active_course_slider_position_2 = false;
        old_course_slider_position_1 = false;
        old_course_slider_position_2 = false;
        active_range_slider_position = false;
        course_slider_maxWert = 24;
        range_slider_min = 0;
        range_slider_max = 50;
        $("#mainsearch_location").val("Ort oder PLZ eingeben");
        $("#mainsearch_body").html(' ');
        $("#wrap_count_result .count").html('0');
        active_full_text = false;
        $("#mainsearch_full_text").val('');
        $("#mainsearch_is_fundable").attr("checked","");
        if(sub_active_location != false)
        {
        	active_location = sub_active_location;
			active_lon = sub_active_lon;
			active_lat = sub_active_lat;
			$("#mainsearch_location").val(active_location);
        }
//neu
		searchIt = this.rel.split('|');
		searchIt2 = searchIt[1].split('^');
		
			//alert(searchIt);	
		
		if(searchIt[0] == 'fachbereich')
		{
			active_field_of_profession = searchIt2[0]
		}
		else
		{
			active_targetgroup = searchIt2[0]
		}
		if(searchIt2[1])
			active_lat = searchIt2[1];
		if(searchIt2[2])
			active_lon = searchIt2[2];
		if(searchIt2[3])
			active_range_slider_position = searchIt2[3];
		if(searchIt2[4])
		{
			$('#mainsearch_location').val(searchIt2[4]);
			active_location = searchIt2[4];
		}
		$('#wrap_search_fulltext').hide();
		active_full_text = false;
		$("div.search_backlink").fadeOut();
		$("div.wrap_search_interface").slideDown();
		$('#mainsearch_full_text').val(active_full_text);
		$("#mainsearch_overlay").show();
	    $("#mainsearch_frame").fadeIn();
	    $("#mainsearch_overlay").fadeTo(0,0.4);
	    $('html,body').scrollTop(0);
	    callRebuildMainSearch();
	});
	
	// FAQ Infofolder
	
	$('#FAQ h3').hide();
	$('#FAQ .info-folder').hide();
	$('#FAQ h3:eq(0)').show();
	$('#FAQ .info-folder:eq(0)').show();
	$('#FAQ .info-folder-remote li:eq(0) a').attr('class','active');
	$('#FAQ .info-folder-letters li:eq(0) a').attr('class','active');
	
	$('.info-folder-remote li a').click(function(){
		$('.info-content').slideUp();
		$('.info-folder li a.do-fold-active').attr('class','do-fold');
		$('.info-folder-remote li a').removeClass('active');
		$(this).addClass('active');
		
		
		var title = $(this).text();
		$('#FAQ h3').each(function(){
			if($(this).text() == title){
				$(this).show();
				$(this).next('.info-folder').show();
			}else{
				$(this).hide();
				$(this).next('.info-folder').hide();
			}
		});
		
		return false;
	});
	
	$('.info-folder-letters li a').click(function(){
		$('.info-content').slideUp();
		$('.info-folder li a.do-fold-active').attr('class','do-fold');
		$('.info-folder-letters li a').removeClass('active');
		$(this).addClass('active');
		
		
		var title = $(this).text();
		$('.glossary .info-folder').each(function(){
			if($(this).attr('id') == 'letter-' + title){
				$(this).show();
				$(this).next('.info-folder').show();
			}else{
				$(this).hide();
				$(this).next('.info-folder').hide();
			}
		});
		return false;
	});
	
	
	$('.info-folder li .do-fold').click(function(){
		if($(this).next('.info-content').length > 0){
			$('.info-content').slideUp();
			$('.do-fold-active').attr('.do-fold');
			if ($(this).hasClass('do-fold')){
				$('.info-folder li .do-fold-active').attr('class','do-fold');
				$(this).nextAll('.info-content').slideDown();
				$(this).attr('class','do-fold-active');
			}else{
				$(this).attr('class','do-fold');
				$(this).nextAll('.info-content').slideUp();
			}
		}
	});
	
	
	
	// IndexTabs
	$('#IndexTabs li, .info-folder-letters li').hover(
		function(){
			$(this).toggleClass('hover-tab');
		},
		function(){
			$(this).toggleClass('hover-tab');
		}
	);
	$('#IndexTabs li, .info-folder-letters li').click(function(){
		if(!$(this).hasClass('empty')){
			var letter = $(this).text();
			if(letter == '#'){letter = 'digit'}
			$('#IndexTabs li, .info-folder-letters li').removeClass('active-tab');
			$(this).addClass('active-tab');
			
			$('.info-folder').hide();
			$('#Block_' + letter).show();
			if(letter == 'digit'){ $('#IndexBlocks ul:first-child').show(); }
		}
	});
	
	fillIndexTabs();

	// IndexTabs END
	
	
	
	
	$('#set_group').click(function(){
		$(this).parent().prevAll('li').find('input').attr('checked','checked');
	});
	$('#unset_group').click(function(){
		$(this).parent().prevAll('li').find('input').attr('checked','');
	});
	$('#set_field').click(function(){
		$(this).parent().prevAll('li').find('input').attr('checked','checked');
	});
	$('#unset_field').click(function(){
		$(this).parent().prevAll('li').find('input').attr('checked','');
	});
	
	
	$('.room-detail ul li.active').next('li').css('margin-left','0');
	$('.room-detail ul li').click(function(){
		$(this).closest('ul').find('li').removeClass('active');
		$(this).addClass('active');
		
		$(this).closest('ul').find('li').css('margin-left','5px');
		$(this).next('li').css('margin-left','0');
		$(this).css('margin-left','0');
		
		$(this).closest('.room-detail').find('.info-box').hide();
		var tabname = $(this).attr('rel');
		$(this).closest('.room-detail').find('.' + tabname).show();
	});
	
	$('.detail-switch').click(function(){
		$(this).toggleClass('closed');
		if(!jQuery.data( this,'init')){
			jQuery.data( this,'init',1);
			jQuery.data( this,'open',$(this).attr('rel') );
			jQuery.data( this,'closed',$(this).text() );
		}
		if($(this).text() == jQuery.data(this,'closed')){
			$(this).text(jQuery.data(this,'open'));
		}else{
			$(this).text(jQuery.data(this,'closed'));
		}
		$(this).closest('.room-info').next('.room-detail').slideToggle();
	});
	
	$('#wrap_is_fundable .search_inline_info').hover(
		function(){
			$('#FundableInfoLayer').css('z-index',1000).show();
			$('#wrap_search_interface').css('z-index',1000);
		},
		function(){
			$('#FundableInfoLayer').css('z-index',0).hide();
			$('#wrap_search_interface').css('z-index',0);
		}
	);
	$('#MapSelectList label').click(function(){
		window.location.href = $(this).parent().attr('href');
	});
	
	$('table.altering tbody tr:nth-child(2n) td').css({'background-color':'#FFF','background-image':'none'});
	$('#ContentLeft .download-link:last').css('border-bottom','1px solid #A8D2FD');
	
	/* faq exmpaiation mark */
	$('.tx-powermail-pi1_formwrap_85').prepend('<span class="ex-mark"></span>');
	
	
	/* dirty faked select field of ajax suggests */
	$('#locMiniSearchInput').keydown(function(e){
		var list = '#location_list_mini';
		if ($(list).is(':visible')){
			if (e.keyCode == 40) {
				doFakeSelect(list);
			}
		}
	});
	$('#searchFieldLeftSearchForm').keydown(function(e){
		var list = '#full_text_list_mini';
		if ($(list).is(':visible')){
			if (e.keyCode == 40) {
				doFakeSelect(list);
			}
		}
	});
	$('#mainsearch_location').keydown(function(e){
		var list = '#location_list';
		if ($(list).is(':visible')){
			if (e.keyCode == 40) {
				doFakeSelect(list);
			}
		}
	});
	$('#locMiniSearchInput').keydown(function(e){
		var list = '#location_list_mini';
		if ($(list).is(':visible')){
			if (e.keyCode == 40) {
				doFakeSelect(list);
			}
		}
	});
	
	
	
});

function doFakeSelect(list){
	$(list + ' table tr:first-child a').focus();
				
	$(list +  ' a:focus').live('keydown',function(e2){
		var curRow = $(this).closest('tr');
		var prevLink = $(this).closest('tr').prev('tr').find('a');
		var nextLink = $(this).closest('tr').next('tr').find('a');
		if (e2.keyCode == 38) {
			if(!curRow.is(list + ' table tr:first-child')){
				prevLink.focus();
				e2.preventDefault();
			}
		}else if(e2.keyCode == 40){
			if(!curRow.is(list + ' table tr:last-child')){
				nextLink.focus();
				e2.preventDefault();
			}
		}
	});
}


function doSplitDate(){
	// Calendar Split Date
	$('td.has-event').each(function(){
		var rel = $(this).attr('rel');
		var aInfos = $('#DateList li.' + rel);
		var infos = '';
		for(var i = 0; i <= aInfos.length-1; i++){
			$(this).addClass($(aInfos[i]).attr('rel'));
		};
	});
}

function fillIndexTabs(){
	$('#IndexTabs li, .info-folder-letters li').removeClass('active-tab');
	$('#IndexTabs li, .info-folder-letters li').each(function(){
		var letter = $(this).text();
		if(letter == '#'){letter = 'digit'}
		//console.debug(letter);
		if( $('#Block_' + letter + ' li').length > 0 ){
			//console.debug( $('#Block_' + letter + ' li').length );
			$(this).removeClass('empty');
		}
		else{
			$(this).addClass('empty');
			$(this).removeClass('active-tab');
		}
		if($('#IndexBlocks ul:first-child li').length > 0){
			$('#IndexTabs li:first-child').removeClass('empty');
		}
	});
}



