/*
	mintAjax 1.2.2
	www.mintajax.pl
	Copyright 2007 Piotr Korzeniewski modyfy by Marcin Winiarczyk 
	Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
 
var mint = {};

mint.ext = {};

mint.Request = function(url, target, OnSuccess, OnError)
{
	var req = {
		xhr : null,
		
		responseText : null,
		responseXML : null,
		responseJSON : null,
		
		getJSON : false,
		clearParams : true,
		clearHeader : true,
		evalScripts : false,
		evalResponse : false,
		
		params : [],
		header : [],
		
		group : null,
		
		url : null,
		async : true,
		method : "GET",
		encoding : "utf-8",
		contentType : "text/plain",
		username : "",
		password : "",
		
		form : null,
		resetForm : false,
		disableForm : true,
		
		status : null,
		statusText : null,
		
		reqDone : false,
		retryCount : 0,
		retryNum : 5,
		timeout : 60000,
		
		OnStateChange : function() {},
		OnLoading : function() {},
		OnLoaded : function() {},
		OnInteractive : function() {},
		OnComplete : function() {},
		OnSuccess : function() {},
		OnError : function() {},
		OnAbort : function() {},
		OnRetry : function() {},
		OnTimeout : function() {},
		
		Send : function(url, target) {
			this.reqDone = false;
			
			if(window.XMLHttpRequest) {
				this.xhr = new XMLHttpRequest();
			}
			else if(window.ActiveXObject) {
				try	{
					this.xhr = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e) {
					this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			
			if(url) this.url = url;
			
			var paramStr = "";
			
			with(this) {
				for(var i = 0; i < params.length; ++i) {
					if(i != 0) paramStr += "&";
					paramStr += encodeURIComponent(params[i].name)+"="+encodeURIComponent(params[i].value);
				}
				
				if(method.toLowerCase() == "post")
					xhr.open(method.toUpperCase(), url, async, username, password);
				else
					xhr.open(method.toUpperCase(), params.length > 0 ? url+(!/\?/.test(url) ? "?"+paramStr : "&"+paramStr) : url, async, username, password);
				
				for(var i = 0; i < header.length; ++i)
					xhr.setRequestHeader(header[i].name, header[i].value);
				
				if(method.toLowerCase() == "post")
					xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"+(encoding ? "; charset="+encoding : ""));
				else
					xhr.setRequestHeader("Content-Type", contentType);
					
				xhr.setRequestHeader("If-Modified-Since", "Sat, 11 Jan 1977 00:00:00 GMT");
			}
			
			var that = this;
			
			this.xhr.onreadystatechange =
			function() {
				that.OnStateChange();
				
				switch(that.xhr.readyState) {
					case 1 : that.OnLoading(); break;
					case 2 : that.OnLoaded(); break;
					case 3 : that.OnInteractive(); break;
					case 4 : {
						that.OnComplete();
						
						try {
							if(that.xhr.status >= 200 && that.xhr.status < 300) {
								that.reqDone = true;
								
								that.responseText = that.xhr.responseText;
								that.responseXML = that.xhr.responseXML;
								
								that.status = that.xhr.status;
								that.statusText = that.xhr.statusText;
								
								//if(target) mint.$D(target).innerHTML = that.responseText; --remove by Marcin Winiarczyk
								
								if(that.getJSON) {
									that.responseJSON = eval("("+that.responseText+")");
								}
								else if(that.evalScripts) {
									var script;
									var reg = /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/gi;
									var reg2 = /(?:<script.*?src=['"]{1}([^>]*)['"]{1}[^>]*>)((\n|\r|.)*?)(?:<\/script>)/gi;
									
									script = reg.exec(that.responseText);
									while(script) {
										if(script[1]) window.execScript ? window.execScript(script[1]) : setTimeout(script[1], 0);
										script = reg.exec(that.responseText);
									}
									
									script = reg2.exec(that.responseText);
									while(script) {
										if(script[1].length > 0) {
											var s = document.createElement("script");
											s.type = "text/javascript";
											s.src = script[1];
											document.body.appendChild(s);
										}
										script = reg2.exec(that.responseText);
									}
								}
									
								if(that.form) {
									if(that.disableForm) { //modyfi by Marcin Winiarczyk
										for(var i = 0; i < document.forms[that.form.name].elements.length; ++i)
											document.forms[that.form.name].elements[i].disabled = false;
									}
									
									if(that.resetForm) document.forms[that.form.name].reset();
								}
								
								if(that.group) {
									var groupDone = true;
									
									for(var i = 0; i < that.group.req.length; ++i)
										if(!that.group.req[i].reqDone) groupDone = false;
										
									if(groupDone) {
										that.group.isRunning = false;
										that.group.OnDone();
									}
								}
								
								that.OnSuccess();
								
								if(that.clearParams) while(that.params.length > 0) that.params.pop();
								if(that.clearHeader) while(that.header.length > 0) that.header.pop();
							}
							else that.OnError(that.status);
						}
						catch (e) {
							that.OnError(-1);
						}	
						
						break;
					}
				}
			}
			
			with(this) {
				if(group) {
					if(!group.isRunning) {
						group.isRunning = true;
						group.OnStart(this);
					}
				}
				
				xhr.send(method.toLowerCase() == "post" ? paramStr : null);
			}
			
			if(this.retryNum) {
				setTimeout(
						function() {
							if(!that.reqDone) {
								that.xhr.onreadystatechange = function() {};
								that.xhr.abort();
								that.OnTimeout();
								
								if(that.retryCount < that.retryNum) {
									that.retryCount++;
									that.Send();
									that.OnRetry();
								}
								else {
									that.retryCount = 0;
									that.OnAbort();
								}
							}
						},
						this.timeout);
			}
		},
		
		SendForm : function(form, url, method) {
			this.form = mint.$D(form);
			this.url = url || this.form.action || this.url;
			this.method = method || this.form.method || "post";
			
			var input = document.forms[this.form.name].elements; // modyfi by Marcin Winiarczyk

			for(var i = 0; i < input.length; i++) {
				if(this.disableForm){
					input[i].disabled = true;
				}
				switch(input[i].type) {
					case "radio":
						if (input[i].checked) {
							this.AddParam(input[i].name, input[i].value);
						}
						break;
					case "checkbox":
						if(input[i].checked){
							this.AddParam(input[i].name, input[i].value);
						}
						break;
					case "select-one":
						this.AddParam(input[i].name, input[i].options[input[i].selectedIndex].value);
						break;
					case "select-multiple":
						for(var x = 0; x < input[i].options.length; ++x) {
							if(input[i].options[x].selected){
								this.AddParam(input[i].name, input[i].options[x].value);
							}
						}
						break;
					default:
						this.AddParam(input[i].name, input[i].value);
				}
			}
			
			this.Send(this.url);
		},
		
		Set : function(attr, value) {
			if(typeof value != "undefined") this[attr] = value;
			else for(var a in attr) this[a] = attr[a];
			return this;
		},
		
		AddParam : function(name, value) {
			this.params.push({name:name, value:value});
			return this;
		},
		
		RemoveParam : function(name) {
			for(var i = 0; i < this.params.length; ++i) {
				if(this.params[i].name == name) {
					this.params.splice(i, 1);
					break;
				}
			}
			return this;
		},
		
		AddHeader : function(name, value) {
			this.params.push({name:name, value:value});
			return this;
		},
		
		RemoveHeader : function(name) {
			for(var i = 0; i < this.header.length; ++i) {
				if(this.header[i].name == name) {
					this.header.splice(i, 1);
					break;
				}
			}
		}
	}
		
	if(OnSuccess && typeof OnSuccess == "function")
		req.OnSuccess = OnSuccess;
		
	if(OnError && typeof OnError == "function")
		req.OnError = OnError;
		
	if(url) target ? req.Send(url, target) : req.Send(url);
		
	return req;
};

mint.RequestGroup = function() {
	var group = {
		req : [],
		isRunning : false,
		
		OnStart : function() {},
		OnDone : function() {},
		
		Add : function() {
			for(var i = 0; i < arguments.length; ++i) {
				arguments[i].group = this;
				this.req.push(arguments[i]);
			}
		}
	}
	
	for(var i = 0; i < arguments.length; ++i)
		group.Add(arguments[i]);
	
	return group;
};

mint.$D = function(obj) {
	if(typeof(obj) == "string"){ //poprawione ze wzgledu na zla konstrukcje
		return document.getElementById(obj);
	}
	else{
		return obj;
	}
}

mint.$R = function(url, target, OnSuccess, OnError) {
	return mint.Request(url, target, OnSuccess, OnError);
}

mint.fx =
{
	GetElapsedTime : function(obj, style) {
		obj = mint.$D(obj);
		
		if(!obj.$fx || !obj.$fx[style])
			return null;
		
		return obj.$fx[style].elapsedTime;
	},
	
	IsRunning : function(obj, style) {
		obj = mint.$D(obj);
		return ((obj.$fx && obj.$fx[style] && obj.$fx[style].timeoutID) ? true : false);
	},
	
	Stop : function(obj, style) {
		obj = mint.$D(obj);
		
		if(!obj.$fx) return null;
		
		if(!style) {
			for(var s in obj.$fx) {
				obj.$fx[s].Stop();
			}
		}
		else {
			switch(style) {
				case "move" : {
					if(obj.$fx["left"]) obj.$fx["left"].Stop();
					if(obj.$fx["top"]) obj.$fx["top"].Stop();
					break;
				}
				case "size" : {
					if(obj.$fx["width"]) obj.$fx["width"].Stop();
					if(obj.$fx["height"]) obj.$fx["height"].Stop();
					break;
				}
				case "fade" : {
					if(obj.$fx["opacity"]) obj.$fx["fade"].Stop();
					break;
				}
				default : {
					if(obj.$fx[style]) obj.$fx[style].Stop();
				}
			}
		}
		return true;
	},

	Size : function(obj, width, height, steps, duration, OnStep, OnDone) {
		obj = mint.$D(obj);
		
		var group = mint.fx.Group(null, steps, duration);
		
		group.OnStep = OnStep || function() {};
		group.OnDone = OnDone || function() {};

		if(width !== null) group.Add(obj, "width", GetWidth(obj), width);
		if(height !== null) group.Add(obj, "height", GetHeight(obj), height);
		
		group.Run();
		return group;
	},

	Move : function(obj, x, y, steps, duration, OnStep, OnDone) {
		obj = mint.$D(obj);
		
		if(GetStyleFast(obj, "position") != "absolute")
			SetPos(obj, GetX(obj), GetY(obj));
		
		with(obj) {
			margin = "0px";
			padding = "0px";
			position = "absolute";
		}
		
		var group = mint.fx.Group(null, steps, duration);
		
		group.OnStep = OnStep || function() {};
		group.OnDone = OnDone || function() {};
		
		if(x !== null) group.Add(obj, "left", GetX(obj), x);
		if(y !== null) group.Add(obj, "top", GetY(obj), y);
		
		group.Run();
		return group;
	},

	Fade : function(obj, endOpacity, steps, duration, OnStep, OnDone) {
		this.Style(obj, "opacity", null, endOpacity, steps, duration, OnStep, OnDone);
	},
	
	Style : function(obj, style, start, end, steps, duration, OnStep, OnDone) {
		obj = mint.$D(obj);
		
		if(!obj.$fx) obj.$fx = {};
		
		if(this.IsRunning(obj, style))
			obj.$fx[style].Stop();
			
		var value = start !== null ? parseInt(start) : parseInt(GetStyle(obj, style));
		
		obj.$fx[style] = {
			end : end,
			value : value,
			style : style,
			step : (end-value)/steps,
			stepTime : duration/steps,
			elapsedTime : 0,
			timeoutID : null,
			OnStep : OnStep || function() {},
			OnDone : OnDone || function() {},
			Stop : function() {
				if(this.timeoutID)
					clearTimeout(this.timeoutID);
					
				this.timeoutID = null;
			}
		}
		
		mint.fx.$Style(obj, obj.$fx[style], style);
	},
	
	$Style : function(obj, fx, style) {
		with(fx) {
			OnStep(obj);
			value += step;
			elapsedTime += stepTime;
			
			if((step > 0 && value > end) || (step < 0 && value < end))
				value = end;
			
			style == "opacity" ? SetOpacity(obj, value) : obj.style[style] = value+"px";
			
			if(value == end) {
				OnDone(obj);
				timeoutID = null;
			}
			else
				obj.$fx[style].timeoutID = setTimeout(function() {mint.fx.$Style(obj, fx, style)}, stepTime);
		}
	},
	
	Group : function(style, steps, duration, OnStep, OnDone) {
		var group = {
			fx : [],
			style : style,
			steps : steps,
			stepCount : 0,
			duration : duration,
			stepTime : duration/steps,
			elapsedTime : 0,
			timeoutID : null,
			
			OnStep : OnStep || function() {},
			OnDone : OnDone || function() {},
			
			Add : function(obj, style, start, end, OnStep, OnDone) {
				obj = mint.$D(obj);
				
				if(!obj.$fx) obj.$fx = {};
				if(!style) style = this.style;
				
				var value = start !== null ? parseInt(start) : parseInt(GetStyle(obj, style));
				
				if(mint.fx.IsRunning(obj, style))
					obj.$fx[style].Stop();
				
				obj.$fx[style] = {
					obj : obj,
					end : end,
					value : value,
					step : (end-value)/this.steps,
					style : style,
					group : this,
					timeoutID : false,
					OnStep : OnStep || function() {},
					OnDone : OnDone || function() {},
					Stop : function() {
						for(var i = 0; i < this.group.fx.length; ++i) {
							if(this.group.fx[i] == this) {
								this.group.fx.splice(i, 1);
								break;
							}
						}
						
						this.timeoutID = false;
					}
				}
				
				this.fx.push(obj.$fx[style]);
				return obj.$fx[style];
			},
			
			Run : function() {
				this.fx.reverse();
				
				for(var x in this.fx)
					this.fx[x].timeoutID = true;
				
				mint.fx.$Group(this);
			},
			
			Stop : function() {
				if(this.timeoutID)
					clearTimeout(this.timeoutID);
					
				this.timeoutID = null;
			},
			
			Clear : function() {
				while(this.fx.length) {
					this.fx.pop().group = null;
				}
			}
		}
		
		return group;
	},
	
	$Group : function(g) {
		g.OnStep(g);
		g.elapsedTime += g.stepTime;
		
		for(var i = g.fx.length-1, fx = g.fx[i]; i >= 0; fx = g.fx[--i]) {
			fx.OnStep(fx.obj);
			fx.value += fx.step;
			
			if((fx.step > 0 && fx.value > fx.end) || (fx.step < 0 && fx.value < fx.end)) {
				fx.value = fx.end;
				fx.OnDone(fx.obj);
			}
		}
		
		for(var i = g.fx.length-1, fx = g.fx[i]; i >= 0; fx = g.fx[--i]) {
			fx.style == "opacity" ? SetOpacity(fx.obj, fx.value) : fx.obj.style[fx.style] = fx.value+"px";
		}

		if(g.stepCount++ == g.steps) {
			g.stepsElapsed = 0;
			g.timeoutID = false;
			g.OnDone(g);
		}
		else
			g.timeoutID = setTimeout(function() {mint.fx.$Group(g)}, g.stepTime);
		
	},
	
	Color : function(obj, style, startColor, endColor, steps, duration, OnStep, OnDone) {
		obj = mint.$D(obj);
			
		if(!obj.$fx) obj.$fx = {};
		
		if(this.IsRunning(obj, style))
			obj.$fx[style].Stop();
		
		obj.$fx[style] = {
			end : {},
			endHex : endColor,
			value : {},
			step : {},
			stepTime : duration/steps,
			elapsedTime : 0,
			timeoutID : null,
			OnStep : OnStep || function() {},
			OnDone : OnDone || function() {},
			Stop : function() {
				if(this.timeoutID)
					clearTimeout(this.timeoutID);
					
				this.timeoutID = null;
			}
		}
		
		var fx = obj.$fx[style];
		
		if(!startColor) {
			var value = GetStyle(obj, style == "borderColor" ? "borderLeftColor" : style);
			
			if(/^rgb\( ?(\d{1,3}), ?(\d{1,3}), ?(\d{1,3})\)$/.test(value))
				fx.value =  {r : parseInt(RegExp.$1), g : parseInt(RegExp.$2), b : parseInt(RegExp.$3)}
			else
				fx.value = HexToRGB(value);
		}
		else
			fx.value = HexToRGB(startColor);
			
		fx.end = HexToRGB(endColor);
			
		for(var c in fx.value) {
			fx.step[c] = (fx.end[c]-fx.value[c])/steps;
		}
		
		mint.fx.$Color(obj, fx, style);
	},

	$Color : function(obj, fx, style) {
		with(fx) {
			OnStep(obj);
			elapsedTime += stepTime;
			
			for(var c in value) {
				value[c] += step[c];
				
				if((step[c] > 0 && value[c] > end[c]) || (step[c] < 0 && value[c] < end[c]))
					value[c] = end[c];
			}
			
			obj.style[style] = "rgb("+parseInt(value.r)+", "+parseInt(value.g)+", "+parseInt(value.b)+")";
			
			if(value.r == end.r && value.g == end.g & value.b == end.b) {
				OnDone(obj);
				timeoutID = null;
				obj.style[style] = endHex;
			}
			else
				obj.$fx[style].timeoutID = setTimeout(function() {mint.fx.$Color(obj, fx, style)}, stepTime);
		}
	}
};

mint.gui =
{
	drag : null,
	startX : 0,
	startY : 0,
	startW : 0,
	startH : 0,
	offsetX : 0,
	offsetY : 0,

	stack : [],
	stackOffset : 0,

	dragObjects : [],
	dropZones : [],

	Init : function() {
		var that = this;
		AddEvent(document.getElementsByTagName("html")[0], "mousemove", function(e) {that.OnMouseMove(e)});
		AddEvent(document.getElementsByTagName("html")[0], "mouseup", function(e) {that.OnMouseUp(e)});
		return false;
	},

	OnMouseMove : function(e) {
		if(this.drag) {
			if(window.getSelection)
				window.getSelection().removeAllRanges();
			else if(document.getSelection)
				document.getSelection().removeAllRanges();
			else if(document.selection)
				document.selection.empty();
				
			var d = this.drag, z, x = e.clientX-this.offsetX, y = e.clientY-this.offsetY;
			
			if(!d.isDragged && d.threshold != 0 && Math.pow(x, 2)+Math.pow(y, 2) > Math.pow(d.threshold, 2)) {
				SetPos(d.obj, GetX(d.obj), GetY(d.obj));
				d.obj.style.position = "absolute";	
				
				d.OnDragStart(d.obj);
				d.isDragged = true;
			}
			
			if(d.isDragged) d.OnDrag(d.obj);
			
			if(!d.resize || (d.resize && d.resizeCtrl && !e.ctrlKey)) {
				if(!d.lockX) {
					if(d.minX !== null && x < d.minX) SetX(d.obj, d.minX);
					else if(d.maxX !== null && x+GetWidth(d.obj) > d.maxX) SetX(d.obj, d.maxX-GetWidth(d.obj));
					else SetX(d.obj, x);
				}
				
				if(!d.lockY) {
					if(d.minY !== null && y < d.minY) SetY(d.obj, d.minY);
					else if(d.maxY !== null && y+GetHeight(d.obj) > d.maxY) SetY(d.obj, d.maxY-GetHeight(d.obj));
					else SetY(d.obj, y);
				}
			}
			else {
				var w = this.startW+(e.clientX-this.startX), h = this.startH+(e.clientY-this.startY);
				
				mint.$(d.obj).addClass(d.resizeClass);
				
				if(!d.lockWidth) {
					if(d.minWidth !== null && w < d.minWidth) SetWidth(d.obj, d.minWidth);
					else if(d.maxWidth !== null && w > d.maxWidth) SetWidth(d.obj, d.maxWidth);
					else SetWidth(d.obj, w);
				}
				
				if(!d.lockHeight) {
					if(d.minHeight !== null && h < d.minHeight) SetHeight(d.obj, d.minHeight);
					else if(d.maxHeight !== null && h > d.maxHeight) SetHeight(d.obj, d.maxHeight);
					else SetHeight(d.obj, h);
				}
				
				d.OnResize(d.obj);
			}
			
			for(var i = this.dropZones.length-1, z = this.dropZones[i]; i >= 0; --i, z = this.dropZones[i]) {
				if(IsInside(z.obj, e.clientX+GetScrollX(), e.clientY+GetScrollY(), true)) {
					if((z.acceptClass && mint.$(d.obj).hasClass(z.acceptClass)) || (!z.acceptClass && z.OnAccept(d.obj))) {
						if(!z.hover) {
							mint.$(z.obj).addClass(z.hoverClass);	
							z.hover = d;
							z.OnHoverIn(d.obj);
						}
						
						if(!z.over && (z.over = GetChildAtPos(z.obj, e.clientX+GetScrollX(), e.clientY+GetScrollY(), true))) {
							if(z.useDummyNode) {
								if(z.dummyNode)
									z.obj.removeChild(z.dummyNode);
									
								z.dummyNode = d.obj.cloneNode(false);
								z.dummyNode.style.position = "static";
								
								if(z.autoInline) {
									z.dummyNode.style.cssFloat = "left";
									z.dummyNode.style.clear = "none";
								}
								
								z.dummyNodeClass ? z.dummyNode.className = z.dummyNodeClass : z.dummyNode.style.visibility = "hidden";
									
								z.obj.insertBefore(z.dummyNode, z.over);
								z.over = z.dummyNode;
							}
							else {
								mint.$(z.over).addClass(z.overClass);
								z.OnOverIn(d.obj, z.over);
							}
						}
						else {
							if(z.over != GetChildAtPos(z.obj, e.clientX+GetScrollX(), e.clientY+GetScrollY(), true))
								z.ResetOverState();
							else if(!z.dummyNode)
								z.OnOver(d.obj, z.over);
						}
						
						z.OnHover(d.obj);
						return true;
					}
				}
				else if(z.hover) {
					z.ResetOverState();
					z.ResetHoverState();
				}
			}
		}
		
		return true;
	},

	OnMouseUp : function(e) {
		if(this.drag) {
			var d = this.drag;
			
			for(var i = this.dropZones.length-1, z = this.dropZones[i]; i >= 0; --i, z = this.dropZones[i]) {
				if(IsInside(z.obj, e.clientX+GetScrollX(), e.clientY+GetScrollY(), true)) {
					if((z.acceptClass && mint.$(d.obj).hasClass(z.acceptClass)) || (!z.acceptClass && z.OnAccept(d.obj))) {
						d.dropZone = z;
						
						z.over && z.insertInside ? z.InsertItem(d.obj, z.over) : z.InsertItem(d.obj);
							
						z.ResetOverState();
						z.ResetHoverState();
					}
					
					break;
				}
			}
			
			mint.$(d.obj).removeClass(d.dragClass);
			mint.$(d.obj).removeClass(d.resizeClass);
			
			d.OnDragStop(d.obj);
			
			this.drag.isDragged = false;
			this.drag = null;
		}
		return false;
	},

	AddToStack : function(obj) {
		mint.$D(obj).style.zIndex = this.stack.push(mint.$D(obj))+this.stackOffset;
		return false;
	},
	
	RemoveFromStack : function(obj) {
		with(this) {
			for(var i = mint.$D(obj).style.zIndex-1-stackOffset; i < stack.length-1; ++i) {
				stack[i] = stack[i+1];
				stack[i].style.zIndex = i+1+stackOffset;
			}
			
			stack.pop();
		}
		return false;
	},
	
	MoveOnTop : function(obj) {
		this.RemoveFromStack(obj);
		this.AddToStack(obj);
		return false;
	},
	
	SetStackOffset : function(offset) {
		this.stackOffset = offset;
		return false;
	},

	$DragStart : function(e) {
		var m = mint.gui, d = this.dragObject, t = this, pos = GetPos(t);
		
		m.drag = d;
		m.startX = e.clientX;
		m.startY = e.clientY;
		m.startW = GetWidth(d.obj)-(parseInt(GetStyle(d.obj, "paddingLeft")) || 0)-(parseInt(GetStyle(d.obj, "paddingRight")) || 0);
		m.startH = GetHeight(d.obj)-(parseInt(GetStyle(d.obj, "paddingTop")) || 0)-(parseInt(GetStyle(d.obj, "paddingBottom")) || 0);
		m.offsetX = e.clientX-pos.x;
		m.offsetY = e.clientY-pos.y;
		
		mint.$(d.obj).addClass(d.dragClass);
		
		if(d.moveOnTop) m.MoveOnTop(t);
		if(d.threshold) return false;
		
		if(d.dropZone) {
			d.dropZone.RemoveItem(t);
			m.OnMouseMove(e);
			d.dropZone = null;
		}
		
		if(GetStyleFast(t, "position") != "absolute") {
			SetPos(t, pos.x, pos.y);
			t.style.position = "absolute";
		}
		
		if(t.parentNode != document.body) document.body.appendChild(t);
			
		d.OnDragStart(t);
		d.isDragged = true;
		
		return false;
	},

	RegisterDragObject : function(obj) {
		obj = mint.$D(obj);
		
		this.AddToStack(obj);
		
		AddEvent(obj, "mousedown", mint.gui.$DragStart);
		AddEvent(obj, "dragstart", function() {return false;});
		
		if(GetStyleFast(obj, "right") !== "") obj.style.right = "";
		if(GetStyleFast(obj, "bottom") !== "") obj.style.bottom = "";
		
		var ch = obj.childNodes;
		
		for(var i = 0; i < ch.length; ++i) {
			if(ch[i] && ch[i].tagName) {
				AddEvent(ch[i], "mousedown", function(e) {
					e.cancelBubble = true;
					if(e.stopPropagation) e.stopPropagation();
					return true;
				});
			}
		}
		
		var d = {
			obj : obj,
			minX : null,
			maxX : null,
			minY : null,
			maxY : null,
			minWidth : null,
			maxWidth : null,
			maxWidth : null,
			maxHeight : null,
			lockX : false,
			lockY : false,
			lockWidth : false,
			lockHeight : false,
			dragClass : null,
			hoverClass : null,
			dropZone : null,
			moveOnTop : true,
			threshold : 0,
			isDragged : false,
			resize : false,
			resizeCtrl : false,
			OnDrag : function() {},
			OnDragStart : function() {},
			OnDragStop : function() {},
			OnResize : function() {},
			
			AddGrip : function(grip, only) {
				AddEvent(mint.$D(grip), "mousedown", function(e) {mint.gui.$DragStart.call(obj, e)});
				if(only) RemoveEvent(obj, "mousedown", mint.gui.$DragStart);
			},
			
			SetBBox : function(obj)  {
				//obj = obj ? this.obj.parentNode : mint.$D(obj); - powoduje ustawiene rodzica czyli calego body - blad
				if(obj){
					var pos = GetPos(obj), size = GetSize(obj);
					if (navigator.appName.indexOf("Microsoft")!=-1) {
						this.minX = pos.x-1; //poprawki wprowadzone dla projektu examiner
						this.maxX = pos.x+size.width-9; //poprawki wprowadzone dla projektu examiner
						this.minY = pos.y-1; //poprawki wprowadzone dla projektu examiner
						this.maxY = pos.y+size.height-6; //poprawki wprowadzone dla projektu examiner
					}
					else{
						this.minX = pos.x;
						this.maxX = pos.x+size.width-6; //poprawki wprowadzone dla projektu examiner
						this.minY = pos.y;
						this.maxY = pos.y+size.height-3; //poprawki wprowadzone dla projektu examiner
					}
				}
			},
			
			RemoveBBox : function() {
				with(this) {
					minX = maxX = minY = maxY = null;
				}
			}
		}
		
		obj.dragObject = d;
		
		this.dragObjects.push(d);
		
		return d;
	},

	UnregisterDragObject : function(obj) {
		obj = mint.$D(obj);
		
		RemoveEvent(obj, "mousedown", this.$DragStart);
		RemoveEvent(obj, "dragstart", function() {return false;});
		
		for(var i in this.dragObjects) {
			if(this.dragObjects[i] == obj.dragObject) {
				this.dragObjects.splice(i, 1);
				obj.dragObject = null
				return true;
			}
		}
		
		return false;
	},

	RegisterDropZone : function(obj) {
		obj = mint.$D(obj);
		
		var z = {
			obj : obj,
			over : null,
			hover : null,
			overClass : null,
			hoverClass : null,
			acceptClass : null,
			dummyNode : null,
			dummyNodeClass : null,
			useDummyNode : true,
			insertInside : true,
			autoInline : true,
			OnAdd : function() {},
			OnRemove : function() {},
			OnDrag : function() {},
			OnDrop : function() {},
			OnHover : function() {},
			OnHoverIn : function() {},
			OnHoverOut : function() {},
			OnOver : function() {},
			OnOverIn : function() {},
			OnOverOut : function() {},
			OnAccept : function() {return true;},
			
			InsertItem : function(obj, before) {
				obj = mint.$D(obj);
				
				this.obj.insertBefore(obj, before || null);
				
				obj.dragObject.dropZone = this;
				obj.style.position = "static";
				
				if(this.autoInline) {
					obj.style.cssFloat = "left";
					obj.style.clear = "none";
				}
				
				this.OnAdd(obj);
			},
			
			RemoveItem : function(obj) {
				obj = mint.$D(obj);
				
				SetPos(obj, GetX(obj), GetY(obj));
				obj.style.position = "absolute";
				
				document.body.appendChild(obj).dragObject.dropZone = null;
				
				this.OnRemove(obj);
			},
			
			ResetOverState : function() {
				with(this) {
					if(!over) return;
					
					if(dummyNode) {
						obj.removeChild(dummyNode);
						dummyNode = null;
					}
					else {
						mint.$(over).removeClass(overClass);
						OnOverOut(over);
					}
					
					over = null;
				}
			},
			
			ResetHoverState : function() {
				if(!this.hover) return;
				
				mint.$(this.obj).removeClass(this.hoverClass);
				
				this.OnHoverOut(this.hover);
				this.hover = null;
			}
		}
		
		obj.dropZone = z;
		
		this.dropZones.push(z);
		
		return z;
	},
	
	UnregisterDropZone : function(obj) {
		obj = mint.$D(obj);
		
		for(var i in this.dropZones) {
			if(this.dropZones[i] == obj.dropZone) {
				this.dropZones.splice(i, 1);
				obj.dropZone = null;
				return true;
			}
		}
		
		return false;
	}
};

mint.gui.Init();

mint.$ = function(id) {
	if(!id) return null;
		
	var e = (typeof id == "string") ? document.getElementById(id) : id;
	
	if(!e) return null;
	
	e.getTagName = function(tag) {
		return this.tagName ? this.nodeName.toLowerCase() : null;
	}
		
	e.next = function(tag, offset) {
		var next = this.nextSibling;
		
		for(var i = 0; next; next = next.nextSibling) {
			if(!mint.$(next).isWhitespace() && ((tag && mint.$(next) && mint.$(next).getTagName() == tag) || (!tag && mint.$(next))))
				if(!offset || ++i == offset)
					break;
		}
			
		return mint.$(next);
	}
	
	e.prev = function(tag, offset) {
		var prev = this.previousSibling;
		
		for(var i = 0; prev; prev = prev.previousSibling) {
			if(!mint.$(prev).isWhitespace() && ((tag && mint.$(prev) && $(prev).getTagName() == tag) || (!tag && mint.$(prev))))
				if(!offset || ++i == offset)
					break;
		}
		
		return mint.$(prev);
	}
	
	e.up = function() {
		return mint.$(this.parentNode);
	}
	
	e.down = function(tag, offset) {
		var child = this.firstChild;
		
		if((tag && mint.$(child).getTagName() != tag) || offset) child = mint.$(child).next(tag, offset);
		
		while(mint.$(child).isWhitespace()) {
			child = mint.$(child).next(tag, offset);
		}
			
		return child;
	}
	
	e.appendTo = function(parent) {
		return parent.appendChild(this);
	}
	
	e.getElementsByClass = function(className, tag) {
		var r = [];
		var n = this.getElementsByTagName(tag || '*');
		var p = new RegExp('(^|\\s)'+className+'(\\s|$)');
		
		for(var i = 0; i < n.length; ++i)
			if(p.test(n[i].className))
				r.push(n[i]);
		
		return r;
	}
	
	e.hide = function() {
		if(!this._style) this._style = {};
		this._style.width = GetStyleFast(this, "width");
		this._style.height = GetStyleFast(this, "height");
		this._style.display = GetStyleFast(this, "display");
		this.style.display = "none";
	}
	
	e.show = function() {
		this.style.display = this._style && this._style.display ? this._style.display : "";
	}
	
	e.toggle = function() {
		GetStyleFast(this, "display") == "none" ? this.show() : this.hide();
	}
	
	e.hasClass = function(name) {
		if(!name) return false;
		
		var cl = this.className.split(" ");
		
		for(var c in cl)
			if(cl[c] == name) return true;
		
		return false;
	}
	
	e.addClass = function(name) {
		if(!name) return;
		
		if(!this.hasClass(name)) {
			var cl = this.className.split(" ");
			cl.push(name);
			this.className = cl.join(" ");
		}
	}
	
	e.removeClass = function(name) {
		if(!name) return;
		
		var cl = this.className.split(" ");
		
		for(var c in cl) {
			if(cl[c] == name) {
				cl.splice(c, 1);
				break;
			}
		}
		
		this.className = cl.join(" ");
	}
	
	e.isWhitespace = function() {
		return (this.nodeName != "#text") ? false : !/\S/.test(this.nodeValue);
	}

	return e;
}

mint.$C = function(type, id, className, style, attr, parent) {
	var c = (type == "#text") ? document.createTextNode(type) : document.createElement(type);
	c.id = id || "";
	c.className = className || "";
	
	if(style) for(var s in style) c.style[s] = style[s];
	if(attr) for(var a in attr) c[a] = attr[a];
	
	if(parent) parent.appendChild(c);
	return mint.$(c);
}

function AddEvent(obj, type, handler) {
	obj = mint.$D(obj);
	if(obj){
		if(!obj.events) obj.events = [];
		
		if(!obj.events["on"+type]) {
			obj.events["on"+type] = [];
			if(obj["on"+type]) obj.events["on"+type].push(obj["on"+type]);
		}
		
		obj.events["on"+type].push(handler);
	
		obj["on"+type] = function(event) {
			event = event || window.event;
			
			var returnValue = true;
			var eventHandlers = this.events["on"+event.type];
			
			if(eventHandlers) {
				for(var i = 0; i < eventHandlers.length; ++i) {
					this.$eventHandler = eventHandlers[i];
					returnValue = this.$eventHandler.call(this, event);
				}
			}
			
			return returnValue;
		}
	}
	return false;
}

function RemoveEvent(obj, type, handler) {
	obj = mint.$D(obj);
	if(obj){
		if(!obj.events) return false;
		var eventHandlers = obj.events["on"+type];
	
		for(var i in eventHandlers) {
			if(String(eventHandlers[i]) === String(handler)) {
				eventHandlers.splice(i, 1);
				return true;
			}
		}
	}
	return false;
}

function GetPos(obj, ignoreMargins) {
	obj = mint.$D(obj);

	var x = obj.offsetLeft || 0, y = obj.offsetTop || 0;
	
	if(!ignoreMargins) {
		var marginLeft = parseInt(GetStyleFast(obj, "marginLeft", "margin-left"));
		var marginTop = parseInt(GetStyleFast(obj, "marginTop", "margin-top"));
		
		if(marginLeft) x -= marginLeft;
		if(marginTop) y -= marginTop;
	}	
	
	obj = obj.offsetParent;
	while(obj) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		
		if(obj.clientLeft) x += obj.clientLeft;
		if(obj.clientTop) y += obj.clientTop;
		obj = obj.offsetParent;
	}
	
	return {x:x, y:y};
}

function GetX(obj, ignoreMargins) {
	return GetPos(obj, ignoreMargins).x;
}

function GetY(obj, ignoreMargins) {
	return GetPos(obj, ignoreMargins).y;
}

function SetPos(obj, x, y) {
	SetX(obj, x);
	SetY(obj, y);
	return mint.$D(obj);
}

function SetX(obj, x) {
	if(mint.$D(obj)){
		mint.$D(obj).style.left = x+"px";
		return mint.$D(obj);
	}
	return null;
}

function SetY(obj, y) {
	if(mint.$D(obj)){
		mint.$D(obj).style.top = y+"px";
		return mint.$D(obj);
	}
	return null;
}

function GetSize(obj) {
	return {'width':GetWidth(obj), 'height':GetHeight(obj)};
}

function GetWidth(obj) {
	if(obj){
		if(mint.$D(obj)){
			if(!mint.$D(obj).clientWidth) mint.$D(obj).style.zoom = "1.0";
			return mint.$D(obj).clientWidth;
		}
	}
	return 0;
}

function GetHeight(obj) {
	if(obj){
		if(mint.$D(obj)){
			//obj = mint.$D(obj);
			if(!mint.$D(obj).clientHeight) mint.$D(obj).style.zoom = "1.0";
			return mint.$D(obj).clientHeight;
		}
	}
	return 0;
}

function SetSize(obj, width, height) {
	if(mint.$D(obj)){
		this.SetWidth(obj, width);
		this.SetHeight(obj, height);
		return mint.$D(obj);
	}
	return null;
}

function SetWidth(obj, width) {
	if(obj){
		if(mint.$D(obj)){
			mint.$D(obj).style.width = width+"px";
			return mint.$D(obj);
		}
	}
	return null;
}

function SetHeight(obj, height) {
	if(obj){
		if(mint.$D(obj)){
			mint.$D(obj).style.height = height+"px";
		}
	}
	return null;
}

function GetOpacity(obj) {
	obj = mint.$D(obj);

	if(obj.style.opacity)
		return Math.round(obj.style.opacity*100);
	else if(obj.style.filter)
		return Math.round(/\d+/.exec(obj.style.filter)[0]);
	else
		return 100;
}

function SetOpacity(obj, opacity) {
	obj = mint.$D(obj);
	obj.style.opacity = opacity*0.01;
	obj.style.filter = "alpha(opacity="+opacity+")";
	obj.style.zoom = "1.0";
	return obj;
}

function GetStyle(obj, style) {
	obj = mint.$D(obj);
	
	if(style == "opacity")
		return GetOpacity(obj);
		
	if(obj.currentStyle) return obj.currentStyle[style];
	else if(window.getComputedStyle) return getComputedStyle(obj, "").getPropertyValue(style.replace(/[A-Z]/g, function(obj, ch) {return "-"+style.charAt(ch).toLowerCase()}));
	return false;
}

function GetStyleFast(obj, style, styleCSS) {
	obj = mint.$D(obj);
	if(obj){
		if(obj.currentStyle){
			return obj.currentStyle[style];
		}
		else{
			if(window.getComputedStyle(obj, "")){ 
				return window.getComputedStyle(obj, "").getPropertyValue(styleCSS || style);
			}
		}
	}
	return false;
}

function GetScrollX() {
	return window.scrollX || document.scrollLeft || document.documentElement.scrollLeft;
}

function GetScrollY() {
	return window.scrollY || document.scrollTop || document.documentElement.scrollTop;
}

function IsInside(obj, x, y, ignoreMargins) {
	obj = mint.$D(obj);
	
	var pos = GetPos(obj, ignoreMargins), size = GetSize(obj);
	
	if(pos.x < x && pos.x+size.width > x && pos.y < y && pos.y+size.height > y)
		return true;

	return false;
}

function GetChildAtPos(obj, x, y, ignoreMargins) {
	var child = mint.$D(obj).childNodes;

	for(var i = child.length-1, ch = child[0]; i >= 0; i--) {
		if(child[i].nodeName != "#text" && IsInside(child[i], x, y, ignoreMargins))
			return child[i];
	}

	return null;
}

function HexToRGB(hex) {
	hex = hex.replace(/#/, "");

	return {	r: parseInt(hex.substring(0, 2), 16),
				g: parseInt(hex.substring(2, 4), 16),
				b: parseInt(hex.substring(4, 6), 16)};
}

function RGBToHex(r, g, b) {
	return ToHex(r)+ToHex(g)+ToHex(b)
}

function ToHex(n) {
	if (!n) return "00";

	n = Math.round(Math.min(Math.max(0,parseInt(n)),255));

	return "0123456789ABCDEF".charAt((n-n%16)/16) + "0123456789ABCDEF".charAt(n%16);
}

function AddHover(obj, className) {
	AddEvent(obj, "mouseover", function() {$(obj).addClass(className)});
	AddEvent(obj, "mouseout", function() {$(obj).removeClass(className)});	
}

function RemoveHover(obj) {
	RemoveEvent(obj, "mouseover", function() {$(obj).addClass(className)});
	RemoveEvent(obj, "mouseout", function() {$(obj).removeClass(className)});		
}

function SetCookie(name, value, days, path, domain, secure) {
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
		var expires = "";
		
	document.cookie = name+"="+escape(value)+expires+
		(path ? "; path="+path : "")+
		(domain ? "; domain="+domain : "")+
		(secure ? "; secure="+secure : "");
}

function GetCookie(name) {
	if(document.cookie.length > 0) {
		var start = document.cookie.indexOf(name+"=");
		if(start != -1) {
			start = start+name.length+1;
			var end = document.cookie.indexOf(";",start);
			if(end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(start, end));
		}
	}
	return "";
}

function DeleteCookie(name) {
	SetCookie(name, "", -1);
}

function InArray(array, value) {
	for(var i = 0; i < array.length; ++i) {
		if(array[i] === value) return true;
	}
	
	return false;
}

var $ = mint.$;
var $C = mint.$C;
var $D = mint.$D;
var $R = mint.$R;