
$(document).ready(function() {
  $('#pane2').jScrollPane({showArrows:true});
	tabNavi.init();
	toggle.init();	
	contentHeaderNavi.init();
	grayOut.init();
	defaultValueBehavior.init();
	submitLink.init();
	getUrl.init();
	picTextTeaserAlign.init();
	alternateTable.init();
	internalLinks();
	formSubmitByEnterkey.init();
});

var alternateTable = {
	init: function () {
		$("table[border]").each(function(i){
			$(this).attr("cellpadding", "0");		
			$(this).attr("cellspacing", "0");		
			$(this).addClass('alternate');															 
			$(this).find("tbody tr:even").addClass('even');
			$(this).find("tr").find("td:last").css({'border-right' : '0'});	
		});	
		
		$("table.downLoad").each(function(i){
			$(this).attr("cellpadding", "0");		
			$(this).attr("cellspacing", "0");		
		});			
	}
}

var picTextTeaserAlign = {
	
	init: function () {
		$(".picTextTeasers").each(function(i){
			var evenTeaser;
			var evenHeight;
			$(this).find(".picTextItem").each(function(j){																				 
				if( (j%2) == 0 ) {	
					evenTeaser = this;
					evenHeight = $(this).height();				
				} else {
					
					var oddHeight = $(this).height();
					var rowHeight = ( oddHeight > evenHeight ) ? oddHeight : evenHeight;
					$(this).height( rowHeight );
					$(evenTeaser).height( rowHeight );

					if($.browser.msie){
						$(this).wrap('<div></div>');
						$(evenTeaser).prependTo( $(this).parent() );
						$(this).parent().width( '100%' );
						$(this).parent().css({
							'border-bottom': 'solid 1px #e0e0e0',
							'margin-bottom:': '-1px'
						});
					}

				}	
				
				$(this).css({
					'border-right': 'solid 1px #e0e0e0',
					'margin-right': '-1px'
				})	
				
			});			
		});
	}
}

var renderChart = {

	init: function () {
		var counter = $("#PageImpressions").find(".singleChart").length;
		
		var werte = new Array();
		
		var maxVal = 0;
		var breite = 162; // Horizontale Ausdehnung des Charts in px
		var cent = breite / 100;
		
		for ( i=0; i < counter; i++){

			p = i + 1;
			werte[i] = $("#PageImpressions").find("#Chart" + p).find("dd").text().replace(/\,/, ".");
			werte[i] = parseFloat(werte[i]);
			
			if(werte[i] > maxVal) {
					maxVal = werte[i];
			}
		}
		for ( i=0; i < counter; i++){
			p = i + 1;
			werte[i] = Math.round((werte[i] / maxVal) * 100 * cent);
			$("#PageImpressions").find("#Chart" + p).find(".bar").attr("style","width:" + werte[i] + "px");
		}
	}
}


function fillDisplay( obj, dir ) {
	var myThumbs = $(obj).parents('#ModularLayer').find('.jCarouselLite div');
	
	if ( dir < 0 ) {
		var myActiveThumb = $(obj).parents('#ModularLayer').find('.jCarouselLite div.active:last'); 
	} else {
		var myActiveThumb = $(obj).parents('#ModularLayer').find('.jCarouselLite div.active:first'); 
	}
	var myNextThumbIndex = myThumbs.index( myActiveThumb ) + dir;
	var myNextActiveThumb = myThumbs.get(myNextThumbIndex);		
	
	myClickEvent = $(myNextActiveThumb).find('img').get(0).onclick;
	myActiveThumb = $(obj).parents('#ModularLayer').find('.jCarouselLite div.active'); 
	myActiveThumb.removeClass('active');
	$(myNextActiveThumb).addClass('active');
	myThumbs.each(function(i){																					
		if( $(this).html() == $(myNextActiveThumb).html() ) {
			$(this).addClass('active');
			$('#ModularLayer #ControlText').html( $(this).next().html() );
		}
	});	
	if( typeof(myClickEvent) == 'function') {
		myClickEvent();
	}
	return false;
}


