function extendedDiv(settings){
	
	this.settings = settings;

	this.state = "hidden";
	this.mX = 0;
	this.mY = 0;
	this.content = null;
	this.systembar = null;
	this.curPos = null;
	this.addSize = new Object();
	this.addSize["width"] = 0;
	this.addSize["height"] = 0;
	this.stage = "hidden";
	this.listener = new Object();
	
//	this.wrappers = new Array();

	this.registerListener = function(event, listenerFunction){
		if(!this.listener[event]){
			this.listener[event] = new Array();
		}
		this.listener[event].push(listenerFunction);
		return this.listener[event].length - 1;
	}

	this.removeListener = function(event, listenerId){
		this.listener[event][listenerId] = null;	
	}

	this.addButton = function(text, onClickFunc){

		var footer = getChildrenByClassName(this.content, "footer")[0];
		if(!footer){
			footer = document.createElement("div");
			CssClasses(footer).add("footer");
			this.content.appendChild(footer);
		}
		var button = document.createElement("button");
		button.innerHTML = text;
		
		attachEventToObj(button, onClickFunc, "onclick" );
/*		
		button.onclick = function(e){
			call_user_func(onClickFunc);
		}
*/				
		footer.appendChild(button);
		this.redraw();
		return button;
	}

	this.processListener = function(event){

		var ret = true;		

		if(this.listener[event]){
			for(var key in this.listener[event]){
				if(this.listener[event][key]){
					ret = (ret && call_user_func(this.listener[event][key], event, this));
				}
			}		
		}

		return ret;

	}

	this.setCloseOnBlur = function(){
		this.registerListener("onBlur", new Array(this, "close"));
	}

	this.blur = function(){
		this.processListener("onBlur");			
	}


	this.init = function() {
		this.processListener("onInit");
		this.div = document.createElement("div");
		this.div.backRef = this;
		this.div.id = this.settings["name"];
		this.div.className = this.settings["classname"];
		this.div.style.display = "none";

		if(this.settings["addClassname"] != ""){
			CssClasses(this.div).add(this.settings["addClassname"]);	
		}
		
		if(this.settings["sysbar"]){
			var systembar = document.createElement("div");
			systembar.className = "systembar";
			systembar.popupTitle = document.createTextNode(this.settings["title"]);
			systembar.icons = document.createElement("div");
			systembar.icons.className = "icons";
			if (settings["minable"]) {
				systembar.icons.minmax = document.createElement("a");
//				systembar.icons.minmax.backRef = this;
				systembar.icons.minmax.href = "#";
				systembar.icons.minmax.innerHTML = "_";
				systembar.icons.minmax.className = "min";
				systembar.icons.appendChild(systembar.icons.minmax);
				systembar.icons.appendChild(document.createTextNode(" "));
			}
			
			if(settings["closeable"]) {
			
				systembar.icons.close = document.createElement("a");
				systembar.icons.close.href = "#";
				systembar.icons.close.innerHTML = "x";
				systembar.icons.close.className = "close";
				systembar.icons.appendChild(systembar.icons.close);
	
			}
			
			systembar.appendChild(systembar.popupTitle);
			systembar.appendChild(systembar.icons);
			systembar.backRef = this;
	
			if (settings["dragable"]) {
				if(!document.onmousemove){
					document.onmousemove = function(e){
						DBMGpopup.moveFrame(e);		
						return true;
					}
				
					document.onmouseup = function(e){
						DBMGpopup.stopDrag(e);		
					}
				
				}

			}
			
			this.systembar = systembar;
			this.setEventHandlersForSystemBar();
		}

		this.content = document.createElement("div");
		this.content.className = "content";

		var content = this.settings["content"];

	 	if(typeof(content) == "string") {
			if(content.substr(0, 7) == "http://"){
				this.setContent("url", content); 
			}else{
				this.setContent("html", content); 
			}
		}else{
			this.setContent("dom", content); 
		}

		this.div.appendChild(this.content);

		if(this.settings["sysbar"]){
			if(this.settings["sysbarPos"] == "top"){
				this.div.insertBefore(this.systembar, this.content);
			}else{
				this.div.appendChild( this.systembar);
			}	
		}

		if(this.settings["canBeFocused"]){
			var tttt = new Array(this, "focus");
			this.div.onmousedown = function(evt){
				call_user_func(tttt);
			}
		}

		if(this.settings["shadow"]){
			this.addShadow();
		}

		if(this.settings["addIFrame"] && document.all){
			this.addIframe();
		}

		if(document.getElementsByTagName("body")[0]){
			this.appendElement();
		}else{
			attachEventToObj(window, new Array(this, "appendElement"), "onload");
		}
	}

	this.appendElement = function(){
		document.getElementsByTagName("body")[0].appendChild(this.div);			
	}
	
	this.addIframe = function(){
			if(this.ifr){
				this.div.removeChild(this.ifr);			
			}else{
				this.registerListener("onBlur", new Array(this, "addIframe"));
				this.registerListener("onFocus", new Array(this, "addIframe"));
			}
			this.ifr = this.div.insertBefore(document.createElement("iframe"), getChildrenByTagName(this.div, "div")[0]);
			this.ifr.className = "ifr";
	}
	
	this.addShadow = function(){
		var first = this.addWrapper();
		var second = this.addWrapper();
		if(this.settings["shadowMethod"] == "alpha"){
			first.className = "shadow1alpha";
			second.className = "shadow2alpha";
			CssClasses(this.div).add('shadow0alpha');
		}else{
			first.className = "shadow1";
			second.className = "shadow2";
			CssClasses(this.div).add('shadow0');
		}
		this.addSize["width"] = parseInt(getStyle(this.div, "margin-left"));
		this.addSize["height"] = parseInt(getStyle(this.div, "margin-top"));
	}
	
	this.addWrapper = function(){
		var nw = document.createElement("div");
		moveAllChilds(this.div, nw);
//		this.wrappers.push(nw);
		removeAllChilds(this.div);
		this.div.appendChild(nw);
		return nw
	}
	
	this.setEventHandlersForSystemBar = function(){
			if (this.settings["minable"]) {
				var test = function(e){
					var targ = getEventTarget(e);
					targ = popupGetParentFrame(targ);
					if(targ){
						targ.minimize();
					}
					return false;
				}	
				
				this.systembar.icons.minmax.onclick = test;
			
				this.systembar.ondblclick = test;
			}
			
			if (this.settings["dragable"]) {
				this.systembar.onmousedown = function(e){
					var targ = getEventTarget(e);
					targ = popupGetParentFrame(targ);
					if(targ){
						targ.startDrag(e);
					}
					return false;
				}		
			}

			if (this.settings["closeable"]) {
				this.systembar.icons.close.backRef = this;
				this.systembar.icons.close.onclick = function(e){
					var targ = getEventTarget(e);
					targ = popupGetParentFrame(targ);
					if(targ){
						targ.close();
					}
					return false;
				}
			}
	}
	
	this.setContent = function(type, data) {
		this.processListener("onSetContent");
		removeAllChilds(this.content);
		if (type == "html") {
			this.content.innerHTML = data;
		}else if(type == "url") {
			dbmgError("Not yet implemented");
/*
			var contentFrame = document.createElement("iframe");
			contentFrame.src = data;
			content.appendChild(this.contentFrame);
*/
		}else if(type == "dom"){
			this.content.appendChild(data);
		}else{
			dbmgError("Wrong point");
		}
		if(this.stage == "showed"){
			this.redraw();
		}	
	}

	this.show = function() {
			this.processListener("onShow");
			this.div.style.display = "block";		
			this.redraw();
			this.focus();
			this.stage = "showed";
	};

	this.hide = function(){
		this.processListener("onHide");
		this.div.style.display = "none";		
		this.stage = "hidden";
	}

	this.focus = function(){

		if(this.div.style.zIndex < DBMGpopup.maxZindex){
			this.processListener("onFocus");
			DBMGpopup.maxZindex++;
			this.div.style.zIndex = DBMGpopup.maxZindex;
			DBMGpopup.sendBlurForAllBut(this.settings["name"]);
		}
	}

	this.exlusiveFocus = function(){
		if(this.settings["canBeFocused"]){
			var blendwin = DBMGpopup.makeBlendWindow();
			this.focus();
			this.registerListener("onClose", new Array(blendwin, "close"));
		}
	}



	this.redraw = function(){
		if(!this.curPos){
			var initLeft = parseInt(getStyle(this.div, "offsetLeft"));
			var initTop = parseInt(getStyle(this.div, "offsetTop"));
			if(!initLeft){
				this.div.style.left = "0";
			}
			if(!initTop){
				this.div.style.top = "0";
			}
		}
		this.processListener("onRedraw");
		if(this.settings["width"] != "0"){
			this.div.style.width = this.settings["width"];
		}
		var width = parseInt(getStyle(this.div, "offsetWidth"));

		if(this.settings["height"] != "0"){
			this.div.style.height = this.settings["height"];
		}
		var height = parseInt(getStyle(this.div, "offsetHeight"));
		var winSize = getWinSize();

		if(!this.curPos){

			var cssLeft = parseInt(getStyle(this.div, "offsetLeft"));
			var cssTop = parseInt(getStyle(this.div, "offsetTop"));
			
			var left = "";
			var top = "";
			
			if(this.settings["xposition"] == "fromCSS"){
				left = cssLeft + "px";
			}else if(this.settings["xposition"] == "center"){
				left = (winSize["width"]/2 - width/2) + "px";
			}else{
				left= this.settings["xposition"];
			}

			if(this.settings["yposition"] == "fromCSS"){
				top = cssTop + "px";
			}else if(this.settings["yposition"] == "center"){
				top = (winSize["height"]/2 - height/2) + "px";
			}else{
				top = this.settings["yposition"];
			}
						
			this.div.style.left = left;
			this.div.style.top = top;
			this.curPos = new Object();
			this.curPos["left"] = parseInt(getStyle(this.div, "offsetLeft"));
			this.curPos["top"] = parseInt(getStyle(this.div, "offsetTop"));
			if(isNaN(this.curPos["left"])){
				this.curPos["left"] = 0;
			}
			if(isNaN(this.curPos["top"])){
				this.curPos["top"] = 0;
			}
		}

		this.div.style.left = this.curPos["left"] + "px";
		this.div.style.top = this.curPos["top"] + "px";
		
		this.div.style.width = width + "px";
		



//		this.div.style.height = height + "px";	
	}

	this.movePixel = function(offsetX, offsetY){
		this.processListener("onMovePixel", offsetX, offsetY);
		var winSize = getWinSize();
		var width = parseInt(getStyle(this.div, "offsetWidth"));
		var height = parseInt(getStyle(this.div, "offsetHeight"));

		if( this.curPos["left"] + width + offsetX + this.addSize["width"] >= winSize["width"]) {
			offsetX = winSize["width"] - this.curPos["left"] - this.addSize["width"] - width - 1;
		}

		if(	this.curPos["left"] + offsetX < 0 ){
			offsetX = -this.curPos["left"];		
		}

		this.curPos["left"] += offsetX;
		this.div.style.left = this.curPos["left"] + "px";	

		if( this.curPos["top"] + height + offsetY + this.addSize["height"] >= winSize["height"]) {
			offsetY = winSize["height"] - this.curPos["top"] - height - this.addSize["height"]- 1;
		}

		if(	this.curPos["top"] + offsetY < 0 ){
			offsetY = -this.curPos["top"];		
		}

		this.curPos["top"] += offsetY;
		this.div.style.top = this.curPos["top"] + "px";

	}

	this.startDrag = function(evt) {
		this.processListener("onStartDrag");
		if(this.settings["dragable"]){
			if (!evt) {	
				evt = window.event;
			}
			this.mX = evt.clientX - this.curPos["left"];
			this.mY = evt.clientY - this.curPos["top"];
			DBMGpopup.frameDragged = this;
			this.focus();
			return false;
		}
	};

	this.getFirstWrapper = function (){
		return this.content.parentNode;
	}


	this.minimize = function(){
		if(this.settings["minable"]){
			this.processListener("onMinimize");
			this.hide();
			var temp = this.getFirstWrapper();
		
			var sysBar = temp.removeChild(this.systembar);
			sysBar.icons.minmax.innerHTML = "&macr;";
			
			var test = function(e){
					var targ = getEventTarget(e);
					targ = popupGetParentFrame(targ);
					if(targ){
						targ.maximize();
					}
					return false;
				}
			
			sysBar.icons.minmax.onclick = test;
			sysBar.onmousedown = test;
			
			DBMGpopup.frameDragged = null;
			
			CssClasses(sysBar).add('minimized');
			DBMGpopup.minimizedFrames[this.settings["name"]] = sysBar;
			DBMGpopup.minContainerRedraw();	
		}
		this.stage = "minimized";
		return false;
	}

	this.maximize = function(){
		this.processListener("onMaximize");
		var ttt = this.getFirstWrapper();
		if(this.settings["sysbarPos"] == "top"){
			ttt.insertBefore(this.systembar, this.content);
		}else{
			ttt.appendChild( this.systembar);
		}
		this.systembar.icons.minmax.innerHTML = "_";
		this.setEventHandlersForSystemBar();
		CssClasses(this.systembar).remove('minimized');
		DBMGpopup.minimizedFrames[this.settings["name"]] = null;
		DBMGpopup.minContainerRedraw();	
		this.show();	
	}

	this.close = function(){
		if(this.stage != "closed"){
			this.stage = "closed";
			document.getElementsByTagName("body")[0].removeChild(this.div);
			DBMGpopup.minimizedFrames[this.settings["name"]] = null;
			DBMGpopup.minContainerRedraw();
			DBMGpopup.elements[this.settings["name"]] = null;	
			this.blur();
			this.processListener("onClose");
		}
	}
	
/* compatabilitat mit alte Version */

	this.setContentDOM = function(ob){
		this.setContent("dom", ob);
	}	
/* ------------------------------- */	
	
	this.init();

}

