(function($){
	$.fn.extend({
		dialog: function(options) {
			var defaults = {
				loadingImage: "style/images/ajax-loader.gif",
				height: 400,
				width: 800,
				buttons: '',
				close: ''
			};
			var options = $.extend(defaults, options);
			return this.each(function() {
				sessionwindowopen();
				var obj = $(this);
				var titel = obj.attr('title');
				var loadingImage = options.loadingImage;
				var width = options.width;
				var height = options.height;
				var fensterwidth = width - 30;
				var fensterheight = height - 45;
				var buttons = options.buttons;
				var close = options.close;
				try {
					if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
						$("body","html").css({height: "100%", width: "100%"});
						$("html").css("overflow","hidden");
						if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
							$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
						}
					}else{//all others
						if(document.getElementById("TB_overlay") === null){
							$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
						}
					}

					if(tb_detectMacXFF()){
						$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
					}else{
						$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
					}
					var buttonausgabe = "";
					for (var button in buttons) {
						buttonid = mergeStr(button);
						buttonausgabe = buttonausgabe+"<input type='button' class='footerbuttons' id='"+buttonid+"' name='"+button+"' value='"+button+"'/>";
					}
					if (buttonausgabe == "") {
					} else {
						buttonausgabe = "<div id='TB_footer'><div id='TB_footerbuttons'>"+buttonausgabe+"</div></div>";
					}
					if(titel===null){titel="";}
					$("body").append("<div id='TB_load'><img src='"+loadingImage+"' alt='Lade Daten...' /></div>");//add loader to the page
					$('#TB_load').show();//show loader
					$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+titel+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Fenster schlie&szlig;en'><img src='style/images/loeschen.gif' alt='Fenster schlie&szlig;en'/></a></div></div><div id='TB_ajaxContent' style='width:"+fensterwidth+"px;height:"+fensterheight+"px'></div>"+buttonausgabe);
					var clone = $(this).clone();
					clone.appendTo("#TB_ajaxContent");
					clone.show();
					tb_position(width,height);
					$("#TB_load").remove();
					$("#TB_window").css({display:"block"});
					$("#TB_closeWindowButton").click(close);
					$("#TB_closeWindowButton").click(tb_remove);


					for (button in buttons) {
						buttonid = mergeStr(button);
						$("#"+buttonid).click(buttons[button]);
					}

					document.onkeyup = function(e){
						if (e == null) { // ie
							keycode = event.keyCode;
						} else { // mozilla
							keycode = e.which;
						}
						if(keycode == 27){ // close
							tb_remove(close);
						}
					};
					$(this).clone().appendTo("body");
					$(this).remove();
				} catch(e) {
					//nothing here
				}
			});
		}
	});
})(jQuery);



function tb_position(width,height) {
$("#TB_window").css({marginLeft: '-' + parseInt((width / 2),10) + 'px', width: width + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((height / 2),10) + 'px'});
	}
}

function tb_remove() {
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",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","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	sessionwindow();
	return false;
}


function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function mergeStr (Str){
	newStr = [];
	for(i=0;i<Str.length;i++){
		sub=Str.substr(i,1);
		if(sub!=" "){
			newStr.push(sub);
		}
	}
	return newStr.join("");
}
