/**
 *  Technologia mlx
 * 
 */

/**
 * @class: SIGNAL 
*/
function SIGNAL() {
	this.CLICK 			= 'click';
	this.DBLCLICK 		= 'dblclick';
	this.MOUSEOVER 		= 'mouseover';
	this.MOUSEOUT       = 'mouseout';
	this.MOUSEDOWN 		= 'mousedown';
	this.MOUSEUP 		= 'mouseup';
	this.SIGKILL 		= 'SIGKILL';
	this.SIGTERM 		= 'SIGTERM';
	this.SIGSYS			= 'SIGSYS';
	this.SIGQUIT		= 'SIGQUIT';
	this.SIGABRT		= 'SIGABRT';
	this.SIGSEGV    	= 'SIGSEGV';
	this.SIGSTOP    	= 'SIGSTOP';
	this.SIGALRM 		= 'SIGALRM';
	this.SIGMSG 		= 'SIGMSG';
	this.SIGERR     	= 'SIGERR';
};
SIGNAL.prototype.connect = function(widget, signal, signal_handler, signal_handler_args) {
	if(widget instanceof WIDGET) {
		if(!widget._signals[signal]) widget._signals[signal] = new Array();
		widget._signals[signal].push({handler: signal_handler, handler_args: signal_handler_args, id: widget.id, cid: widget.cid}); 
	}
}
SIGNAL.prototype.disconnect = function(widget, signal, signal_handler) {}

SIGNAL.prototype.signal = function(signal, pid) {
	
}
signal = new SIGNAL();


/**
 * @class: GSIGNAL
 * @extends : SIGNAL 
*/
function GSIGNAL() {
	this.CLICK 			= 'click';
	this.DBLCLICK 		= 'dblclick';
	this.MOUSEOVER 		= 'mouseover';
	this.MOUSEOUT       = 'mouseout';
	this.MOUSEDOWN 		= 'mousedown';
	this.MOUSEUP 		= 'mouseup';
	this.MOUSEMOVE 		= 'mousemove';
	this.CONTEXTMENU 	= 'contextmenu';
	this.COPY 			= 'copy';
	this.SELECT 		= 'select';
	this.FOCUS 			= 'focus';
	this.BLUR 			= 'blur';
}
GSIGNAL.prototype.listen = function(widget, gsignal, gsignal_handler) {
	if(widget instanceof WIDGET) {
		gsignal_handler = gsignal_handler ? gsignal_handler : widget.gsignal_handler;
		if (widget.widget.addEventListener) {
			widget.widget.addEventListener (gsignal, gsignal_handler, false);
		} else if (widget.widget.attachEvent) {
			widget.widget.attachEvent ('on'+gsignal, gsignal_handler );
		} else {
			switch(gsignal){
				case this.CLICK: widget.widget.onclick = gsignal_handler; break;
				case this.DBLCLICK: widget.widget.ondblclick = gsignal_handler; break;
				case this.MOUSOVER: widget.widget.onmouseover = gsignal_handler; break;
				case this.MOUSEOUT: widget.widget.onmouseout = gsignal_handler; break;
				case this.MOUSEDOWN: widget.widget.onmousedown = gsignal_handler; break;
				case this.MOUSEUP: widget.widget.onmouseup = gsignal_handler; break;
				case this.MOUSEMOVE: widget.widget.onmousemove = gsignal_handler; break;
				default:
					return false;
					break;	
			}
		}
		return true;
	}
}
GSIGNAL.prototype.remove = function(widget, gsignal, gsignal_handler) {
	
}
gsignal = new GSIGNAL();

