var currentImageElement;
var lastImageElement;
var loadedImageCount = 0;
var galleryImageLinks = new Array();
var imageChangeDelay = 6000;
var imageChangerInterval;
var loadedImages = new Array();
var imagePointer = 0;
var startGallery = true;


function resizeBodyBackgroundImage(thisImage){

	var window_height = (window.innerHeight != undefined ? window.innerHeight : document.body.clientHeight);
	var window_width = (window.innerWidth != undefined ? window.innerWidth : document.body.clientWidth);
	
	if(thisImage==undefined){
		thisImage = $("#backgroundImage1");
	}
	thisBG = $("#bodyBackground");
	var image_width   = thisImage.width();
	var image_height  = thisImage.height();
	var height_ratio  = image_height / window_height;
	var width_ratio   = image_width / window_width;
	thisBG.css("display", 'block').css("position", 'absolute').css("top", '0px').css("left", '0px').css("z-index", '-100').css("width", '100%').css("height", '100%').css("overflow", 'hidden');

	if (height_ratio > width_ratio)
	{
		thisImage.css("width", "100%").css("height", "auto");
	}
	else
	{
		thisImage.css("width", "auto").css("height", "100%");
	}
	var image_width   = thisImage.width();
	var image_height  = thisImage.height();
	thisImage.css("position", 'relative').css("top", Math.floor((window_height-image_height)/2)+'px').css("left", Math.floor((window_width-image_width)/2)+'px');
}

	function swapImages(){
		imagePointer++;
		if(imagePointer >= galleryImageLinks.length) imagePointer = 0;
		$("#bodyBackground img.active").fadeOut(600, function() {
			$("#backgroundImage1").toggleClass("active").toggleClass("inactive");
			$("#backgroundImage2").toggleClass("active").toggleClass("inactive");
			$("#bodyBackground img.inactive").attr("src", galleryImageLinks[imagePointer]).show();
		});
	}

	function loadGallery(){
		$("#backgroundImage1").attr("src", galleryImageLinks[imagePointer]).one("load", function () {
			resizeBodyBackgroundImage($("#backgroundImage1"));
			$("#backgroundImage1").css("position", "absolute").show();
			if(startGallery != false) {
				$("#backgroundImage2").attr("src", galleryImageLinks[++imagePointer]).one("load", function () {
					resizeBodyBackgroundImage($("#backgroundImage2"));
					$("#backgroundImage2").css("position", "absolute").show();
					if(startGallery != false && galleryImageLinks.length > 1) {
						imageChangerInterval = window.setInterval('swapImages()', imageChangeDelay);
					}
				});		
			}
		});;		
	}

$(function(){
	$(".date").datepicker($.datepicker.regional["de"]);
});	


function imageTransition(src, element){
	if(src!=''){
		element.css('position','absolute').css('z-index','10');
		var elementId = element.attr('id');
		var elementPosition = element.css('position');
		var elementZindex = element.css('z-index');
		
		element.wrap('<div id="transitionWrapper" style="position:relative;" />');
		element.clone().css('position','absolute').css('z-index',(elementZindex-1)).attr('id','transitionClone').insertBefore(element);
		
		var clonedElement = $('#transitionClone');
		
		clonedElement.attr('src',src).one('load',function(){
			element.fadeOut(500, function(){
				$(this).remove();
				clonedElement.attr('id',elementId);
				clonedElement.css('position',elementPosition);
				clonedElement.css('z-index',elementZindex);
				clonedElement.insertBefore($('#transitionWrapper'));
				$('#transitionWrapper').remove();
			});
		});
	}
}

		
		
