/**
 * Project:     aimscore - All Information Management System core
 * File:        gui.js
 *
 * see aimscore.php for licence informations
 *
 * @copyright 2007-2009 Andrej Zieger
 * @author Andrej Zieger <andrej@ziegers.net>
 */

Number.prototype.format = function(places,seperator,fixed){
	var temp = (fixed ? this.toFixed(fixed) : this).toString();
	start = temp.lastIndexOf(".");
	start -= 3;
	
	temp = temp.replace(/\./g, seperator);
	
	while(start > 0){
		temp = temp.substr(0,start)+places+temp.substr(start);
		start -= 3;
	}
	return temp;
}

var gui = new Object();
gui.document = new Object();
gui.document.old = new Object();
gui.windows = new Array();
gui.guiObjects = new Array();

gui.effectQueues = 0;

gui.WND_TYPE_DLG = 1;
gui.WND_TYPE_MODAL = 3;
gui.WND_OK = 4;

gui.WND_SPEC_DEFAULT = {titlebar:true,
				resize:true,
				minimize:true,
				drag:true
				}

			
			
			

if(typeof homePath == "undefined")
	homePath = "";

gui.MSG_TYPE_PRIO_LOW = {icon:homePath+"/layout/images/gui/message_notify.gif",duration:10};
gui.MSG_TYPE_PRIO_DEFAULT = {icon:homePath+"/layout/images/gui/message_default.gif"};
gui.MSG_TYPE_PRIO_HIGHT =  {icon:homePath+"/layout/images/gui/message_error.gif"};
gui.MSG_TYPE_PRIO_CRITICAL =  {icon:homePath+"/layout/images/gui/message_critical.gif"};

gui.MSG_TYPE_NOTIFY = gui.MSG_TYPE_PRIO_LOW;
gui.MSG_TYPE_WARNING = gui.MSG_TYPE_PRIO_DEFAULT;
gui.MSG_TYPE_ERROR = gui.MSG_TYPE_PRIO_HIGHT;
gui.MSG_TYPE_CRIT = gui.MSG_TYPE_PRIO_CRITICAL;

gui.addGuiObject = function(obj){
	gui.guiObjects.push(obj);
}

gui.render = function(){
	
	var time = new Date().getTime();
	for(var z = 0; z < gui.windows.length;z++){
		gui.windows[z].render();
	}
	
	for(var z = 0; z < gui.guiObjects.length;z++){
		if(gui.guiObjects[z].render)
			gui.guiObjects[z].render();
	}
	
	
}

gui.removeWindow = function(window){
	for(var i =0;i < gui.windows.length;i++)
		if(gui.windows[i] == window)
			gui.windows.splice(i,1);
}

//Knotenkopie samt event handlern
gui.cloneNodeXl = function(node){
	if(!node.cloneNode)
		return;
	var copynode = node.cloneNode(false);
	for(var attr in node){
		if(attr.indexOf("on") == 0)
			copynode[attr] = node[attr];
	}
	for(var i = 0; i < node.childNodes.length; i++){
		copynode.appendChild(gui.cloneNodeXl(node.childNodes[i]));
	}
	return copynode;
}

gui.cloneNodeTo = function(node,parent,replace){
	if(!node.cloneNode)
		return;
	var copynode = node.cloneNode(false);
	for(var attr in node){
		if(attr.indexOf("on") == 0)
			copynode[attr] = node[attr];
	}
	for(var i = 0; i < node.childNodes.length; i++){
		copynode.appendChild(gui.cloneNodeXl(node.childNodes[i]));
	}
	
	if(parent&&copynode)
		if(!replace)
			parent.appendChild(copynode);
		else if(replace.parentNode == parent)
			gui.replaceNode(copynode,replace);
	return copynode;
}

gui.deleteNode = function (node){
	
	while(node.childNodes.length)
		gui.deleteNode(node.lastChild);
	
	//Eventhandler unhooken... liste machen =(
	if(node.onclick)
		node.onclick = null;
	if(node.onmousedown)
		node.onmousedown = null;
	if(node.onmouseup)
		node.onmouseup = null;
	if(node.onmouseover)
		node.onmouseover = null;
	if(node.onmousemove)
		node.onmousemove = null;
	if(node.onkeydown)
		node.onkeydown = null;
	if(node.onkeyup)
		node.onkeyup = null;
	
	if(!node.parentNode)
		return;

	node.parentNode.removeChild(node);
}

gui.replaceNode = function(newnode,node){
	var time = new Date().getTime();
	while(node.childNodes.length){
		gui.deleteNode(node.lastChild);
		
	}
	if(!node.parentNode)
		return;
	
	node.parentNode.replaceChild(newnode,node);
}

