var len = {};
var images = {};
var cur = {};
$(document).ready(function(){
	var x = 45;
	var y = 45;	
	var font_size = $("#font_size").height();
	var box = $(".karte .box").map(function () {
	
		var position_left = $(this).position().left/font_size;
		var position_top = $(this).position().top/font_size;
		var box_width = $(this).find(".inhalt").outerWidth()/font_size;
		var box_height = $(this).find(".inhalt").outerHeight()/font_size;
		var box_2 = $(this).find(".inhalt");
		var postion_x;
		var postion_y;
					
		if(box_width + position_left >= x && box_height + position_top >= y) {
			postion_x = 0.5 - box_width;
			postion_y = 2 - box_height;
			
			$(this).find(".inhalt_head").clone().appendTo(box_2);
			$(this).find(".inhalt_head:first").remove();
			
			var ecke = '<div style="position:absolute; left:20.5em; bottom:-0.09em; width:1.5em; height:1.5em;"><img src="/images/karte-box-ecke-r-u.gif" style="width:1.5em; height:1.5em;" alt="" /></div>';
			$(ecke).appendTo(box_2);
		} else if(box_width + position_left >= x && box_height + position_top < y) {
			postion_y = 2;
			postion_x = 0.5 - box_width;
			
			var ecke = '<div style="position:absolute; left:20.5em; top:-0.09em; width:1.5em; height:1.5em;"><img src="/images/karte-box-ecke-r-o.gif" style="width:1.5em; height:1.5em;" alt="" /></div>';
			$(ecke).appendTo(box_2);
		} else if(box_width + position_left < x && box_height + position_top >= y) {
			postion_x = 3.5;
			postion_y = 0.5 - box_height;	
			var ecke = '<div style="position:absolute; left:-1.5em; bottom:-0.09em; width:1.5em; height:1.5em;"><img src="/images/karte-box-ecke-l-u.gif" style="width:1.5em; height:1.5em;" alt="" /></div>';
			$(ecke).appendTo(box_2);
		} else {
			postion_x = 3.5;
			postion_y = 2;	
			var ecke = '<div style="position:absolute; left:-1.5em; top:-0.09em; width:1.5em; height:1.5em;"><img src="/images/karte-box-ecke-l-o.gif" style="width:1.5em; height:1.5em;" alt="" /></div>';
			$(ecke).appendTo(box_2);
		}
		
		$(this).find(".inhalt").css({'left':postion_x + 'em','top':postion_y + 'em'});
	});
	
	$(".karte .box").hover(
		function () {
			$(this).find(".inhalt").fadeIn(300);
		},
		function () {
			$(this).find(".inhalt").fadeOut(300);
		}
	);
	
	$(".bildergalerie").each(function(i) {
		images[i] = $(this).find(".img");
		len[i] = images[i].length;
		cur[i] = 0;
		
		if(1<len[i]) {
			aniscroller(i);
			window.setInterval("aniscroller("+i+")",8000);
		}
	});
});
function aniscroller(i) {
	next = (cur[i]+1!=len[i]) ? cur[i]+1 : 0;
	images[i].eq(cur[i]).fadeOut(2000);
	images[i].eq(next).fadeIn(2000);
	cur[i] = next;
}