/* - No bfcache - */
jQuery(window).bind("unload", function(){});

jQuery(document).ready(function() {

	$('#search_submit').click(function() {
		
//		var params = {
//				target = '#searchword',
//				projects = '#select_1',
//				institutions = '#select_2'
//		}
//		getcHash(params,null);
//		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 ("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','Dezember']
	});
});
*/

/*
 * 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
 */
function initHistoryCarousel(carousel){
	carousel.nextCount = 0;
	carousel.prevCount = 0;
	
	$('.jcarousel-next').click(function(){
		carousel.nextCount++;
		return false;
	});
	$('.jcarousel-prev').click(function(){
		carousel.prevCount++;
		return false;
	});
	$('.jcarousel-next').mousehold(450,function(i){
		if(i > 1) carousel.next();
		if(carousel.buttonNext[0].disabled) carousel.buttonNext.mouseup();
	});
	$('.jcarousel-prev').mousehold(450,function(i){
		if(i > 1) carousel.prev();
		if(carousel.buttonPrev[0].disabled) carousel.buttonPrev.mouseup();
	});
	
	var isIE8 = $.browser.msie && $.browser.version == 8;
	var isIE7 = $.browser.msie && $.browser.version == 7;
	if (isIE8 || isIE7){
		$('.jcarousel-next').dblclick(function(){
			carousel.nextCount++;
			return false;
		});
		$('.jcarousel-prev').dblclick(function(){
			carousel.prevCount++;
			return false;
		});
	}
}
function onBeforeHistoryCarousel(carousel, item, idx, state){
	if(state == 'next' && carousel.nextCount > 0){
		carousel.nextCount--;
		if(idx == carousel.size())carousel.nextCount = 0;
	}else if(state == 'prev' && carousel.prevCount > 0){
		carousel.prevCount--;
		if(idx == 0)carousel.prevCount = 0;
	}
}
function onAfterHistoryCarousel(carousel, item, idx, state){
	if(state == 'next' && carousel.nextCount > 0 ){
		carousel.next();
	}else if(state == 'prev' && carousel.prevCount > 0){
		carousel.prev();
	}
	
	if(carousel.buttonPrev[0].disabled) carousel.buttonPrev.mouseup();
	if(carousel.buttonNext[0].disabled) carousel.buttonNext.mouseup();
}
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');
	}
}


/*
 * LOADER + get cHash for navigator.
 */
function initSL(text,imgSrc,slClass){
	var init = new Array();
	init['text'] = text;
	init['imgSrc'] = imgSrc;

	$('body').append('<div id="SL-image-Preloader" style="display: none;"><object type="application/x-shockwave-flash" data="'+init['imgSrc']+'" width="31" height="31"><param name="movie" value="'+init['imgSrc']+'" /></object></div>');
	
	var input = $('.' + slClass);
	
	input.click(function(){
		createLoader(init);
		
		if ($(this).attr('id') == 'search_submit') {
			// see above
			var params = {
					target : '#searchword',
					projects : '#select_1',
					institutions : '#select_2',
					viewtype : '',
					callback : submitSearchForm,
					cbParam : this
			}
			getcHash(params);
			
		} else if($(this).hasClass('search_btn')){
			var params = {
					target : '#searchword',
					projects : '#select_1',
					institutions : '#select_2',
					viewtype : '#viewtype',
					callback : submitSearchForm,
					cbParam : this
			}
			getcHash(params);
			
		} else if($(this).attr('search_btn_1')){
			var params = {
					target : '#searchword_1',
					projects : '#select_3',
					institutions : '#select_4',
					viewtype : '#viewtype_1',
					callback : submitSearchForm,
					cbParam : this
			}
			getcHash(params);
			
		} else {		
			submitSearchForm(this);
		}
		return false;
	});
}

function submitSearchForm(ele){
	var ele = $(ele);
	var form = ele.parentsUntil('form').last().parent();
	var url = ele.attr("href");
	if(url == undefined){
		url = '';
	}
	var navig_agt = navigator.userAgent.toLowerCase();
	
	if (url.length) {
		RedirectUrl = 'http://'+window.location.host+'/'+url;
		
		if (navig_agt.indexOf("msie")) {
			setTimeout( "window.location = RedirectUrl",300);
		} else {
			window.location = RedirectUrl;
		}
		
	} else {
		if (form.length == 1) {
			form = form[0];
			form.submit();
		}
	}
}

function createLoader(init){
	var overlay = $('<div id="SL_Overlay"></div>');
	var loader = $('<div id="SL_Window"></div>');

	var textbox = $('<div class="textbox"></div>');
	textbox.html(init['text']);
	loader.append(textbox);

	var loaderImage = $('<div class="loaderImage"></div>');
	loader.append(loaderImage);
	//var image = $('#SL-image-Preloader').find('img').first();
	var image = $('#SL-image-Preloader').find('object').first();
	
	//loaderImage.append("<img src='"+init['imgSrc']+"' width='31' height='31' />");
	loaderImage.append(image);
	
	$('body').append(overlay);
	$('body').append(loader);
	
	sl_calculatePosition(loader);
	
	$(window).resize(function() {
		sl_calculatePosition(loader);
	});	

}

