/**
 * Disable IE6 background-image flickering
 */
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

/**
 * some jQuery code if needed
 */
$(document).ready(function(){

	if (typeof(jQuery.ui) !== 'undefined') {
	
		/**
		 * jquery-ui accordion (faq)
		 */
		$("#accordion").accordion( {
			header : 'h2',
			autoHeight : false,
			change : function(event, ui) { return false; },
			animated : 'easeInOut'
		});
	
		/**
		 * jquery-ui tabs (hotel detail)
		 */
		$("#hotel").tabs( {
			fx : {
				height : 'toggle',
				opacity : 'toggle',
				duration : 'fast'
			},
			select: function(event, ui) {
				if ($(ui.tab).attr('id') === 'tab-gal') {
					Shadowbox.open($("#hotelRight a.gallery").get(0));
					return false;
				}
				return true;
			}
		});

		/**
		 * jquery-ui datepicker (form input elements)
		 */
		$("input.datepicker").datepicker( {
			duration : '',
			dateFormat : 'dd.mm.yy',
			firstDay : 1,
			monthNames : [ 'Januar', 'Februar',	'M&auml;rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September',	'Oktober', 'November', 'Dezember' ],
			dayNamesMin : [ 'So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa' ]
		});
	}
	
	/**
	 * Reiseanfrage Formular ... Init Airlines on document ready
	 */
	if ($("form#reiseanfrage-form select#airport").length) {
		
		ets.updateAirlinesByAirportId($("form#reiseanfrage-form select#airport").val(),$('#hotel_id').val(),$('#special_id').val());
	}
	
	/**
	 * Reiseanfrage Formular bei Flughafen onchange die Airlines anpassen
	 */
	$("form#reiseanfrage-form select#airport").change(function () {
		ets.updateAirlinesByAirportId($(this).val(),$('#hotel_id').val(),$('#special_id').val());
    });
	
	$("input#no_flight").click(function () {
		$(this).parent("li").siblings("li").toggle();
	});
	
	/**
	 * bind reiseanfrage-form using 'ajaxForm'
	 */
	$('#reiseanfrage-form').ajaxForm({
		target : '#message-box',
		beforeSubmit : function(formData, jqForm, options) {
			var queryString = $.param(formData);
			$('html, body').animate( {
				scrollTop : 0
			}, 'slow');
			return true;
		},
		success : function(responseText, statusText){
			if (responseText.indexOf('<ul>') !== 0) {
				$('#reiseanfrage-form').slideUp('slow');
			}
		}
	});
	
});

var ets;
if(!ets) ets = {};

ets.updateAirlinesByAirportId = function(airportId,hotelId,specialId) {
	$.post("/region/airlines/",
			
		   /**
		    * POST vars
		    */
		   { airportId: airportId,
			 hotelId: hotelId? hotelId : 0,
			 specialId: specialId? specialId : 0 },
		   
		   /**
		    * Json Response Handling
		    */
		   function(j){
			   if (typeof(j['airlines']) == 'object') {
				   $("form#reiseanfrage-form select#airline").empty();
				   for (var val in j['airlines']) {
					   $("form#reiseanfrage-form select#airline").append('<option label="'+j['airlines'][val]+'" value="'+val+'">'+j['airlines'][val]+'</option>');
				   }
				   $("form#reiseanfrage-form select#airline option[value='"+j["preferred_airline_id"]+"']").attr('selected','selected');
		       }
	       },
	       "json");
};

ets.bindLayerForm = function() {
	$('.layer-form').ajaxForm({
		target : '#sb-content .message-box',
		beforeSubmit : function(formData, jqForm, options) {
			var queryString = $.param(formData);
			$('#sb-content').animate( {
				scrollTop : 0
			}, 'slow');
			return true;
		},
		success : function(responseText, statusText){
			if (responseText.indexOf('<h3>') !== 0) {
				$('#sb-content p.description, #sb-content .layer-form').hide();
				$('#sb-content div.layer-inner').removeClass('status-warn').addClass('status-ok');
			} else {
				$('#sb-content div.layer-inner').addClass('status-warn');
			}
			ets.initShadowboxCloseLinks();
		}
	});
};

/**
 * shadowbox close-links
 */
ets.initShadowboxCloseLinks = function() {
	$('.sb-close').click(function(event) {
		event.preventDefault();
		Shadowbox.close();	
	});
}
