// $ 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() {
	// Google Site Search fix
	$('#search form, [name=tx_indexedsearch]').bind(
		'submit', 
		function (e){
			var form   = $(this);
			var action = form.attr('action');
			$(this).attr(
				'action',
				action + ((action.indexOf('?') == '-1') ? '?' : '&') + 'q=' + encodeURIComponent($('[name="tx_indexedsearch[sword]"]', form).val())
			);
		}
	);

	

	var input = $('#search input#q');
	var label = $('#search label');
	input.bind(
		'click focus change',
		function() {
			label.css('visibility', 'hidden');
		}
	);
	input.bind(
		'blur',
		function() {
			if (input.val() == '') {
				label.css('visibility', '');
			}
		}
	);
	

	$('#quickinfo span.toggle').toggle(
		function() {
			$('#after').animate(
				{
					height: '320px'
				}
			);
			//trackEvent ('info', 'open');
			$(this).css('background-position', '0 -39px');
		},
		function() {
			$('#after').animate(
				{
					height: '0'
				}
			);
			$(this).css('background-position', '0 0');
		}
	);
	
	//$('.home #after').delay(1000).animate({height: '250px'});
	//$('.home #after').delay(1000).animate({height: '0'});

});

$(document).ready(function() {
	// wrap span-Element into a.button
	$('a.button').wrapInner('<span />');
	
	// lib.MEDIA
	$('#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
									}
								);
							});
					}
				);
			}
		});
	});
});

$(document).ready(function() {
	
	$('a.ajax, a.highslide').append('<span class="zoom" />');
	
	$('body.home #col2').append('<div class="scroller" /><div class="controls" />');
	$('body.home #col2 .section:not(:first-child)').appendTo('body.home #col2 .scroller');
	

	$('body.home #col2 .scroller>.section').each(
		function() {
			$(this).wrap('<div class="itemgroup" />');
		}
	);
	$('.scroller .itemgroup .section .section').unwrap();
	
	$('body.home #col2 .scroller').cycle({
		fx:      'scrollHorz',
		timeout: 7000,
		speed:   500,
		before:  function(currSlideElement, nextSlideElement, options, forwardFlag) {
			//$('body.home #col2 .controls>li').removeClass('cur');
			//$('body.home #col2 .controls>li:eq(' + $(nextSlideElement).index() + ')').addClass('cur');
		}
	});

	if($('body.home #col2 .scroller .itemgroup').length > 1){
		$('body.home #col2 .controls').prepend('<a class="prev"/>');
		$('body.home #col2 .controls').append('<a class="next"/>');	
	}
	
	$('body.home #col2 .controls>a.prev').bind(
		'click',
		function() {
			$('body.home #col2 .scroller').cycle('prev').cycle('pause');
		}
	);
	
	$('body.home #col2 .controls>a.next').bind(
		'click',
		function() {
			$('body.home #col2 .scroller').cycle('next').cycle('pause');
		}
	);
	
	//Startseite Accordion
	
	$('body.home #col0 .section:first-child').addClass('first-child');
	
	$('body.home #col0 .section').each(
		function() {
			 $('h3', this).insertBefore($('.csc-textpic', this)); 
			 $('h3', this).append('<div><span class="state" /></div>');
			 
		}
	);

	$('body.home #col0 .section:first-child>h3').addClass('cur');
	
	$('body.home #col0 .section:not(:first-child)>*:not(h3)').slideUp(250);
	
	$('body.home #col0 .section>h3:not(.cur)').live(
		'click',
		function() {
			$('body.home #col0 .section h3').removeClass('cur');
			$('body.home #col0 .section>*:not(h3)').slideUp(250);
			$(this).addClass('cur');
			$(this).parent().find('*:not(h3)').stop(true).slideDown(300);
		}
	);
	

	
	//Startseite Produkt Slider
	
	var scrollamount = 140;
	var productelements = $('#col4 .section');
	var maxposition = (productelements-6);
	var position = 0;
	
	
	$('#col4').append('<div class="scroller"><div id="sliderbox"></div></div>');
	$('#col4 .section').appendTo('#col4 .scroller #sliderbox');
	
	if(productelements.length > 6) {
		$('#col4')
			.append('<a id="slidePrev"/>')
			.append('<a id="slideNext"/>')
			.append('<div id="slider_mask_left"/>')
			.append('<div id="slider_mask_right"/>');
			
		window.setTimeout("myautoslide()", 4000);
	}
	

	$('#col4>a#slideNext').bind(
		'click',
		function() {
			autoslideMe = false;
			if($('#sliderbox').position().left == 0){
				$('#sliderbox').animate({left: '-' + scrollamount + 'px'}, 500, 'easeInOutSine',
					function() {
						$(this)
							.css({left: '0px'})
							.find('.section:first-child').appendTo('#sliderbox');
					}
				);
			}
		}
	);
	
	$('#col4>a#slidePrev').bind(
		'click',
		function() {
			autoslideMe = false;
			if($('#sliderbox').position().left == 0){
				$('#sliderbox').find('.section:last-child').prependTo('#sliderbox');
				$('#sliderbox').animate({left: '-' + scrollamount + 'px'}, 0, 'easeInOutSine',
						function() {
							$(this).animate({left: '0px'}, 500, 'easeInOutSine');
						}
					);
			}
		}
	);
	
	

	//Downloads
	$('#col0 .foldable').each(
		function() {
			 $('h3', this).append('<span class="state" />');
		}
	);
	
	$('#col0 .foldable>*:not(h3)').slideUp(250);
	

	$('#col0 .foldable>h3').toggle(
		function(){
			$(this).addClass('open');
			$(this).parent().find('>*:not(h3, .state)').slideDown(300);

		},function() {
			$(this).removeClass('open');
			$(this).parent().find('>*:not(h3, .state)').slideUp(250);
		}
	);


});


// Autoslide(Timeout) Startseite Produkt Slider
var autoslideMe = true;

function myautoslide() {
	if(autoslideMe == true) {
		$('#col4>a#slideNext').trigger('click');
		window.setTimeout("myautoslide()", 4000);
		autoslideMe = true;
	}
}

function gmap2_after_initialize() {
	$('#media_outer .media_mask').css('display', 'none');
};


