/**
 * Initialisierung
 */
$(function() {
	
	/*
	 * Center MainNav - setTimeout für Webkit
	 */
	setTimeout(function() { albonaNova.centerMainNav(); }, 200);
	
	/*
	 * Center Booking Form - setTimeout für Webkit
	 */
	setTimeout(function() { albonaNova.centerBookingForm(); }, 200);
	
	/*
	 * DropDown Menü
	 */
	albonaNova.sfMenu('#mainNav');
	
	/*
	 * Fancybox
	 */
	$('.fancybox').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack'
	});
	/*
	 * Virtual Tour
	 */
	$('.fancyboxVirtualTour').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'width'			: 795,
		'height'		: 625
	});
	
	/*
	 * headerTitle Hover
	 */
	$('ul#pictures li').hover(function() {
			albonaNova.headerTitle(this, "show");
		}, function() {
			albonaNova.headerTitle(this, "hide");
		}
	);
	
	/*
	 * headerTitle Animation
	 */
	$('ul#pictures li').each(function(i, el) {
		setInterval(function() {
			var shown = $(el).find('img:not(:hidden)');
			shown.css({'z-index': '1'});
			$(el).find('img:hidden').css({'z-index': '5'}).delay(i * 200).fadeIn('fast', function() {
				shown.fadeOut();
			});
		}, 10000);
	});
	
	/*
	 * Tooltips
	 */
	albonaNova.initiatTooltips();
	
});