function carouselInit() {
  $("#ModularLayer .thmbDisplay .jCarouselLite, #ModularLayerSmall .thmbDisplay .jCarouselLite").jCarouselLite({
      btnNext: ".thmbDisplay .next",
      btnPrev: ".thmbDisplay .prev",
			visible: 4
  });
	$(".jCarouselLite div").bind("click", function(){
		//alert("Hallo");
		$(this).parents(".jCarouselLite").find("div").removeClass("active");																					 
		$(this).addClass("active");
		$("#ModularLayer #ControlText").html( $(this).next().html() );
	});
	var myActiveText;
	if ( myActiveText = $("#ModularLayer .jCarouselLite div.active").next().html() ) {
		$("#ModularLayer #ControlText").html( myActiveText );
	}
	//relatedCarouselInit();
}

/* fuer Seite "tc82110_layer_additional.html"

function relatedCarouselInit() {
  $(".related .jCarouselLite").jCarouselLite({
      btnNext: ".related .next",
      btnPrev: ".related .prev",
			visible: 1
  });	
}
*/

var submitLink = {
	init: function() {
		$('a.submit').bind("click", function(){															 
				$(this).parents('form')[0].submit();
				return false;
		});
	}
}


var grayOut = {
	init: function() {

		$('#GrayOut').height($(window).height());
		$('#GrayOut').width($(window).width());

		$(window).resize(function(){			
			$('#GrayOut').height($(window).height());
			$('#GrayOut').width($(window).width());	
		});
	}
}


/* Dynamisierung Reiternavigation  */

var tabNavi = {
	
	switchTab: function( myTab ) {
		
		var tabNavConts = $(myTab).parents('.tabNav').parent().find('.tabNavContent');
		var tabNavItems = $(myTab).parents('ul').find('li');
		var idx = tabNavItems.index(myTab);
		tabNavItems.removeClass("active");
		$(myTab).addClass("active");
		tabNavConts.addClass("passive");
		tabNavConts.eq(idx).removeClass("passive");
		if ($(myTab).hasClass("bcHash")) {
			$('#breadCrumb li:last').text($(myTab).text());			
		}
		if( !$(myTab).parents('.related').length ) {
			$('.related .tabNav li').removeClass("active");
			$('.related .tabNav li:first').addClass("active");
			$('.related .tabNavContent').eq(0).removeClass("passive");
		}
	},

	init: function() {
		var myTabs = $('.tabNav li');
		var preset = false;
		
		if(myTabs.parent().get(0)) {
			myTabs.height( myTabs.parent().get(0).offsetHeight );	
			$('.tabNav li span:not(.amount)').height(  myTabs.parent().get(0).offsetHeight );
			if ($('.tabNav li:first').hasClass("bcHash")) {
				enhanceBreadcrumb($('.tabNav li:first').text());
			}
			myTabs.click(function () {	
					tabNavi.switchTab( $(this) );
					
    	});	
			myTabs.each(function(i){
				if( $(this).hasClass('active') && !$(this).parents('.related').length  ) {									 
					preset = true;
				}
			});
			
			if( !preset ) {

				if( window.location.hash ) {
					
					var myOpenTab = window.location.hash;
					myOpenTab = myOpenTab.substring( 1 );
					if( myOpenTab ) {	
						$('.tabNav a').each(function(i){	
																				 
							if( this.name == myOpenTab ) {
								tabNavi.switchTab( $(this).parents('li')[0] );	
							}						
						});															
					}	
	
				} else {
					var myOpenTab =  $('.tabNav li:first-child');
					
					myOpenTab.each(function(i){
						var tabNavConts = myOpenTab.eq(i).parents('.tabNav').parent().find('.tabNavContent');
						myOpenTab.siblings().removeClass("active");																												 
						myOpenTab.addClass("active");
						tabNavConts.addClass("passive");
						tabNavConts.eq(0).removeClass("passive");	
					
							
					});					
					
				}
			
			}
			
		}
	}
}

function internalLinks () {	
	$("a:not(a[name])").bind("click", function(){	
		if( $(this).attr('href').indexOf( $('meta[name=relpath]').get(0).content ) > -1 ) {	
			var hash =  this.href.substring( this.href.indexOf('#') );
			toggle.openHash( hash );
		}
	});
}

