$(document).ready(function(){

	// Falls #notizen existiert + die page verlassen wird
	if($('#notizen')[0]){
		$(window).unload(function(){
			alert("Eingegeben Notizen: \n----------------------\n"+$('#notizen').val());
		});
	}
	
						   
	// Tabellen & Aktionsdivs
	$('.aktion ol').css('display','none');
	$('head').append('<link rel="stylesheet" type="text/css" href="css/script.css" media="screen" />');
	$('.aktion span').css('cursor','pointer');
	$('.aktion span').live('click',function(){
		if($(this).next().next().css('display') == 'none'){
			var display = 'block';
			$(this).css('background-image','url(img/icon_dropdown_open.png)');
		}
		else{
			var display = 'none';
			$(this).css('background-image','url(img/icon_dropdown.png)');
		}
		$('.aktion ol').css('display','none');
		$(this).next().next().css('display',display);
	});
	
	$('.info').next().next().css('display','none');
	$('.info').css('cursor','pointer');
	$('.info').live('click',function(){
		if($(this).next().next().css('display') == 'none'){
			var display = 'block';
		}
		else{
			var display = 'none';
		}
		$('.info').next().next().css('display','none');
		$(this).next().next().css('display',display);
	});
	
	$('#tabelle_projekte tbody tr').each(function(){
		if($(this).children('.small').children().children().length > 1){
			var el = $(this).children('td:eq(0)').prepend('<a class="open"><span>&ouml;ffnen</span></a>');

			el.children('strong').addClass('click');
			
		}
		$(this).children('.small').children().children().css('display','none');
		$(this).children('.small').children().children(':eq(0)').css('display','block');
	});
	$('.open').css('cursor','pointer');
	$('#tabelle_projekte .open').live('click',function(){
		if($(this).parent().next().children().children(':eq(1)').css('display') == 'none'){
			var display = 'block';	
			$(this).css('backgroundPosition','bottom');
		}
		else{
			var display = 'none';	
			$(this).css('backgroundPosition','top');
		}
		$(this).parent().next().children().children().css('display',display);
		$(this).parent().next().children().children(':eq(0)').css('display','block');
	});	
	
	$('.click').css('cursor','pointer');
	$('.click').live('click',function(){
		$(this).prev().trigger('click');
	});
		
	// Detailseite: Toggles
	$('.toggle .tabs').css('display','none');
	$('.toggle .tabs:eq(0)').css('display','block');
	$('.toggle .nav a:eq(0)').addClass('active');
	$('.toggle .nav a').live('click',function(){
		$('.toggle .tabs').css('display','none');
		$('.toggle .nav a').removeClass('active');
		$(this).addClass('active');
		var open_id = $(this).attr('href');
		$(open_id).css('display','block');
		return false;
	});
	
	
	var toggle_heights = {};
	$('.toggle').each(function(item){
		var my_index = $('#content .toggle').index($(this));
		toggle_heights[my_index] = $(this).height();
	});
	$('.toggle').css({
		'height':0,
		'overflow':'hidden'
	});
	$('.toggle:eq(0),.toggle:eq(1)').css({'height':'auto'});
	$('.toggle_control').css({
		'cursor':'pointer',
		'background-image':'url(img/icon_closed.gif)'
	});
	$('.toggle_control:eq(0),.toggle_control:eq(1)').css({
		'cursor':'pointer',
		'background-image':'url(img/icon_down.gif)'
	});
	$('.toggle_control').live('click',function(){
		var my_index = $('#content .toggle_control').index($(this));
		if($(this).next().css('height') == '0px'){
			var new_height=toggle_heights[my_index];	
			$(this).css('background-image','url(img/icon_down.gif)');		
		}
		else{	
			var new_height=0;
			$(this).css('background-image','url(img/icon_closed.gif)');		
		}

		$(this).next().animate({
			'height':new_height
		});
	});
	
	
	// Detailseite: Galerie
	$('.gallery a').css('cursor','pointer');
	$('.gallery a:eq(0)').addClass('active');
	$('.gallery a').live('click',function(){
		if($(this).parent().attr('class') != 'video_button'){
			$('.gallery a').removeClass('active');
			$(this).addClass('active');
			var my_index = $('.gallery a').index($(this));
			var marginLeft = my_index * 335 * (-1);
			$('.inner').animate({'marginLeft':marginLeft});
		}
	});
	

	// Galerie
	var opened = false;
	var opened_image = {};
	$('#galerie dt').live('mouseover',function(){
		$(this).css('cursor','pointer');
			var my_index = $("*", document.body).index($(this));
			left[my_index] = false;
			opened_image[my_index] = $(this);

			if($(this).position().left < 200){
				$(this).next().next().css('marginLeft',-50);	
			}
				
			$(this).next().next().delay(1000,function(){
				if(opened == false && left[my_index] != true){
					opened = true;
					$(this).css({
						'display':'block'				
					});
				}
			});
	});

	$('#galerie dt').live('click',function(){
			var my_index = $("*", document.body).index($(this));
			left[my_index] = false;
			opened_image[my_index] = $(this);

			if($(this).position().left < 200){
				$(this).next().next().css('marginLeft',-50);	
			}
				
			if(opened == false && left[my_index] != true){
				opened = true;
				$(this).next().next().css({
					'display':'block'				
				});
			}
	});
	
	var left = {};
	$('#galerie dt').live('mouseout',function(){
		var my_index = $("*", document.body).index($(this));
		left[my_index] = true;
	});
	$('#galerie .popup').mouseleave(function(){
		opened = false;
		opened_image = false;
		$(this).css({
			'display':'none'
		});
	});
	
	
	
	// Suchergebnisse
	$('#suchergebnis dl,#suchergebnis address').css('cursor','pointer');
	$('#suchergebnis dl').live('click',function(){
		window.location.href = $(this).next().next().children('.detail').attr('href');
	});
	$('#suchergebnis address').live('click',function(){
		window.location.href = $(this).next().children('.detail').attr('href');
	});
	
	
	
	// ie7 Fix
	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="msie" && jQuery.browser.version.substr(0,1)=="7"){
			$('.aktion ol').each(function(){
				var width = $(this).prev().prev().width()+45;
				$(this).css('marginLeft','-'+width);
			});
		}
	});
	
});

$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}
