
jQuery(document).ready(function() {

	$('#search_submit').click(function() {

		if ($("#searchword").val() == 'Ihr Suchbegriff') {
			$('#searchword').val('');
		}
		
		if($("#searchword").val() == ''){

			var projects = $('#select_1 :selected').val();
			var institutions = $('#select_2 :selected').val();
			var viewtype = '';
			
			if ( $(".projects.project-list").length > 0 ) {
					viewtype = 'projects';
			}
			if ( $(".institution.institution-list").length > 0 ) {
				viewtype = 'institutions';
			}
			
			 $.post('index.php?eID=navfilter&projects='+ projects + '&institutions=' + institutions + '&viewtype=' + viewtype, function(data) {
				 	$('input#chash').val(data);
				 	document.forms["navigator-searchform"].submit();
				  });
		}
		else {
			document.forms["navigator-searchform"].submit();
		}
	});

	$('.search_btn').click(function() {

		if ($("#searchword").val() == 'Ihr Suchbegriff') {
			$('#searchword').val('');
		}
		
		if($("#searchword").val() == ''){

			var projects = $('#select_1 :selected').val();
			var institutions = $('#select_2 :selected').val();
			var viewtype = $('#viewtype').val();
			
			 $.post('index.php?eID=navfilter&projects='+ projects + '&institutions=' + institutions + '&viewtype=' + viewtype, function(data) {
				 	$('input#chash').val(data);
				 	document.forms["navigator-searchform"].submit();
				  });
		}
		else {
			document.forms["navigator-searchform"].submit();
		}
	});
	
	$('.search_btn_1').click(function() {

		if ($("#searchword_1").val() == 'Ihr Suchbegriff') {
			$('#searchword_1').val('');
		}
		
		if($("#searchword_1").val() == ''){
			var projects = $('#select_3 :selected').val();
			var institutions = $('#select_4 :selected').val();
			var viewtype = $('#viewtype_1').val();
			
			 $.post('index.php?eID=navfilter&projects='+ projects + '&institutions=' + institutions + '&viewtype=' + viewtype, function(data) {
				 	$('input#chash_1').val(data);
				 	document.forms["navigator-searchform_1"].submit();
				  });
		}
		else {
			document.forms["navigator-searchform_1"].submit();
		}
	});
	
});


jQuery(document).ready(function() {
	$('.science-navigator .middle-box .header').show();
});

/*
 * COMPETENCE TEASER
 */
jQuery(document).ready(function() {	
	//competence-teaser begin 
	$(".competence .icons a").bind ("mouseover click",
		function(){
			var currentID = $(this).attr('id');
			var parentClass = 'elements';
			// currentID = "icon-1-1"
			var id = currentID.replace(/icon/,'comp');
			
			//show/hide element
			$('.competence .elements div.element').addClass('hidden');
			$('#'+id).removeClass('hidden');
			
			//set icon of each on inactive
			$('.competence .icon span.image-wrapper').each(function(){
				//active as hidden					
				if ($(this).hasClass('active') && !$(this).hasClass('hidden')){
					$(this).addClass('hidden');
				}
				//inactive as nohidden
				if (!$(this).hasClass('active') && $(this).hasClass('hidden')){
					$(this).removeClass('hidden');
				}
										
			});
						
			$('#'+currentID+' span.image-wrapper').each(function(){										
				if ($(this).hasClass('active')){
					$(this).removeClass('hidden');
				}
				if (!$(this).hasClass('active') && !$(this).hasClass('hidden')){
					$(this).addClass('hidden');
				}
			});
			return false;
		}
	);
		//competence-teaser end						
});


/*
 * TOGGLE FAQ TABS
 */
jQuery(document).ready(function() {
/*	jQuery('.faq-foldable').find('.content').hide();
	jQuery('.faq-foldable').find('.header').click(function(e){
		toogleFAQTab(this);													
	});
*/
});

function toogleFAQTab(element) {

	var tabdiv = jQuery(element).parent();
	
	if(jQuery(tabdiv).hasClass('tab-active')) {
		jQuery(tabdiv).find(".content").slideUp();
		jQuery(tabdiv).removeClass('tab-active');
	} else {
		jQuery(tabdiv).parent().find(".content").parent().removeClass('tab-active');
		jQuery(tabdiv).parent().find(".content").slideUp();
		
		jQuery(tabdiv).find(".content").slideDown();
		jQuery(tabdiv).addClass('tab-active');
	}
}