var contentHeaderNavi = {
	init: function() {
		$('.stage .navi ul').find("a").click(function () {	
				var stageConts = 	$(this).parents('div.stage').find('div.content');
				var stageText =  $(this).parents('div').find('p.StageSubline');
				var stageNavLinks = $(this).parents('ul').find('a');
				var idx = stageNavLinks.index(this);
				stageNavLinks.removeClass("active");
				$(this).addClass("active");
				stageConts.hide();
				stageText.hide();
				stageConts.each(function(i){
					if(this.getElementsByTagName('OBJECT').length) {
						try {
							if ( thisMovie( this.getElementsByTagName('OBJECT')[0].id ).stopVideo != undefined ) {
								thisMovie( this.getElementsByTagName('OBJECT')[0].id ).stopVideo()
							}
						} catch(e){}
					}
				});
				stageConts.eq(idx).show();
				stageText.eq(idx).show();
				return false;
    });																									 
	}
}


/* Austauschen des Inhalts eines DIVs */
var contentUpdater = {
	
	get: function(selector, pageUrl) {
		$.ajax ({
			url: pageUrl,
			cache: false,
			success: function(html){
				$(selector).html(html);
			}
		});
	},
	post: function(selector, pageUrl) {
		var str = $(selector + " form").serialize();
		$.ajax ({
			url: pageUrl,
			cache: false,
			type: "POST",
			data: str,
			success: function(html){
				$(selector).html(html);
			}
		});
	},
	
	postPw: function(selector, pageUrl) {		
		var str = $(selector + " form").serialize();
		$.ajax ({
			url: pageUrl,
			cache: false,
			type: "POST",
			data: str,
			success: function(html){
				$(selector).html(html);
				$(selector).html(html);
			}						
		
		});
	},
	
	
	
	
	
	copy: function(sourceSelector, destinationSelector) {
		$(destinationSelector).html($(sourceSelector).html());
	}
	
}


var getUrl = {
	init: function() {
		myUrl = window.location.href;
		
		$('.tabNav').find('a').click(function(){
			getUrl.cutHash();
			myUrl = window.location.href + "#" + $(this).attr('name');
			//console.log(myUrl);
		});
		
		
		$('.toggleContainer').find('h4').find('a').click(function(){
			getUrl.cutHash();
			myUrl = window.location.href + "#" + $(this).attr('name');
			//console.log(myUrl);
		});
	},
	
	attach: function() {
		$('.layerContent').find('input.link').val(myUrl);
	},
	
	cutHash: function(){
		hash = myUrl.search('#');
		myUrl = myUrl.substr(0,hash)
	}
}


var toggle = {
	speed: "normal", 
	openHash: function( hash ) {
		
		var myOpenSection = hash || window.location.hash;
		/*
		$(".toggleContainer").each(function(i){
				$(this).find(".toggleContent").hide();
				$(this).find(".toggleContent").parents("li").eq(0).removeClass("active");			
		});	
		*/
		if( myOpenSection ) {
			myOpenSection = myOpenSection.substring( 1 );			
			$(".toggleContainer").find("a").each(function(i){																							
				if ( this.name == myOpenSection ) {	
					if($(this).parents(".toggleContent").length){
						$(this).parents(".toggleContent").eq(0).show();
						$(this).parents("li").filter(".toggleContainer > ul > li").addClass("active");
					} else {
						$(this).parents("li").eq(0).find(".toggleContent").show();
						$(this).parents("li").eq(0).addClass("active");
					}
					if( myOpenSection.length ) {
						if( openParentSec = $(this).parents('.tabNavContent')[0] ) {				
							allTabSibs = $(openParentSec).parent().find('.tabNavContent');
							var openIdx =  allTabSibs.index( openParentSec );
							tabNavi.switchTab( $(openParentSec).parent().find('.tabNav').find('li')[openIdx] );
						}	else {
							$('.tabNav a').each(function(i){																			 
								if( this.name == myOpenSection ) {
									tabNavi.switchTab( $(this).parents('li')[0] );	
								}						
							});																	
						}
						
					} else {
						tabNavi.switchTab(0);
					}
				}
			});
		} 
	
		$(".toggleContainer").each(function(i){
			var myFirstSection = $(this).find("li:first");																						
			if( $(this).find(".toggleContent:hidden").length == $(this).find(".toggleContent").length ) {
				myFirstSection.find(".toggleContent").show();
				myFirstSection.addClass("active");						
			} 
		});	
		
	},
	init: function() {
		
		$(".toggleContainer").find("a[href][name]").click(function () {		
      if ($(this).parent().parent().find(".toggleContent").is(":hidden")) {
        $(this).parent().parent().find(".toggleContent").slideDown(toggle.speed);
        $(this).parent().parent().addClass("active");
      } else {		
        $(this).parent().parent().find(".toggleContent").slideUp(toggle.speed);
        $(this).parent().parent().removeClass("active");
      }
			if( this.name == this.href.substring( this.href.indexOf('#')+1 ) ) {
				return false;
			}
    });
		toggle.openHash();
	}
}



