var currentImg	= 1;
var lastImg		= 1;
var imgCount	= 0;
var height		= 0;

jQuery(document).ready(function(){

	var userOSAgent = navigator.userAgent.toLowerCase();

	// Figure out what browser is being used
	jQuery.platform = {
			mac: /mac/.test(userOSAgent),
			osx: /mac os x/.test(userOSAgent),
			win: /win/.test(userOSAgent),
			linux: /linux/.test(userOSAgent)
	};
	
	if(jQuery.platform.mac)
	{
		jQuery('body').addClass('mac');
	}

	resize();
	$('#gallery li').show();
	
	$(window).resize(resize);
	$('.note').draggable();
	
	$('#gal_nav .next, #gal_nav .prev, #gal_nav .jumpList').click(imgSwitch);
	$('#intro_language li').delay(1000).fadeIn(1000);
});

function imgSwitch() {
	if(imgCount == 0) {
		imgCount = $('#galleryDetail li').length;
	}

	if($(this).is('.next')) {
		// nächstes Bild
		currentImg = currentImg+1;
		if(currentImg > imgCount) {currentImg = 1;}
		
	} else if($(this).is('.prev')){
		// vorheriges Bild
		currentImg = currentImg-1;
		if(currentImg < 1) {currentImg = imgCount;}
		
	} else if($(this).is('.square')){
		// klick auf mittleren Button, wir sind in der Textansicht - zurück zum vorigen Bild
		currentImg	= lastImg;
		lastImg		= 1;
		
	}  else if($(this).is('.jumpList')){
		// klick auf mittleren Button, - aktuelles Bild merken und Sprung zum Text
		lastImg		= currentImg;
		currentImg	= imgCount;
	} 
	
	if($(this).is('.next')) {
		$('#galleryDetail li:visible').delay(20).hide('slide',{direction:'left'}, 2000);
		$('#galleryDetail li:eq('+(currentImg-1)+')').show('slide',{direction:'right'}, 2000);
	} else if($(this).is('.prev')){
		$('#galleryDetail li:visible').delay(20).hide('slide',{direction:'right'}, 2000);
		$('#galleryDetail li:eq('+(currentImg-1)+')').show('slide',{direction:'left'}, 2000);
	} else {
		$('#galleryDetail li:visible').delay(20).fadeOut(2000);
		$('#galleryDetail li:eq('+(currentImg-1)+')').fadeIn(2000);
	}
	
	
	
	if($('#galleryDetail li:eq('+(currentImg-1)+')').is('.text')) {
		$('#gal_nav .jumpList').addClass('square');
	}
	else if ($('#gal_nav .jumpList').is('.square'))
	{
		$('#gal_nav .jumpList').removeClass('square');
	}
	
}

function resize() {
	var head = $('#head').outerHeight(true);
	var win  = $(window).height();
	$('#contentHolder').css({
								height: (win - head)+'px'
							});
	resizeGalleryItems();
}

function resizeGalleryItems() {
	var w = $('#gallery li:first').outerWidth();
	height = Math.ceil(w / 640 * 475); //480
	$('#gallery li').each(function(){
		$(this).css({height: height+'px'});
	});
}