gui.removeChilds =function (node){
	if(!node)
		return;
	
	while(node.childNodes.length)
		if(node.lastChild)
			gui.deleteNode(node.lastChild);
}

gui.createElementTo = function(ele,parent,replace,before){
	var element = document.createElement(ele);
	replace = (replace)? replace : "";
	before = (before)? before : "";
	if(parent&&element)
		if(before.parentNode == parent)
			parent.insertBefore(element,before);
		else if(replace.parentNode == parent)
			gui.replaceNode(element,replace);
		else
			parent.appendChild(element);
		
	return element;
}

gui.createTextNodeTo = function(str,parent,replace){
	var element = document.createTextNode(str);
	if(parent&&element)
		if(!replace)
			parent.appendChild(element);
		else if(replace.parentNode == parent)
			gui.replaceNode(element,replace);

	return element;
}

gui.document.disableSelect = function(){

	if(document.onselectstart)
		this.old.onselectstart = document.onselectstart;
	document.onselectstart = new Function ("return false")
			
	if (window.sidebar){
			if(document.onmousedown)
				this.old.onmousedown = document.onmousedown;
			document.onmousedown = function(e){return false};
		}
}
	
gui.document.enableSelect = function(){
		document.onselectstart=this.old.onselectstart;
			
		if (window.sidebar){
				document.onmousedown=this.old.onmousedown
			}
	}
	

gui.focus = function(element){

	parentO = element.parentNode;
	if(!element.style.zIndex)
		element.style.zIndex = 1;

	if(parentO)
	for(var i = 0; i < parentO.childNodes.length; i++){
		if(!parentO.childNodes[i].style)
			continue;
		if(( parentO.childNodes[i].style.zIndex*1 >= element.style.zIndex*1) && (element != parentO.childNodes[i])){
			element.style.zIndex = parentO.childNodes[i].style.zIndex*1 +1;

		}
	}
	
	if(element.style.zIndex < 1000)
	element.style.zIndex = 1000;
}
	
