// $ 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_spitz/img/icons/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() {
	// Lupe in vergrösserbare Bilder
	$('a.ajax').append('<span />');


	$('#col1 .section:not(:first-child)>*:not(h3)').hide(0);
	$('#col1 .section').bind(
		'click',
		function() {
			FlowObject.moveTo($(this).index());
		}
	);
	
	$('.home #col0 div.csc-textpic-imagewrap,.homeb2b #col0 div.csc-textpic-imagewrap,#col1 div.csc-textpic-imagewrap').each(
		function() {
			$(this).append('<span class="arrow" />').prependTo($(this).parent().parent());
		}
	);
	
	$('.home #col0 .section:not(:first-child)>*:not(h3), .homeb2b #col0 .section:not(:first-child)>*:not(h3)').hide(0);
	$('.home #col0 .section:first-child>h3, .homeb2b #col0 .section:first-child>h3').addClass('cur');
	$('.home #col0 .section>h3, .homeb2b #col0 .section>h3').bind(
		'click',
		function() {
			$('#col0 .section h3').removeClass('cur');
			$('#col0 .section>*:not(h3)').slideUp(250);
			$(this).addClass('cur');
			$(this).parent().find('*:not(h3)').stop(true).slideDown(300);
		}
	);
	
	$('.home #col0, .homeb2b #col0,').append('<span class="prev">prev</span><span class="next">next</span>');
	
	$('.home #col0 span.prev, .homeb2b #col0 span.prev').bind(
		'click',
		function() {
			count = ($('#col0 .section').length-1); 
			prev =  ($('#col0 .section h3.cur').parent().index()-1) ;
			if (prev < 0) { prev = count; }
			
			$('#col0 .section h3').removeClass('cur');
			$('#col0 .section>*:not(h3)').slideUp(250);
			$('#col0>:eq(' + prev + ')>h3').addClass('cur');	
			$('#col0>:eq(' + prev + ')>*:not(h3)').stop(true).slideDown(300);	
		}
	);

	$('.home #col0 span.next, .homeb2b #col0 span.next').bind(
		'click',
		function() {
			count = ($('#col0 .section').length-1); 
			next =  ($('#col0 .section h3.cur').parent().index()+1) ;
			if (next > count) { next = 0; }
			
			$('#col0 .section h3').removeClass('cur');
			$('#col0 .section>*:not(h3)').slideUp(250);
			$('#col0>:eq(' + next + ')>h3').addClass('cur');	
			$('#col0>:eq(' + next + ')>*:not(h3)').stop(true).slideDown(300);	
		}
	);

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

function resize() {
	if ($('body.home, body.homeb2b').length > 0) {
		new_height = $(window).height()-2-170;
		if (new_height < 610) { new_height = 610; }
		$('#container').height(new_height);	
	}
}

$(document).ready(function() { resize(); });

$(window).resize(function()  { resize(); });


/*
 * Vorbelegtes Suchfeld leeren:
 * http://suit.rebell.at/artikel/vorbelegte-formularfelder-leeren
 */
$(document).ready(function() {
	var input = jQuery('#search input#q');
	var label = jQuery('#search label[for=q]');
	input.bind(
		'click focus change',
		function() {
			label.css('visibility', 'hidden');
		}
	);
	input.bind(
		'blur',
		function() {
			if (input.val() == '') {
				label.css('visibility', '');
			}
		}
	);	
});

jQuery(document).ready(function(){
	jQuery("#lp_close").click(function() {
		// alert('foo');
		// trackEvent('Landingpage Overlay', 'close');
  		jQuery("#lp_close, #landingoverlay").fadeOut("slow");
	});
}); 

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