// $ UI Datepicker
$(function($) {
	$.datepicker.regional['de'] = {
		prevText:        'zurück',
		nextText:        'weiter',
		monthNames:      ['Jänner','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
		dayNames:        ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		dayNamesMin:     ['So','Mo','Di','Mi','Do','Fr','Sa'],
		firstDay:        1,
		buttonText:      'Kalender',
		isRTL:           false
	};
	$.datepicker.regional['en'] = {
		buttonText:      'Calendar'
	};
	$.datepicker.regional['fr'] = {
		prevText:        'préc',
		nextText:        'suiv',
		monthNames:      ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'], 
		dayNames:        ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'], 
		dayNamesMin:     ['Di','Lu','Ma','Me','Je','Ve','Sa'],
		firstDay:        0,
		buttonText:      'Calendrier',
		isRTL:           false
	};
	$.datepicker.regional['it'] = {
		prevText:        'prec',
		nextText:        'succ',
		monthNames:      ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'], 
		dayNames:        ['Domenica','Lunedì','Martedì','Mercoledì','Giovedì','Venerdì','Sabato'], 
		dayNamesMin:     ['Do','Lu','Ma','Me','Gi','Ve','Sa'], 
		firstDay:        1,
		buttonText:      'Calendario',
		isRTL:           false
	};
	$.datepicker.setDefaults($.datepicker.regional[$('html').attr('lang')]);
	$.datepicker.setDefaults({
		buttonImage:     '/fileadmin/template/img/16x16/calendar.png',
		buttonImageOnly: true,
		dateFormat:      'yy-mm-dd', // ISO 8601
		minDate:         '0y',
		showOn:          'both',
		showOtherMonths: true
	});
	
	function select_to_datepicker(prefix, min_date, bind_to) {
		if($('select[name="' + prefix + '_yyyy"], select[name="' + prefix + '_mm"], select[name="' + prefix + '_dd"]').length == 3) {
			$('select[name="' + prefix + '_yyyy"], select[name="' + prefix + '_mm"], select[name="' + prefix + '_dd"]').css({display: 'none'});
			$('select[name="' + prefix + '_yyyy"]').before('<input type="text" name="' + prefix + '" readonly="readonly" class="s25" />');
			$('input[name="' + prefix + '"]').val(
				$('select[name="' + prefix + '_yyyy"]').val() + '-' +
				$('select[name="' + prefix + '_mm"]'  ).val() + '-' +
				$('select[name="' + prefix + '_dd"]'  ).val()
			);
			
			// Wenn die select-Elemente keine Werte haben, wird das Textfeld wieder geleert.
			if ($('input[name="' + prefix + '"]').val() == '--') {
				$('input[name="' + prefix + '"]').val('');
			}
			$('input[name="' + prefix + '"]').datepicker({
				minDate: min_date,
				onSelect: function(dateText, inst) {
					var arr = dateText.split('-');
					$('select[name="' + prefix + '_yyyy"]').val(arr[0]);
					$('select[name="' + prefix + '_mm"]'  ).val(arr[1]);
					$('select[name="' + prefix + '_dd"]'  ).val(arr[2]);
					
					// Bei der Auswahl eines Anreisedatums wird der Picker des Abreisedatums auf eine Mindestauswahl von +1 Tag (86400000 Millisekunden) gesetzt.
					$('input[name="' + bind_to + '"]').datepicker(
						'option',
						$.extend({
							minDate: new Date(
								parseInt(
									Date.parse(
										$('input[name="' + prefix + '"]').datepicker('getDate')
									)
								)+86400000
							)
						})
					);
					
				}
			});
		}
	}
	
	select_to_datepicker('mf_datum_von', '+1d', 'mf_datum_bis');
	select_to_datepicker('mf_datum_bis', '+2d');
	select_to_datepicker('mf_datum', '+1d');
});

$(document).ready(function() {
	$('body').append('<div id="ajax-indicator" />');
	$('#ajax-indicator').ajaxStart(function(){ $(this).show(); })
		                .ajaxStop (function(){ $(this).hide(); });
						
	
	if($('body.layout2').length > 0) {
		$('.layout2 #content>*').wrapAll('<div class="item" />');
		
		if($('.layout2 #menu3 a').length > 1) {
			$('.layout2 #container').append('<ul class="controls"><li class="prev">‹</li><li class="next">›</li></ul>');
		} else {
			$('#aside, #content').css('border', 'none');
		}
		
		

		if(($('.layout2 #menu3 a.current').index('.layout2 #menu3 a')) > 0) {
			for(i = 0; i < $('.layout2 #menu3 a.current').index('.layout2 #menu3 a'); i++) {
				$('.layout2 #content').prepend('<div class="item" />');
			}
		}
		if(($('.layout2 #menu3 a').length-$('.layout2 #menu3 a.current').index('.layout2 #menu3 a')-1) > 0) {
			for(i = 0; i < ($('.layout2 #menu3 a').length-$('.layout2 #menu3 a.current').index('.layout2 #menu3 a')-1); i++) {
				$('.layout2 #content').append('<div class="item" />');
			}
		}
			
		$('#content').cycle(
			{
				cleartypeNoBg: true,
				fx:     'scrollHorz',
				timeout: 400,
				speed:   250,
				random:  0,
				startingSlide: $('.layout2 #menu3 a.current').index('.layout2 #menu3 a')
			}
		).cycle('pause');
		
	} 			
	$('.layout2 #menu3 a').bind(
		'click',
		function(event) {
			event.preventDefault();
			$('.layout2 #menu3 a').removeClass('current');
			$(this).addClass('current');
			index = $(this).index('.layout2 #menu3 a');
			if($('#content>.item:eq(' + $(this).index('.layout2 #menu3 a') + ')>*').length == 0) {
				$.ajax({
					type: 'GET',
					url: $(this).attr('href'),
					data: 'type=10',
					dataType: 'xml',
					success: function(xml) {
						$('#content>.item:eq(' + index + ')').append($('content', xml).text());
						$('#content').cycle(index);
					}
				});		
			} else {
				$('#content').cycle(index);
			}
		}
	);
	
	$('.layout2 .controls .prev').bind(
		'click',
		function(event) {
			event.preventDefault();
			if($('.layout2 #menu3 a').index($('.layout2 #menu3 a.current')) == 0) {
				$('.layout2 #menu3 li:last-child a').trigger('click');
			} else {
				$('.layout2 #menu3 a.current').parent().prev().find('a').trigger('click');
			}
		}
	);
	
	$('.layout2 .controls .next').bind(
		'click',
		function(event) {
			event.preventDefault();
			if(($('.layout2 #menu3 a').index($('.layout2 #menu3 a.current'))) == ($('.layout2 #menu3 a').length-1)) {
				$('.layout2 #menu3 li:eq(1) a').trigger('click');
			} else {
				$('.layout2 #menu3 a.current').parent().next().find('a').trigger('click');
			}
		}
	);
	
	if($('body.layout2').length == 0) {
		$('#col4 a').hover(
			function() {
				$(this).parent().siblings().find('a').stop(true).animate(
					{
						opacity: .25
					},
					750
				);
			},
			function() {
				$('#col4 a').stop(true).animate(
					{
						opacity: 1
					},
					250
				);
			}
		);
	} else {
		$('#col4 a').css('opacity', '.60');
		$('#col4 a.current').css('opacity', '1');
		$('#col4 a').hover(
			function() {
				$(this).stop(true).animate(
					{
						opacity: 1
					},
					250
				);
				$(this).parent().siblings().find('a').stop(true).animate(
					{
						opacity: .60
					},
					750
				);
			},
			function() {
				$('#col4 a').stop(true).animate(
					{
						opacity: .60
					},
					750
				);
				$('#col4 a.current').stop(true).animate(
					{
						opacity: 1
					},
					250
				);
			}
		);
	}
						

	// wrap span-Element into a.button
	$('a.button:not(:has(input))').wrapInner('<span />');
		
	$('.section.foldable').each(
		function() {
			$(this).find('>*:not(h3)').wrapAll('<div class="foldable_body" />');
		}
	);
	$('.section.foldable').append('<span class="control" />')
	$('.section.foldable.closed .foldable_body').slideUp(0);
	$('.section.foldable.closed h3, .section.foldable.closed .control').live(
		'click',
		function() {
			$(this).parent('.foldable').removeClass('closed').find('.foldable_body').stop(true, true).slideDown(500);
		}
	);
	$('.section.foldable:not(.closed) h3, .section.foldable:not(.closed) .control').live(
		'click',
		function() {
			$(this).parent('.foldable').addClass('closed').find('.foldable_body').stop(true, true).slideUp(500);
		}
	);
	

	now = new Date()
	// nach 14.00 Uhr wird das mittagsmenü vom nächsten Tag anzeigt
	if(now.getUTCHours()-now.getTimezoneOffset()/60 >= 14) {
		currentDay = now.getDay()+1;
	} else {
		currentDay = now.getDay();
	}	

	$('#col1 .outside>.section:not(:eq(0))').wrapAll('<div class="scrollbody" />');
	$('#col1 .outside').append('<ul class="controls"><li class="prev">‹</li><li class="next">›</li></ul>');
	$('#col1 .outside .scrollbody').css('margin-bottom', '-3em').cycle(
		{
			cleartypeNoBg: true,
			fx:     'scrollHorz',
			timeout: 1000,
			speed:   250,
			random:  0,
			prev: '#col1 .controls .prev',
			next: '#col1 .controls .next',
			startingSlide: currentDay
		}
	).cycle('pause');
	
	// lib.MEDIA
	if($('#media').length > 0) {
		$.ajax({
			global: false,
			type: 'GET',
			url: window.location.href,
			data: 'type=71629',
			dataType: 'xml',
			success: function(xml) {
				$(xml).find('media>img').each(
					function() {
						$('<img />')
							.attr('src',    $(this).attr('src'   ))
							.attr('width',  $(this).attr('width' ))
							.attr('height', $(this).attr('height'))
							.attr('alt',    $(this).attr('alt'   ))
							.attr('title',  $(this).attr('title' ))
							.load(function(){
								// static: new images will be appended to the bottom of the list
								$('#media').append($(this));
								// random: new images will be added randomly to the list 
								// $('#media>img:nth-child(' + Math.ceil(Math.random() * $('#media>img').length) + ')').after($(this));
								$('#media').cycle(
									{
										fx:     'fade',
										timeout: 4000,
										speed:   2500,
										random:  0      // does not work with with $.cycle.lite.min.js
														// but we don't care, because the preloading would mix up and brake the slideshow anyways
									}
								);
							});
					}
				);
			}
		});
	}
});





/*
$(document).ready(function() {
	$('body').toggle(
		function() { $(this).css('font-size', '14px'); },
		function() { $(this).css('font-size', '16px'); },
		function() { $(this).css('font-size', '12px'); }
	);
});
*/
