/**
 * 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();
	});
	
	$("select#travel_type").change(function () {
		var flightElements = $(this).parent("li").nextAll("li");
		if ($(this).find("option:selected").val() == 'hotel_only') {
			flightElements.hide();
		} else {
			flightElements.show();
		}
	});
	
	/**
	 * Reisekombinationsfelder ein- und ausblenden
	 */
	$("select#kombi_usage").change(function () {
		var kombiElements = $(this).parent("li").nextAll("li");
		var kombiUsage = $(this).find("option:selected").val();
		if (kombiUsage == 'NULL') {
			kombiElements.hide();
		} else {
			kombiElements.show();
			ets.updateHotelsByLocationId($("select#kombi_location").find("option:selected").val());
		}
	});
	// initially hidden
	$("select#kombi_usage").parent("li").nextAll("li").hide();	
	
	/**
	 * Reisekombination update hotel on location change
	 */
	$("select#kombi_location").change(function () {
		ets.updateHotelsByLocationId($(this).find("option:selected").val());
	});

	/**
	 * Reisekombination update roomCategories on hotel change
	 */
	$("select#kombi_hotel").change(function () {
		ets.updateHotelRoomCategoriesByHotelId($(this).find("option:selected").val());
	});
	
	/**
	 * Reiseanfrage Person / Firma Felder ein- und ausblenden
	 */
	$("#reiseanfrage-form select#title").change(function () {
		var title = $(this).find("option:selected").val();
		if (title == 'company') {
			$("#reiseanfrage-form").find("#firstname, #lastname").parent("li").hide();	
			$("#reiseanfrage-form").find("#company, #company_contact").parent("li").show();	
		} else {
			$("#reiseanfrage-form").find("#company, #company_contact").parent("li").hide();	
			$("#reiseanfrage-form").find("#firstname, #lastname").parent("li").show();	
		}
	});
	// initially non company
	$("#reiseanfrage-form").find("#company, #company_contact").parent("li").hide();	
	
	/**
	 * Reiseanfrage Beste Erreichbarkeit ein- und ausblenden
	 */
	$("#reiseanfrage-form select#contact_type").change(function () {
		var contactElements = $("#reiseanfrage-form").find("#contact_time").parent("li");
		var contactType = $(this).find("option:selected").val();
		if (contactType == 'phone') {
			contactElements.show();	
		} else {
			contactElements.hide();	
		}
	});
	
	/**
	 * bind reiseanfrage-form using 'ajaxForm'
	 */
	if ($("#reiseanfrage-form").length) {
		$('#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");
};

/**
 * request hotels from server
 */
ets.updateHotelsByLocationId = function(locationId) {
	$("form#reiseanfrage-form select#kombi_hotel").empty();
	$.post("/region/hotels/",
			/**
		    * POST vars
		    */
		   { locationId: locationId },
		   
		   /**
		    * Json Response Handling
		    */
		   function(j){
			   if (typeof(j['hotels']) == 'object') {
				   for (var val in j['hotels']) {
					   $("form#reiseanfrage-form select#kombi_hotel").append('<option label="'+j['hotels'][val]+'" value="'+val+'">'+j['hotels'][val]+'</option>');
				   }
				   ets.updateHotelRoomCategoriesByHotelId($("form#reiseanfrage-form select#kombi_hotel").find("option:selected").val());
		       }
	       },
	       "json");
};

/**
 * request roomCategories from server
 */
ets.updateHotelRoomCategoriesByHotelId = function(hotelId) {
	$("form#reiseanfrage-form select#kombi_hotel_room_category").empty();
	$.post("/region/hotel-room-categories/",
			/**
		    * POST vars
		    */
		   { hotelId: hotelId },
		   
		   /**
		    * Json Response Handling
		    */
		   function(j){
			   if (typeof(j['hotel_room_categories']) == 'object') {
				   for (var val in j['hotel_room_categories']) {
					   $("form#reiseanfrage-form select#kombi_hotel_room_category").append('<option label="'+j['hotel_room_categories'][val]+'" value="'+val+'">'+j['hotel_room_categories'][val]+'</option>');
				   }
		       }
	       },
	       "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();	
	});
}

