$(document).ready(function(){


	// check whether content exists
	
	if($('#content').length > 0) overlay = true;
	else overlay = false;


	// IMAGE NAVIGATION

	$('div.project_images img').hide();
	$('div.project_images img:first').show().addClass('active');

	
	function showImage(nr) {
		$('div.project_images img.active').hide().removeClass('active');
		$('#image_list li.active').removeClass('active');
		
		$('div.project_images #img'+nr).show().addClass('active');
		
		$('#caption').html($('div.project_images #img'+nr).attr('alt'));
		$('#image_list li:eq('+(parseInt(nr)-1)+')').addClass('active');

		
	}
	
	$('#image_list a').click(function() {
		showImage($(this).html());		
		return false;
	});
	
	function image_click(way) {
		// find out which image is next
		active = $('#image_list li.active');
		
		if(way=='next') {		
			if($(active).next('li').length > 0) {
				var next = $(active).next();
			} else {
				var next = $('#image_list li:first');
			}
		} else if(way=='prev') {
			if($(active).prev('li').length > 0) {
				var next = $(active).prev();
			} else {
				var next = $('#image_list li:last');
			}
		}
		
		showImage($(next).children('a').html());
	
	}
	
// 	$('a.img_prev').click(function() { 
// 		image_click('prev');
// 		return false;
// 	});
// 	$('a.img_next').click(function() { 
// 		image_click('next');
// 		return false;
// 	});
	$('#image').click(function(e) {
		var x = parseInt(e.pageX) - $(this).offset().left;
		if(x>=($('#image').width()/2)) image_click('next');
		else image_click('prev');
		return false;
	});
	
	// NAVIGATION OVER IMAGE
	
	$('#caption').hide();
	$('#arrow_next').hide();
	$('#arrow_prev').hide();


	$('#image').hover(
		function(e) { 
			// $('#caption').show();
			$('#arrow_next').show();
			$('#arrow_prev').show();
		},
		function() { 
			// $('#caption').hide(); 
			$('#arrow_next').hide();
			$('#arrow_prev').hide();
		}
	)
	
	$('#image').mousemove(function(e){
		var x = parseInt(e.pageX) - $(this).offset().left;
		if(x>=($('#image').width()/2)) {
			$('#arrow_prev').removeClass('active');
			$('#arrow_next').addClass('active');
		} else {
			$('#arrow_next').removeClass('active');
			$('#arrow_prev').addClass('active');
		}
		
	});



	// HIDE SUBMENU, SHOW/HIDE ON CLICK, HIDE MENUDELAY MS AFTER MOUSEOUT

	if($('#text').length > 0) $('#submenu').hide();

	// open submenu with active project
	$('ul').each(function(){
		if($(this).find('li.active').length > 0) {
			$(this).addClass('open');
		}
	});

	var menuDelay = 300;
	menuTimeout = false;
	
	function hideMenu() {
		if(!hidesub) return;
		menuTimeout = setTimeout(function(){ 
			$('#submenu').fadeOut(200);
			if(overlay) $('#content').fadeIn(150);
			clearTimeout(menuTimeout);
		}, menuDelay);
	}
	
	function showMenu() {
		if(!hidesub) return;
		$('#submenu').fadeIn(200);
		if(overlay) $('#content').fadeOut(150);
		clearTimeout(menuTimeout);
	}

	// click on active item -> show submenu
	$('#menu li.active a').click(function() {
		showMenu();
		return false; // don't go to page
	});
	
	// hide submenu when mouse is not over menu or submenu
	$('#menu').mouseleave(hideMenu);
	$('#submenu').mouseenter(function() {
			clearTimeout(menuTimeout); 
	});
	$('#submenu').mouseleave(hideMenu);
	
	// open show submenu hierarchy
	$('#submenu li a[href="#"]').click(function(){
		$('#submenu ul').removeClass('open'); // close all
		$(this).parents('ul').addClass('open'); // add to parents
		$(this).siblings('ul').addClass('open'); // add to clicked ul
		return false;
	});

	// click on category title -> open submenu
	$('#category_title').click(function(){
		showMenu();
		menuTimeout = setTimeout(function(){ hideMenu(); }, menuDelay * 3);
		return false;
	});

	// click on back link on page
	$('#page_back').click(function(){
		showMenu();
		menuTimeout = setTimeout(function(){ hideMenu(); }, menuDelay * 3);
		return false;
	});

	
	// hide project description
	$('#desc_text').hide();
	$('a.toggleDesc').click(function() {
		$('#desc_text').slideToggle(150);
		return false;
	});
	
	// project description scroll
	// $(function() { $('#desc_text_scroll').jScrollPane(); });

	
	if($('div.team_images').length > 0) {

		function show_tm(id) {
			$('div.team_images #img'+id+' img').addClass('active').fadeTo(0,1);
			$('div.team_list #li'+id).addClass('active');
		}
		
		function hide_tm() {
			$('div.team_images img.active').removeClass('active').fadeTo(0,.5);
			$('div.team_list li.active').removeClass('active');
		}
	
	
		$('div.team_images img').fadeTo(0,.5);
	
		$('div.team_images a').hover(
			function() {
				var id = $(this).attr('id');
				id = id.substr(3, id.length-3)
				show_tm(id);			
			},
			function() { hide_tm(); }
		);

		$('div.team_list li').hover(
			function() {
				var id = $(this).attr('id');
				id = id.substr(2, id.length-2)
				show_tm(id);			
			},
			function() { hide_tm(); }
		);

	}
	
	// search
	
	$('div.dropdown ul').hide();
	
	// check for active on start
	$('div.dropdown li.active a').each(function(){
		$(this).parents('div.dropdown').children('a:first').children('span').html(': '+$(this).html());
	});

	
	$('div.dropdown a:first-child').click(function(){
		$(this).next('ul').slideToggle(150);
		return false;
	});
	
	$('div.dropdown').mouseleave(function(){
		$(this).children('ul').slideUp(150);
	});
	
	$('div.dropdown li a').click(function(){
		$(this).parents('ul').children('li.active').removeClass('active');
		$(this).parents('li').addClass('active');
		$(this).parents('div.dropdown').children('a:first').children('span').html(': '+$(this).html());
		$(this).parents('ul').slideUp(150);
		return false;
	});

	$('#dropdown_go').click(function(){
		var params = '';
		$('div.dropdown li.active a').each(function(){
			param = $(this).attr('href').split('?')[1];
			if(param) params += '&' + param;
		});
		if (params != '') window.location = '/search/?' + params.substr(1,params.length-1);
		return false;
	});
	
	
	// fulltext search
	
	$('#fulltext_submit').hide();
	
	
	var search_placeholder;
	
	$('#id_q').focus(function() {
		// save default value
		search_placeholder = $(this).attr('value');
		// hide when clicked on
		if($(this).hasClass('initial_state')) $(this).attr('value','');
	});
	$('#id_q').blur(function(){
		// restore if nothing entered
		if($(this).attr('value') == '') $(this).attr('value',search_placeholder); 
	});

	$('#fulltext_go').click(function() {
		if($('#id_q').attr('value') != '' && $('#id_q').attr('value') != 'Volltextsuche') $('#fulltext_form').submit();
		else return false;
	});
	
	
	// homepage slideshow
	
	if($('div.homepage').length > 0) {
		$('#image').cycle({
			timeout: 4000,
			speed: 1000
		});	
	}
	
	// decrypt email
	
	dec = function(b,a) { 
		e = 'mailto:' + a + '@' + b;
		window.location = e;
		return false;
	}
	

	


});