// JavaScript Document

$(document).ready( function(){
														
	$("#nav TABLE TABLE TD").each( function(){
		$(this).replaceWith($(this).html());
	});	
	
	$("#nav TABLE TABLE").each( function(){
		var itemWidth = $(this).parent().width();																 
		$(this).replaceWith('<div class="submenu"' + $(this).html() + '</div>');
	});			
	
	// Cufon
	Cufon.replace('h1, h2,  #logobox, BLOCKQUOTE, .quote P, .reiseTitel', { hover: true });
	
	$('.clearInput').clearInput();

	
	$('#buchungsformular').ajaxForm({
		beforeSubmit:  formValidate,  // pre-submit callback 
		success:       inquirySent,  // post-submit callback 

		// other available options: 
		//url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		//clearForm: true        // clear all form fields after successful submit 
		//resetForm: true        // reset the form after successful submit 

		// $.ajax options can be used here too, for example: 
		timeout:   3000 														 
	});
	
	$('#formNewsletter').ajaxForm({
		beforeSubmit:  newsletterFormValidate,  // pre-submit callback 
		success:       newsletterFormSent,  // post-submit callback 

		// other available options: 
		//url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		//clearForm: true        // clear all form fields after successful submit 
		//resetForm: true        // reset the form after successful submit 

		// $.ajax options can be used here too, for example: 
		timeout:   3000 														 
	});	
	
	$("#thumbnails A").fancybox({
	  'titleShow'     : 'true',
		'titlePosition' : 'over',
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'overlayColor'	: '#000',
		'overlayOpacity': 0.9
	});		

	$("#nav TD").hover(
		function(){ $(this).children(".submenu").show(); },
		function(){ $(this).children(".submenu").hide(); }
	);
	
	$("#linkNewsletter").click( function(){ $("#formNewsletter").toggle(); });
	
	$("#buttonNewsletterAnmelden").click( function(){ $("#newsletterAction").val("anmelden"); });
	$("#buttonNewsletterAbmelden").click( function(){ $("#newsletterAction").val("abmelden"); });	
	
});

function inquirySent(responseText){
	setTimeout( function(){ $("#submitting").html(responseText); }, 3000);
}

function newsletterFormSent(responseText){
	setTimeout( function(){ $("#submitting").html(responseText); }, 3000);
}

function formValidate(formData, jqForm, options){
	
	if($("#reisetermin2").length > 0 && $("[name=reisetermin]").fieldValue()==""){
		alert(unescape("W%E4hlen") + " Sie bitte einen Reisetermin aus!");
		return false;
	}
	
	$(jqForm).validate();
	if($(jqForm).valid()){ 
		$("#buttonSubmit").hide();
		$("#submitting").show();
		return true;
	} else { 
		return false; 
	}
}


function newsletterFormValidate(formData, jqForm, options){

$(jqForm).validate();
	if($(jqForm).valid()){ 
		$("#formNewsletter .buttons").hide();
		$("#submitting").show();
		return true;
	} else { 
		return false; 
	}
}

jQuery.fn.clearInput = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

function slideshow(){

		var slide = setTimeout( function(){
			
			var introImageActive = $("#masthead .active");
			$(introImageActive).removeClass("active")
												 .fadeOut(1000);
															 
			if($(introImageActive).next().length == 1){
				$(introImageActive).next().fadeIn(1000).addClass("active"); 
			} else {
				$(introImageActive).siblings(".first").fadeIn(1000).addClass("active");
			}

			slideshow();
	
		},5000);

}	