gui.cWindow = function (content,parent,title,before,spec){
	/*
	##		Model
	*/
	this.autoSpec = function(){
		var defaultSpec = {
			titlebar:true,
			resize:true,
			minimize:true,
			drag:true
		};
		
		return defaultSpec;
	}
	
	this.getDocument = function(){
		return doc;	
	}
	
	this.getWindow = function(){
		return win;	
	}
	
	this.destroy = function(){
		gui.removeWindow(this);
		gui.deleteNode(win);

		if(modal){
			gui.deleteNode(modal);
		}
		if(modalbg){
			gui.deleteNode(modalbg);
		}
		
		win = null;
		titlebar = null;
		closeButton = null;
		doc = null;
		controlbar = null;
		
		corner = null;
		borderRight = null;
		borderBottom = null;
	}
	
	/*
	##		Präsentation
	*/
	this.appendTo = function(parent){
		parent.appendChild(win);
	}
	this.insertTo = function(parent,before){
	
		if(parent == win.parentNode)
			return;
			
		if(!before)
			before = parent.firstChild;
		parent.insertBefore(win,before);
		this.parent = parent;
	}
	
	this.show = function(){
		if(!win)
			return false;
		if(!this.before)
			this.before = parent.firstChild;
		if(this.parent)
		if(this.before == -1)
			this.parent.appendChild(win);
		else
			this.parent.insertBefore(win,this.before);
	}
	
	this.focus = function(){
		gui.focus(win);
	}
	
	this.hide = function(){
		win.style.display = "none";
	}
	this.close = function(){
		win.parentNode.removeChild(win);
		if(modal){
			if(modal.parentNode)
				modal.parentNode.removeChild(modal);
			modal.appendChild(win);
		}
		if(modalbg){
			if(modalbg.parentNode)
				modalbg.parentNode.removeChild(modalbg)
		}
		
		if(this.onclose)
			this.onclose();
	}
	this.setTitle = function(title){
		if(title.nodeName)
			titlebar.replaceChild(title,titlebar.firstChild);
		else
			titlebar.replaceChild(document.createTextNode(title),titlebar.firstChild);
	}
	
	this.addMenue = function(menue,parent){
		var ul = document.createElement("ul");
		win.insertBefore(ul,doc);
		ul.className = "controlbar";
		for(item in menue){
			var li = null;
			//Wenn es ein normaler Menüpunkt ist
			if(isFunction(menue[item])){
				li = document.createElement("li");
				li.className = "menueItem";
				
				var a = document.createElement("a");
				a.className = "menueItemButton";
				a.onclick = menue[item];
				a.appendChild(document.createTextNode(item));
				
				li.appendChild(a);
				
			}
			//Menüpunkt mit unterpunkten
			if(isObject(menue[item])||isArray(menue[item])){
				
			}
			//ein html element, das in die leiste soll (buttonleiste z.b.)
			if(menue[item].tagName){
				
			}
			if(li)
				if(li.tagName)
					ul.appendChild(li);
		}
		this.menueCount++;
		
		this.render();
	}
	
	this.appendGuiChild = function(element){
		element.element = document.createElement("div");
		doc.appendChild(element.element);
		
		if(element.render)
			element.render();
		
		guiChilds.push(element);
	}
	
	
	this.render = function(){
		for(var i = 0; i < guiChilds.length;i++){
			if(guiChilds[i].render) 
				guiChilds[i].render();
		}
		win.style.paddingBottom = (5 + (25*(this.spec['titlebar'] ? 1 : 0)) + (5*(this.spec['resize'] ? 1 : 0)) + (20*this.menueCount))+"px";
	}
	
	
	this.setHeight = function(height){
		win.style.height = height;
	}
	
	this.getHeight = function(){
		return win.style.height;
	}
	
	this.setWidth = function(width){
		win.style.width = width;
	}
	
	this.getWidth = function(){
		return win.style.width;
	}
	
	this.setPosition = function(x,y){
		win.style.left = x;
		win.style.top = y;
	}
	
	/**
	 * Setzt das Fenster zur Mausposition
	 */
	this.setPositionToMouse = function(e,offsetx,offsety){
		var xPos = (e.pageX) ? e.pageX : e.clientX ;
		var yPos = (e.pageY) ? e.pageY : e.clientY ;
		
		//for(var x in e)
			//debug(x + " = " + e[x]);
		
		if(offsetx){
			if((offsetx+"").lastIndexOf("%") > 0){
				xPos = xPos + win.offsetWidth*parseInt(offsetx)/100;
			}else{
				xPos = xPos + 1*offsetx;
			}
		}
		
		if(offsety){
			if((offsety+"").lastIndexOf("%") > 0){
				yPos = yPos + win.offsetHeight*parseInt(offsety)/100;
			}else{
				yPos = yPos + 1*offsety;
			}
		}
		this.setPosition(xPos+"px",yPos+"px");
				
	}
	/*
	##		Steuerung
	*/
	
	this.mouseOver = function(e){

	}

	this.mouseOut = function(e){
	}
	

	
	this.startSize= function(e,direction){
		var e = e || window.event;
		var x = e.pageX || e.clientX;
		var y = e.pageY || e.clientY;
		
		this.sizeing = new Object();
		this.sizeing.width = win.offsetWidth;
		this.sizeing.height= win.offsetHeight;
		this.sizeing.mouseX 	= x;
		this.sizeing.mouseY 	= y;
		this.sizeing.layerX	= e.layerX || e.offsetX;
		this.sizeing.layerY	= e.layerY || e.offsetY;
		this.sizeing.direction  = direction;
		
		if(window.onmousemove)
			this.sizeing.oldMouseMove = window.mousemove;
		
		if(window.onmouseup)
			this.sizeing.oldMouseUp = window.mouseup;
		
		window.onmousemove 	= function(e){instance.size(e,this);};
		window.onmouseup 		= function(e){instance.stopSize(e,this);};
	}
	
	this.stopSize = function(e,oldthis){
		var e = e || window.event;
		
		window.onmousemove = this.sizeing.oldMouseMove;
		window.onmouseup = this.sizeing.oldMouseUp;
		
		if(window.onmouseup)
			window.onmouseup.call(oldthis);
		
		this.dragging = null;
	}
	
	this.size = function(e,oldthis){
		var e = e || window.event;
		var x = e.pageX || e.clientX;
		var y = e.pageY || e.clientY;
		
		if(!this.sizeing)
			return;
		if((((y - this.sizeing.mouseY ) > 0)&&(this.sizeing.direction&4))
		 ||(((y - this.sizeing.mouseY ) < 0)&&(this.sizeing.direction&1)))
			win.style.height = this.sizeing.height +(y - this.sizeing.mouseY )- parseInt(win.style.paddingBottom)-2 +"px" ;

		if((((x - this.sizeing.mouseX ) > 0)&&(this.sizeing.direction&2))
		 ||(((x - this.sizeing.mouseX ) < 0)&&(this.sizeing.direction&8)))
		win.style.width = this.sizeing.width +(x - this.sizeing.mouseX ) +"px" ;
		
		if(this.sizeing.oldMouseMove)
			this.sizeing.oldMouseMove.call(oldthis);
	}
	
	this.startDrag = function(e){
		var e = e || window.event;
		var x = e.pageX || e.clientX;
		var y = e.pageY || e.clientY;
		
		this.dragging = new Object();
		this.dragging.mouseX 	= x;
		this.dragging.mouseY 	= y;
		this.dragging.layerX	= e.layerX || e.offsetX;
		this.dragging.layerY	= e.layerY || e.offsetY;
		this.dragging.top		= win.offsetTop;
		this.dragging.left	= win.offsetLeft;
		
		if(window.onmousemove)
			this.dragging.oldMouseMove = window.mousemove;
		
		if(window.onmouseup)
			this.dragging.oldMouseUp = window.mouseup;
		
		window.onmousemove 	= function(e){instance.drag(e,this);};
		window.onmouseup 		= function(e){instance.stopDrag(e,this);};
		
	}
	
	this.stopDrag = function(e,oldthis){
		var e = e || window.event;
		
		window.onmousemove = this.dragging.oldMouseMove;
		window.onmouseup = this.dragging.oldMouseUp;
		
		if(window.onmouseup)
			window.onmouseup.call(oldthis);
		
		this.dragging = null;
	}
	
	this.drag = function(e,oldthis){
		var e = e || window.event;
		var x = e.pageX || e.clientX;
		var y = e.pageY || e.clientY;
		
		if((!this.dragging)||(!this.spec.drag))
			return;
		
		if(win.style.position != "absolute")
			win.style.position ="absolute";
		if(win.style.margin !== 0)
		win.style.margin = 0;
		
		win.style.top = ( y  - this.dragging.layerY) +"px" ;
		win.style.left = ( x - this.dragging.layerX) +"px" ;
		
		if(this.dragging.oldMouseMove)
			this.dragging.oldMouseMove.call(oldthis);
	}
	
	
	/*
	##		Constuction
	*/
	
	this.title = title;
	this.content = content;
	this.parent = parent ? parent : document;
	this.before = before;
	this.spec = spec ? copyObject(spec) : this.autoSpec();
	
	this.menueCount = 0;
	
	var guiChilds = new Array();
	
	var win = document.createElement("div");
	var titlebar = document.createElement("div");
	var closeButton = document.createElement("a");
	var doc = document.createElement("div");
	var controlbar = document.createElement("div");
	
	var corner = document.createElement("div");
	var borderRight = document.createElement("div");
	var borderBottom = document.createElement("div");
	var instance = this;
	
	win.onclick = function(e){instance.focus()};
	
	titlebar.className = "titlebar";
	var dragObject = titlebar;
	if(!this.spec.titlebar && this.spec.drag)
		dragObject = win;
	
	dragObject.onmousedown = function(e){instance.startDrag(e)};
	dragObject.onmouseover = function(e){ gui.document.disableSelect.call(gui.document,e)};
	dragObject.onmouseout  = function(e){ gui.document.enableSelect.call(gui.document,e)};
	
	
	
	controlbar.className = "controlbar";
	
	closeButton.className = "closebutton";
	closeButton.appendChild(document.createTextNode("x"));
	closeButton.onclick = function(){instance.close()};
	
	doc.className = "document";
	corner.className = "corner";
	borderRight.className = "rightBorder";
	borderBottom.className = "bottomBorder";


	corner.onmousedown = function(e){instance.startSize(e,15)};
	borderRight.onmousedown = function(e){instance.startSize(e,10)};
	borderBottom.onmousedown = function(e){instance.startSize(e,5)};
	
	
	
	if(!before)
		before = parent.firstChild;
	if(parent)
		if(before == -1)
			parent.appendChild(win);
		else
			parent.insertBefore(win,before);
	
	win.appendChild(closeButton);
	if(this.spec.titlebar)
		win.appendChild(titlebar);
	//win.appendChild(controlbar);
	if(content)
	if(content.nodeName)
		doc.appendChild(content);
	else 
		doc.appendChild(document.createTextNode(content));
	
	if(!title)
		title = " ";
	if(title.nodeName)
		titlebar.appendChild(title);
	else
		titlebar.appendChild(document.createTextNode(title));
	
		
	win.appendChild(doc);
	if(this.spec.resize){
		win.insertBefore(borderRight,doc);
		win.appendChild(corner);
		win.appendChild(borderBottom);
	}
	
	win.className = "window";

	if(this.spec.type & gui.WND_TYPE_DLG){
		win.className += " dialog";
	}
	
	if((this.spec.type & gui.WND_TYPE_MODAL) == gui.WND_TYPE_MODAL){
		//win.className += " dialog";
		var modal = document.createElement("div");
		modal.style.position = "fixed";
		modal.style.top = "0px";
		modal.style.left = "0px";
		modal.style.width = "100%";
		modal.style.height = "100%";
		modal.style.zIndex = "10000";
		
		var modalbg = modal.cloneNode(true);
		modalbg.style.backgroundColor = "#222222";
		modalbg.style.zIndex = "9999";
		modalbg.style.width = document.body.offsetWidth+"px";
		modalbg.style.height = document.body.offsetHeight+"px";
		
		var transpEffect = new Effect.Opacity(modalbg, {duration:0.0,  to:0.5});
		
		document.body.appendChild(modalbg);
		document.body.appendChild(modal);
		modal.appendChild(win);
		
	}
	
	if(this.spec.type & gui.WND_OK){ 
		var button_ok = document.createElement("input");
		button_ok.type = "button";
		button_ok.value = "OK";
		button_ok.onclick = function(){if(instance.onok)instance.onok()};

		
		if(win.lastChild != doc){
			win.insertBefore(button_ok,doc.nextSibling);
		}else{
			doc.appendChild(button_ok);
		}
		
		//Per enter ok aufrufen
		win.onkeydown = function(e){
			if (!e) {e = window.event;}
			if (e.which) {key = e.which;}
			else if (e.keyCode) {key = e.keyCode;}
			if (key == 13) {
				button_ok.onclick();
			}
			
		};
	}
	
	//weitere classNames einfügen
	if(this.spec.style)
		if(this.spec.style.win)
			win.className += " "+this.spec.style.win;
			
	// Spezifische specs weiter verarbeiten
	for(var key in this.spec){
		switch (key){
			case "width":
				win.style.width = this.spec.width;
			break;
			case "height":
				win.style.height = this.spec.height;
			break;
			
		}
	}
	
	this.render();
	
	gui.windows.push(this);
	
}