/**
 * @class: GUI 
 * @requires: WIDGET, GSIGNAL
*/
function GUI() {
	this.signal 	= new GSIGNAL();
	this._widgets 	= new Array();
}
GUI.prototype.widget = {
	get : function(cid, id) { return ((cid && (cid=cid.toLowerCase()) && id && (id = id.toLowerCase()) && gui._widgets[cid] && gui._widgets[cid][id]) ? gui._widgets[cid][id] : false);},
	body : function(id) {return new WIDGET('body', 'mbody', id);},
	container : function(id) { return new WIDGET('container', false, id);}	
}
GUI.prototype.remove = function(cid) { return (cid && this._widgets[cid] && ((this._widgets[cid] = null) == null)) ? true : false;}
GUI.prototype.effects = {
	INUSE: true,
	container : Array(),
	zoomout : function(widget, x,y) {
		if(!this.INUSE) { widget.style('display:block'); return false;}
		x = (x>=0) ? x : 0;
		y = (y>=0) ? y : 0; 
		if(!this.container['zoomout']) this.container['zoomout'] = new Array();
		var id = this.container['zoomout'].length;
		var A = {x: parseInt(widget.widget.style.left), y: parseInt(widget.widget.style.top)};
		var B = {x: (A.x + parseInt(widget.widget.style.width)), y: A.y};
		var C = {x: A.x, y: (A.y + parseInt(widget.widget.style.height))};
		var D = {x: x, y: y};
		this.container['zoomout'].push( {A: A, B: B, C: C, D: D, widget: widget, zoomlay: false} );
		this._zoomoutAnimationStart(id);
	},
	_zoomoutAnimationStart : function(id) {
		
		if(this.container['zoomout'] && this.container['zoomout'][id]){
			widget = this.container['zoomout'][id].widget;
			var container = gui.widget.container();
			
			//ZoomLay
			var zoomlay = container.vbox();
			zoomlay.layer(10000);
			zoomlay.style('position:absolute;background:#d3d3d3;border:1px solid #000000');
			zoomlay.opacity(60);
			
			//Umieszczanie ZoomLay na aktywnym pulpicie
			/*var cname 	= sys.env.desk.desktop.cname
			var deskpid = sys.env.desk.desktop.cdesktops[cname];*/
			
			if(true /*sys.process.is_exist(deskpid) && (cdesktop = sys.process.get(deskpid))*/){
				var p =  this.container['zoomout'][id];
				gui.widget.get('main', 'main').append(zoomlay);
				var SX = p.D.x;
				var SY = p.D.y;
				this.container['zoomout'][id].zoomlay = {widget: zoomlay, SX: SX, SY: SY};
				if(Math.abs(p.B.x - p.D.x)>Math.abs(p.C.y - p.D.y /*+ sys.env.desk.bar.HEIGHT + sys.env.desk.stripe.HEIGHT*/)) {
					setTimeout('gui.effects._zoomoutAnimationX('+id+')', 10);
				}
				else {
					setTimeout('gui.effects._zoomoutAnimationY('+id+')', 10);
				}
				return true;
			}
		}
		return false;
	},
	_zoomoutAnimationX : function(id){
		//alert('zoomoutAnimationX');
		if(this.container['zoomout'] && this.container['zoomout'][id]){
			p = this.container['zoomout'][id];
			var x = p.zoomlay.SX;
			var y = (((p.D.y - p.A.y)*(x - p.A.x))/(p.D.x - p.A.x)) + p.A.y;
			var x2 =  p.B.x + (((y - p.B.y)*(p.D.x - p.B.x))/(p.D.y - p.B.y));
			var y2 = (((p.D.y - p.C.y)*(x - p.C.x))/(p.D.x - p.C.x)) + p.C.y;
			y = parseInt(y);
			x2 = parseInt(x2);
			y2 = parseInt(y2);
			if(Math.abs(p.A.x - x)<=21) {
				p.zoomlay.widget.pos(p.A.x,p.A.y);
				p.zoomlay.widget.size(Math.abs(p.A.x-p.B.x), Math.abs(p.A.y - p.C.y));
				p.zoomlay.widget.style('display:none');
				p.widget.opacity(20);
				p.widget.style('display:block');
				//sys.env.desk.desktop._active();
				setTimeout('gui.effects._zoomoutAnimationAppear('+id+', 30)', 100);
				return true;
			}
			else {
				//alert('dupa');
				p.zoomlay.widget.pos(x,y);
				p.zoomlay.widget.size(Math.abs(x-x2), Math.abs(y-y2));
				setTimeout('gui.effects._zoomoutAnimationX('+id+')', 10);
			}
			this.container['zoomout'][id].zoomlay.SX = x + ((p.A.x >= x) ?  ((Math.abs(p.D.x - x)<=21) ? 2 : 20) : ((Math.abs(p.D.x - x)<=21) ? -2 : -20));
		}
		return false;
	},
	_zoomoutAnimationY : function(id){
		//alert('zoomoutAnimationY');
		if(this.container['zoomout'] && this.container['zoomout'][id]){
			p = this.container['zoomout'][id];
			var y = p.zoomlay.SY;
			var x = p.A.x + (((y - p.A.y)*(p.D.x - p.A.x))/(p.D.y - p.A.y));
			var x2 = p.B.x + (((y - p.B.y)*(p.D.x - p.B.x))/(p.D.y - p.B.y));
			var y2 = (((p.D.y - p.C.y)*(x - p.C.x))/(p.D.x - p.C.x)) + p.C.y;
			x = parseInt(x);
			x2 = parseInt(x2);
			y2 = parseInt(y2);
			//alert('y='+y+',x='+x+',y2='+y2+',x2='+x2);
			if(Math.abs(p.A.y - y )<=21) {
				p.zoomlay.widget.pos(p.A.x,p.A.y);
				p.zoomlay.widget.size(Math.abs(p.A.x-p.B.x), Math.abs(p.A.y - p.C.y));
				p.zoomlay.widget.style('display:none');
				p.widget.opacity(20);
				p.widget.style('display:block');				
				setTimeout('gui.effects._zoomoutAnimationAppear('+id+', 30)', 100);
				return true;
			}
			else {
				p.zoomlay.widget.pos(x,y);
				p.zoomlay.widget.size(Math.abs(x-x2), Math.abs(y-y2));
				setTimeout('gui.effects._zoomoutAnimationY('+id+')', 10);
			}
			this.container['zoomout'][id].zoomlay.SY = y + ((p.A.y >= y) ? ((Math.abs(p.D.y - y)<=21) ? 2 : 20) : ((Math.abs(p.D.y - y)<=21) ? -2 : -20));
		}
	},
	_zoomoutAnimationAppear : function(id, value) {
		if(this.container['zoomout'] && this.container['zoomout'][id]){
			if(value >= 100) {
				this.container['zoomout'][id].widget.opacity(100);
				this._zoomoutAnimationStop(id);
			}
			else {
				this.container['zoomout'][id].widget.opacity(value);
				value = value+ (window.event ? 49 : 20); 
				setTimeout('gui.effects._zoomoutAnimationAppear('+id+','+value+')', 15);
			}
		}
	},
	_zoomoutAnimationStop : function(id){
		if(this.container['zoomout'] && this.container['zoomout'][id]){
			p = this.container['zoomout'][id];
			p.zoomlay.widget.widget.parentNode.removeChild(p.zoomlay.widget.widget);
			this.container['zoomout'][id] = null;
		}
	},
	zoomin : function(widget, x,y) {
		if(!this.INUSE) { widget.style('display:none'); return false;}
		x = (x>=0) ? x : 0;
		y = (y>=0) ? (y /*- sys.env.desk.bar.HEIGHT - sys.env.desk.stripe.HEIGHT*/) : 0; 
		if(!this.container['zoomin']) this.container['zoomin'] = new Array();
		var id = this.container['zoomin'].length;
		var A = {x: parseInt(widget.widget.style.left), y: parseInt(widget.widget.style.top)};
		var B = {x: (A.x + parseInt(widget.widget.style.width)), y: A.y};
		var C = {x: A.x, y: (A.y + parseInt(widget.widget.style.height))};
		var D = {x: x, y: y};
		this.container['zoomin'].push( {A: A, B: B, C: C, D: D, widget: widget, zoomlay: false} );
		this._zoominAnimationDisappear(id, 100);
	},
	_zoominAnimationDisappear : function(id, value) {
		if(this.container['zoomin'] && this.container['zoomin'][id]){
			if((value <= 0) || window.event) {
				this.container['zoomin'][id].widget.style('display:none');
				this._zoominAnimationStart(id);
			}
			else {
				this.container['zoomin'][id].widget.opacity(value);
				value = value-20;
				setTimeout('gui.effects._zoominAnimationDisappear('+id+','+value+')', 15);
			}
		}
	},
	_zoominAnimationStart : function(id) {
		
		if(this.container['zoomin'] && this.container['zoomin'][id]){
			widget = this.container['zoomin'][id].widget;
			var container = gui.widget.container();
			
			//ZoomLay
			var zoomlay = container.vbox();
			zoomlay.layer(10000);
			zoomlay.style('position:absolute;background:#d3d3d3;border:1px solid #000000');
			zoomlay.opacity(60);
			
			//Umieszczanie ZoomLay na aktywnym pulpicie
			/*var cname 	= sys.env.desk.desktop.cname
			var deskpid = sys.env.desk.desktop.cdesktops[cname];*/
			
			if(true/*sys.process.is_exist(deskpid) && (cdesktop = sys.process.get(deskpid))*/){
				var p =  this.container['zoomin'][id];
				zoomlay.size(p.B.x - p.A.x, p.C.y - p.A.y);
				zoomlay.pos(p.A.x, p.A.y);
				gui.widget.get('main', 'main').append(zoomlay);
				var SX = p.A.x;
				var SY = p.A.y;
				this.container['zoomin'][id].zoomlay = {widget: zoomlay, SX: SX, SY: SY};
				
				if(Math.abs(p.B.x - p.D.x)>Math.abs(p.C.y - p.D.y/* + sys.env.desk.bar.HEIGHT + sys.env.desk.stripe.HEIGHT*/)) {
					setTimeout('gui.effects._zoominAnimationX('+id+')', 10);
				}
				else {
					setTimeout('gui.effects._zoominAnimationY('+id+')', 10);
				}
				
				return true;
			}
		}
		return false;
	},
	_zoominAnimationX : function(id){
		if(this.container['zoomin'] && this.container['zoomin'][id]){
			p = this.container['zoomin'][id];
			var x = p.zoomlay.SX;
			var y = (((p.D.y - p.A.y)*(x - p.A.x))/(p.D.x - p.A.x)) + p.A.y;
			var x2 =  p.B.x + (((y - p.B.y)*(p.D.x - p.B.x))/(p.D.y - p.B.y));
			var y2 = (((p.D.y - p.C.y)*(x - p.C.x))/(p.D.x - p.C.x)) + p.C.y;
			y = parseInt(y);
			x2 = parseInt(x2);
			y2 = parseInt(y2);
			if(Math.abs(p.D.x - x)<=2) {
				//alert(p.D.x + '-' + x);
				p.zoomlay.widget.pos(x,y);
				p.zoomlay.widget.size(Math.abs(x-x2), Math.abs(y-y2));
				p.zoomlay.widget.style('display:none');
				this._zoominAnimationStop(id);
				return true;
			}
			else {
				//alert('dupa');
				p.zoomlay.widget.pos(x,y);
				p.zoomlay.widget.size(Math.abs(x-x2), Math.abs(y-y2));
				setTimeout('gui.effects._zoominAnimationX('+id+')', 10);
			}
			this.container['zoomin'][id].zoomlay.SX = x + ((p.D.x <= x) ? ((Math.abs(Math.abs(p.D.x) - Math.abs(x))>21) ? -20 : -4) : ((Math.abs(Math.abs(p.D.x) - Math.abs(x)>21)) ? 20 : 4));
		}
		return false;
	},
	_zoominAnimationY : function(id){
		//alert('zoominAnimationY');
		if(this.container['zoomin'] && this.container['zoomin'][id]){
			p = this.container['zoomin'][id];
			var y = p.zoomlay.SY;
			var x = p.A.x + (((y - p.A.y)*(p.D.x - p.A.x))/(p.D.y - p.A.y));
			var x2 = p.B.x + (((y - p.B.y)*(p.D.x - p.B.x))/(p.D.y - p.B.y));
			var y2 = (((p.D.y - p.C.y)*(x - p.C.x))/(p.D.x - p.C.x)) + p.C.y;
			x = parseInt(x);
			x2 = parseInt(x2);
			y2 = parseInt(y2);
			//alert('y='+y+',x='+x+',y2='+y2+',x2='+x2);
			if(Math.abs(p.D.y - y )<=2) {
				p.zoomlay.widget.pos(x,y);
				p.zoomlay.widget.size(Math.abs(x-x2), Math.abs(y-y2));
				p.zoomlay.widget.style('display:none');
				this._zoominAnimationStop(id);
				return true;
			}
			else {
				p.zoomlay.widget.pos(x,y);
				p.zoomlay.widget.size(Math.abs(x-x2), Math.abs(y-y2));
				setTimeout('gui.effects._zoominAnimationY('+id+')', 10);
			}
			this.container['zoomin'][id].zoomlay.SY = y + ((p.D.y <= y) ? ((Math.abs(Math.abs(p.D.y) - Math.abs(y))>21) ? -20 : -4) : ((Math.abs(Math.abs(p.D.y) - Math.abs(y))>21) ? 20 : 4));
		}
	},
	_zoominAnimationStop : function(id){
		if(this.container['zoomin'] && this.container['zoomin'][id]){
			p = this.container['zoomin'][id];
			p.zoomlay.widget.widget.parentNode.removeChild(p.zoomlay.widget.widget);
			this.container['zoomin'][id] = null;
		}
	}
	
}
var gui = new GUI();

