 $(document).ready(function(){
 
	var tab_width = 496;
	var tab_height = 300;
	
	var idcount = 0;
	var tabellen_container = $(".container_tabelle");
	tabellen_container
	.attr("id", function (idcount) {
          return "cont_tab_id_" + idcount;
        })
	.attr("style", "overflow:auto;width:"+tab_width+"px;height:"+tab_height+"px;padding:2px;background:#ffffff;border:1px solid #dbdbdb");
	tabellen_container.each( 
	function () {
		$(this).after("<div id='"+ $(this).attr("id") +"_ph' style='height:8px'></div>");
		$(this).before("<a name='"+ $(this).attr("id") +"_button' id='"+ $(this).attr("id") +"_button' class='container_tabelle_button' style='display:block;float:left;cursor:pointer'>Tabelle ausklappen &raquo;</a>");
		if (tabellen_container.length > 1) {$(this).before("<a name='"+ $(this).attr("id") +"_button_all' id='"+ $(this).attr("id") +"_button_all' class='container_tabelle_button_all' style='display:block;float:right;cursor:pointer'>alle Tabellen ausklappen</a>");}
		$(this).before("<div style='clear:both;height:4px'></div>");
	});
		
	$(".container_tabelle table:first-child").each( 
	function () {
		$(this).attr("id", $(this).parent().attr("id")+"_table");
	});	
	
	$(".container_tabelle_button").toggleClass('eingeklappt');
	$(".container_tabelle_button_all").toggleClass('eingeklappt');
	
   $(".container_tabelle_button").click(function(event){
   var buttonid = $(this).attr("id");
   var id = "#"+ buttonid.substring(0,buttonid.length-7);
   var tableid = id+"_table";
   var phid = id+"_ph";
   
	if ($(id).width() < $(tableid).width()) {
		$(id)
		.css("position", "absolute")
		.height($(tableid).height()+30)
		.width($(tableid).width()+6);
		$(phid).height($(tableid).height()+36);
		$(this).html("Tabelle einklappen &laquo;")
		.addClass('ausgeklappt').removeClass('eingeklappt');
	}
	else if ($(id).height() < $(tableid).height()) {
		$(id)
		.css("position", "absolute")
		.height($(tableid).height()+10)
		.width($(tableid).width());
		$(phid).height($(tableid).height()+30);
		$(this).html("Tabelle einklappen &laquo;")
		.addClass('ausgeklappt').removeClass('eingeklappt');
	}
	else {
		$(id)
		.height(tab_height)
		.width(tab_width)
		.css("position", "relative");
		$(phid).height(8);
		$(this).html("Tabelle ausklappen &raquo;")
		.addClass('eingeklappt').removeClass('ausgeklappt');
	}
   });
   
   $(".container_tabelle_button_all").click(function(event){
	   if ($(this).hasClass("eingeklappt")) {
			$(".container_tabelle_button.eingeklappt").click();
			$(".container_tabelle_button_all")
			.html("alle Tabellen einklappen")
			.toggleClass('eingeklappt');
		} else {
			$(".container_tabelle_button.ausgeklappt").click();
			$(".container_tabelle_button_all")
			.html("alle Tabellen ausklappen")
			.toggleClass('eingeklappt');
		}
   });
 });