/*

Toolboxen in der rechten Spalte.

*/
var toolbarBox = {
	
	/* Oeffnen + schliessen */
	toggle: function (id) {
		obj = $('#' + id);
		if(obj.find('.head').find('span').hasClass('close')) {
			obj.find('.body').css('display','none');
			obj.find('.head').find('span').removeAttr('class').attr("class","open");
			if (!obj.hasClass("blue")) {
				obj.find('.head').css({ borderBottom:"1px solid #e0e0e0" });
			}	
		}
		else {
			obj.find('.body').css('display','block');
			obj.find('.head').find('span').removeAttr('class').attr("class","close");
			obj.find('.head').css({ borderBottom:"0" });
		}
		
	}
}

var flyOut = {
	init: function () {
		$('.flyOutWrapper').each(function(i) {
			$(this).css('z-index', 50 - i );
			var myDisplay = $(this).find('input.readOnlyTxt')[0];
			var myCbs = $(this).find('input[type="checkbox"], input[type="radio"]');
			$(myCbs).each(function(i){		
				this.displ = myDisplay;
				this.sibs =  myCbs;
			});	
			
			if( myCbs.eq(0).is(':checked') ) {
				myCbs.eq(0).next().text( 'Alle abw\u00E4hlen' );
			}
			
			$(myCbs).bind("click", function(){

				if(this.type == 'radio') {
					
					this.displ.value = $(this).next().text();
					//this.click();
					$(this).parents('.flyOut').hide();
					this.displ.focus();

				} else {
																			
					if(this == this.sibs[0]){
						var chk = this.checked;
						this.sibs.each(function(i){												
							this.checked = chk;
						});	
						if(chk) {
							this.displ.value = 'Mehrfachauswahl'; //$(this).next().text();
							$(this.sibs[0]).next().text('Alle abw\u00E4hlen')
						} else {
							this.displ.value = 'Bitte ausw\u00E4hlen';
							$(this.sibs[0]).next().text('Alle w\u00E4hlen')
						}
					} else {
						
						var allChecked = true;
						var checkCount = this.sibs.length -1;
						var chosen = 0;
						var valueText = "";
						for( i=1; i<this.sibs.length; i++ ) {
							if( this.sibs[i].checked) {
								chosen = i;
								if( !valueText ) {
									valueText = $(this).next().text();
								}							
							} else {
								allChecked = false;
								checkCount--;
							}
						}
						this.sibs[0].checked = allChecked;
						if(allChecked) {
							$(this.sibs[0]).next().text('Alle abw\u00E4hlen')
						} else {
							$(this.sibs[0]).next().text('Alle w\u00E4hlen')
						}
					
						
						//var pluralTerm = $(this.sibs[0]).next().text();
						switch(checkCount) {
							case 0:
								this.displ.value = 'Bitte ausw\u00E4hlen';
								break;	
							case 1:
								this.displ.value = $(this.sibs[chosen]).next().text();
								break;	
							default:	
								
								this.displ.value = 'Mehrfachauswahl';//pluralTerm.replace(/(A|a)lle/, "mehrere");							
								//this.displ.value = valueText + ', \u2026';
								break;
						}
					}

				}
			});
		});
		
		allFlyOuts = $('.flyOut');
		$('.flyOutWrapper').bind("click", function(e){
			e = e || window.event;
			if( e.stopPropagation ) {
				e.stopPropagation();
			} else {
				e.cancelBubble = true;
			}
		});
		myToggles = $('.flyOutWrapper .flyOutToggle, .flyOutWrapper .readOnlyTxt');
		myToggles.bind("click", function(e){
			myFlyOut = 	$(this).siblings('.flyOut');
			myFlyOut.toggle();
			with($(this).parent().find('img')[0]) {
				if( myFlyOut.is(":hidden")	) {
					src = src.replace(/minus/g, "plus");
				} else {
					src = src.replace(/plus/g, "minus"); 
				}
			}			
			for( i=0; i<allFlyOuts.length; i++ ) {
				if(allFlyOuts[i] != myFlyOut[0]) {
					if( myFlyOut.is(":visible")	) {
						$(allFlyOuts[i]).hide();
						with( $(allFlyOuts[i]).parent().find('img')[0] ) {
							src = src.replace(/minus/g, "plus"); 
						}
					}					
				}
			}
			return false;
		});
		
		$(document).bind("click", function(){
			for( i=0; i<allFlyOuts.length; i++ ) {
				$(allFlyOuts[i]).hide();
				with( $(allFlyOuts[i]).parent().find('img')[0] ) {
					src = src.replace(/minus/g, "plus"); 
				}
			}					
		});
	}
}



