if(!window.FV_VHB){  
	FV_VHB = {};
}

FV_VHB.Selector = {};

FV_VHB.Footnote = {};

footnotes = [];

FV_VHB.Selector.getSelected = function() {  
	var t = '';  
	if (window.getSelection){    
		t = window.getSelection();  
	}
	else if (document.getSelection) {    
		t = document.getSelection();  
	}
	else if (document.selection) {    
		t = document.selection.createRange().text;  
	} 
	return t;
}

FV_VHB.Selector.bind = function() {
	$(document).bind("mouseup", FV_VHB.Selector.mouseup);
}

FV_VHB.Selector.unbind = function() {
	$(document).unbind("mouseup", FV_VHB.Selector.mouseup);
}

FV_VHB.Selector.close = function() {
	$("#fv_schnellsuche").remove();	
	FV_VHB.Selector.bind();
}

FV_VHB.Selector.search = function() {
	st = $('#input_fv_schnellsuche').val()
	FV_VHB.Selector.close();
	window.location = 'http://www.der-betrieb.de/content/_p=285,qsd=0,query=' + st;

}

FV_VHB.Selector.mouseup = function(e) {  
	var st = FV_VHB.Selector.getSelected();
	if (st == '' || st.length > 80)
		return
	x = e.pageX;
	y = e.pageY + 5;
	$('body').append('<div id="fv_schnellsuche" style="background-image:url(/images/background.png);height:80px;">'+
	                 '<div style="margin-top:6px;font-size:10px;font-family:Verdana;background-image:url(/images/bg_top_db.gif);height:60px;width:150px;border-color:#E0DDAC;border-width:1px;border-style:solid">' +
			 '<form id="form_fv_schnellsuche" name="form_fv_schnellsuche" action="javascript:FV_VHB.Selector.search()">' +
			 '<DIV style="margin-left:5px;margin-top:3px"> Suche nach: ' +
	                 '<img onclick="FV_VHB.Selector.close()" style="margin-left:128px;margin-top:-12px" src="/images/close_print1.gif">' +
	                 '</div>'+
	                 '<input name="input_fv_schnellsuche" id="input_fv_schnellsuche" type="text" style="width:145px;margin-left:1px;margin-top:0;font-size:10px;font-family:Verdana" name="such_string" value="' + st + '"/><br>' + 
	                 '<button style="margin-top:2px;margin-left:116px;height:20px;width:30px;font-size:10px;font-family:Verdana;" onclick="javascript:FV_VHB.Selector.search()"><img style="margin-left:-5px" src="/images/icon-search-new.png"></button></div>' +
	                 '</form>' +
	                 '</div>');
	FV_VHB.Selector.unbind();
	$('#fv_schnellsuche').css({'position': 'absolute', 'top': y, 'left': x});
	$('#input_fv_schnellsuche').focus();

}



FV_VHB.Footnote.collect = function() {
	$("tr:has('.footnote_nr')").each( function(index){
		var num = parseInt($(this).text().match(/\d+/));
		var foot_html = $(this).find('.footnote').html().replace(/<A/g, "<A style='color:#948f00'");
		footnotes[num]= foot_html;
	});
	$('.footnote_link sup').each( function(index) {
		var num = parseInt($(this).text().match(/\d+/));
		$(this).bind("mouseover", {nr: num}, function(evt) {
			$('#fv_footnote').remove();
			$('body').append(
				'<div id="fv_footnote" style="width:300px;background-color:#EEEEEE;padding:5px;border-style:solid;border-width:1px">'+
				evt.data.nr + ') ' + footnotes[evt.data.nr] +
				'</div>'
			);
			x = evt.pageX+10;
			y = evt.pageY+10;
			$('#fv_footnote').css({'position': 'absolute', 'top': y, 'left': x});
			$('body').bind("click", function(evt) {
				$('body').unbind();
				$('#fv_footnote').remove();
			});
		});
	});
}
 
 
 $(document).ready(function(){
 
	if(top != self) top.location.replace(location);
 
 	FV_VHB.Footnote.collect();
 	FV_VHB.Selector.bind();
 	
	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');
		}
   });
 });
 

