// Globale Parameter definieren
var globalVars=new Array();
var selector=new String();

var processAjaxRequestsURL=new Array()
processAjaxRequestsURL['de-de']='de/meta/ajax';
processAjaxRequestsURL['fr-fr']='fr/meta/ajax';
processAjaxRequestsURL['de-at']='at/meta/ajax';

$(document).ready(function() {
	
	//
	// DOM-Manipulationen Anfang
	//
	
	// IE6-Helper
	if($.browser.msie && $.browser.version=='6.0')
	{
		// Hover-Indikator
		$('div.angebote ul li').hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	}
	
	// Tabellen mit Zeilenlinien
	$('table.row_lines').each(function() {
		var table=$(this);
		table.find('tr:odd').addClass('odd');
		table.find('tr:even').addClass('even');
	});
	
	// Tabellen letzte Spalte
	$('table tr').each(function() {
		$(this).find('th:last, td:last').addClass('last');
	});
	
	// Tabelle: Flughafen-Termin- und Preise
	$('div.flughafen_termine_preise table tr:odd').addClass('odd');
	$('div.flughafen_termine_preise table tr:even').addClass('even');
	
	// Tabelle: Ferientermine
	$('div.ferientermine table tr:odd').addClass('odd');
	$('div.ferientermine table tr:even').addClass('even');
	
	
	//
	// jQuery File Upload
	//
	
	if($('#fileupload').length) $('#fileupload').fileupload(
	{
		dataType: 'json',
		sequentialUploads: true
	});
	
	// Load existing files:
	$('#fileupload').each(function () {
		var that = this;
		$.getJSON(this.action, function (result) {
			if (result && result.length) {
				$(that).fileupload('option', 'done')
				.call(that, null, {result: result});
			}
		});
	});
	
	
	//
	// Formulare
	//
	
	// Formular Reservierungscode
	$('#reservierungscode_form').submit(function() {
		if($('#reservierungscode_checkform').val()=='1')
		{
			checkReservierungscode();
			return false;
		}
	});
	
	
	//
	// Tabs
	//
	
	if(!globalVars['activeTab']) globalVars['activeTab']='tab_1';

	$('div#tabs ul.tab_triggers li a').click(function() {
		
		// Parameter setzen
		var oldTab=globalVars['activeTab'];
		var newTab=this.id.replace(/_trigger/, '');
		
		// Abbrechen falls der aktive Reiter nicht wechselt
		if(newTab==oldTab) return false;
		
		// CSS-Klassen setzen
		$('a#'+newTab+'_trigger').parent().addClass('active');
		$('a#'+oldTab+'_trigger').parent().removeClass('active');
		
		// Ein- / Ausblenden
		$('div#'+oldTab).css('display', 'none');
		$('div#'+newTab).css('display', 'block');
		
		// Neuen aktiven Tab setzen
		globalVars['activeTab']=newTab;
		
		return false;	
	});
	
	
	//
	// jQuery-Tools: Tooltips
	//
	
	// Tooltip vorbereiten
	$('.show_tooltip').each(function(index) {
		
		// Alle Links anpassen
		if(this.href)
		{
			// href-Attribut entfernen ...
			$(this).data('href', this.href).removeAttr('href').addClass('link');
			
			// ... und bei Klick wieder einfügen
			$(this).click(function() {
				$(this).attr('href', $(this).data('href'));
			});
		}
	});
	
	// Tooltip Initialisieren
	$('.show_tooltip').tooltip({
		tipClass: 'tooltip',
		position: 'bottom right',
		offset: [0, 0],
		effect: 'toggle', // slide | toggle | fade
		predelay: 0,
		delay: 0
	});
	
	
	//
	// jQuery-Tools: Overlays
	//
	
	// Overlay deutsche Startseite
	if($('body').hasClass('de-de') && $('#overlay_start_de_trigger').length)
	{
		var overlay_start_de_trigger=$('#overlay_start_de_trigger');
		var overlay_start_de=$('#overlay_start_de');
		
		$('body').append(overlay_start_de);
	
		// Overlay deutsche Startseite: aktivieren
		overlay_start_de_trigger.overlay({
			target: '#overlay_start_de',
			mask: {
				color: 'black',
				loadSpeed: 200,
				opacity: 0.7
			},
			speed: 400,
			closeOnClick: true
		});
	
		// Overlay Autostart
		if(open_overlay_start_de) overlay_start_de_trigger.click();
	
		// Overlay deutsche Startseite: Formular anpassen und Fehlerprüfung
		var overlay_start_de_form=overlay_start_de.find('form');
		overlay_start_de_form.removeClass('validation');
		overlay_start_de_form.submit(function(event) {
			var emailInput=overlay_start_de_form.find('#Form_E-Mail');
			var email=emailInput.val();
			var emailContainer=emailInput.parents('p');
		
			if(email=='' || email.search('@')==-1 || email.search('.')==-1) {
				emailContainer.addClass('error');
				event.preventDefault();
			}		
		});	
	}
	
	
	//
	// Formular-Validierung
	//
	
	var form=$('form.validation');
	if(form.length) $.getScript('assets/site/jquery.formValidaton.js', function() {
		form.formValidation();
	});
	
	
	//
	// E-Mails entschlüsseln
	//
	
	// Expression erweitern
	$.extend($.expr[':'], {
		mailToLink: function (obj) {
			return obj.href.match(/^mailto\:/);
		}
	});
	
	// Mailto-Links anpassen
	$('a:mailToLink').each(function() {
		this.href=this.href.split("?")[0].replace("ANTI[at]SPAMBOT", "@");
		$(this).html($(this).html().replace("TOBMAPS]ta[ITNA", "@"));
	});


	//
	// jQuery SmoothScroll | Version 11-06-11
	//
	
	$('a[href*=#]').click(function() {

		// skip SmoothScroll on links inside sliders or scroll boxes also using anchors or if there is a javascript call
		if($(this).parent().hasClass('scrollable_navigation') || $(this).attr('href').indexOf('javascript')>-1) return;

		// duration in ms
		var duration=1000;

		// easing values: swing | linear
		var easing='swing';

		// get / set parameters
		var newHash=this.hash;
		var oldLocation=window.location.href.replace(window.location.hash, '');
		var newLocation=this;
		
		// make sure it's the same location		
		if(oldLocation+newHash==newLocation)
		{
			// get target
			var target=$(this.hash+', a[name='+this.hash.slice(1)+']').offset().top;

			// adjust target for anchors near the bottom of the page
			if(target > $(document).height()-$(window).height()) target=$(document).height()-$(window).height();			
			
			// set selector
			if($.browser.safari) var animationSelector='body:not(:animated)';
			else var animationSelector='html:not(:animated)';
			
			// animate to target and set the hash to the window.location after the animation
			$(animationSelector).animate({ scrollTop: target }, duration, easing, function() {

				// add new hash to the browser location
				window.location.href=newLocation;
			});

			// cancel default click action
			return false;
		}
	});
	
});