var defaultValueBehavior = {
	
	clear: function ( obj ) {
		with (obj) {
			if( value == defaultValue ) {
				value = '';
			}
		}		
	},
	
	restore: function ( obj ) {
		with (obj) {
			if( value == '' ) {
				value = defaultValue;
			}
		}			
	},
	
	init: function () {
		$('#colTools input.txt, .headerSearch input.txt').bind("click", function(){
			defaultValueBehavior.clear( this );
		});
		$('#colTools input.txt, .headerSearch input.txt').bind("blur", function(){
			defaultValueBehavior.restore( this );
		});
	
	}
	
}



var modLayer = {
	local: function (sourceSelector, destinationSelector) {
		if (destinationSelector == null) {
			destinationSelector = '#ModularLayer';
		}
		contentUpdater.copy(sourceSelector, destinationSelector);
		return this.appear(destinationSelector);
	},		
	remote: function (pageUrl, selector) {
		if (selector == null) {
			selector = '#ModularLayer';
		}
		if (pageUrl != null) {
			contentUpdater.get(selector, pageUrl);
		}
		return this.appear(selector);
	},		
	appear: function (selector) {
		if (selector == null) {
			selector = '#ModularLayer';
		}
		$('#GrayOut').css('opacity', 0);
		$('#GrayOut').show();
		$('#GrayOut').fadeTo(1000, .42);
		$(selector).fadeIn(1000, carouselInit);
		var posTop = ($(document).scrollTop() > 156) ? ($(document).scrollTop() + 'px') : '157px';
		$(selector).css('top', posTop );
		return false;
	}, 
	disappear: function () {
		
		$('#GrayOut').fadeTo(1000, 0, this.remove);
		$('#ModularLayer, #ModularLayerSmall').fadeOut(1000);
		return false;
	},
	remove: function () {
		$('#GrayOut').hide();
	}
}

var filter = {
	reset: function (selector) {
		$(selector).find("ul.check input").attr("checked", "");
		this.apply(selector);
	},
	apply: function (selector) {
		var checked = $(selector).find("ul.check input:checked");		
		var numShown = 0;
		teasers = $(selector).find("div.teaser136").each(function(i) {
			var showIt = checked.size() == 0;
			if (!showIt) {
				for (i = 0; i < checked.size(); i++) {
					if ($(this).hasClass("flt_" + checked[i].value)) {				
						showIt = true;
						break;
					}
				}
			}
			if (showIt) {					
				if 	(++numShown % 2 == 0) {
					$(this).addClass("bteaser");
				} else {
					$(this).removeClass("bteaser");
				}
				$(this).show();								
			} else {			
				$(this).hide();
			}						
		});     		
	}
}

function resetExpertiseForm ( myResetButton ) {
	myForm = $(myResetButton).parents('form').eq(0);
	myForm.find('input[type=checkbox]').attr("checked", "");
	myForm.find('#KeyWordExpertiseFinder, #TextSearch').attr("value", "");	
	myForm.find('.flyOutWrapper input[type=text]:not(input[size=3])').attr("value", "Bitte w\u00E4hlen");
	$('.flyOut').each(function() {
		$(this).find('input[type="checkbox"]').eq(0).next().text( 'Alle w\u00E4hlen' );
		if( $(this).is(":visible")	) {
			$(this).hide();
			with( $(this).parent().find('img')[0] ) {
				src = src.replace(/minus/g, "plus"); 
			}
		}					
	});
	
	return false;
}
	

