/**
 * maxon jQuery JavaScript
 *
 * @since 36
 * @require jQuery 1.3
 */

/**
 * Document ready
 *
 * @since 36
 * @require jQuery 1.3
 */
$(document).ready(function(){
	var iconDir = (Url.site+'/images/icons').replace( /\/\//, '/' );

	// js yes
	$('.no-js').removeClass('no-js');

	// important message scroller
	if ($('#important-message').length) {
		var itemNum = $('#important-message .item').length;
		if (1 < itemNum) {
			var maxScroll = 30*itemNum - 30;
			$.timer(5000, 
				function (timer) {
					if (maxScroll == $('#important-message').scrollTop()) {
						// "rewind"
						$('#important-message').animate({"scrollTop": "-="+maxScroll+"px"},300);
						} else {
						// "play"
						$('#important-message').animate({"scrollTop": "+=30px"},500);
						}
		//			timer.stop();
					}
				);
			}
		}
	// last minute offers tabs
	if ($('#last-minute-offers-tabs').length) {
		
		// count to offers
		var countTo = $('#link_to_to_airport .to_airport').length;
		// count from offers
		var countFrom = $('#link_to_from_airport .from_airport').length;
		if (0 < countTo) {
			// if non empty add selected class
			$('#last-minute-offers-tabs li:first').addClass('selected');
			// hide the second tab content
			$('#link_to_from_airport').hide();
			} else if (0 < countFrom) {
			// if non empty add selected class
			$('#last-minute-offers-tabs li:last').addClass('selected');
			// hide the first tab content
			$('#link_to_to_airport').hide();
			}
		$('#last-minute-offers-tabs li:first').click(
			function() {
				$('#last-minute-offers-tabs li:first').removeClass('selected');
				$('#last-minute-offers-tabs li:last').removeClass('selected');
				$('#last-minute-offers-tabs li:first').addClass('selected');
				$('#link_to_from_airport').hide();
				$('#link_to_to_airport').show();
				return false;
				}
			);
		$('#last-minute-offers-tabs li:last').click(
			function() {
				$('#last-minute-offers-tabs li:first').removeClass('selected');
				$('#last-minute-offers-tabs li:last').removeClass('selected');
				$('#last-minute-offers-tabs li:last').addClass('selected');
				$('#link_to_from_airport').show();
				$('#link_to_to_airport').hide();
				return false;
				}
			);
		}
	/**
	 * Closable filedsets
	 * @since 64
	 */
	$('fieldset.closable').each(function(){
		if ( 0 <= $( '.closable_content', this ).size() ) {
			$( '> :not(legend, .closable_content)', this ).wrapAll(
				'<div class="closable_content"></div>'
			);
		}
		$('> legend', this ).click(function(){
			$( '.closable_content', $(this).parent() ).slideToggle();
		});
		if ( $(this).hasClass( 'closed' ) ) {
			$( '.closable_content', this ).hide();
		}
	});

	/**
	 * Help toggler
	 * @since
	 */
	$('#help-box #help-content').hide();
	$('#help-box .toggler').click(function(){
		$(this).toggleClass( 'toggler-open' );
		$('#help-box #help-content').slideToggle();
		return false;
	});

	/**
	 * FormElement Description toggler
	 * @since
	 */
	$('form .description').hide();
	$(':input', $('form .description').parent() ).each(function(){
		if ( $(this).hasClass( 'dont-hide-description' ) ) {
			$( '.description', $(this).parent() ).show();
		}
		else {
			var imgUrl = iconDir+'/help.png';
			var $img = $( '<img src="'+imgUrl+'" alt="'+t('Help')+'" id="'+$(this).attr('id')+'-help" class="showhelp" />' );
			$('label[for="'+$(this).attr('id')+'"]').after($img);
			var $inp = $(this);
			$('#'+$(this).attr('id')+'-help').click(function(){
				var $desc = $inp.parent().find( '.description' ).toggle();
				$img.css('opacity',( $desc.is(':visible') ? 0.7 : 1 ));
			});

		}
	});

/**
 * FormTurbo
 */

	/**
	 * DatePicker
	 * @since
	 */
	if ( $.datepicker.regional[ Lang.lang ] !== undefined ) {
		$.datepicker.setDefaults($.datepicker.regional[ Lang.lang ]);
	}
	$('input.date, input.dateISO')
		.attr('autocomplete','off')
		.datepicker({
			showMonthAfterYear: true,
			changeYear: true,
			changeMonth: true,
			defaultDate: null,
			minDate: +1,
			yearRange: '-0:+1',
			dateFormat: 'yy-mm-dd'
		});

	/**
	 * PassangerList
	 */
	var passangerCount = $('#add_passangers_form .passangers-cover fieldset').size();
	if ( 0 < passangerCount ) {
		var tabsSelect = '#add_passangers_form .passangers-cover';
		/**
		 * Event.ChildChanger
		 * @since
		 */
		var childChange = function(){
			var wrapper = $(
				'label[for$="child_seat"],select[name$="[child_seat]"]',
				$(this).parents('fieldset:first')
			).parents('dt,dd');
			if ( $(this).is(':checked' ) ) {
				wrapper.show();
			}
			else {
				wrapper.hide();
			}
		};
		$(tabsSelect+' fieldset input.passanger_child:checkbox')
			.bind( 'change focus blur select click', childChange)
			.change();

		/**
		 * MultiplePassanger
		 */
		if ( 1 < passangerCount )
		{
			$(tabsSelect)
				.prepend('<ul id="passanger-tabs" class="tabs passanger-tabs"></ul>');
			$(tabsSelect+' fieldset').each(function(i,el){
				$('#passanger-tabs').append('<li><a href="#'+this.id+'">'+(i+1)+'</a></li>');
				$(this).data('panelIndex', i);
			});
			var $nameInputs = $(tabsSelect+' input.passanger_name');
			$nameInputs.addClass('required');
			/**
			 * Event.nameChange
			 * @since
			 * @param Event
			 * @return void
			 */
			var nameChange = function (ev) {
				var tabIndex = null;
				$nameInputs.each(function(i,el){
					$( 'ul.tabs li a[href="#' + $(this).parents('fieldset').attr('id') +'"]' )
						.html( $(this).val() == '' ? i+1 : $(this).val() );
				});
			};
			$nameInputs.change(nameChange);

			/**
			 * Preparate passanger clonable datas
			 * @since
			 */
			var $addressInputs = $(tabsSelect+' fieldset input.passanger_address').addClass('clonable');
			$addressInputs.tCloner({
				tabsSelect: tabsSelect,
				dialogTitle: t('Choose existing address'),

				cloneSrc: iconDir+'/'+'page_go.png',
				cloneClass: 'clone_address',

				copySrc:  iconDir+'/'+'page_paste.png',
				copyClass: 'copy_address',
				copyAlt: t('Copy address')

			});
			var $mobileInputs = $(tabsSelect+' fieldset input.passanger_cell').addClass('clonable');
			$mobileInputs.tCloner({
				tabsSelect: tabsSelect,
				dialogTitle: t('Choose existing telephone'),

				cloneSrc: iconDir+'/'+'page_go.png',
				cloneClass: 'clone_mobile',

				copySrc:  iconDir+'/'+'page_paste.png',
				copyClass: 'copy_mobile',
				copyAlt: t('Copy mobile')
			});


			// force to use tCloner, if the empty address input is focused
			$addressInputs.focus(function() {
				if (!$(this).attr('data-paste-helper-active')) {
					$(this).attr('data-paste-helper-active',1);
				}
				var pasteHelper = $(this).attr('data-paste-helper-active');
				if (!$(this).val()) {
					if (1 == pasteHelper) {
						$(this).parent().find('.copy_address').click();
						$(this).attr('data-paste-helper-active','0');
					} else {
						$(this).attr('data-paste-helper-active','1');
					}
				}

			});





			/**
			 * Preparate passanger submit buttons
			 * @since
			 */
			$('#add_passangers_form #passangers .button').click(function(){
				var ok = true;
				var goTo = null;
				$(
					tabsSelect+' fieldset input.passanger_name, '
					+tabsSelect+' fieldset input.passanger_address'
				).each(function(i,el){
					if ( $(this).val() == '' ) {
						ok = false;
						if ( null == goTo ) {
							goTo = $(this).parents('fieldset.passanger:first').data('panelIndex');
						}
					}
				});
				if ( !ok ) {
					$(tabsSelect).tabs('select', goTo );
					$('input',$(tabsSelect+' .passanger').eq(goTo) ).valid();
					return false;
				}
			});
			$(tabsSelect).tabs({ fx: {
				opacity: 'toggle'
			} });
		}
	};

	/**
	 * Event.InvoiceChanger
	 * @since
	 */
	var invoiceChange = function(){
		var $wrappers = $(this).parents('fieldset:first').find(
			'label[for^="invoice_"]:not([for$="_require"]),:input[name^="invoice_"]:not([name$="_require"])'
		).parents('dt,dd');
		if ( $(this).is(':checked' ) ) {
			$wrappers.show();
		}
		else {
			$wrappers.hide();
		}
	};
	$('#invoice_require:checkbox').bind( 'change focus blur select click', invoiceChange ).change();

	/**
	 * Init validator
	 * @since
	 */
	jQuery.validator.setDefaults({success:'valid'});
	$('form input:hidden').removeClass('required');
	$('form.validate').each(function(){
		$(this).validate();
	});

});

/* end */

