//drjs.ui.js
//UI Functions

var UI = {
	ZIndex: 9000,	
	AlphaBg: {
		Color: "#FFFFFF",
		div: null,
		lvl: [],
		Opacity: "75",
		CheckBg: function() {
			if(this.div==null) {
				this.CreateBg();
			}
			this.div.style.width = "100%";
			var height = 0;
			height = UI.Document.GetMeasures().height;
			this.div.style.height = height+"px";
		},
		CreateBg: function(color, opacity) {
			if($("drjsuiAlphaBg")) {
				UI.Document.RemoveFromBody("drjsuiAlphaBg");
			}
			this.div = UI.Document.AddToBody("div", "drjsuiAlphaBg");
			this.div.style.position = "absolute";
			this.div.style.top = "0px";
			this.div.style.left = "0px";
			this.div.style.background = (color==null) ? this.Color : color;
			this.div.style.display = "none";
			this.div.style.display = "none";
			this.div.style.opacity = (opacity==null) ? "."+this.Opacity : "."+opacity;
			this.div.style.filter = (opacity==null) ? "alpha(opacity="+this.Opacity+")" : "alpha(opacity="+opacity+")";
		},
		DecBgLevel: function(index) {
			this.CheckBg();			
			if(this.lvl.length>1) {
				this.lvl = this.lvl.removePos(this.lvl.length-1);
				this.div.style.zIndex = this.lvl[this.lvl.length-1];
			} else {
				this.HideBg();
			}
			this.div.style.zIndex;
		},
		IncBgLevel: function() {
			this.CheckBg();
			if(this.div.style.display!="none") {
				this.div.style.zIndex = UI.GetZIndex();
				this.lvl.push(this.div.style.zIndex);
			} else {
				this.ShowBg();
			}
			this.div.style.zIndex;
		},
		HideBg: function() {
			this.CheckBg();
			this.div.style.display = "none";
		},
		ShowBg: function() {
			this.CheckBg();
			this.div.style.zIndex = UI.GetZIndex();
			this.div.style.display = "inline";
			this.lvl = [];
			this.lvl.push(this.div.style.zIndex);
			return this.div.style.zIndex;
		}
	},
	DragObj: {
		Obj: null,
		oldMMove: null,
		oldMUp: null,
		tx: 0,
		ty: 0,
		x: 0,
		y: 0,
		StartDrag: function(obj, dontStopOnUp) {
			this.Obj = $(obj);
			this.Obj.style.position = "absolute";
			this.Obj.style.zIndex = UI.GetZIndex();
			this.tx = parseInt(this.Obj.style.left+0);
			this.ty = parseInt(this.Obj.style.top+0);
			this.x = event.clientX;
			this.y = event.clientY;
			document.body.focus();
			document.onmousemove = this.Drag;
			if(dontStopOnUp!=true) {
				document.onmouseup = this.StopDrag;
			}
		},
		Drag: function(e) {
			if(e==null) {
				e = window.event;
			}
			UI.DragObj.Obj.style.left = UI.DragObj.tx + e.clientX - UI.DragObj.x;
			UI.DragObj.Obj.style.top = UI.DragObj.ty + e.clientY - UI.DragObj.y;
		},
		StopDrag: function() {
			document.onmousemove = null;
			document.onmouseup = null;
		}
	},
	Center: function(element) {
		element.style.position = "absolute";
		element.style.top = (((window.document.body.offsetHeight/2)-element.offsetHeight/2) + window.document.body.scrollTop) - 10;
		element.style.left = ((window.document.body.offsetWidth/2)-element.offsetWidth/2);
	},
	Document: {
		onclickFArray: [],
		AddOnClickFunction: function(fn, checked, params) {
			checked = (checked==null) ? 1 : checked;
			UI.Document.onclickFArray.push([fn, checked, params]);
			return UI.Document.onclickFArray.length-1;
		},
		RemoveOnClickFunction: function(fn) {
			for(var i=0; i<UI.Document.onclickFArray.length; i++) {
				if(this.onclickFArray[i][0]==fn) {
					this.onclickFArray = this.onclickFArray.removePos(i);
				}
			}
		},
		CheckOnClickFunction: function() {
			for(var i=0; i<UI.Document.onclickFArray.length; i++) {
				var params = (UI.Document.onclickFArray[i][2]!=null) ? UI.Document.onclickFArray[i][2] : null;
				if(UI.Document.onclickFArray[i][1] == 1) {
					UI.Document.onclickFArray[i][0](params);
				} else {
					UI.Document.onclickFArray[i][1] = 1;
				}
			}
		},
		AddToBody: function(tag, id) {
			var element = document.createElement(tag);
			document.body.appendChild(element);
			element.setAttribute("id", id);
			return element;
		},
		RemoveFromBody: function(id) {
			document.body.removeChild(document.getElementById(id));	
		},
		GetMeasures: function() {
			var pageWidth, pageHeight;
			if(window.innerHeight && window.scrollMaxY) { //FF
				pageWidth = window.innerWidth + window.scrollMaxX;
				pageHeight = window.innerHeight + window.scrollMaxY;
			} else if(document.body.scrollHeight > document.body.offsetHeight) {
				pageWidth = document.body.scrollWidth;
				pageHeight = document.body.scrollHeight;
			} else { // IE Stric, MM, Safari
				pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
				pageHeight = document.body.offsetHeight + document.body.offsetTop; 
			}
			return {width: pageWidth, height:pageHeight};
		}
	},
	Fade: {
		FadeIn: function(obj, time, callback) {
			target = $(obj);
			if(target.getAttribute("fadingout")==true) {
				target.setAttribute("fadingout", false);
			}
			target.setAttribute("fadingin", true);
			alpha = 0;
			timer = (time*1000)/100;
			var i = setInterval(
					function() {
						if (alpha >= 100) {
							clearInterval(i);
							if(callback!=null && target.getAttribute("fadingin")==true) {
								callback();
							}
							target.setAttribute("fadingin", false);
						}
						UI.Fade.SetAlpha(target, alpha);
						alpha += 10;
					}, timer);
		},
		FadeOut: function(obj, time, callback) {
			target = $(obj);
			if(target.getAttribute("fadingin")==true) {
				target.setAttribute("fadingin", false);
			}
			target.setAttribute("fadingout", true);
			alpha = 100;
			timer = (time*1000)/100;
			var i = setInterval(
					function() {
						if ((alpha <= 0) || (target.getAttribute("fadingout")==false)) {
							clearInterval(i);
							if(callback!=null && target.getAttribute("fadingout")==true) {
								callback();
							}
							target.setAttribute("fadingout", false);
						}
						UI.Fade.SetAlpha(target, alpha);
						alpha -= 10;
					}, timer);
		},
		SetAlpha: function(obj, alpha) {
			$(obj).style.filter = "alpha(opacity="+ alpha +")";
			$(obj).style.opacity = alpha/100;
		}
	},
	FindPosX: function (obj){		
		var curleft = 0;
		/*if(obj.offsetParent) {
			while(1) {
				curleft += obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		} else if(obj.x) {
			curleft += obj.x;
		}*/
		do {
			curleft += obj.offsetLeft;
		} while(obj = obj.offsetParent);
		/*
		var curleft = obj.offsetLeft;
		while(obj.offsetParent) {
			curleft += obj.offsetParent.offsetLeft;
			obj = obj.offsetParent;
		}
		*/
		return curleft;
	},
	FindPosY: function(obj){
		var curTop = obj.offsetTop;
		while(obj.offsetParent) {
			curTop += obj.offsetParent.offsetTop;
			obj = obj.offsetParent;
		}
		return curTop;
	},
	GetZIndex: function(factor) {
		this.ZIndex += 4;
		if(factor!=null) {
			return this.ZIndex+factor;
		}
		return this.ZIndex;
	},
	GoTo: function(src) {
		window.location = src;
	},
	Hide: function(obj) {
		if(isArray(obj)) {
			for(var i=0; i<obj.length; i++) {
				$(obj[i]).Hide();
			}
		} else {
			$(obj).Hide();
		}
	},
	SetBgColor: function(obj, color) {
		$(obj).style.backgroundColor = color;
	},	
	SetColor: function(obj, color) {
		$(obj).style.color = color;
	},
	Show: function(obj) {
		if(isArray(obj)) {
			for(var i=0; i<obj.length; i++) {
				$(obj[i]).Show();
			}
		} else {
			$(obj).Show();
		}
	},
	SwapImage: function(obj, src) {
		$(obj).src = src;
	},
	Themes: {
		theme: null,
		themeList: [],
		themesFolder: "themes",
		AddTheme: function(name, path) {
			//document.appendStyleSheet(path + "specialforms.css");
			includeCss(path + "wysiwyg.css");
			includeCss(path + "specialforms.css");
			this.themeList[name] = path;
		},
		GetImage: function(src, theme) {
			if(theme==null) {
				if(this.theme==null) {
					this.init();
				}
				theme = this.theme;
			}
			return this.themeList[theme] + src;
		},
		GetTheme: function() {
			if(this.theme==null) {
				this.init();
			}
			return this.theme;
		},
		init: function() {
			this.SetTheme("default", _drjsPath + this.themesFolder + "/default/");
		},
		SetTheme: function(name, path) {
			if(!this.themeList[name]) {
				this.AddTheme(name, path);
			}
			this.theme = name;
		}
	}
}

drjs.loadComplete(drjs.files.ui);