gui.cMessage	= function(text,title,type,spec,iconParent,windowParent,onok,oncancel,onclose){
	
	this.autoSpec =function(type,addspec){
		var spec = type ? copyObject(type) : gui.MSG_TYPE_NOTIFY;
		for(var prop in addspec){
			spec[prop] = addspec[prop];
		}
		return spec;
	}
	
	this.display = function(){
		clearTimeout(this.timeout);
		if(!this.window){
			var spec = {drag:true,titlebar:true,type:gui.WND_TYPE_DLG|gui.WND_OK};
			if(this.type == gui.MSG_TYPE_CRIT)
				spec = {drag:true,titlebar:true,type:gui.WND_TYPE_MODAL|gui.WND_OK};

			this.win = new gui.cWindow(this.text,this.windowParent,this.title,null,spec);
			this.win.onok = this.onok;
			this.win.onclose = this.onok;
		}else{
			this.win.appendTo(this.windowParent);
		}
	}
	
	this.renderIcon = function() {
		if(!this.icon){
			this.icon = document.createElement("a");
			this.icon.className = "msgIcon";
			this.icon.style.backgroundImage = "url('"+this.spec.icon+"')";
			this.icon.onclick = function(){instance.display()};
		}
		this.iconParent.appendChild(this.icon);
	}
	
	this.destroy = function(){
		gui.deleteNode(this.icon);
		this.icon = null;
		if(this.win)
			this.win.destroy();
		this.window = null;

	}

	this.text = text;
	this.title= title;
	this.type = type;
	this.spec = this.autoSpec(type,spec);
	this.iconParent = iconParent;
	this.windowParent = windowParent ? windowParent : document.body;
	this.onok = onok;
	this.oncancel = oncancel;
	this.onclose = onclose;
	
	var instance = this;
	
	if(this.type == gui.MSG_TYPE_CRIT)
		this.display();
	
	if(this.spec.duration){
		this.timeout = setTimeout(function(){instance.destroy()},this.spec.duration*1000);
	}
}

