/* -------------------------------------------------------------
 *	@Project: Frankfurt Airport City
 *	@Author: Florian Ludwig (triplesense.de)
 *	@Created: 2008-11-13
 *	@Last edited: Florian Ludwig, 2008-12-12
 * ------------------------------------------------------------- */

function resetColoredTabs()
{
	$(".teaser").each(
		function ( i, dE )
		{
			$(dE).find(".slider-tab").each(
				function ( index, domElement )
				{
					var intIndex = index + 1;
					$(domElement)
						.removeClass("color" + intIndex + "-opened")
						.addClass("color" + intIndex + "-closed");
				}
			);
		}
	);
}

jQuery.fn.clearForm = function() {
	return this.each(function() {
		jQuery('input,select,textarea', this).clearFields();
	});
};

jQuery.fn.clearFields = jQuery.fn.clearInputs = function() {
	return this.each(function() {
		var t = this.type, tag = this.tagName.toLowerCase();
		if (t == 'text' || t == 'password' || tag == 'textarea')
			this.value = '';
		else if (t == 'checkbox' || t == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

jQuery.fn.resetForm = function() {
	return this.each(function() {
		// guard against an input with the name of 'reset'
		// note that IE reports the reset function as an 'object'
		if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
			this.reset();
	});
};

//
// Funktion zum wechseln der Themenfelder
//
	function rolliereThemenfelder( )
	{
		var anzahlthemenfelder = 3;
		var verfuegbarefelder = $('.thf');
		
		if ( verfuegbarefelder && verfuegbarefelder.length > 0 )
		{
  		if ( verfuegbarefelder.length < anzahlthemenfelder ) anzahlthemenfelder = verfuegbarefelder.length;

  		var sec = 0;
  		var mostleft = verfuegbarefelder.length;
  
  		for ( var i=0 ; i<anzahlthemenfelder && sec < 20 ; i++ )
  		{
  				// Zufallszahl generieren
  				var zufall = Math.round(Math.random()*(verfuegbarefelder.length-1));
  				
  				var currentfield = $('#'+verfuegbarefelder[zufall].id);
  				if ( currentfield.css("display") == 'none' )
  				{
  					// Feld anzeigen...
  					if ( zufall < mostleft ) mostleft = zufall;
  
  					currentfield.css("display","inline");
  					currentfield.removeClass("nomarginleft");
  				}
  				else
  				{
  					// Andere Zahl generieren
  					i--;
  				}
  				sec++;						
  		}
  		
  		$('#'+verfuegbarefelder[mostleft].id).addClass("nomarginleft");
		}
}



$(document).ready(
	function ()
	{
	
		$("#weather").load("/include/airportcity.com/jsp/dyndata/wetter/wetter.jsp");
		
		// define elements getting tooltipped
		$("a.tooltipp").tooltipp();
		
		// define elements getting png-fixed
		$.ifixpng('/cms/airportcity.com/images/global/pixel.gif');
		$('img[@src$=.png], div, span, input, li').ifixpng();
		
		// show/hide nav-description
		$(".nav-level1 a").hover(
			function ()
			{
				var thisId = $(".nav-level1 a").index(this) + 1;
				$(".nav-description p").hide();
				$(".breadcrumb").hide();
				$(".nav-description").show();
				$("#desc" + thisId).show();
			},
			function ()
			{
				$(".nav-description").hide();
				$(".breadcrumb").show();
			}
		);
		
		// language-selector
		$(".language-select ul li ul").hide();
		var $langTimer;
		$(".language-select").hover(
			function ()
			{
				clearTimeout($langTimer);
				$(".language-select ul li ul").slideDown();
			},
			function ()
			{
				$langTimer = setTimeout(function(){$(".language-select ul li ul").slideUp();}, 500);
			}
		);
		
		// hide all
		$(".slider-content, .tab-content-holder, .tab-content").hide();
		resetColoredTabs();
		
		$(".maincol-right > .slider-opener").click(
			function ()
			{
				if ( ! $(this).hasClass("opened") )
				{
					// close open tab
					$(".maincol-right > .slider-opener.opened").next().slideUp();
					$(".maincol-right > .slider-opener.opened").removeClass("opened");
					// open new tab
					$(this).next().slideDown();
					$(this).toggleClass("opened");
				}
				return false;
			}
		);
		
		$("form .slider-opener").click(
			function ()
			{
				$(this).next().slideToggle();
				$(this).toggleClass("opened");
				return false;
			}
		);
		
		$(".teaser").each(
			function ( i, dE )
			{
				$(dE).find(".slider-tab").each(
					function ( index, domElement )
					{
						var intIndex = index + 1;
						
						// bind click-event to tabs
						$(domElement).click(
							function ()
							{
								if ( ! $(this).hasClass("color" + intIndex + "-opened") )
								{
									// first reset css-classes of all tabs
									resetColoredTabs();
									// get current open content and close it
									$(this).parent().find(".tab-content-holder .tab-content.active")
										.slideUp("slow", function () { $(this).parent().css("display", "none"); })
										.removeClass("active");
									// get current tab and deactivate it
									$(this).parent().find(".slider-tab.active").removeClass("active");
									// set next tab
									$(this).addClass("active color" + intIndex + "-opened");
									// open next content
									$(this).prev().css("display", "block");
									$(this).prev().children(".tab-content")
										.slideDown()
										.addClass("active");
								}
								return false;
							}
						);
						
						// open first tab in all teasers
						if ( intIndex == 1 ) {
							$(domElement).addClass("active color" + intIndex + "-opened");
							$(domElement).prev().css("display", "block");
							$(domElement).prev().find(".tab-content")
								.show()
								.addClass("active");
						}
					}
				);
			}
		);
		
		$("input.button").each(
			function ()
			{
				var cssClasses = $(this).attr("class");
				var btnType = $(this).attr("type");
				var val = $(this).val();
				
				if ( $(this).attr("type").toLowerCase() != "button" )
				{
					cssClasses += " " + $(this).attr("type").toLowerCase();
				}
				
				$(this).replaceWith(
					"<button class=\"" + cssClasses + "\" type=\"" + btnType + "\">" +
					"<span class=\"text\">" + val + "</span>" +
					"<span class=\"br\"></span>" +
					"</button>"
				);
				
			}
		);
		
		$.dpText = {
			TEXT_PREV_YEAR		:	'Vorheriges Jahr',
			TEXT_PREV_MONTH		:	'Vorheriger Monat',
			TEXT_NEXT_YEAR		:	'Nächstes Jahr',
			TEXT_NEXT_MONTH		:	'Nächster Monat',
			TEXT_CLOSE			:	'Schließen',
			TEXT_CHOOSE_DATE	:	'Datum auswählen'
		}
		
		$("input.jquery-datepicker").datePicker(
			{
				displayClose: true,
				showYearNavigation : false,
				clickInput : true
			}
		);
		
		$(".travelbooking:not(#travelbooking-selection)").hide();
		$("#travelbooking-selection input:radio").each(
			function ( i )
			{
				$(this).click(
					function ()
					{
						var today = new Date();
						if ( today.getDate() < 10 )
						{
							var day = "0" + today.getDate();
						} else {
							var day = today.getDate();
						}
						if ( today.getMonth() < 10 ) {
							var month = "0" + today.getMonth();
						} else {
							var day = today.getMonth();
						}
						var year = today.getFullYear();
						var startDate = day + "." + month + "." + year;
						
						$(".travelbooking:not(#travelbooking-selection)").hide();
						
						$(".travelbooking:not(#travelbooking-selection)").find('input.jquery-datepicker').each(
							function ()
							{
								var selectedDate = $(this).dpGetSelected();
								$(this).dpSetStartDate(startDate);
								if ( $(this).val() != "Datum eingeben" && $(this).val() != "Enter date" ) {
									$(this).dpSetSelected( $(this).val(), false );
								}
							}
						);
						
						$(".travelbooking:not(#travelbooking-selection)").resetForm();
						
						$("#travelbooking" + (i+1)).show();
					}
				);
			}
		);
		$("#travelbooking-selection input:radio:first").click();
		
		
		$('input.jquery-datepicker.startDate').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$(this).parent().children(".endDate").dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		
		$('input.jquery-datepicker.endDate').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$(this).parent().children(".startDate").dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);
		
		$('#return').bind('click', function() {
			var $this = $(this);
			$('#date_return').dpSetDisabled(!$this.attr('checked'));			
			
			var $timeInput = $this.parent().children('.time');
			if(!$this.attr('checked')) {
				$timeInput.attr('disabled', 'disabled')				
			}else{
				$timeInput.removeAttr("disabled")
			}
		});
	
		
		
		// replace links containing css-class "iframe" with iframe-tag
		$("a.iframe").each(
			function (i) {
				$(this).replaceWith('<iframe src="' + this.getAttribute('href') + '" frameborder="0" scrolling="no"></iframe>');
			}
		);
		// Flackern von Images im IE6 unterdrücken...
    try {
    	document.execCommand("BackgroundImageCache", false, true);
    } catch(err) {}

    rolliereThemenfelder();
	
	if (typeof(airlines)!="undefined") 
	{
		$("#airline").autocomplete(airlines,
			{
				autoFill: true,
				matchContains:true,
				formatItem: function(row, i, max) {return row.n;},
				formatMatch: function(row, i, max) {return row.n;},
				formatResult: function(row) {return row.n;}
			}
		).result(function(event, item) {
		  location.href = item.i;
		});
	}

if (typeof(shop)!="undefined") 
	{
		$("#shops").autocomplete(shop,
			{
				autoFill: true,
				matchContains:true,
				formatItem: function(row, i, max) {return row.n;},
				formatMatch: function(row, i, max) {return row.n;},
				formatResult: function(row) {return row.n;}
			}
		).result(function(event, item) {
		  location.href = item.i;
		});
	}
	
if (typeof(restaurant)!="undefined") 
	{
		$("#restaurants").autocomplete(restaurant,
			{
				autoFill: true,
				matchContains:true,
				formatItem: function(row, i, max) {return row.n;},
				formatMatch: function(row, i, max) {return row.n;},
				formatResult: function(row) {return row.n;}
			}
		).result(function(event, item) {
		  location.href = item.i;
		});
	}
	
if (typeof(service)!="undefined") 
	{
		$("#services").autocomplete(service,
			{
				autoFill: true,
				matchContains:true,
				formatItem: function(row, i, max) {return row.n;},
				formatMatch: function(row, i, max) {return row.n;},
				formatResult: function(row) {return row.n;}
			}
		).result(function(event, item) {
		  location.href = item.i;
		});
	}	
	
    // Definition für Standardwerte...

    defaultValues = [];
    $(".default_value").each(function(i){
        defaultValues[i] = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == defaultValues[i]) {
                $(this).val("");
            }
        });
        $(this).blur(function(){
            if ($.trim($(this).val()) == "") {
                $(this).val(defaultValues[i]);
            }
        });
	 });


	}
	
);