
function animateCalendarLegend(cookieName){
	
	var _cookieName = cookieName;
	var legendContainer = $('div.legende');
	var animationDelay = 350;
	
	legendContainer.css({
		'top': 'auto',
		'overflow': 'hidden'
	});
	
	var legendHeader = legendContainer.find('div.legendeHeader');
	legendHeader.css({
		'position': 'relative',
		'top': 'auto',
		'z-index': '1',
		'background-color': '#fff',
		'cursor': 'pointer'
	});
	
	var legendContent = legendContainer.find('div.legendeContent');
	legendContent.css({
		'position': 'relative',
		'top': 'auto',
		'z-index': '0'
	});
	
	var legendContainerHeight = legendContainer.outerHeight();
	var legendContentHeight = legendContent.outerHeight();
	var legendHeaderHeight = legendHeader.outerHeight();
	var imageButton = legendHeader.find('.legendeBt img');
	
	if(_cookieName != undefined) {
		var cookieSettings = {expires: 7};
		if($.cookie(_cookieName) == 'closed') {
			legendContent.animate({'top': -legendContentHeight+'px'}, 0);
			legendContainer.animate({'height': legendHeaderHeight+'px'}, 0);
			legendHeader.removeClass('closeBox');
			imageButton.attr('src', 'fileadmin/templates/main/images/icon.pfeil.down.png');
		}else{
			legendContent.animate({'top': '0px'}, 0);
			legendContainer.animate({'height': legendContainerHeight+'px'}, 0);
			legendHeader.addClass('closeBox');
			imageButton.attr('src', 'fileadmin/templates/main/images/icon.pfeil.up.png');
		}
	}
	
	legendHeader.click(function(el){
		el.preventDefault();
		var _this = $(this);
		
		if(_this.hasClass('closeBox')) {
			if(_cookieName != undefined)
				$.cookie(_cookieName, 'closed', cookieSettings);
			_this.removeClass('closeBox');
			imageButton.attr('src', 'fileadmin/templates/main/images/icon.pfeil.down.png');
			legendContent.animate({'top': -legendContentHeight+'px'}, animationDelay);
			legendContainer.animate({'height': legendHeaderHeight+'px'}, animationDelay);
		}else{
			if(_cookieName != undefined)
				$.cookie(_cookieName, 'opened', cookieSettings);
			_this.addClass('closeBox');
			imageButton.attr('src', 'fileadmin/templates/main/images/icon.pfeil.up.png');
			legendContent.animate({'top': '0px'}, animationDelay);
			legendContainer.animate({'height': legendContainerHeight+'px'}, animationDelay);
		}
	});
	
	// closeLegende wird im Fluid Template gesetzt
	// PATH:EXT:wbo_mediacenter/Resources/Private/Templates/Media/List.html
	if((document.getElementById('legendeWboMediacenter')) && (closeLegendeOnLoad == true)) {
		legendContent.animate({'top': -legendContentHeight+'px'}, 0);
		legendContainer.animate({'height': legendHeaderHeight+'px'}, 0);
		legendHeader.removeClass('closeBox');
		imageButton.attr('src', 'fileadmin/templates/main/images/icon.pfeil.down.png');
	}
}

// Pressebereich -> Mediathek
// Aufbereitung der Informationen für Fancybox
function loadFancyImageInformation(obj) {
	var parent = obj.parent();
	if(parent.find('span.mediaInfos').html() != null) {
		var container = $('div.inlineContentTemplate');
		var image = obj.find('img');
		var imageInfo = parent.find('span.mediaInfos').html().split(';;;');
		var downloadLink = 'fileadmin/mediacenter_dl.php?f=';
		var imgPath = imageInfo[imageInfo.length-1].split('/');
		image.attr('alt', imageInfo[0]);
		imageInfo.shift();
		imgPath.shift();
		downloadLink = downloadLink+imgPath.join('/');
		var linkElement = $('<a>').html('Bild herunterladen').attr('href', downloadLink).addClass('downloadImage');
		container.find('p.fancyImageInfoHeadline').html(obj.attr('title'));
		container.find('p.fancyImageInfoDate').html(imageInfo[0]);
		container.find('p.fancyImageInfoData').html(imageInfo[1]);
		container.find('p.fancyImageDownloadLink').html(linkElement);
		return container.html();
	}
	return false;
}


