// $ 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.setDefaults($.datepicker.regional[$('html').attr('lang')]);
	$.datepicker.setDefaults({
		buttonImage:     '/fileadmin/template/img/icons/datepicker.gif',
		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="mf[' + prefix + '_yyyy]"], select[name="mf[' + prefix + '_mm]"], select[name="mf[' + prefix + '_dd]"]').length == 3) {
			$('select[name="mf[' + prefix + '_yyyy]"], select[name="mf[' + prefix + '_mm]"], select[name="mf[' + prefix + '_dd]"]').css({display: 'none'});
			$('select[name="mf[' + prefix + '_yyyy]"]').before('<input type="text" id="mf_' + prefix + '" name="mf[' + prefix + ']" readonly="readonly" class="s25" />');
			$('input[name="' + prefix + '"]').val(
				$('select[name="mf[' + prefix + '_yyyy]"]').val() + '-' +
				$('select[name="mf[' + prefix + '_mm]"]'  ).val() + '-' +
				$('select[name="mf[' + prefix + '_dd]"]'  ).val()
			);
			
			// Wenn die select-Elemente keine Werte haben, wird das Textfeld wieder geleert.
			if ($('input[name="mf[' + prefix + ']"]').val() == '--') {
				$('input[name="mf[' + prefix + ']"]').val('');
			}
			$('input[name="mf[' + prefix + ']"]').datepicker({
				minDate: min_date,
				onSelect: function(dateText, inst) {
					var arr = dateText.split('-');
					$('select[name="mf[' + prefix + '_yyyy]"]').val(arr[0]);
					$('select[name="mf[' + prefix + '_mm]"]'  ).val(arr[1]);
					$('select[name="mf[' + 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="mf[' + bind_to + ']"]').datepicker(
						'option',
						$.extend({
							minDate: new Date(
								parseInt(
									Date.parse(
										$('input[name="mf[' + prefix + ']"]').datepicker('getDate')
									)
								)+86400000
							)
						})
					);	
				}
			});
		}
	}
	
	select_to_datepicker('date_from', '+1d', 'date_till');
	select_to_datepicker('date_till', '+2d');
	select_to_datepicker('date', '+1d');
});

$('#media').ready(function() {
	$.ajax({
		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
								}
							);
						});
				}
			);
		}
	});
	
	$('a.ajax').append('<span />');

});

jQuery(document).ready(function() {
	jQuery('a.button').wrapInner('<span />');
	
		/**** QUICKFORM ****/
	if($('select[name="mf[date_from_yyyy]"]').val()){
		$('input[name="mf[date_from]"]').datepicker('setDate', $('select[name="mf[date_from_yyyy]"]').val() + '-' + $('select[name="mf[date_from_mm]"]').val() + '-' + $('select[name="mf[date_from_dd]"]').val());
	}
	if($('select[name="mf[date_till_yyyy]"]').val()){
		$('input[name="mf[date_till]"]').datepicker('setDate', $('select[name="mf[date_till_yyyy]"]').val() + '-' + $('select[name="mf[date_till_mm]"]').val() + '-' + $('select[name="mf[date_till_dd]"]').val());
	}
	
	
	/**** STARTSEITE ****/

	/*$('body.home #col0').append('<div class="scroller" /><ul class="controls" />');
	$('body.home #col0>.section>.section:not(:first-child)').appendTo('body.home #col0 .scroller');
	
	$('body.home #col0>.scroller>.section:nth-child(3n-2)').each(
		function() {
			$(this).wrap('<div class="itemgroup" />');
			$(this).parent().next().appendTo($(this).parent());
			$(this).parent().next().appendTo($(this).parent());
		}
	);
	if($('body.home #col0 .scroller .itemgroup').length > 1){
		for (var i = 1; i <= $('body.home #col0 .scroller .itemgroup').length ; i++) {
			$('body.home #col0 .controls').append('<li />');
		}
	}
	
	$('body.home #col0 .controls>li').bind(
		'click',
		function() {
			$('body.home #col0 .scroller').cycle($(this).index('li')).cycle('pause');
		}
	);

	$('body.home #col0 .scroller').cycle({
		fx:      'scrollHorz',
		timeout: 4000,
		speed:   333,
		before:  function(currSlideElement, nextSlideElement, options, forwardFlag) {
			$('body.home #col0 .controls>li').removeClass('cur');
			$('body.home #col0 .controls>li:eq(' + $(nextSlideElement).index() + ')').addClass('cur');
		}
	}).cycle('pause');

	if($('body.home #col0 .scroller .itemgroup').length > 1){
		$('body.home #col0 .controls').prepend('<a class="first"/>');
		$('body.home #col0 .controls').append('<a class="last"/>');	
	}
	
	$('body.home #col0 .controls>a.first').bind(
		'click',
		function() {
			$('body.home #col0 .scroller').cycle(0).cycle('pause');
		}
	);
	
	$('body.home #col0 .controls>a.last').bind(
		'click',
		function() {
			$('body.home #col0 .scroller').cycle($('body.home #col0 .scroller .itemgroup').length-1).cycle('pause');
		}
	);*/
	
	$('body.home #col2 #mowisweather ul li:nth-child(4)').remove();
});
