// JS for otten, using jQuery

var ieversion = 0;
var colorboxOpen = false;
var oldHeight = new Array();

$(document).ready(function(){
	// Fix for IE9: getElementsByTagName
	if ( jQuery.browser.msie && jQuery.browser.version >= 9 ){
        jQuery.support.noCloneEvent = true
    }
	
	// makes whole div of meta links clickable
	$(".meta_link").css("cursor","pointer").click(function(){
		window.location = $("a", this).attr("href");
	});
	// pull down the contact information
	$("#contact-handle").click(function(){
		var topvalue=-170;
		var contactfile = _sitepath+"daten/"+_project+"/Templates/Pfeil_dropdown.png";
		if($("#contact").css("marginTop")!="0px"){
			topvalue=0;
			contactfile = _sitepath+"daten/"+_project+"/Templates/pfeil_dropdown_up.png";
		}
		$("#contact-handle a img:first-child").attr("src",contactfile);
		$("#contact").animate({
			marginTop: topvalue+"px"
		}, 500);
		return false;
	});
	
	// initializing the expander elements#
	// timeout makes the initializing wait until all elements are created so that always the correct height is used
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion>=8)window.setTimeout("initializeExpander()",1000);
	}else{
		window.setTimeout("initializeExpander()",1000);
	}
	
	// makes all lightbox links to colorboxes
	initializeColorBox();
});

function initializeColorBox(){
	//$("a[rel='lightbox']").colorbox();
	$("a[rel='lightbox']").each(function(){
		if($(this).attr("id")=="panorama"){
			$(this).colorbox({
				iframe:true, 
				innerWidth:920, 
				innerHeight:690
			});
		}else{
			$(this).colorbox({
				onOpen:function(){colorboxOpen=true;},
				onClosed:function(){colorboxOpen=false;}
			});
		}
	});
}

function initializeExpander(){
	var expandCounter = 0;
	$(".double-element-expander").each(function(){
		$(this).wrapInner('<div class="flexpart" />')
		var old_height = 0;
		
		// putting a following news-slider inside the expander
		$(this).attr("id","expander_"+expandCounter);
		var nextElement = $(this).next();
		if(nextElement.attr("class")=="news_slider"){
			var nextElementId = "#"+nextElement.attr("id");
			if($(nextElementId).appendTo("#expander_"+expandCounter)){
				old_height = $("#expander_"+expandCounter).height();
			}
		}
		if(old_height==0)old_height = $("#expander_"+expandCounter).height();
		var old_height = $("#expander_"+expandCounter).height() + 50;
		
		oldHeight[expandCounter]=old_height;
		
		//$(this).attr("oldHeight",old_height+"px");
		$(this).animate({
			height: "165px"
		}, 500);
		
		// shortening the paragraph
		
		$($(this).children(".flexpart")).expander({
		    slicePoint:       300,  // default is 100
		    expandText:         ' weiterlesen ><br><br><br>', // default is 'read more...'
		    userCollapseText: ' < minimieren',  // default is '[collapse expanded text]'
		    expandSpeed: 10,
		    beforeExpand: function($thisEl) {
				//var slide_height = $thisEl.parent().attr("oldHeight");
				var getHeight = $thisEl.parent().attr("id").split("_");
				var slide_height = oldHeight[getHeight[1]];
				
				$thisEl.parent().delay(20).animate({
					height: slide_height
				}, 500);
				
				$thisEl.parent().children(".doppel_ueberschrift_inaktiv").attr("class","doppel_ueberschrift_aktiv");
			},
			afterExpand: function($thisEl) {
				//$thisEl.parent().removeAttr("style");
			},
			onCollapse: function($thisEl, byUser) {
				var slide_height = "165px";
					
				$thisEl.parent().animate({
					height: slide_height
				}, 500);
				
				$thisEl.parent().children(".doppel_ueberschrift_aktiv").attr("class","doppel_ueberschrift_inaktiv");
			}
		  });
		
		expandCounter++;
	});
	
}