$(document).ready(function() {
	
	/**
	 * 
	 * Ein-/Ausklapp Funktion für Legende im Seminar Terminkalendar aufrufen
	 * Events zum Abschicken des Formulars (Filterfunktionen)
	 */
	if(document.getElementById('legendeWboSeminarverwaltung')) {
		// Cookie-Handling, Eventlistener zum Öffnen und Schließen der Legende
		animateCalendarLegend('wboSeminarLegendToggleStatus');
		// Form DOM Object referenzieren
		var calendarFilterForm = $('div.tx-wbo-seminars form.seminarCalendarFilterForm');
		// Fallback Submit Button entfernen
		calendarFilterForm.find('input[type="submit"]').remove();
		// onChange Event Select Field Kategorie
		calendarFilterForm.find('select.calendarFilterCategories').change(function(ev){
			calendarFilterForm.submit();
		});
		// onChange Event Select Field Datum
		calendarFilterForm.find('select.calendarFilterDates').change(function(ev){
			calendarFilterForm.submit();
		});
	}
	
	/**
	 * 
	 * Ein-/Ausklapp Funktion für Legende im Pressebereich Fotoarchiv aufrufen
	 * Events zum Abschicken des Formulars (Filterfunktionen)
	 */
	if(document.getElementById('legendeWboMediacenter')) {
		// Eventlistener zum Öffnen und Schließen der Legende, kein Cookie
		animateCalendarLegend();
	}
	
	/**
	 * 
	 * Standardinhalt für Suchfeld in Top Navigation
	 */
	$("#topnav .topnav-search input#topSearch").each(function() {
		var defaultValue = "Suchen";
		$(this).focus(function(){
			if($(this).val() == defaultValue) $(this).val("");
			else $(this).select();
		});
		$(this).blur(function(){
			if($(this).val() == "") $(this).val(defaultValue);
		});
		$(this).blur();
	});
	
	
	/**
	 * 
	 * Standardinhalt für Loginformular auf Startseite -> Nutzername
	 */
	$("#visualright form#wbologinform input#loginname").each(function() {
		var defaultValue = "Nutzername";
		$(this).focus(function(){
			if($(this).val() == defaultValue) $(this).val("");
			else $(this).select();
		});
		$(this).blur(function(){
			if($(this).val() == "") $(this).val(defaultValue);
		});
		$(this).blur();
	});
	
	/**
	 * 
	 * Standardinhalt für Loginformular auf Startseite -> Passwort
	 */
	$("#visualright form#wbologinform input#loginpw").each(function() {
		var defaultValue = "Passwort";
		$(this).focus(function(){
			if($(this).val() == defaultValue) $(this).val("");
			else $(this).select();
		});
		$(this).blur(function(){
			if($(this).val() == "") $(this).val(defaultValue);
		});
		$(this).blur();
	});
	
	/**
	 * 
	 * Links mit dem Attribut rel="external" in neuem Fenster öffnen
	 */
	$('a[rel="external"]').click(function(ev){
		ev.preventDefault();
		window.open(this.href);
	});
	
	/**
	 * 
	 * Tooltips in Seminar Registration
	 */
	var qtipClass = function() {
		this.show = {event: 'click'};
		this.hide = {event: 'false'};
		this.position = {
			my: 'top left',  
			at: 'bottom left',
			adjust: {x:3, y:0}
		};
		this.style = {
			classes: 'ui-tooltip-light',
			tip: {
				corner: 'top left',
			 	mimic: 'center',
			 	height: 5,
			 	width: 8,
			 	offset: 3
			}
		};
		this.content = {
			 title: {
				 text: '&nbsp;',
				 button: 'Schliessen'
			},
			text: function(api) {
				var contentElement = $(this).attr("id")+"-content";
				return $("#"+contentElement);
			}
		};
	};
	var qtipDefault = new qtipClass();
	$('.tx-wbo-seminars .tooltip').qtip(qtipDefault);
	$('.tx-wbo-seminars .tooltip').click(function(ev){
		ev.preventDefault();
	});
	
	var birthdayDayDefaultValue = 'TT';
	var birthdayMonthDefaultValue = 'MM';
	var birthdayYearDefaultValue = 'JJJJ';
	$('.tx-wbo-seminars input[type="text"]').each(function(ev){
		var _this = $(this);
		_this.focus(function(){
			if(_this.attr('id') == 'birthdayDay') {
				if(_this.val() == birthdayDayDefaultValue) _this.val('');
				else _this.select();
			}else if(_this.attr('id') == 'birthdayMonth') {
				if(_this.val() == birthdayMonthDefaultValue) _this.val('');
				else _this.select();
			}else if(_this.attr('id') == 'birthdayYear') {
				if(_this.val() == birthdayYearDefaultValue) _this.val('');
				else _this.select();
			}else {
				_this.select();
			}
		});
		_this.blur(function(){
			if(_this.attr('id') == 'birthdayDay') {
				if(_this.val() == '') _this.val(birthdayDayDefaultValue);
			}else if(_this.attr('id') == 'birthdayMonth') {
				if(_this.val() == '') _this.val(birthdayMonthDefaultValue);
			}else if(_this.attr('id') == 'birthdayYear') {
				if(_this.val() == '') _this.val(birthdayYearDefaultValue);
			}
		});
		_this.blur();
	});
	
	
	/**
	 * 
	 * Standardkonfiguration für jQuery Fancybox
	 */
	jQuery.fn.makeFancy = function(el, options) {
		var _this = el;
		var container = this;
		
		var settings = jQuery.extend({
			'type'				: 'image',
			'title'				: loadFancyImageInformation(_this),
			'titleShow'			: true,
			'titlePosition'		: 'inside',
			'overlayShow'		: true,
			'overlayOpacity'	: 0.7,
			'overlayColor'		: '#000000',
			'padding'			: 10,
			'margin'			: 120,
			'cyclic'			: false,
			'speedIn'			: 400,
			'speedOut'			: 200,
			'changeSpeed'		: 150,
			'changeFade'		: 150,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'easingIn'			: 'easeInQuint',
			'easingOut'			: 'swing',
			'centerOnScroll'	: false,
			'width'				: 800,
			'height'			: 600,
		}, options);
		
		container.fancybox(settings);
		return this;
	};
	$("a.fancyboxImage").each(function(){
		$(this).makeFancy($(this));
	});
	
	$("a.downloadImage").click(function(ev){
		ev.preventDefault();
		var imgPath = $(this).attr("href").split('/');
		imgPath.shift();
		imgPath = imgPath.join('/');
		window.open("fileadmin/mediacenter_dl.php?f="+imgPath);
		return false;
	});
	
	/**
	 * 
	 * Pressebereich Fotoarchiv - alle Checkboxen wählen/abwählen
	 */
	if(document.getElementById('legendeWboMediacenter')) {
		$('#allimg').click(function(ev) {
			ev.preventDefault();
			$('#legendeWboMediacenter input[name^="tx_wbomediacenter_pi1[categoryFormValues]"]').attr('checked', true);
		});
		$('#noimg').click(function(ev) {
			ev.preventDefault();
			$('#legendeWboMediacenter input[name^="tx_wbomediacenter_pi1[categoryFormValues]"]').attr('checked', false);
		});
		$('#legendeWboMediacenter .wrapCheckboxes :checkbox.level-one').change(function(){
			$(this).parent().find(':checkbox.level-two').attr('checked', $(this).is(':checked'));
		});
	}
	
	$('div.tx-wbo-mediacenter .news-amenu-container a').click(function(ev){
		ev.preventDefault();
		$('form[name="categoryFormValues"] input[name="tx_wbomediacenter_pi1[currentPage]"]').val($(this).attr('rel'));
		$('form[name="categoryFormValues"]').submit();
	});
	
	
});