//
// Funktionssammlung
//

function show(id, src)
{
	$(id).attr('src', src);
	$(id).fadeIn('fast');
}

function hide(id)
{
	$(id).fadeOut('fast');
}

function setIframeHeight()
{
	var frameURL=$('iframe#live_buchung').attr('src');
	//var test=document.getElementById('live_buchung').contentWindow.location.href;
	//console.log(frameURL);
}

// Anzeigen des Tabs „Weitere Infos“ und des Eintrags „HanseMerkur Reiseversicherung“
function openRRKV(anker)
{
	// Tab aktivieren
	$('#tabs .tab_triggers .weitereinfos a').click();
	
	// Selectbox-IDs festlegen
	var selectboxID=new Array();
	selectboxID['de-de']=443;
	selectboxID['de-at']=444;
	
	// Selectbox-Auswahl
	$('#infoSelector').val(selectboxID[lang]).change();
}


//
// Weitere Informationen
//

function toggleInfo(id)
{
	// Parameter setzen
	var oldInfo=globalVars['activeInfo'];
	var newInfo='info_'+id;

	// Abbrechen falls der aktive Reiter nicht wechselt
	if(newInfo==oldInfo) return false;
	
	// Ein- / Ausblenden
	$('div#'+oldInfo).css('display', 'none');
	$('div#'+newInfo).css('display', 'block');
	
	// Neue aktive Info setzen
	globalVars['activeInfo']=newInfo;
}


//
// Reservierungscode
//

function checkReservierungscode()
{
	// Prüfen ob ein Code eingegeben wurde
	if($('#reservierungscode').val()=='Bitte hier eingeben' || $('#reservierungscode').val()=='')
	{
		// Hinweis ausgeben
		$('div.reservierungscode p.info').html('Bitte geben Sie einen Reservierungscode ein.').addClass('red strong');
	}
	// Reservierungscode per Ajax abfragen
	else
	{
		// Ajax-Abfrage
		$.get(processAjaxRequestsURL[lang], { type: 'checkReservierungscode', code: $('#reservierungscode').val() }, function(data) {
			
			// Falls der Code nicht existiert
			if(data=='false') 
			{
				var hinweis=new Array();
				hinweis['de-de']='Der eingegebene Reservierungscode ist nicht hinterlegt. Bitte prüfen Sie Ihre Eingabe.';
				hinweis['de-at']='Der eingegebene Reservierungscode ist nicht hinterlegt. Bitte prüfen Sie Ihre Eingabe.';
				hinweis['fr-fr']='Ce code n´est pas enregistré, merci de le vérifier.';
				
				// Hinweis ausgeben
				$('div.reservierungscode p.info').html(hinweis[lang]).addClass('red strong');
			}
			// Falls der Code existiert
			else
			{
				var aktionsURL=data;

				// Formular-Aktion setzen
				$('#reservierungscode_form').attr('action', aktionsURL);
				
				// Prüffeld setzen
				$('#reservierungscode_checkform').val('0')
				
				// Formular senden
				$('#reservierungscode_form').submit();
			}
			
		});
	}
}

//
// Funktionen Divers
//

// Tag-Name ermitteln
$.fn.tagName=function() {
    return this.get(0).tagName.toLowerCase();
}
