﻿Array.prototype.shuffle = function() {
	function randOrd() {return (Math.round(Math.random())-0.5); }
	for(i=0; i<10; i++)	this.sort( randOrd );
} 

$(function() {
			
	// Aspect Ratio überprüfen und hintergrund bild anpassen
	function check_aspect() {
		var winaspect = $(window).height()/$(window).width();
		$('.fullbackground').each(function() {
			var $div = $(this).find('div');
			var aspect = $div.data('aspect');
			var portrait = winaspect<aspect;
			if ($div.data('portrait')!=portrait) {
				$div.data('portrait',portrait);
				if (portrait) {
					$div.addClass('portrait').find('img').css('margin-top',0); 
				} else {
					$div.removeClass('portrait').find('img').css('margin-top',(-aspect*50)+'%');
				}
			}
		});
	}
	
	function setimage(pos,nr) {
		$('#img'+pos+' div')
			.html('<img src="'+images[nr].name+'">')
			.data({ aspect: images[nr].height/images[nr].width });
	}

	function nextimage() {
		// nächstes Bild laden
		position= 1-position;
		image++;
		if (image>=images.length) {
			images.shuffle();
			image= 0;
		}
		setimage(position, image);
		check_aspect();
		setTimeout(function() {									  	  	// Bild eine gewisse Zeitlang anzeigen
			if (position) {
				$('#img1').fadeIn(fadeTime, function() { nextimage(); });
			} else {
				$('#img1').fadeOut(fadeTime, function() { nextimage(); });
			}
		}, showTime );
	};
	

	var position= 0;
	var image= 0;
	var fadeTime= 2000;
	var showTime= 3000;

	images.shuffle();

	setimage(0,0);
	$('#img1').hide();
	check_aspect();
	$(window).bind('resize', function() { check_aspect() } );
	nextimage();
	
 });