/*
 * GALLERY FUNCTIONS
 */
function activate(num) {
	$(".image-main").hide();
	$("#image-main-" + num).fadeIn("fast");
	$(".image-container-active").removeClass("image-container-active");
	$("#image-preview-" + num).addClass("image-container-active");
	if($("#image-main-" + num).attr("alt")) {
		$(".gallery-outer .image-caption").show();
		$(".gallery-outer .image-caption").text($("#image-main-" + num).attr("alt"));
	} else {
		$(".gallery-outer .image-caption").hide();
	}
	
	if($(".gallery-outer .button-prev").css("display") == "none") {
		$(".gallery-outer .filmstrip-outer").css({"height" : "47"});
		$(".gallery-outer .filmstrip-inner").css({"margin-left" : "0", "overflow" : "hidden"});
		$(".gallery-outer .button-prev").show();
	}
	
	if($(".gallery-outer .button-next").css('display') == 'none') {
		$(".gallery-outer .button-next").show();
	}

	return false;
}

var canNavigate = true;

function navigateBack() {
	if(canNavigate){
		canNavigate = false;
		
		var value = 79;
		var left = $(".filmstrip-inner ul").position().left;
		if (left + value > 0) {
			value = 0 - left;
		}
		return navigate(value);
	}
}

function navigateForward() {
	if(canNavigate){
		canNavigate = false;
		
		var value = -79;
		var left = $(".filmstrip-inner ul").position().left;
		var width = $(".filmstrip-inner ul").width();
		
		//console.debug("left" + left);
		//console.debug("width" + width);
		
		if (left + value + width < 386) {
			value = 386 - (width + left - 11);
			//console.debug(value);
		}
		
		return navigate(value);
	}
}

function navigate(value) {
	$(".filmstrip-inner ul").animate({
		"left": "+=" + value + "px"
		}, "slow", function(){canNavigate = true;});
	return false;
}

/**
 * 
 * @return
 */
function jumpToURL() {
  dropdown = document.getElementById('select_competence');  
  for(i=0; i < dropdown.length; i++)
  {
  	if(document.getElementById('select_competence').options[i].selected == true && document.getElementById('select_competence').options[i].value!='')
  	{
  		window.location.href = document.getElementById('select_competence').options[i].value;
  	}
  }    
}

/* 
 * Datepicker 
 */
$(document).ready(function(){		
	$("input.date").datepicker({
		showAnim: 'fold',
		inputClick: true,
		dateFormat: 'dd.mm.y', 
		firstDay: 1,
		dayNamesMin:  ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
		monthNames: ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','December']
	});
});


/*
 * Orte des Wissens
 */
var defaultMap = "";
var defaultMap_marginal = "";	

$(document).ready(function() {  
	initDefaultPlace();			// if JS activ
	
	$('.placesOfknowledge map').find('area').mouseover(function(){
		highlightPlace(this);														 
	});
	
	$('#content-section .placesOfknowledge map').find('area').mouseout(function(){
		showDefaultPlace();																		 
	});
	
	$('#marginal-section .placesOfknowledge map').find('area').mouseout(function(){
		showDefaultPlaceMarginal();																		 
	});
});

function highlightPlace(element){
	var place 		= element.title;
	var destination = $(element).parent().parent();
	
	destination.find('img').attr('src', place);
}
function initDefaultPlace(){
	showDefaultPlaceMarginal();
	showDefaultPlace();
}
function showDefaultPlace(){
	$('#content-section .placesOfknowledge img').attr('src', defaultMap);
}function showDefaultPlaceMarginal(){
	$('#marginal-section .placesOfknowledge img').attr('src', defaultMap_marginal);
}

	





/*
 * HISTORY GALLERY
 */

jQuery(document).ready(function() {
	$('.history-carousel').find('.no-script').removeClass('no-script');
	
	//$('.history-carousel .content-box li.element:not(li:first)').addClass('hidden');
	//$('.history-carousel .carousel-box li:first a.element').addClass('active');
	
	$('.carousel-box ul li a.element').mouseenter(
		function(){
			posPicture(this.id);
			showToolTip(this.id);
		}
	);
	

	
	$('.history-carousel .tooltip-box, .history-carousel .picture-box').mouseout(
		function(){
			var box = $(this).parent();
			hideToolTipAndPicture(box);
		}
	);
	
	$('.history-carousel .content-box').mouseenter(
		function(){
			var box = $(this).parent();
			hideToolTipAndPicture(box);
		}
	);
	
	

	$('.picture-box').click(function(){
		var id = $(this).find('img').attr('id');
		id = id.substring(0,id.lastIndexOf('_'));
		var contentID = id+'_content';
		
		$('#' + id).parent().parent().find('a').removeClass('active');
		$('#' + id).addClass('active');
		$('#' + contentID).parent().find('li.element').addClass('hidden')
		$('#' + contentID).removeClass('hidden');
	});
});