/* Flash-Funktionen */

function onLoaded () {
	//stopVideo();
	try {
		if ( document.getElementById("getPlayerImg") != undefined ) {
			document.getElementById("getPlayerImg").src = "images/content/videoplayer_previews/get_flash_player.gif";
		}
	} catch (e) {}
}

function thisMovie(movieId) {
	if ( navigator.appName.indexOf("Microsoft") != -1 ) {
		return window[movieId];
	} else {
		return document.getElementById( movieId );
	}
}

function stopVideo () {
	thisMovie("videoplayer").stopVideo();
}

function playVideo () {
	thisMovie("videoplayer").playVideo();
}

function openVideoInFullscreen ( p_path, p_preview ) {
	//alert( "openVideoInFullscreen\np_path: " + p_path + "\np_preview: " + p_preview );
	var url = "/ipd/ajaxsnippets/videofullscreen.cfml";
	url += "?video=" + escape(p_path);
	url += "&preview=" + escape(p_preview);
	url += "&nocache=" + Math.random();
	//alert(url);
	return modLayer.remote(url);
}

function openPlaylistInFullscreen (playlistUrl, videoId) {		
	//alert( "openPlaylistInFullscreen\nplaylistUrl: " + playlistUrl );
	var delimiter = playlistUrl.indexOf("?") > 0 ? "&" : "?";	
	playlistUrl += delimiter + "fullscreen=1";
	var url = "/ipd/ajaxsnippets/videofullscreen.cfml";	
	url += "?playlist=" + escape(playlistUrl);
	url += "&videoId=" + videoId;
	url += "&nocache=" + Math.random();
	// alert(url);
	return modLayer.remote(url);
}

function openImageInFullscreen ( p_path ) {
	var url = "/ipd/ajaxsnippets/imagefullscreen.cfml?" + Math.random();
	url += "&image=" + escape(p_path);	
	return modLayer.remote(url);
	//return modLayer.appear();
}

function browseTeaser( browseLink ) {
	var myTeaserItem = $(browseLink).parents('.teaserItem')[0];
	var myTeaser = $(browseLink).parents('.teaser');
	var myTeaserItemSibs = myTeaser.find('.teaserItem');
	var nextIndex = (myTeaserItemSibs.index(myTeaserItem) + 1) % myTeaserItemSibs.length;
	$(myTeaserItem).hide();
	$(myTeaserItemSibs[nextIndex]).show();
	return false;
}


function replaceHolder ( url ) {
	$('#PlaceHolder').load( url );
}


function getForm ( obj ) {
	return $(obj).parents('form')[0];
}

function createBookmark(title, url) {
	if (window.sidebar) {
		// Mozilla Firefox Bookmark
		//window.sidebar.addPanel(title, url,"");
		return false;
	} else if( window.external ) {
		// IE Favorite
		window.external.AddFavorite( url, title); 
	} else if(window.opera && window.print) { 
		// Opera Hotlist
		return false; 
	}
}

function enhanceBreadcrumb(title, url) {	
	entries = $('#breadCrumb ul');
	if (url != null) {
		entries.append('<li><a href="' + url + '">' + title + '</a></li>');
	} else {
		entries.append('<li>' + title + '</li>');
	}
}

var currentInput = false;

var formSubmitByEnterkey = {

	init: function () {
	
		

		$("input").focus(function () {
			currentInput = $(this);
		});

		$("body").keypress(function (e) {
			if(e.which == 13){
				id = $(currentInput).parents('form').attr('id');
				if( document.forms[id] ) {					
					document.forms[id].submit();
				}
			}
		});
	} 
}

function loadSwf(playlistUrl) {	
	var flashvars = {};
	flashvars.configPath = "/ipd/xml/videoplayer_config.xml";
	flashvars.playlistPath = playlistUrl;

	flashvars.autoStart = "false";
	var params = {};
	params.quality = "best";
	params.base = "";
	params.allowScriptAccess = "always";
	params.wmode = "transparent";

	var attributes = {};
	attributes.id = "flash_video";
	attributes.name = attributes.id;	
	swfobject.embedSWF("/ipd/swf/videoplayer.swf", "flash_video", "640", "385", "9.0.0", "/ipd/swf/expressInstall.swf", flashvars, params, attributes);
}











