var lang = 'en';
var content = '';

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

$(document).ready(function() {
	
	$(window).bind("resize", function ( e ) {
		resize();
	});
	
	$('#buttons .prev').click(function() {
		var index = $('#galleries .active').find('img').index( $('#galleries .active').find('img:visible') );
		if (index > 0) {
			$('#galleries .active').find('img:eq(' + index + ')').fadeOut();
			index--;
			$('#galleries .active').find('img:eq(' + index + ')').fadeIn();
		} else {
			$('#galleries .active').find('img:eq(' + index + ')').fadeOut();
			index = $('#galleries .active').find('img').length - 1;
			$('#galleries .active').find('img:eq(' + index + ')').fadeIn();
		}
	});
	
	$('#buttons .next').click(function() {
		var index = $('#galleries .active').find('img').index( $('#galleries .active').find('img:visible') );
		if (index < ($('#galleries .active').find('img').length - 1)) {
			$('#galleries .active').find('img:eq(' + index + ')').fadeOut();
			index++;
			$('#galleries .active').find('img:eq(' + index + ')').fadeIn();
		} else {
			$('#galleries .active').find('img:eq(' + index + ')').fadeOut();
			index = 0;
			$('#galleries .active').find('img:eq(' + index + ')').fadeIn();
		}
	});
	
	$('.close a').click(function() {
		hideContent();
		if (!$('a.rooms').hasClass('active')) return false;
		$('a.rooms').removeClass('active');
		$('#sub-rooms').animate({
			'left':'-=220px'
		});
		return false;
	});
	
	$('#nav a, #sub-rooms a').not('a.rooms').click(function() {
		if ($('a.rooms').hasClass('active')) {
			$('a.rooms').removeClass('active');
			$('#sub-rooms').animate({
				'left':'-=220px'
			});
		}
		$('#nav a').removeClass('active');
		$(this).addClass('active');
		var pos = $('#content').position();
		content = $(this).attr('href').replace('#','');
		$('#content .inner').html($('.lang-' + lang + ' .' + content).html());
		//$('.gallery').not('#gallery-' + content).fadeOut();
		$('ul.gallery').not('#gallery-' + content).each(function() {
			$(this).hide();
			$(this).removeClass('active');
		});
		$('#gallery-' + content).fadeIn(function() {
			$(this).addClass('active');
			if ($('#gallery-' + content + ' li').length > 1) {
				$('#buttons').fadeIn();
			} else {
				$('#buttons').fadeOut();
			}
		});
		if (pos.left > 0) {
			switchContent();
		} else {
			showContent();
		}
		resize();
		return false;
	});
	
	$('a.rooms').click(function() {
		if ($(this).hasClass('active')) {
			$('#sub-rooms').animate({
				'left':'-=220px'
			});
			$(this).removeClass('active');
			return false;
		}
		$('#nav a').removeClass('active');
		$(this).addClass('active');
		hideContent();
		$('#sub-rooms').animate({
			'left':'+=220px'
		});
		return false;
	});
	
	$('#lang a').click(function() {
		setTimeout(function() {
		$('#galleries img').each(function(){
   		$(this).attr('src', $(this).attr('delayedsrc'));
 		});
		}, 500);
		if ($(this).hasClass('en')) lang = 'en';
		if ($(this).hasClass('de')) lang = 'de';
		if ($(this).hasClass('fr')) lang = 'fr';

		$('.lbl-lang-en').hide();
		$('.lbl-lang-fr').hide();
		$('.lbl-lang-de').hide();
		$('.lbl-lang-' + lang).show();
		
		$('#intro').fadeOut(function() {
			$('#step-1').hide();
		});
		$('body').addClass('interior');
		$('#step-2').fadeIn(function() {
			resize();
		});
		$('#step-2 .gallery:eq(0)').show();
		return false;
	});
	
	$('.switch-lang').click(function() {
		if ($(this).hasClass('en')) {
			lang = 'en';
		}
		if ($(this).hasClass('fr')) {
			lang = 'fr';
		}
		if ($(this).hasClass('de')) {
			lang = 'de';
		}
		if (content != '') $('#content .inner').html($('.lang-' + lang + ' .' + content).html());
		
		$('.lbl-lang-en').hide();
		$('.lbl-lang-fr').hide();
		$('.lbl-lang-de').hide();
		$('.lbl-lang-' + lang).show();
		
	});
	
	var url = document.URL;
    if (url.indexOf('#')>0) {
        var anchor = url.substring(url.indexOf('#'), url.length);
        $('#lang a.en').click();
        setTimeout(function() {
        	$('#sub-rooms a').each(function() {
        		if ($(this).attr('href') == anchor) {
        			$(this).click();
        		}
        	});
        }, 500);
        
    }
	
});

function resize() {
	
	//var docH = getDocHeight();
	
	var docH = $(window).height();
	
	$('.gallery li').css({
		'height': docH,
		'width':  ($(window).width()-220)
	});
	
	$('.gallery').find('img').css({
		'height':docH
	});
	
	var left = ($('.gallery').find('img').width() - $(window).width()) / 2;

	$('#main').css({'height': docH});
	
	$('#content').css({
		'height': docH - $('#content').css('padding-top').replace('px','')
	});
	
	$('#gallery-contact iframe').css({
		'width': ($(window).width()-220),
		'height': (docH)
	});
	
	setTimeout(function() { resize(); }, 300);
	
}

function switchContent() {
	
	var pos = $('#content').position();
	if (pos.left > 0) {
		$('#content').animate(
			{'left':'-=' + pos.left},
			300,
			function() {
				$('#content').css({'left':'0px'});
				$('#content').animate({'left':'+=220px'});
			}
		);
		$('#buttons').animate(
			{'left':'-=' + pos.left},
			300,
			function() {
				$('#buttons').css({'left':'220px'});
				$('#buttons').animate({'left':'+=220px'});
			}
		);
	}
	
}

function hideContent() {
	
	var pos = $('#content').position();
	if (pos.left > 0) {
		$('#content').animate(
			{'left':'-=' + pos.left},
			300
		);
		$('#buttons').animate(
			{'left':'-=' + pos.left},
			300
		);
	}
	
}

function showContent() {
	
	$('#content').animate({'left':'+=220px'});
	$('#buttons').animate({'left':'+=220px'});
	
}