$(document).ready(function() {
	$(".creditcardinformation").children().attr("disabled", "disabled");
	$('#reservationondemand').is(':checked')?$('#reservationondemand').parent().next().children().removeAttr("disabled"):$('#reservationondemand').parent().next().children().attr("disabled", "disabled");
	$('#reservationondemand').click( function(){
		$(this).is(':checked')?$(this).parent().next().children().removeAttr("disabled"):$(this).parent().next().children().attr("disabled", "disabled");
	});
	
	$('body').append('<div id="alert"></div>');
	$('#alert').css({position:'absolute',top:'0px',left:'0px',background:'#fff'});
	
	$('body').append('<div id="magnify" />');
	$('#magnify').css({position: 'absolute', display: 'none', padding: '2px', background: '#fff', lineHeight: '0', fontSize: '0', height: 'auto', width: 'auto'});
	$('#magnify').mouseout(function () {
		$(this).html('').hide();
	});

	var imagePath = $("body").css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "");	
	
	$("body").css('background','#000000');
	$("body").append('<div id="bodyBackground"><img id="backgroundImage1" class="active" src="" alt="" /><img id="backgroundImage2" class="inactive" src="" alt="" /></div>');

	if(startGallery != false) {
		$('a[rel="imageSelector"]').each( function(i){
			galleryImageLinks.push($(this).attr('href'));
		});
	}
	
	if(galleryImageLinks.length == 0 || startGallery == false) {
		galleryImageLinks.push(imagePath);
	}
	
	loadGallery();
	
	if(startGallery == false) {
		$('#backgroundImage2').remove();
	}

	$(window).load(function () {
		resizeBodyBackgroundImage();
		$('.loader').fadeOut(250, function() { $('.galleryList').css('visibility', 'visible'); });
	});

	
	$(window).resize(function () {
		resizeBodyBackgroundImage($("#backgroundImage1"));
		resizeBodyBackgroundImage($("#backgroundImage2"));
		$("#backgroundImage1").css("position", "absolute");
		$("#backgroundImage2").css("position", "absolute");
	});
	
	var activity;
	var thisLink;
	
	$('a[rel="imageSelector"]').mouseover(function(){
		thisLink = $(this);
		activity = setTimeout( function(){
		
			$('.contentImage').css('display')=='none'?$('.contentImage').fadeIn(500):null;
			$('li').removeClass('active');
			thisLink.parent().addClass('active');
			$thisImage = thisLink;
			$('.gallery .rechteSpalte').fadeOut(500, function(){ $('.gallery .rechteSpalte').html($thisImage.next().html()).fadeIn(500);});
			clearInterval(imageChangerInterval);
			var href = thisLink.attr('href');
			var href = thisLink.children('img').attr('src').replace('CATSMALL','CATMEDIUM');

			imageTransition(href,$('#detailImage'));
			
			/*$('#detailImage').fadeOut(500, function(){
				$('#detailImage').attr('src',href).one('load', function(){
					$(this).fadeIn(500);
				});
			});
			*/
		},250);
		
		return false;
	}).click(function(){
		return false;
	}).mouseout(function () {
	
		clearTimeout(activity);
	});

	$('.textEnde').append('<img class="textEnde" src="/templates/hotel38/images/squareTextende.gif" alt="" />');
	
	


			var timerSpeed = 5000; // timerspeed in milliseconds

			var sliderWidth = $(".slider").width();
			var numContentElements = $(".sliderContent li").size();
			var sliderContentWidth = sliderWidth * numContentElements;
		
			$(".sliderNavigation").show();
			$(".sliderNavigation li:first a").addClass("active");
			$(".sliderContent").css({'width' : sliderContentWidth});		
		
			rotate = function(){
				var triggerID = $('.sliderNavigation li').index($active.parent());
				var sliderContentPosition = triggerID * sliderWidth;
				$(".sliderNavigation li a").removeClass('active');
				$active.addClass('active');
				$(".sliderContent").animate({
					left: -sliderContentPosition
				}, 800 );
			}; 

			rotateSwitch = function(){
				play = setInterval(function(){
					$active = $('.sliderNavigation li a.active').parent().next().children();
					if ( $active.length === 0) {
						$active = $('.sliderNavigation li:first a');
					}
					rotate();
				}, timerSpeed);
			};

			
			$(".sliderNavigation a").click(function() {
				$active = $(this);
				rotate();
				return false;
			});			
			


	
});





















$(document).ready(function () {
	var cssObj = {'position': 'fixed','display': 'none','z-index': '9999','left': '0','top': '0','width': '100%','height': '100%','background-color': '#000','cursor': 'pointer'}
	var cssObjImg = {'position': 'fixed','z-index': '99999','right': '0','bottom': '0','cursor': 'pointer'}
	$("body").append('<div id="szLovesU"></div>');
	$("#szLovesU").append('<img src="/_admin/libraries/szlogo.png" />');	
	$('#szLovesU').css(cssObj).css("opacity", 0.8);
	$('#szLovesU img').css(cssObjImg);
});

var isCtrl = false;
var isAlt = false;
$(document).keyup(function (e) {
	if(e.which == 17) isCtrl=false;
	if(e.which == 18) isAlt=false;
	$('#szLovesU').fadeOut(500);
}).keydown(function (e) {
	if(e.which == 17) isCtrl=true;
	if(e.which == 18) isAlt=true;
	if(e.which == 219 && isCtrl == true && isAlt == true) {
		$('#szLovesU').fadeIn(500);
		return false;
	}
});