gui.cMessageMapper = function(element,spec){
	this.element = element;
	this.spec = spec;
	
	this.messages = new Array();
	
	this.addMessage = function(text,title,type,spec){
		var message = new gui.cMessage(text,title,type,spec,this.element,null,function(){message.destroy();});
		var id = this.messages.push(message);
		//message.onok = function(){alert("?");message.destroy();};
		message.renderIcon();
		return message;
	}
	
	this.clear = function(){
		for(var i = 0; i < this.messages.length; i++){
			this.messages[i].destroy();
			delete this.messages[i];
		}
		
		this.messages = new Array();
	}
}
// /style=(["|'])^\n]*\1/

gui.cCalendar = function(parent,month,year,onchange,cssClass){
	
	if(year < 1900)
		year = "20"+year;
	
	var monatstage = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var monate= new Array("Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");
	var instance = this;
	
	this.year = year;
	this.month = month;
	
	if(parent){
		var calendar = gui.createElementTo("div",parent);
	}else{
		var calendar = document.createElement("div");
	}
	
	if(!cssClass){
		cssClass = "cCalendar";
	}
	
	calendar.className = "cCalendar";
	
	var head = gui.createElementTo("div",calendar);
	head.className = "head";
	
	var previousMonth = gui.createElementTo("a",head);
	gui.createTextNodeTo("<<",previousMonth);
	previousMonth.onclick = function(){instance.previousMonth();};
	previousMonth.style.cssFloat = "left";
	
	var nextMonth = gui.createElementTo("a",head);
	gui.createTextNodeTo(">>",nextMonth);
	nextMonth.onclick = function(){instance.nextMonth();};
	nextMonth.style.cssFloat = "right";
	
	
	var titleMonth =  gui.createElementTo("span",head);
	
	
	
	
	var body = gui.createElementTo("div",calendar);
	
	this.changeDate = function(month,year){
		gui.removeChilds(body);
		
		if(year >1900)
			this.year = year;
		
		this.month = month;
		
		while(this.month < 1){
			this.year--;
			this.month += 12;
		}
		
			
		this.date = new Date(this.year,this.month-1,1);

		titleMonth.innerHTML = monate[this.date.getMonth()]+" "+this.date.getFullYear();
			
		var days = monatstage[this.date.getMonth()];
		if((days == 28) &&(this.year % 4 == 0))
			days = 29;
		
			
		var firstday = this.date.getDay();
		if(firstday == 0)
			firstday = 7
		for(var i = 1; i < firstday;i++){
			var day = gui.createElementTo("a",body);
			day.className = "day";
		}
			
		for(var i = 1; i <= days; i++){
			var day = gui.createElementTo("a",body);
			day.className = "day";
			var daytext = gui.createTextNodeTo(i+".",day);
			
		}
	}
	
	this.nextMonth = function(){
		this.changeDate(this.month+1);
	}
	
	this.previousMonth = function(){
		this.changeDate(this.month-1);
	}
	
	this.changeDate(this.month,this.year);
}

