// $ 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');
});



function autoGoNext (actPic, dir) {
	numPics = ($('#myBook .image').length-1)/2+1;
	//alert(actPic +', '+dir);
	if(dir == 'left') {
		actPic--;
		goprev();
	}else{
		actPic++;
		gonext();
	}
	if(actPic == numPics) {
		dir = 'left';
	}
	if(actPic == 0) {
		dir = 'right';
	}
	var timer;
	timer = setTimeout ('autoGoNext('+actPic+', "'+dir+'")', 5000);
}

$(document).ready(function() {
	actPic = 0;
	dir = "right";
	autoGoNext(actPic, dir);
	
	$('.chgallery').addClass('ajax').attr('rel', 'chgallery');
	
	var speed = 500;
	
	$('#toggle_footer').toggle(
		function() {
			$('#footer').animate( {height : '473px' }, speed, 'linear');
			setTimeout ('window.scrollBy(0, 473)', speed);			
		},
		function() {
			$('#footer').animate( {height : '33px' }, speed, 'linear');
		}
	);
	
	/**** STARTSEITE ****/
	$('body.home #col0').append('<div class="scroller" /><ul class="controls" />');
	$('body.home #col0 .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());
		}
	);
	$('.scroller .itemgroup .section .section').unwrap();
	
	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()-1).cycle('pause');
		}
	);
	
	$('body.home #col0 .scroller').cycle({
		fx:      'scrollHorz',
		timeout: 5000,
		speed:   speed,
		before:  function(currSlideElement, nextSlideElement, options, forwardFlag) {
			$('body.home #col0 .controls>li').removeClass('cur');
			$('body.home #col0 .controls>li:eq(' + $(nextSlideElement).index() + ')').addClass('cur');
		}
	});	

	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('prev').cycle('pause');
		}
	);
	
	$('body.home #col0 .controls>a.last').bind(
		'click',
		function() {
			$('body.home #col0 .scroller').cycle('next').cycle('pause');
		}
	);
	
	$('#mf_date_from').val($.url.param('mf%5Bdate_from%5D'));
	$('#mf_date_from_yyyy').val($.url.param('mf%5Bdate_from_yyyy%5D'));
	$('#mf_date_from_mm').val($.url.param('mf%5Bdate_from_mm%5D'));
	$('#mf_date_from_dd').val($.url.param('mf%5Bdate_from_dd%5D'));
	$('#mf_date_till').val($.url.param('mf%5Bdate_till%5D'));
	$('#mf_date_till_yyyy').val($.url.param('mf%5Bdate_till_yyyy%5D'));
	$('#mf_date_till_mm').val($.url.param('mf%5Bdate_till_mm%5D'));
	$('#mf_date_till_dd').val($.url.param('mf%5Bdate_till_dd%5D'));
	
	$("#lp_close").click(function() {
		// alert('foo');
		// trackEvent('Landingpage Overlay', 'close');
  		$("#lp_close, #landingoverlay").fadeOut("slow");
	});

	$('.overlay #col0 .section').wrapAll('<div class="scroller" />');
	$('.overlay #col0').append('<ul class="controls" />');
	$('.overlay #col0 .scroller')
			.cycle(
				{
					cleartypeNoBg: true,
					fx:     'scrollHorz',
					timeout: 5000,
					speed:   250,
					random:  0,
					pager: '.overlay #col0 .controls',
					pagerAnchorBuilder: function(index, slide) {
						return '<li><a href="#">' + (index+1) + '</a></li>';
					},
					prev: '#teaser .prev',
					next: '#teaser .next',
					activePagerClass: 'cur',
					updateActivePagerLink: function(pager, currSlide, activePagerClass) { 
						$('li', pager)
							.removeClass(activePagerClass) 
							.filter('li:eq('+(currSlide)+')').addClass(activePagerClass); 
					}
				}
			);
	if($('body.overlay #col0 .scroller .section').length > 0){
		$('body.overlay #col0 .controls').prepend('<a class="first"/>');
		$('body.overlay #col0 .controls').append('<a class="last"/>');	
	};
	
	$('.overlay #col0 .controls>a.first').bind(
		'click',
		function() {
			$('.overlay #col0 .scroller').cycle('prev').cycle('pause');
		}
	);
	
	$('.overlay #col0 .controls>a.last').bind(
		'click',
		function() {
			$('.overlay #col0 .scroller').cycle('next').cycle('pause');
		}
	);

	
});