/**
 * @class: WIDGET
*/
function WIDGET(tag, id, cid) {
	this._signals   = new Array();
	this.tag 		= tag ? tag : false;
	this.id			= id ? id.toLowerCase() : false;
	this.widget		= false;
	this.cid		= cid ? cid.toLowerCase() : false;
	if(this.tag){
		switch(this.tag) {
			case 'body' :
				this.widget = document.getElementsByTagName('body')[0];
				break;
			case 'container':
				this.widget = document.createDocumentFragment();
				break;
			case 'hbox':
				this.widget = document.createElement('div');
				var tmp = document.createElement('div');
				if(tmp.style.setProperty) tmp.style.setProperty('clear', 'both', null);
				else tmp.style['clear'] = 'both';
				tmp.innerHTML = ' ';
				this.widget.appendChild(tmp);
				break;
			case 'vbox':
				this.widget = document.createElement('div'); 
				break;
			default:
				this.widget = document.createElement(this.tag);
				break;
		} //switch	
	}
	
	if(this.widget && this.cid && this.id) {
		this.setAttr('id', this.id);
		if(!gui._widgets[this.cid]) gui._widgets[this.cid] = new Array();
		gui._widgets[this.cid][this.id] = this;
	}
}

WIDGET.prototype.append = function(widgetObj) {
	if(widgetObj && (widgetObj instanceof WIDGET)) {
		if(this.tag == 'hbox') {
			lc = this.widget.lastChild;
			this.widget.insertBefore(widgetObj.widget, lc);
		}
		else this.widget.appendChild(widgetObj.widget); 
		return this;
	}
	else return false;
}

