var keepCheckingSearch;
var WEEKDAYS = new Array(
	"Sonntag",
	"Montag",
	"Dienstag",
	"Mittwoch",
	"Donnerstag",
	"Freitag",
	"Samstag"
);


function insertSearch(selector){	//used for dozenten
	$(selector+':first').before('<div id="search"><span>Volltextsuche:</span><input type="text" value="" /><span class="example"> z.B. Schneider, Schlagzeug oder 2007 …</span></div>');
	$('#search input').change(function(){
		execSearch(selector,$(this).val());
	});
	$('#search input').focus(function(){
		keepCheckingSearch = window.setInterval("execSearch('"+selector+"',$('#search input').val());",1000);
	});
	$('#search input').blur(function(){
		window.clearInterval(keepCheckingSearch);
	});
}


function execSearch(selector,needle){
	if(needle == ''){
		$(selector).show();
		//$('h2').show();
		return;
	}	else	{
		//$('h2').hide();
		$(selector).each(function(){
			if($(this).text().toLowerCase().indexOf(needle.toLowerCase()) == -1 ){
				$(this).hide();
			}	else	{
				$(this).show();
			}
		});
		
	};
}

function glow(selector){
	if($(selector).length == 0) return;
	$(selector).fadeOut('slow',function(){
		$(selector).fadeIn('slow',function(){
			glow(selector);
		})
	});
}

function toggleDetail(node){
	
	
	if(node.next('div:first').length == 0){
		//alert('adding span')
		node.after('<div class="loading">loading…</div>');
		glow('.loading');
		var parts = node.attr('href').split('#');
		var link = parts[0];
		var id = parts[1];
		var url = link+' #'+id;
		node.next('div:first').load(url,function(){
			$('#'+id).parent().removeClass('loading');
			$('#'+id).parent().fadeIn();
			$('#'+id).removeClass('dozent');
			$('#'+id).removeClass('dozent').remove('h2').attr('id','')
			/*$(this).click(function(){
				alert($(this).parent().prev('a').first().nodeName);
				//alert($(this).nodeName);
			});
			*/
		});
		
	}	else if(node.next('div:first').css('display') == 'none'	){
		
		node.addClass('open');
		node.next('div:first').css({display:'block'});
		
	}	else if(node.next('div:first').css('display') == 'block'){
		node.removeClass('open');
		node.next('div:first').css({display:'none'});
	}
	
}

function togglePage(node){
	
	$('div.aside').first().addClass('open');
		
	if($('div.aside div').length == 0){
		$('ul.aside').before('<div class="loading" id="load-page">loading…</div>');
	}	else	{
		//alert('here');
		$('div.aside div:first').empty();
		$('div.aside div:first').text('loading…');
		$('div.aside div:first').addClass('loading');
		//alert($('div.aside div:first').attr('class'));
	}
	glow('.loading');
	
	$('div.aside').addClass('open');
	var url = node.attr('href').split(' ').join('%20')+' #top';
	
	$('div.aside div.loading:first').load(url,function(){
		//alert($(this).text());
		//$('div.aside div.loading').removeClass('loading');
		$('#load-page').removeClass('loading');
		//$('div.aside div').first().text('loaded');
		//$('div.aside div').first().fadeIn();
		$('#load-page').fadeIn();
	
		$('div.aside').click(function(){
			$(this).removeClass('open');
			$(this).unbind('click');
		});
		
	});
		
	/*var url = node.attr('href').split(' ').join('%20');
	+' #top';
	$.ajax({
	    type: "GET",
	    url: url,
	    cache: false,
	    success: function(data) {
			//$('#load-page').html(data);
			//$('div.aside div:first').html('loaded');
			$('div.aside div:first').html(data);
		}
	});
	*/
}

function scrollTo(node){
	
	//alert($('div.body').scrollTop());
	var offset = window.pageYOffset;
	window.location.hash = "#"+$(node).attr('id');
	$('html,body').scrollTop(offset);
	$('html,body').animate({scrollTop: node.offset().top - parseInt($('div.body').css('padding-top')) + 4},'fast');
}


