/* Author: Codekünstler

*/

var imageW = 1280;
var imageH = 800;
var imageWidths = new Array(0,1280,1280,1280,1280,1280,1280,1280,1280,1600,1280,1280);
var imageHeights = new Array(0,800,800,800,800,800,961,850,800,1066,895,800);
var image = $("#bg img");
var bg = $("#bg");
var timer;
var currentImage;
var count= 1;
var numImages = 11;
var images = new Array();
var imageshuffle = new Array();
$(document).ready(function(){
	for(i=0; i< numImages; i++){
		images[i] = i+1;
	}
	imageshuffle = shuffle(images);
	dimensions();
	setStartPic();
})	
$(window).bind('resize', function(){
	dimensions();
})

function imageNum(){
	
	return Math.ceil(Math.random()*numImages) 
	
}
function dimensions(){
	bg.css({
		"width" : $(window).width(),
		"height" : $(window).height()
	});
	$("#content").css({
		"top" : ($(window).height() - $("#content").height())/2
	})
	fitToBox();
}
function fitToBox(){
	//$("#console").html(imageshuffle[count]+"   "+imageHeights[imageshuffle[count]] +"     "+ imageWidths[imageshuffle[count]])
	imAR = imageHeights[imageshuffle[count]] / imageWidths[imageshuffle[count]];
	bgAR = bg.height() / bg.width();
	if(imAR > bgAR){
		image.attr("width" , $(window).width());
		image.attr("height" , $(window).width() * imAR);
	}else{
		image.attr("height" , $(window).height());
		image.attr("width" , $(window).height() / imAR);
	}
	image.css("left" , (bg.width() - image.attr("width"))/2);
	image.css("top" , (bg.height() - image.attr("height"))/2);
}
function setStartPic(){
	image.attr("src" , "img/"+imageshuffle[count]+".jpg").load(function(){
		count++;
		$(this).fadeIn();
		bg.css("background" , "#000");
		fitToBox();
		timer = setTimeout("changePic()" , 5000);
		image.unbind('load');
	}).each(function(){
		if(this.complete) $(this).trigger("load");
	});
}


function shuffle(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};



function changePic(){
	if(count < numImages){
		count++;
	}else{
		count = 1;
	}
	$("#hiddenContainer img").attr("src" , "img/"+imageshuffle[count]+".jpg").load(function(){
		$("#hiddenContainer img").unbind('load');
		image.fadeOut(function(){
			image.attr("src", "")
			image.attr("src", $("#hiddenContainer img").attr("src"))
			fitToBox();
			image.fadeIn(function(){
				timer = setTimeout("changePic()" , 5000);
			});
		});
	}).each(function(){
		if(this.complete) $(this).trigger("load");
	});
}

alert(window.location.hash)
