
function animate_img(image, duration, pause, fade_off) {
	window.setTimeout(function() {

	var theImage = $(new Image());

	theImage.load(function() {

		var mainTable = $('.mainTable');
		//var theImage = $('<img src="'+image+'" style="display: none; position: absolute;" />');

		var pos = mainTable.position();
		var size = {width: $(document).width(), height: mainTable.height()}


		var theDiv = $('<a style="position: absolute; overflow: hidden; display: block" href="#"></a>'); //
		//theDiv.css({top: pos.top+'px', left: pos.left+'px', width: size.width+'px', height: size.height+'px'});
		theDiv.css({top: pos.top+'px', left: '0px', width: size.width+'px', height: size.height+'px'});
		theDiv.attr('href', mainTable.find('a').attr('href'));
		theDiv.appendTo('body');
		//mainTable.wrap(theDiv);

		// relativ zu theDiv
		theImage.appendTo(theDiv);
		theImage.css({top: '0px', left: -theImage.width()+'px', border: '0'});

		pos.left = -theImage.width()/2;
		pos.top = theImage.height()/2;
		pos.right = size.width+theImage.width()/2;
		pos.buttom = size.height-theImage.height()/2;

		//$('<div style="border: solid 1px red; position: absolute; "></div>').appendTo(theDiv).
		//css({top: pos.top+'px', left: pos.left+'px', width: pos.right-pos.left+'px', height: pos.buttom-pos.top+'px'});

		var move_back = function() {
			theImage.css({top: pos.top + Math.random() * (pos.buttom - pos.top) - theImage.height()/2 + 'px',
					left: -theImage.width() + 'px'});
			if(! fade_off) theImage.show();
		}

		var move = function(p, opacity, duration) {
			if(fade_off) {
				theImage.show();
				theImage.animate({left: p.left - theImage.width()/2 + "px", top: p.top - theImage.height()/2 + "px"},
					duration, 'linear', move_back
				);
			} else
				theImage.animate({opacity: opacity, left: p.left - theImage.width()/2 + "px", top: p.top - theImage.height()/2 + "px"},
					duration, 'linear', move_back
				);
		}

		var left_current = pos.left;
		var top_current = pos.top;

		var animate = function() {
			var left_last = left_current;
			var top_last = top_current;

			left_current = pos.right;
			//left_current = (left_current == pos.left) ? pos.right : pos.left;
			//top_current = (top_current == pos.top) ? pos.buttom : pos.top;
			top_current = pos.top + Math.random() * (pos.buttom - pos.top);

			//move({top: top_last + top_current/2 - top_last/2, left: left_last + left_current/2 - left_last/2}, 'toggle', duration/2);
			move({top: top_current, left: left_current}, 'toggle', duration);
		}

		animate();
		window.setInterval(animate, duration + pause);
	});


	theImage.attr('src', image).css({display: 'none', position: 'absolute'});

	}, pause);
}

$(function() {
	//$('.mainTable').ready(function() {
	//window.setTimeout(function() {
	var image = {
		weihnachten: [
			{img: 'santa.gif', fade_off: true, duration: 10000, pause: 3000},
			{img: 'santa-rentiere.gif', fade_off: true, duration: 8000, pause: 6000},
			{call: SNOW_Weather_begin}
		],
		valentinstag: [], // [{img: 'valentinstag.png', duration: 4000, pause: 3000}],
		muttertag: [], // [{img: 'muttertag.png', duration: 4000, pause: 3000}],
		vatertag: [], // [{img: 'vatertag.png', duration: 4000, pause: 3000}],
		nichts: []
	}

	var zeit = [ // muss chronologisch geordnet sein
		{img: image.nichts, m: 1, d: 1},
		{img: image.valentinstag, m: 2, d: 1},
		{img: image.nichts, m: 2, d: 15},
		{img: image.muttertag, m: 5, d: 1},
		{img: image.nichts, m: 5, d: 15},
		{img: image.vatertag, m: 6, d: 1},
		{img: image.nichts, m: 6, d: 15},
		{img: image.weihnachten, m: 9, d: 1}
	]

	var datum = new Date();
	//var datum = new Date(2009, 2, 2);
	var m = datum.getMonth() + 1;
	var d = datum.getDate();

	var img = image.nichts;

	for(i = zeit.length - 1; i >= 0; i --) {
		if(zeit[i].m <= m && zeit[i].d <= d) {
			img = zeit[i].img;
			//alert('hi');
			break;
		}
	}

	for(i in img) {
		 if(img[i].img)
			 animate_img(img[i].img, img[i].duration, img[i].pause, img[i].fade_off);
		 if(img[i].call)
			 img[i].call();
	}

	//window.setTimeout(animate, pause);

	//$(document).ready(function() { animate_img('santa.png', 4000, 3000); });
	//$(document).ready(function() { animate_img('rentiere.png', 2000, 6000); });
	//}, 1000)
	//});
});