WIDGET.prototype.hbox = function(id) { 
	cid = this.cid;
	var widgetObj = new WIDGET('hbox', id, cid);
	this.append(widgetObj);
	if(this.tag == 'hbox') this.style('float:left');
	return widgetObj; 
}
WIDGET.prototype.vbox = function(id) { 
	cid = this.cid;
	var widget = new WIDGET('vbox', id, cid);
	this.append(widget);
	if(this.tag == 'hbox') widget.style('float:left');
	return widget; 
}
WIDGET.prototype.tager = function(tag, id) { 
	cid = this.cid;
	var widget = new WIDGET(tag, id, cid);
	this.append(widget);
	if(this.tag == 'hbox') widget.style('float:left');
	return widget; 
}
WIDGET.prototype.style = function(rules) {
	if(rules && (rules=rules.toLowerCase()) && (rules.length>0)){
		rulesTab = rules.split(';');
		if(rulesTab.length>0)
		    for(var rule=0; rule<rulesTab.length; rule++){
			//for(rule in rulesTab) {
				rv = rulesTab[rule].split(':');
				if((rv.length == 2) && rv[0].length && rv[1].length) {
					if(this.widget.style.setProperty) this.widget.style.setProperty(rv[0], rv[1], null);
					else {
						ruleone = rv[0]
						switch(rv[0]) {
							case 'float':
								this.widget.style.styleFloat = rv[1]; 
								break;
							case 'border-top': 
								this.widget.style.borderTop = rv[1];
							 	break;
							case 'border-right': 
								this.widget.style.borderRight = rv[1];
							 	break;
							case 'border-bottom': 
								this.widget.style.borderBottom = rv[1];
							 	break;
							case 'border-left': 
								this.widget.style.borderLeft = rv[1];
							 	break;
							case 'background-image':
								this.widget.style.background = rv[1];
								break;
							case 'color':
								this.widget.style.color = rv[1]
								break;
							default:
							    try{
									this.widget.style[ruleone] = rv[1];
							    }
							    catch(e){
							    	alert('Widget Error while parsing style: '+ruleone+':'+rv[1]);
							    }
							    
								break;
						}
						
					}
				}
			}
	}
	return this;
}
WIDGET.prototype.text = function(str) {
	if(str && (str.length>0)) this.widget.innerHTML = str;
	return this;
}
WIDGET.prototype.get = function(id) { return (this.cid && id && (id=id.toLowerCase()) && gui._widgets[this.cid] && gui._widgets[this.cid][id]) ? gui._widgets[this.cid][id] : false}
WIDGET.prototype.setAttr = function(attr, value) {
	if(attr && (value || (value==0))){
		this.widget.setAttribute(attr, value);
		return true;
	} else return false;
}
WIDGET.prototype.getAttr = function(attr) {
	if(attr) {
		var tmp = this.widget.getAttribute(attr);
		return (tmp ? tmp : false);
	} else return false;
}
WIDGET.prototype.size = function(x_, y_) {
	if(x_ && y_) {
		this.style('height:'+ y_ + 'px;width:' + x_ + 'px');
		return this; 
	} else {
		x_ = this.widget.style.width;
		y_ = this.widget.style.height;
		return [parseInt(x_), parseInt(y_)];
	}
}

WIDGET.prototype.opacity = function(value) { //0..100
	if((value>=0) && (value<=100)){
		this.widget.style.MozOpacity = value/100;
		this.widget.style.filter = 'alpha(opacity='+value+')';
		this.widget.style.opacity = value/100;
		return this;
	} else return false;
}
WIDGET.prototype.layer = function(layer) {
	if(layer>0) this.widget.style.zIndex = layer;
	return this;
}
WIDGET.prototype.gsignal_handler = function(e) {
	return false;
}
WIDGET.prototype.pos = function(x_, y_) {
	this.style('top:'+y_+'px;left:'+x_+'px');
	return this;
}
WIDGET.prototype.currentPos = function() {
    // zwraca pozycje widget'u
    var obj = this.widget;
    var curleft = 0;
    var curtop  = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop  = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } //while
    } //if
    return [curleft, curtop];
}
WIDGET.prototype.remove = function() {
	obj = this.widget;
	obj.parentNode.removeChild(obj);
	if((cid = this.cid) && (id=this.id)) gui._widgets[cid][id] = null; 
}