var albonaNova = {
	/**
	 * Center MainNav
	 */
	centerMainNav: function() {
		var mainNavWidth = 0;
		$('#mainNav li').each(function(i, el) {
			if(parseInt($(el).parent().css('left')) < 0) {
				return true;
			}
			mainNavWidth += $(el).width();
		});
		$('#mainNav li.first').css({
			"padding-left": Math.max(Math.ceil((800 - mainNavWidth) / 2), 50) + "px"
		});
	},
	
	centerBookingForm: function() {
		var bookingFormWidth = 41; // Datepicker Calender Icon
		$('#bookingForm>*:not(.clearer):not(input[type=hidden])').each(function(i, el) {
			bookingFormWidth += $(el).width() + parseInt($(el).css('margin-right')) + parseInt($(el).css('margin-left')) + parseInt($(el).css('padding-right')) + parseInt($(el).css('padding-left'));
		});
		$('#bookingForm').css({
			"padding-left": Math.max(Math.ceil((990 - bookingFormWidth) / 2), 100) + "px"
		});
	},
	
	initiatTooltips: function() {
		$(".tooltip").mouseover(function () {
			var pos = $(this).offset();
			var text = $(this).attr('title');
			$(this).attr('title', '');
			var decoded = $("<div/>").html(text).html();
			$("#tooltipOverlayContent").html(decoded);
			$("#tooltipOverlay").css({'left': pos.left - 20 + 'px', 'top': pos.top - $("#tooltipOverlay").height() - 2 + 'px'});
			$("#tooltipOverlay").css({'display': 'block'});
	
		});
		$(".tooltip").mouseout(function () {
			$(this).attr('title', $("#tooltipOverlayContent").html());
			$("#tooltipOverlay").css({'display': 'none'});
		});
	},
	
	/**
	 * Son of Suckerfish Menu modifiziert für jQuery
	 * @param str		navID		selector für DD UL
	 */
	sfMenu: function(navID) {
		$(navID + " li").each(function(i, el) {
			$(this).bind('mouseover focusin', function() {
				$(this).addClass("sfhover");
			});
			$(this).bind('mouseout focusout', function() {
				$(this).removeClass("sfhover");
			});
		});
	},
	
	/**
	 * löscht bzw. füllt Beschriftung in Input Felder
	 * @param {Object} 	obj		HTML Objekt
	 * @param str		str		Beschriftung
	 * @param str		action	Event: focus|blur
	 */
	controlInput: function(obj, str, action) {
		switch(action) {
			case "focus":
				if($(obj).val() == str) {
					$(obj).val("");
					$(obj).toggleClass("hasLabel");
				} else {
					$(obj).select();
				}
				break;
			case "blur":
				if($(obj).val() == "") {
					$(obj).val(str);
					$(obj).toggleClass("hasLabel");
				}
				break;	
		}
	},
	
	
	/**
	 * Kleine Galerie
	 * @param {Object} navTo
	 * @param {Object} gal
	 * @param {Object} newSrc
	 * @param {Object} alt
	 */
	
	smallGalleryNextZindex: 2,
	smallGalleries: 0,
	smallGalPics: [],
	smallGalAlts: [],
	smallGalLink: [],
	smallGalInterval: [],
	smallGalCurrentPic: [],
	
	navigateSmallGallery: function(navTo, gal, newSrc, alt) {
		
		if(this.smallGalleryNextZindex%2 == 0) {
			var idPic = "#smallGalleryPicEven" + gal;
			var classPic = ".smallGalleryPicEven";
		} else {
			var idPic = "#smallGalleryPicOdd" + gal;
			var classPic = ".smallGalleryPicOdd";
		}
		
		var dontInterruptInterval = false;
		
		if(!this.smallGalCurrentPic[gal]) {
			this.smallGalCurrentPic[gal] = 0;
		}
		
		switch(navTo) {
			case 'prev':
				navTo = Math.max(0, (this.smallGalCurrentPic[gal] - 1));
				if(this.smallGalCurrentPic[gal] - 1 == -1) {
					navTo = this.smallGalPics[gal].length-1;
				}
				newSrc = this.smallGalPics[gal][navTo];
				alt = this.smallGalAlts[gal][navTo];
				break;
			case 'nextAuto':
				dontInterruptInterval = true;
			case 'next':
				navTo = Math.min((this.smallGalCurrentPic[gal] + 1), this.smallGalPics[gal].length-1);
				if(this.smallGalPics[gal].length == this.smallGalCurrentPic[gal] + 1) {
					navTo = 0;
				}
				newSrc = this.smallGalPics[gal][navTo];
				alt = this.smallGalAlts[gal][navTo];
				break;
		}
		
		if(!dontInterruptInterval) {
			clearInterval(this.smallGalInterval[gal]);
		}
		
		
		$('#gallerySmall .nav .numbersHolder .numbers').animate({
			"margin-left": Math.min(0, Math.min(navTo - 3, this.smallGalPics[gal].length - 7) * 24 * -1) + "px"
		});
	
		
		$('#gallerySmall .gallery .nav .numbers a.active').removeClass("active");
		
		$('#number' + navTo).addClass("active");
		
		$(idPic).attr({
			"src": newSrc,
			"alt": alt
		});
		
		if(this.smallGalLink[gal][navTo] == "" || this.smallGalLink[gal][navTo] == undefined) {
			$(idPic).parent("a").attr('href', newSrc);
			$('#smallGalleryPicZoom' + gal).attr('href', newSrc);
		} else {
			$(idPic).parent("a").attr('href', this.smallGalLink[gal][navTo]);
			$('#smallGalleryPicZoom' + gal).attr('href', this.smallGalLink[gal][navTo]);
		}
		
		$(classPic).css({
			'display': 'none',
			'z-index': ++this.smallGalleryNextZindex
		}).fadeIn('slow');
		
		$('.desc').html(alt).css({
			'display': 'none'
		}).fadeIn('fast');
		
		this.smallGalCurrentPic[gal] = navTo;
		
	},
	
	headerTitle: function(obj, action) {
		switch(action) {
			case 'show':
				$(obj, this).find('span').stop().animate({
					'top': '0'
				}, {
					'queue': false,
					'duration': 200
				});
				break;
				
			case 'hide':
				$(obj, this).find('span').stop().animate({
					'top': '-37px'
				}, {
					'queue': false,
					'duration': 200
				});
				break;
		}
	},
	
	controlChildrenAge: function(obj) {
		var regEx = /\D+/;
		if(regEx.test(obj.value)) {
			obj.value = obj.value.substr(0, obj.value.length - 1);
		}		
		$('#childrenAge .inputs').html('');
		for(i=0;i<parseInt(obj.value);i++) {
			$('#childrenAge .inputs').append('<input type="text" class="required childrenAge" name="childrenAge[' + i + ']" />');
		}
		if(obj.value == 0) {
			$('#childrenAge').slideUp();
		} else {
			$('#childrenAge').slideDown();
		}
	},
	
	controlRooms: function(obj) {
		var regEx = /\D+/;
		if(regEx.test(obj.value)) {
			obj.value = obj.value.substr(0, obj.value.length - 1);
		}
		$('#rooms').val(Math.ceil(obj.value/2));
	},
	
	controlUserRatings: function(){
		if(parseInt($('#userRatings').css('bottom')) < 0) {
			$('#userRatings').animate({
				'bottom': '0'
			});
			$('#userRatingsButton').removeClass('closed');
			$.cookie('albonaNovaUserRating', 'opened');
		} else {
			$('#userRatings').animate({
				'bottom': '-36px'
			});
			$('#userRatingsButton').addClass('closed');
			$.cookie('albonaNovaUserRating', 'closed');
		}
	},
	
	toggleSitemap: function(obj) {
		if (obj == "openAll") {
			$('#sitemap ul').slideDown();
			$('#sitemap li img').attr('src', $('#sitemap li img').attr('src').replace('plus', 'minus'));
		} else if (obj == "closeAll") {
			$('#sitemap ul ul').slideUp();
			$('#sitemap li img').attr('src', $('#sitemap li img').attr('src').replace('minus', 'plus'));
		} else if ($(obj).children('img').attr('src').indexOf('plus') != -1) {
			$(obj).next().slideToggle();
			$(obj).children('img').attr('src', $(obj).children('img').attr('src').replace('plus', 'minus'));
		} else {
			$(obj).next().slideToggle();
			$(obj).children('img').attr('src', $(obj).children('img').attr('src').replace('minus', 'plus'));
		}
	}
}

function fidelioBuchenSave2(name, beschreibung, preis) {
	$("#action").val("saveSchritt2");
	$("#angebotname").val(name);
	$("#angebotbeschreibung").val(beschreibung);
	$("#angebotpreis").val(preis);
	$('#fidelioBuchenForm').submit();
}

/**
 * show that we are in feature testing mode (or not)
 */
function showTestingMode(testing) {
	if(testing) {
		console.log("Testing mode active");
		$("body").prepend('<div class="featureTestNotificationArea">Feature-Test aktiviert</div>');
		$(".featureTestNotificationArea").slideDown("slow");
	}
}