function sl_calculatePosition(ele){
	var loader = $(ele);
	var pageSize = sl_getPageSize();
	
	var leftPos = (pageSize[0] / 2) - (loader.width() / 2);
	leftPos = leftPos + 'px';
	
	var topPos = (pageSize[1] / 2) - (loader.height() / 2);
	topPos = topPos + 'px';
	
	loader.css({
		left: leftPos,
		top: topPos
	});
	
}

function sl_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

/**
 * Do cHash eID request for navigator tool.
 */
function getcHash(params){
	var target = $(params.target);
	if (target.val() == 'Ihr Suchbegriff') {
		target.val('');
	}
	
	var callback = params.callback;
	
	if(target.val() == ''){

		var projects = $(params.projects + ' :selected').val();
		var institutions = $(params.institutions + ' :selected').val();
		var viewtype = null;
		if(params.viewtype == ''){
			viewtype = '';
			
			if ( $(".projects.project-list").length > 0 ) {
				viewtype = 'projects';
			}
			if ( $(".institution.institution-list").length > 0 ) {
				viewtype = 'institutions';
			}
		} else {
			viewtype = $(params.viewtype).val();
		}
		
		 $.post('index.php?eID=navfilter&projects='+ projects + '&institutions=' + institutions + '&viewtype=' + viewtype, function(data) {
			 $('input#chash').val(data);
			 callback(params.cbParam);
		 });
	}
	else {
		callback(params.cbParam);
	}
}

/**
*
* Modified by Andrew Spode Miller (justfdi.com, @spode) on 9th December 2009 to include a "Delay" parameter.
*
*/

/**
* jQuery mousehold plugin - fires an event while a mouse click is held down, at a variable interval.
*
* Additionally, the function is passed a count of times the event has been fired in this session of mousehold.
*
* @author Remy Sharp (leftlogic.com)
* @date 2006-12-15
* @example $("img").mousehold(200, function(i){  }, 1000)
* @desc Repeats firing the passed function while the mouse click is held down
*
* @name mousehold
* @type jQuery
* @param Number timeout The frequency to repeat the event in milliseconds
* @param Function fn A function to execute
* @param Number delay Milliseconds of delay before it should start firing
* @cat Plugin
*/

jQuery.fn.mousehold = function(timeout, f, delay){
	//Allows us to pass only one parameter
	if	(timeout && typeof timeout == 'function'){
		f = timeout;
		timeout = 100;
	}

	//If there is no delay, set a default
	if	(!delay){
		var delay = 500;
	}

	//Only continue if it's been passed a function, this is infact a function
	if (f && typeof f == 'function'){
		var timer = 0;
		var delaytimer = 0;
		var fireStep = 0;

		return this.each(function(){
			jQuery(this).mousedown(function(){
				fireStep = 1;
				var ctr = 0;
				var t = this;

				delaytimer = setTimeout(function (){
					timer = setInterval(function(){
						ctr++;
						f.call(t, ctr);
						fireStep = 2;
					}, timeout);
				}, delay);
			});

			//This makes sure that as we mouse out, or mouse up - all the timers are stopped
			clearMousehold = function(){
				clearInterval(timer);
				clearTimeout(delaytimer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
	
			jQuery(this).mouseout(clearMousehold);
			jQuery(this).mouseup(clearMousehold);
		});
	}
}
/* *************************************** */
/*  Lightbox simple
/* *************************************** */
jQuery(document).ready(function() {
	$('a.lightbox').click(function(event){ 
		baueLightbox();
		openLightbox(this);
		return false;
	});
	function baueLightbox(){
		var lightbox = '<div id="lightbox-overlay"></div><div id="lightbox-box"><div id="lightbox-close"></div><div id="lightbox-content"></div></div>';
		$('body').append(lightbox);
		
		var overlay = $('#lightbox-overlay');
		var box = $('#lightbox-box');
		var content = $('#lightbox-content');
		
		var width = 520;
		var height = 500;
		
		overlay.css({
			'height' 	: $(document).height(),
			'opacity'   : 0.7
		});
		$('#lightbox-close').click(function(){
			box.hide();
			overlay.hide();
		});
		overlay.click(function(){
			box.hide();
			overlay.hide();
		});
	}
	function openLightbox(target){
		var id = target.name;
		var content = $('#'+id).html();
		$('#lightbox-content').html(content);
		$('#lightbox-overlay').show();
		var box = $('#lightbox-box');
		var left = ($(window).width() - box.width()) / 2;
		var top = $(document).scrollTop() + ($(window).height() - box.height() ) / 2;
		box.show();
		box.css({left: left, top: top});
	};
});