$(document).ready(function(){
	
	//enhance scrolling for last element
	//would be better to search for the last local anchor/id
	if($('h2').length > 1){
		//var last_section_height = ($(document).height() - $('h2').last().offset().top);
		var last_top = $('div.body *[id]').length > 0? $('div.body *[id]').last().offset().top : 0;
		var last_section_height = ($(document).height() - last_top);
		//var expand_body_by = $(window).height() - last_section_height - parseInt($('div.body').css('padding-top'));
		var expand_body_by = $(window).height() - last_section_height - 120;
		if(expand_body_by > 0){
			$('div.body p:last').css({paddingTop:expand_body_by+'px'});;
		}
	}
	
	//alert($('div.event-text').first().css('line-height'));
	$('div.event-text').each(function(){
		//definitely put all nodes but the first in an extra container
		var lineHeight = 30;
		if( $(this).height() > lineHeight){
			$(this).addClass('overload');
			$(this).click(function(){
				if($(this).hasClass('overload')){
					$(this).removeClass('overload');
				}	else	{
					$(this).addClass('overload');
				}
				
			});
			
		}
		/*
		var words = $(this).text().split(' ');
		if(words.length > 12){
			var move = $(this).children();
			$(this).append('<a class="event-text-teaser"></a>');
			$(this).find('a.event-text-teaser').click(function(){
				$(this).parent().find('div.event-text-continued').show();
				$(this).hide();
			});
			$(this).append('<div class="event-text-continued"></div>');
			$(this).find('div.event-text-continued').append(move);
			$(this).find('a.event-text-teaser').text(words.slice(0,7).join(' ')+'…');
		}
		*/
	});
	
	/*
	$('ul.secondary li li a').each(function(){
		//var lineHeight = 48;
		var lineHeight = 96;
		if( $(this).height() > lineHeight){
			$(this).addClass('overload');
			$(this).mouseover(function(){
				$(this).removeClass('overload');
			});
			$(this).mouseout(function(){
				$(this).addClass('overload');
			});	
		}
	});
	*/
	
	/*
	if($('*[id]').length > 1){
		//we want extra space below the last id, so that the anchor sits on top of the document if selected
		//we need to add window.height minus topmenu minus elements after the anchor

		var space_after_last_id = $(document).height() - $('div.body *[id]').last().offset().top;
		var space_before_first_element = parseInt($('div.body').last().css('paddingTop'));
		var expand_body_by = $(window).height() - space_after_last_id - space_before_first_element;
		//alert($('div.body h1,div.body p').first().offset().top);
		//alert($('div.body h1,div.body p').first().text());
		alert(space_before_first_element);
		//alert($('h1').first().text());
		if(expand_body_by > 0){
			$('div.body p:last').css({paddingTop:expand_body_by+'px'});;
		}
	}
	*/
	
	$('a').each(function(index,anchor){
		if($(this).attr('href').indexOf('#') == 0){
			$(this).click(function(){
				scrollTo($("#"+$(this).attr('href').split('#')[1]));
				return false;
			});
		}
	});	
	
	$('div.body').hide();
	$('body:first').append('<div class="loading" id="loading-page"></div>');
	//glow('.loading');
	
	if($('.thumbnails a').length > 0){
		$('.thumbnails a').lightBox();
	}
	
	$('a.expandable').click(function(){
		toggleDetail($(this));
		return false;
	});

	//disable aside-loader for msie for now, needs debugging
	if(navigator.userAgent.indexOf('MSIE') == -1){
		$('ul.aside a').click(function(){
			togglePage($(this));
			return false;
		});
	}
	
	if(navigator.userAgent.indexOf('MSIE') != -1){
		$('ul.main').css({position:'fixed',marginLeft:'0',paddingLeft:'0',left:'0'});
		$('div.dozent img').css({marginLeft:'0',marginTop:'40px'});
		/*$('div.body ul.index').css({marginLeft:'0'});*/
		$('div.body ul').css({paddingLeft:'2em',marginLeft:'0'});
		$('div.body ul.index').css({paddingLeft:'1.5em'});
		//$('div.body ul.index').css({marginLeft:'0'});
		$('div.body a').append('<div class="link-img">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>');
		$('div.body dt').css({width:'170px'});
		$('div.body dd').css({marginLeft:'0',paddingLeft:'170px',width:'265px'});
		$('div.body dt').each(function(){
			$(this).find('~ dd').first().css({paddingLeft:'0'});
		});
	}
	
	/*
	$('span.text-continued').hide();
	$('span.text-teaser').append('<a>…</a>');
	$('span.text-teaser').click(function(){
		$(this).find('a').last().remove();
		$(this).next('span.text-continued').show();
	});
	*/
	
	$('span.weekday[title]').each(function(){
		var title = $(this).attr('title');
		var month = Number(title.substr(4,2))-1;
		var day = Number(title.substr(6,2));
		var date = new Date(parseInt(title.substr(0,4)),month,day);
		$(this).text(WEEKDAYS[date.getDay()].substr(0,2)+' ');//+'test:'+date.getFullYear()+''+date.getMonth()+''+date.getDate()+':test');
	});
	
	
});


function toggleDrawer(){
	if($('div.aside').hasClass('open')){
		$('div.aside').removeClass('open');
	}	else	{
		$('div.aside').addClass('open');
	}
	//alert('toggle');
}

$(window).load(function(){
	$('#loading-page').remove();
	$('div.body').fadeIn(function(){
		if(window.location.hash.length > 1){
			scrollTo($(''+window.location.hash));
		}
	});
	if($('div.diashow').length > 0){
		$('div.diashow').cycle({ 
		    fx: 'fade',
			timeout: 6000,
			speed: 2000
		});
	}
	//$.localScroll();
});