gui.progressbar = function(){
	
	var bar;
	var abortCallback;
	var bartype;
	var statustext;
	var backref = this;
	
	this.create = function(type){
		
	}
	
	this.setObject = function(obj,type){
		bar = obj;
		bartype = type;
	}
	
	this.init = function(){
		if(bartype && 1){
			if(!bar.getElementsByTagName("a")[0]){
				var cancel = gui.createElementTo("a",bar);
				cancel.className = "cancelsmall";
				cancel.onclick = function(){backref.onAbort()};
			}
		}
		if(!bar.getElementsByTagName("span")[0]){
			statustext = gui.createElementTo("span",bar);
		}else
			statustext = gbar.getElementsByTagName("span")[0];
		
	}
	
	this.status = function(text){
		statustext.innerHTML = text;
	}
	
	this.progress = function(progress,text){
		
	}
	
	this.setAbort = function(callback){
		abortCallback = callback;
	}
	
	this.onAbort = function(){
		statustext.innerHTML = "User Interupted";
		bar.firstChild.style.display = "none";
		bar.style.backgroundImage = "url('"+homePath+"/layout/images/system/progress_done.jpg')";
		if(abortCallback)
			abortCallback(this);
	}
	
	this.finish = function(text){
		statustext.innerHTML = text;
		bar.style.backgroundImage = "url('"+homePath+"/layout/images/system/progress_done.jpg')";
		bar.firstChild.style.display = "none";
	}
	
	this.loading = function(text){
		
		bar.style.visibility = "visible";
		bar.style.backgroundImage = "url('"+homePath+"/layout/images/system/progress.gif')";
		
		bar.firstChild.style.display = "block";
		if(text)
			this.status(text);
	}
	
	this.getBar = function(){
		return bar;
	}
	
	return this;
}


gui.console = new Object();
gui.console.window = new Object();
gui.console.control = new Object();
gui.console.system = new Object();


gui.console.window.hwnd = false;

gui.console.window.open = function(){
	if(!gui.console.window.hwnd)
		gui.console.window.init();
	gui.console.window.show();
}

gui.console.window.init = function(){
	gui.console.window.hwnd = gui.createElementTo("div",document.body);
	gui.console.window.hwnd.className = "console";
	//gui.console.window.hwnd.style.height = "0px";
	
	gui.console.window.output = gui.createElementTo("ul",gui.console.window.hwnd);
	gui.console.window.input = gui.createElementTo("input",gui.console.window.hwnd);
	gui.console.window.input.onkeypress = gui.console.window.typing;
	gui.console.window.output.className = "output";
}

