var duration = 7000; //Hier die Dauer zwischen den Überblendungen angeben (Millisekunden)
var fadeduration = 600; //Hier die Dauer der Überblendung angeben (Millisekunden)

// Constants
var offsetPercentageV = 100; //(TODO: make overwriteable)
var offsetPercentageH = 50; //(TODO: make overwriteable) percentage - Horizontal offset of image. F.e. 0 -> image will be centered on the left; 50 -> image will be centered horizontally; 100 -> image will be right aligned

// Global vars
var imgWidth = '';
var imgHeight = '';
var imgRatio = '';
var winWidth = '';
var winHeight = '';
var winRatio = '';

// Global Objects
var windowObj = $(window);


$(document).ready(function() {
	
	//setInterval
	var setIndex=1;
	$("#gallery img").each(function() {
		$(this).attr("id","galleryimage"+setIndex);
		setIndex = parseInt(setIndex + 1);
	});
	setIndex=1;
	$("#gallery-text p").each(function() {
		$(this).attr("id","gallerytext"+setIndex);
		setIndex = parseInt(setIndex + 1);
	});
	
	
	$("#gallery img:first-child").addClass("active");
	$("#gallery img:first-child").show();
	$("#gallery-text p:first-child").addClass("active");
	$("#gallery-text p:first-child").show();

	var max = $("#gallery img").length;
	
	function next() {
		var activeId = $("#gallery .active").attr("id");
		var thisindex = parseInt(activeId.substring(12));
		var thatindex = thisindex;
		var nextindex = thisindex + 1;
		if (nextindex>max) {
			nextindex=1;
		}
		$("#gallery #galleryimage"+thatindex).removeClass("active");
		$("#gallery #galleryimage"+thatindex).fadeOut(fadeduration);
		$("#gallery #galleryimage"+nextindex).fadeIn(fadeduration, function() {
			$(this).addClass("active");
		});
		$("#gallery-text #gallerytext"+thatindex).fadeOut(fadeduration);
		$("#gallery-text #gallerytext"+nextindex).fadeIn(fadeduration);	
	}
	
	var automaticAnimation = window.setInterval(next,duration);
	
	
	/*
	imgWidth = $("#background img").attr("width");
	imgHeight = $("#background img").attr("height");
	imgRatio = imgWidth / imgHeight;
	
	setImage();
	$(window).resize(function() {
		setImage();
	});
	
	function setImage() {
		winWidth = $(window).width();
		winHeight = $(window).height();
	
		winRatio = winWidth / winHeight;
	
		if (winRatio>imgRatio) { resizeToWinWidth();} //window is wider than image -> image is 100% width of window
		if (winRatio<imgRatio) { resizeToWinHeight();}	//image is wider than window -> image is 100% height of window
	}
	
	function resizeToWinHeight() {
		var newImgWidth = winHeight * imgRatio;
		$("#background").css({"width":newImgWidth,"height":winHeight+"px"});;
		$("#background img").css({"width":newImgWidth,"height":winHeight+"px"});
		offsetHorizontal();
	}
	
	function offsetHorizontal() { 
		var thisImgWidth = winHeight * imgRatio;
		var imgLeftOffset = (0-(((thisImgWidth-winWidth)/100) * offsetPercentageH));
		 $("#background").css({"left":imgLeftOffset,"top":"0"});
	}	
		
	function resizeToWinWidth() {
		var newImgHeight = winWidth * (1 / imgRatio);
		$("#background").css({"width":winWidth,"height":newImgHeight});
		$("#background img").css({"width":winWidth,"height":newImgHeight});
		offsetVertical();
	}
	
	function offsetVertical() {
		var thisImgHeight = winWidth * (1 / imgRatio);
		 var imgHeightOffset = (0-(((thisImgHeight-winHeight)/100) * offsetPercentageV));
		 $("#background").css({"top":imgHeightOffset,"left":"0"});
	}
	*/

});