function hideToolTipAndPicture(container){
	$(container).find('.tooltip-box').hide();
	$(container).find('.picture-box').hide();
	$(container).find('.picture-box img').remove();
}

function showToolTip(id){
	var element = $("#"+id);
	var offsetX	= 34 - 17;	// padding (10) + prevWidth (20) + margin (8) - versatz (17)
	var offsetY = 5;
	
	var elementWidth = element.width(); 
	var positionLeft = element.position().left;
	var ul			 = element.parent().parent();
	var divpos 		 = ul.position().left;
	var clipWidth 	 = ul.parent().width();

	var carouselId = id.substring(0,id.indexOf('_')); 
	var tooltip    = $('#' + carouselId + '_tooltip');
	
	var coord = positionLeft+(elementWidth/2)+divpos;
	var half  = (clipWidth/2);
	
	var carouselHeight = tooltip.parent().find('.carousel-box').innerHeight(); 
	var contentHeight  = tooltip.parent().find('.content-box').innerHeight(); 
	
	var posTop  = carouselHeight + contentHeight + offsetY;
	var posLeft = positionLeft + divpos + offsetX;
	
	tooltip.removeAttr('style');
	
	if (coord < half){
		tooltip.removeClass("tooltip-box-right");		
	} else {
		tooltip.addClass("tooltip-box-right");
		posLeft = divpos + positionLeft - 272 + elementWidth + elementWidth;	// 272 = tooltip width	
	}
	
	if (jQuery.browser.msie && jQuery.browser.version <= 6){
//		posLeft -= 17;
	}
	
	tooltip.find('.content').html(element.find(".caption").html());
	tooltip.attr('style', 'left:'+posLeft+'px; top:'+posTop+'px;');

	tooltip.show();
}

function posPicture (id){
	var element = $("#"+id);
	var offsetX	= 33;
	var offsetY = 13;
	
	var carouselId	  = id.substring(0,id.indexOf('_')); 
	var picture 	  = $('#' + carouselId + '_picture');

	var positionLeft  = element.position().left;
	var divpos 		  = element.parent().parent().position().left;
	var contentHeight = picture.parent().find('.content-box').innerHeight(); 
	
	var posTop  = contentHeight + offsetY;
	var posLeft = positionLeft + divpos + offsetX;
	
	if (jQuery.browser.msie && jQuery.browser.version <= 6){
//		posLeft -= 20;
	}
	
	var srcImg = element.find('.hover');
	var html = '<img src="'+ srcImg.attr('src') +'" id="'+ id +'_trans" height="60" width="67" />';
	picture.find('.picture').html(html);
	picture.removeAttr('style');
	picture.attr('style', 'left:'+posLeft+'px; top:'+posTop+'px;');
	
	picture.show();
}


/* *************************************** */
/*  Einrichtungsdetail - toggleListTab	   */
/* *************************************** */
jQuery(document).ready(function() {
	$('.toggleListTab .tab').removeClass('tab-open').find('.content').hide();
	$('.toggleListTab .tab .facility').parent().parent().parent().addClass('tab-open').find('.content').show();

	$('.toggleListTab .tab .header').click(function(){
		var tab = $(this).parent();
		
		if(tab.hasClass('tab-open')){
			tab.removeClass('tab-open').find('.content').slideUp();
		} else {
			tab.addClass('tab-open').find('.content').slideDown();
		}
	});
});


/* *************************************** */
/*  Organigramm - toggleListTab	   */
/* *************************************** */
function toogleListTab(element) {
	var header = jQuery(element).parent();
	
	if(jQuery(header).hasClass('header-open')) {
		jQuery(header).parent().find("> .content").slideUp();
		jQuery(header).removeClass('header-open');
	} else {
		if (header.hasClass('.first-level')){
			var ul = header.parent().parent();
			ul.find('.first-level').removeClass('header-open');
			ul.find('.first-level').parent().find('> .content').slideUp();
		}
		
		jQuery(header).parent().find("> .content").slideDown();
		jQuery(header).addClass('header-open');
	}
}