gui.console.window.show = function(){
	gui.console.window.hwnd.style.display = "block";
}

gui.console.window.close = function(){
	gui.console.window.hwnd.style.display = "none";
}

gui.console.window.clear = function(){
	gui.removeChilds(gui.console.window.output);
}

gui.console.window.typing = function(e){
	var e = e || window.event;
	var code = e.witch ? e.witch : e.keyCode;
	switch(code){
		case 13:
			gui.console.control.command(this.value);
			this.value = "";
		break;
		case 38: //up arrow
			
			gui.console.system.history.pointer++;
			var cmds = gui.console.system.history.cmds;
			
			if(gui.console.system.history.pointer > cmds.length)
				gui.console.system.history.pointer = 1;
				
			if(cmds.length == 0)
				break;
				
			this.value = cmds[cmds.length-gui.console.system.history.pointer];
	            break;
	    case 40: //down arrow
			gui.console.system.history.pointer--;
			var cmds = gui.console.system.history.cmds;
			
			if(gui.console.system.history.pointer < 1)
				gui.console.system.history.pointer = cmds.length;
				
			if(cmds.length == 0)
				break;
				
			this.value = cmds[cmds.length-gui.console.system.history.pointer];
	            break;
	}
}


gui.console.control.workingDir = "";
gui.console.control.user = new Object();
gui.console.control.user.user = "coni";

gui.console.control.output = function(output){
	
	var scroll = true;
	if(gui.console.window.output.scrollTop&&
	((gui.console.window.output.scrollTop+gui.console.window.output.offsetHeight) < gui.console.window.output.scrollHeight))
		scroll = false;
	
	
	var line = gui.createElementTo("li",gui.console.window.output);
	var user = gui.createElementTo("span",line);
	var out  = gui.createElementTo("div",line);

	var dir = gui.console.control.workingDir;
	if(gui.console.control.user)
		var username = gui.console.control.user.user;
	else
		var username = "";
	gui.createTextNodeTo(dir+" : "+username+" > ",user);
		user.scrollLeft = user.scrollWidth;
	var cmd;
	var obj;

	if(isObject(output)){
		obj = output;
		cmd = output.request;
		output = output.output;
	}
	
	//gui.createTextNodeTo(output,out);
	if(cmd)
		out.innerHTML = cmd;

	if(output)
		if(isArray(output)){
			var outputUL = gui.createElementTo("ul",out);
			for(var i = 0; i < output.length; i++){
				var li = gui.createElementTo("li",outputUL);
				gui.createTextNodeTo(output[i],li);
			}
		}else{
			if(cmd)
				out.innerHTML += "<br>"+output;
			else
				out.innerHTML = output;
		}
	if(obj)
	if(obj.error){
		out.innerHTML += "<p class='error'>"+obj.error+"</p>";
	}
	
	if(scroll)
		gui.console.window.output.scrollTop = gui.console.window.output.scrollHeight;
	
	return line;
}

gui.console.control.command = function(cmd){
	gui.console.system.history.cmds.push(cmd);
	gui.console.system.history.pointer = 0;
	if(cmd == "exit"){
		gui.console.window.hwnd.style.display = "none";return;
	}
	
	if(cmd == "clear"){
		gui.console.window.clear();return;
	}
	
	var request = new Object();
	request.cmd = cmd;
	request.workingDir = gui.console.control.workingDir;

	//gui.console.control.output(cmd);
	gui.console.control.request(request);
}

gui.console.control.request = function(request){
	url = "https://"+window.location.hostname+homePath+"/module/cms/console/console.ajax.php";
	url = "http://"+window.location.hostname+homePath+"/module/cms/console/console.ajax.php";
	
	onload = gui.console.control.progress;
	

	post = "&request="+Object.toJSON(request);//request.toJSONString();
		
	var ongather = null;
	var onerror = function(){
		gui.console.control.output("error fetching data! Status:"+this.ajax.status);
		gui.console.control.output(this.ajax.responseText);
	};
	
	req = new net.postRequest(url,onload,post,ongather,onerror);
}

gui.console.control.progress = function(){
	try {
		
		var answer = eval("("+this.ajax.responseText+")");
		
	} catch(error) {
		//debug(this.ajax.responseText);
		gui.console.control.output("error using data! Return:"+this.ajax.responseText);
	} finally {
		
		/*if(!answer.user)
			return;*/
			
		//Die Arbeitsvariablen setzen
		gui.console.control.user = answer.user;
		gui.console.control.workingDir = answer.workingDir;
		
		switch(answer.cmd){
			case "access":
				if(answer.access == 1)
					gui.console.window.open();
			break;
			default:
				gui.console.control.output(answer);
				//gui.console.control.output(this.ajax.responseText);
			break;
		}	
	}
	

	
}

