var isPlaceholderReplaced = false;

jQuery( function( $ ) {
	// DatePicker
	$.datepicker.setDefaults(  $.datepicker.regional[ isPlaceholderReplaced ] );
	var dates = $('#expressform input[name=headerFormAnreise],#expressform input[name=headerFormAbreise]').datepicker({
		buttonImage: systemPath + '_img/base/calendar.png',
		showWeek: true,
		firstDay: 1,
		changeMonth: true,
		changeYear: true,
		showAnim: 'drop',
		dateFormat: 'dd.mm.yy',
		onSelect: function( selectedDate ) {
			var option = this.name == 'headerFormAnreise' ? 'minDate' : 'maxDate',
			instance = $( this ).data( 'datepicker' ),
			date = $.datepicker.parseDate(
				instance.settings.dateFormat ||
				$.datepicker._defaults.dateFormat,
				selectedDate, instance.settings 
			);
			dates.not( this ).datepicker( 'option', option, date );
			
			if( this.name == 'headerFormAnreise' ) {
				setTimeout( "$('#expressform input[name=headerFormAbreise]').focus();", 300 );
			} else if( this.name == 'headerFormAbreise' ) {
				setTimeout( "$('#expressform input[name=headerFormErwachsene]').focus();", 300 );
			}
		}	
	});
	
	
	$('.tischreservierung #datum, .beautyform #datum').datepicker({
		buttonImage: systemPath + '_img/base/calendar.png',
		showWeek: true,
		firstDay: 1,
		changeMonth: true,
		minDate: 0,
		changeYear: true,
		showAnim: 'drop',
		dateFormat: 'dd.mm.yy'
	});
	
	var expressDates = $('.expressform #anreise, .expressform #abreise').datepicker({
		buttonImage: systemPath + '_img/base/calendar.png',
		showWeek: true,
		firstDay: 1,
		changeMonth: true,
		changeYear: true,
		showAnim: 'drop',
		dateFormat: 'dd.mm.yy',
		onSelect: function( selectedDate ) {
			var option = this.name == 'anreise' ? 'minDate' : 'maxDate',
			instance = $( this ).data( 'datepicker' ),
			date = $.datepicker.parseDate(
				instance.settings.dateFormat ||
				$.datepicker._defaults.dateFormat,
				selectedDate, instance.settings 
			);
			expressDates.not( this ).datepicker( 'option', option, date );
			
			if( this.name == 'anreise' ) {
				setTimeout( "$('.expressform input[name=abreise]').focus();", 300 );
			} else if( this.name == 'abreise' ) {
				setTimeout( "$('.expressform input[name=erwachsene]').focus();", 300 );
			}
		}
	});
	
	
	var couponDates = $('.couponform #anreise, .couponform #abreise').datepicker({
		buttonImage: systemPath + '_img/base/calendar.png',
		showWeek: true,
		firstDay: 1,
		changeMonth: true,
		changeYear: true,
		showAnim: 'drop',
		dateFormat: 'dd.mm.yy',
		onSelect: function( selectedDate ) {
			var option = this.name == 'anreise' ? 'minDate' : 'maxDate',
			instance = $( this ).data( 'datepicker' ),
			date = $.datepicker.parseDate(
				instance.settings.dateFormat ||
				$.datepicker._defaults.dateFormat,
				selectedDate, instance.settings 
			);
			couponDates.not( this ).datepicker( 'option', option, date );
			
			if( this.name == 'anreise' ) {
				setTimeout( "$('.couponform input[name=abreise]').focus();", 300 );
			} else if( this.name == 'abreise' ) {
				setTimeout( "$('.couponform input[name=erwachsene]').focus();", 300 );
			}
		}
	});

	
	$('a#expressbooking').click( function() {
		var box = $(this).parent( 'div#booking' );

		if( box.hasClass( 'folded' ) ) {
			if( !isPlaceholderReplaced ) {
				$('#expressform input[placeholder], #expressform textarea[placeholder]').placeholder();
				isPlaceholderReplaced = true;
			}
			$('a#expressbooking').css({ color: '#C1131E', backgroundImage: 'none'});
			box.animate({ height: 165, marginTop: 225 }, 500, function() {
				
			});
			$('a#fixbooking').fadeOut( 500 );
			$('#expressform').fadeIn( 1000 );
			box.removeClass( 'folded' );
		} else {
			box.animate({ height: 45, marginTop: 345 }, 500, function() {
				$('a#expressbooking').css( { color: '#5D5D5A', backgroundImage: 'url(' + systemPath + '_img/base/arrow_up_brown.gif)' } );
			});
			
			$('a#fixbooking').fadeIn( 500 );
			$('#expressform').fadeOut( 300 );
			box.addClass( 'folded' );
		}
		
		return false;
	});
	
	$("#nivoSlider").nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        slices: 15, // For slice animations
        boxCols: 16, // For box animations
        boxRows: 8, // For box animations
        animSpeed: 800, // Slide transition speed
        pauseTime: 8000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        controlNav:false, // 1,2,3... navigation
        keyboardNav:false, // Use left & right arrows
        pauseOnHover:false, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8
    });

	// Gallery
	var galleries = $('.ad-gallery').adGallery({
		loader_image: systemPath + '_img/base/loader.gif',
		width: 500,
		height: 325,
		thumb_opacity: 0.7,
		start_at_index: 0,
		animate_first_image: false,
		animation_speed: 400,
		display_next_and_prev: true,
		display_back_and_forward: false,
		scroll_jump: 0,
		slideshow: {
			enable: true,
			autostart: true,
			speed: 4 * 1000,
			start_label: 'Start',
			stop_label: 'Stop',
			stop_on_scroll: false, // Should the slideshow stop if the user scrolls the thumb list?
			countdown_prefix: '',
			countdown_sufix: '',
			onStart: function() {},
			onStop: function() {}
		},
		effect: 'slide-hori', // 'slide-vert', 'resize', 'fade', 'none' or false
		enable_keyboard_move: true,
		cycle: true
	});
	
	// Pretty Photo
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal',
        padding: 40,
        opacity: 0.75,
        show_title: false,
        allow_resize: true,
		default_width: 700,
        counter_separator_label: '/',
		ie6_fallback: true,
		keyboard_shortcuts: true,
        theme: 'facebook',
        callback: function() {
        }
	});
	
	var url = window.location.hash;
	var hash = url.substring(url.indexOf('#')+1); // '#foo'
	
	
	// Attach EProspekt Link
	$("a.eprospekt").each(function() {
		$(this).fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'padding': 0,
			'autoDimensions': false,
			'scrolling' : 'no',
			'width': 980,
			'height': 760,
			'centerOnScroll': true,
			'href': systemPath + '_static/ebrochure/' + $(this).data('eprospekt') + '/index.html',
			'type': 'iframe',
			'overlayOpacity': 0.8,
			'overlayColor': '#000',
			'overlayShow': true
		});
	});
	
	if( $("a.eprospekt").length > 0 && ( hash == 'eprospekt' || hash == 'ebrochure' ) ) {
		$("a.eprospekt").trigger('click');
	}
	
	
	// Attach Slideshow Link
	$("a.slideshow").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'padding': 0,
		'autoDimensions': false,
		'scrolling' : 'no',
		'width': 800,
		'height': 500,
		'type': 'iframe',
		'overlayOpacity': 0.8,
		'overlayColor': '#000',
		'overlayShow': true
	});
	
	if( $("a.slideshow").length > 0 && ( hash == 'slideshow' ) ) {
		$("a.slideshow").trigger('click');
	}
	
	$('a#fixbooking,a.booking-button.fixedBooking').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'padding': 0,
		'autoDimensions': false,
		'scrolling' : 'auto',
		'width': 697,
		'height': 414,
		'type': 'iframe',
		'overlayOpacity': 0.8,
		'overlayColor': '#000',
		'overlayShow': true
	});
	
	// Site print
	$( '#sitePrint' ).click( function() {
		window.print();
		return false;
	});
	
	$('select#land,select#nationality').chosen( { no_results_text: noResultsMatchedText } );
	
	
	$('.flexslider').flexslider({
		animation: "slide",
		controlsContainer: ".flex-navigation",
		slideshow: false,
		animationDuration: 600,
		directionNav: true,
		controlNav: false,
		prevText: "<",
		nextText: ">",
		animationLoop: false
	});
	
	
	// Beauty Request
	$('.beautyform select#treatments, .couponform select#treatments, .bookingform select#treatments').bsmSelect({
		plugins: [$.bsmSelect.plugins.sortable()],
		title: beautyRequestPickerTitle,
		highlight: true,
		addItemTarget: 'bottom',
		animate: true,
		removeLabel: beautyRequestPickerRemove,
		extractLabel: function( $o ) { return $o.parents('optgroup').attr('label') + ":&nbsp;<b>" + $o.html() + "</b>"; }
	});

	
	// Add all placeholders
	$('div.form input[placeholder], div.form textarea[placeholder]').placeholder();
	$('input[placeholder]#siteSearch').placeholder();
	
	// Jobs
	$('div.jobsBox li.jobItem h3.jobTitle').click( function() {
		$('div.jobsBox li.jobItem div.jobDescription').each( function() {
			$(this).slideUp();
		});
		
		$('div.jobDescription', $(this).parent('li') ).slideToggle();
	});
	
	// Booking Form
	var roomCounter =  parseInt( $('.bookingform input#roomcounter').val() );
	if( !roomCounter || roomCounter <= 0 || roomCounter == '' ) {
		roomCounter = 1;
		$('.bookingform input#roomcounter').val( roomCounter );
	}
		
	// Add Room Link
	$('.bookingform div.roomline a.roomLink.addRoom').live( 'click', function() {
		
		// Increase room counter
		roomCounter++;
		$('.bookingform input#roomcounter').val( roomCounter );
		
		// Find the line, clone it, replace names and ids
		var roomLine = $(this).parent('div.roomline');
		var newRoomLine = roomLine.clone().hide();
		
		newRoomLine.attr( 'class', 'roomline room-' + roomCounter );
		
		newRoomLine.find('input.zimmer-anzahl').attr( 'id', 'zimmer-' + roomCounter + '-anzahl' );
		newRoomLine.find('input.zimmer-anzahl').attr( 'name', 'zimmer-' + roomCounter + '-anzahl' );
		newRoomLine.find('input.zimmer-anzahl').val( '' );
		
		newRoomLine.find('select.zimmer-auswahl').attr( 'id', 'zimmer-' + roomCounter + '-auswahl' );
		newRoomLine.find('select.zimmer-auswahl').attr( 'name', 'zimmer-' + roomCounter + '-auswahl' );
		newRoomLine.find('select.zimmer-auswahl').get(0).selectedIndex = 0;
		
		// Append the line
		newRoomLine.appendTo('dd.roomlines').slideDown('fast');
		
		// Hide all add room links
		$('.bookingform div.roomline a.roomLink.addRoom').hide();
		
		// Show the last add room link
		$('.bookingform div.roomline a.roomLink.addRoom').last().show();
		
		// Show all but the first remove room links
		$('.bookingform div.roomline a.roomLink.removeRoom:not(:first)').show();
		
		return false;
	});
	
	// Remove Room Link
	$('.bookingform div.roomline a.roomLink.removeRoom').live( 'click', function() {
		$(this).parent('div.roomline').slideUp( 'fast', function() { 
			$(this).remove(); 
		
			// Show the last add room link
			$('.bookingform div.roomline a.roomLink.addRoom').last().show();
		});
		return false;
	});
	
	
	var bookingDates = $('.bookingform #anreise, .bookingform #abreise').datepicker({
		buttonImage: systemPath + '_img/base/calendar.png',
		showWeek: true,
		firstDay: 1,
		changeMonth: true,
		changeYear: true,
		showAnim: 'drop',
		dateFormat: 'dd.mm.yy',
		onSelect: function( selectedDate ) {
			var option = this.name == 'anreise' ? 'minDate' : 'maxDate',
			instance = $( this ).data( 'datepicker' ),
			date = $.datepicker.parseDate(
				instance.settings.dateFormat ||
				$.datepicker._defaults.dateFormat,
				selectedDate, instance.settings 
			);
			bookingDates.not( this ).datepicker( 'option', option, date );
			
			if( this.name == 'anreise' ) {
				setTimeout( "$('.bookingform input[name=abreise]').focus();", 300 );
			}
		}
	});
	
	
	// Attach E-Cards links
	$("a.ecardgallerypopup").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'padding': 0,
		'autoDimensions': false,
		'scrolling' : 'no',
		'width': 800,
		'height': 600,
		'type': 'iframe',
		'overlayOpacity': 0.8,
		'overlayColor': '#000',
		'overlayShow': true
	});
	
	// Open E-Card if parameter is set
	if( eCardId != false ) {
		$.fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'padding': 0,
			'autoDimensions': false,
			'scrolling' : 'no',
			'width': 800,
			'height': 600,
			'type': 'iframe',
			'href': eCardUrl + '?view=' + eCardId,
			'overlayOpacity': 0.8,
			'overlayColor': '#000',
			'overlayShow': true
		});
	}
});