function DBMGpopupLib(){

	this.maxZindex = 100;
	this.browser = (document.all)?"IE":"MOZILLA";
	this.elements = new Object();
	this.frameDragged = null;
	this.minimizedFrames = new Object();
		
	this.defSettings = {	xposition: 		"center",
							yposition: 		"center",
							title: 			"window",
							dragable: 		true,
							classname: 		"DBMGpopup",
							addClassname: 	"",
							closeable:		true,
							minable: 		true,
							sysbar: 		true,
							width: 			"0",
							height: 		"0",
							content: 		"empty",
							show: 			true,
							sysbarPos:  	"top",
							shadow:			true,
							shadowMethod:	"background",
							canBeFocused: 	true,
							addIFrame:		true
							 
						};

	this.init = function (name, settings){

		attachEventToObj(document, new Array(this, "checkBlur"), "onmousedown");

		var initsettings = new Array();

		if(!name){
			name = generatepass();		
			while(this.elements[name]){
				name = generatepass();	
			}
		}

		for (var eigenschaft in this.defSettings){
			if(	(eigenschaft == "xposition" || 
				eigenschaft == "yposition" ||
				eigenschaft == "width" ||
				eigenschaft == "height" )  &&
					typeof(settings[eigenschaft]) == "number") {

				settings[eigenschaft] = settings[eigenschaft] + "px";
					
			}
			initsettings[eigenschaft] = (settings[eigenschaft] != null)?settings[eigenschaft]:this.defSettings[eigenschaft];

		
		}
		
		initsettings["name"] = name;

		if(!this.elements[name]){
			var element = new extendedDiv(initsettings);
			this.elements[name] = element;
		}
		
		if(initsettings["show"]){
			if(this.elements[name].stage == "minimized"){
				this.elements[name].maximize();
			}else if(this.elements[name].stage == "hidden"){
				this.elements[name].show();
			}else{
				this.elements[name].focus();
			}
		}

		return this.elements[name];
	
	}
	
	this.moveFrame = function(evt) {
	
		if (!evt) {
			evt = window.event;
		}
		if (this.frameDragged != null) {
		
			var offsetX = evt.clientX - this.frameDragged.curPos["left"] - this.frameDragged.mX;
			var offsetY = evt.clientY - this.frameDragged.curPos["top"] - this.frameDragged.mY;
			this.frameDragged.movePixel(offsetX, offsetY);
		}
	}

	this.stopDrag = function(evt) {
		this.frameDragged = null;
	};

	this.hasMinimised = function(){
		var ret = false;
		for(var frameId in this.minimizedFrames){
			if(this.minimizedFrames[frameId] != null){	
				ret = true;
				break;		
			}
		}
		return ret;
	}

	this.minContainerRedraw = function(){
		var mc = document.getElementById("DBMGpopup_minContainer");
		
		if(this.hasMinimised()){
			if(mc){
				removeAllChilds(mc);
			}else{
				mc = document.createElement("div");
				mc.id = "DBMGpopup_minContainer";
				document.getElementsByTagName("body")[0].appendChild(mc);
			}

			for(var frameId in this.minimizedFrames){
				if(this.minimizedFrames[frameId] != null){	
					mc.appendChild(this.minimizedFrames[frameId]);
				}
			}
		}else if(mc){
				document.getElementsByTagName("body")[0].removeChild(mc);
		}
	}

	this.makeBlendWindow = function(){
		return DBMGpopup.init("systemBlendWindow", {
								addClassname: "blendLayer",
								width: "100%",
								height: "100%",
								shadow: false,
								sysbar: false,
//								content: "",
								content: "<iframe onMouseMove=\"DBMGpopup.moveFrame()\" style=\"posistion: absolute; width: 100%; height: 100%;\"><\/iframe>",
								show: true,
								canBeFocused: false
								}
							);
	}

	this.checkBlur = function(e){
		var targFrame = "";
		var targ = getEventTarget(e);
		targ = popupGetParentFrame(targ);
		if(targ){
			targFrame = targ.settings["name"];
		}
		this.sendBlurForAllBut(targFrame);
	}

	this.sendBlurForAllBut = function(framename){
		for(var cname in this.elements)	{
			if(this.elements[cname] && (cname != framename)){
				this.elements[cname].blur();
			}
		}
	}
}

function popupGetParentFrame(el){
		var ret = null;
		var temp = el;
		do{
			if(temp.backRef && (temp.backRef instanceof extendedDiv)){
				ret = temp.backRef;
				break;
			}else{
				temp = temp.parentNode;
			}
		}while(temp.parentNode);

		return ret;
}

DBMGpopup = new DBMGpopupLib();