gui.console.control.open = function(){
	var request = new Object();
	request.cmd = "access";

	gui.console.control.request(request);
	
}
gui.console.system.history = new Object();
gui.console.system.history.cmds = new Array();
gui.console.system.history.pointer = 0;

gui.console.system.onWindowKeyPress = function(e){
	//which ;
	var e = e || window.event;
	var code = e.which ? e.which : e.keyCode;

	str = String.fromCharCode(code);

	if(e.ctrlKey && e.altKey && (code == 48))
		gui.console.control.open();
		
	if(gui.console.system.oldOnWindowKeyDown)
		gui.console.system.oldOnWindowKeyDown(e);
}

/**
 * Tooltip
 */
gui.Tooltip = function(content,css,parent){

	
	this.show = function(){
		tip.style.display = "block";
		this.focus();
	}
	
	this.hide = function(){
		tip.style.display = "none";
	}
	
	this.destroy = function(){
		gui.deleteNode(tip);
		Event.stopObserving(window,'mousemove',onMouseMoveCache);
	}
	
	this.focus = function(){
		gui.focus(tip);
	}
	
	this.setContent = function(content){
		if(isObject(content)){
			body.innerHTML = "";
			body.appendChild(content);
		}else{
			body.innerHTML = content;
		}
	}
	
	this.getBody = function(){
		return body;
	}
	
	/**
	 * Events
	 */
	
	this.onMouseMove = function(e){
		offset = 0;
		
		if(window.innerWidth)	// Alle guten Browser
		if((window.innerWidth- Event.pointerX(e)) < Event.pointerX(e)) 
			offset = -body.offsetWidth;
		
		if(document.body.clientWidth) // Fu**king IE
		if((document.body.clientWidth- Event.pointerX(e)) < Event.pointerX(e)) 
			offset = -body.offsetWidth;
		
		
		
		tip.style.left = Event.pointerX(e)+15+offset+"px";
		tip.style.top = Event.pointerY(e)+15+"px";
	}
	
	/**
	 * Constructor
	 */
	
	parent = parent ? parent : document.body;
	 
	var tip = document.createElement("div");
		tip.style.position = "absolute";
	var body = gui.createElementTo("div",tip);
	body.className = css ? css : "tooltip";

	this.setContent(content);
	this.hide();

	parent.appendChild(tip);
	var onMouseMoveCache = this.onMouseMove.bind(this)
	Event.observe(document,'mousemove',onMouseMoveCache);
	return this;
}

gui.Tooltip.createImageTooltip = function(path,noloading,parent){
	var wait = document.createElement("img");
	wait.src = homePath+"layout/images/system/wait.gif";
	var tip = new gui.Tooltip(wait,false,parent);
	setTimeout(function(){tip.getBody().style.width=wait.width+"px"},4);
	
	var img = document.createElement("img");
	if(noloading){
		tip.setContent(img);
		tip.getBody().style.width=img.width+"px";
		setTimeout(function(){tip.getBody().style.width=img.width+"px"},5);
	}else{
		Event.observe(img,'load',function(){tip.setContent(img);; 
		setTimeout(function(){tip.getBody().style.width=img.width+"px"},5);}.bind(this));
	}
	img.src = path;
	img.onerror =function(){alert(path)};
	return tip;
}


var system = new Object();

system.setCookie = function(name,value){
	var a = new Date();
	a = new Date(a.getTime() +1000*60*60*24*365);
	document.cookie = name+'='+value+'; expires='+a.toGMTString()+';'; 	
}

system.readCookie = function(name){
	var variables = new Object();
	
	a = document.cookie;
	res = '';
	while(a != '')
	{
		cookiename = a.substring(0,a.search('='));
		cookiewert = a.substring(a.search('=')+1,a.search(';'));
		if(cookiewert == '')
		{
			cookiewert = a.substring(a.search('=')+1,a.length);
		}
		
		if(name == cookiename){
			res = cookiewert;
		}
		
		i = a.search(';')+1;
		if(i == 0){i = a.length}
			a = a.substring(i,a.length);
	}
	return(res)
}

system.deleteCookie = function(name){
	document.cookie = name+'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}

/*
	Hooking some Events
*/

gui.console.system.oldOnWindowKeyPress = window.onkeypress;
window.onkeypress = gui.console.system.onWindowKeyPress;
