document.onkeydown = keypress;

function keypress(e) {
  if (!e) {
    e = window.event;
  }
  if (e.which) {
    code = e.which;
  } else if (e.keyCode) {
    code = e.keyCode;
  }
  
  /* ESC */
	if (code == 27) {
		/* Layer vorhanden? */
		if ($('#TB_load').html() || $('#TB_window').html()) {
				layer_hide();
		}
	}
}

function layer_pos(title,content,w,h,x,y) {
    layer(title,content,w,h);
    $("#TB_window").css('marginTop','0').css('top',y+'px');
    $("#TB_window").css('marginLeft','0').css('left',x+'px');
}

function layer_div_pos(title,area,w,h,x,y) {
    layer(title,$(area).html(),w,h);
    $("#TB_window").css('marginTop','0').css('top',y+'px');
    $("#TB_window").css('marginLeft','0').css('left',x+'px');
}

function layer(title,content,w,h) {
	layer_load();
	layer_window(title,content,w,h);
}

function layer_load() {
	tb_show();
	$("#TB_overlay").unbind();
	$("#TB_overlay").fadeIn();
}

function layer_window(title,content,w,h) {

	if (title != "") {
		title = "<div id='TB_ajaxWindowTitle'>"+title+"</div>";
	} else {
		title = "";
	}

	if (content.indexOf("layer_hide()") == '-1') {
		hide = "<div id='TB_closeAjaxWindow'><a href='javascript:layer_hide();' id='TB_closeWindowButton'>&nbsp;</a></div>";
	} else {
		hide = "";
	}
	
	if (hide || title) {
		bar = "<div id='TB_title'>"+title+hide+"</div>";
	} else {
		bar = "";
	}
	
	var pagestyle = $("#container").attr("class");
	
	content = "<div class='"+pagestyle+"'><div class='inhalt'>"+bar+"<div id='TB_ajaxContent' class='TB_modal'><div class='layercontent'>"+content+"</div></div></div></div>";
	$("#TB_window").html(content);
	$("#TB_window").css({height: h+"px", width: w+"px"});
	TB_WIDTH = w;
	TB_HEIGHT = h;

	// standard von der thickbox
	tb_position();
	
	// positionfix (höhe) by LH.de
	layer_position();
		
	$("#TB_load").fadeOut("",function () {$("#TB_window").fadeIn("");} );
}

function layer_position() {
	// scrollbar machen
	$("#TB_window").css('position','absolute');
	
	// fensterdaten holen
	getSize();

	// top abständen entfernen
	$("#TB_window").css('marginTop','0').css('top','0');

	// höhe berechnen incl scroll
	var new_height = myScroll + ((myHeight - TB_HEIGHT) / 2);
	
	if (new_height<myScroll) {
		new_height = myScroll+10;
	}

	// und setzen
	$("#TB_window").css('top',new_height+"px");	
}


function getSize() {
	// Window Size

	if (self.innerHeight) { // Everyone but IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		myScroll = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.clientHeight) { // IE6 Strict
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		myScroll = document.documentElement.scrollTop;
	} else if (document.body) { // Other IE, such as IE7
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		myScroll = document.body.scrollTop;
	}

	// Page size w/offscreen areas

	if (window.innerHeight && window.scrollMaxY) {
		myScrollWidth = document.body.scrollWidth;
		myScrollHeight = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // All but Explorer Mac
		myScrollWidth = document.body.scrollWidth;
		myScrollHeight = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		myScrollWidth = document.body.offsetWidth;
		myScrollHeight = document.body.offsetHeight;
	}
}


function layer_hide_mitclose() {
	layer_hide();
}


function layer_hide() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("",function(){
		
		// wenn TB_load existiert soll es doch bitteschön mit ausfaden.
		if ($("#TB_load").css("display") == 'block') {
			var fadeouts = '#TB_overlay,#TB_load';
		} else {
			var fadeouts = '#TB_overlay';
		}
	
		$(fadeouts).fadeOut("", function() {
				$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
				$("#TB_load").remove();
		});	
	});
	
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
}

