var Html = {
	BOX_NONE: 0,
	BOX_PADDING: 1,
	BOX_BORDER: 2,
	BOX_MARGIN: 4,
	BOX_ALL: 0,
//	BOX_ALL: this.BOX_PADDING | this.BOX_BORDER | this.BOX_MARGIN,
	os: {},
	browser: {},

	getById: function(node) {
		return typeof node == "string" ? document.getElementById(node) : node;
	},

	getComputedStyle: function(node) {
		node = this.getById(node);
		if(this.browser.safari){
			var s = document.defaultView.getComputedStyle(node, null);
			if(!s && node.style){ 
				node.style.display = ""; 
				s = dv.getComputedStyle(node, null);
			}
			return s || {};
		} else if(this.browser.ie) {
			return node.currentStyle;
		} else {
			return document.defaultView.getComputedStyle(node, null);
		}
	},

	toPixelValue: function(node, value){
		if(!this.browser.ie) {
			return parseFloat(value) || 0; 
		} else {
			if (!value) {
				return 0;
			}
			if (value=="medium") {
				return 4;
			}
			// style values can be floats, client code may
			// want to round this value for integer pixels.
			if (value.slice && (value.slice(-2)=="px")) {
				return parseFloat(value);
			}
			with(node) {
				var sLeft = style.left;
				var rsLeft = runtimeStyle.left;
				runtimeStyle.left = currentStyle.left;
				try {
					style.left = value;
					value = style.pixelLeft;
				} catch(e) {
					value = 0;
				}
				style.left = sLeft;
				runtimeStyle.left = rsLeft;
			}

			return value;
		}
	},

	show: function (node) {
		node = this.getById(node);
		node.style.display = "block";
	},

	hide: function (node) {
		node = this.getById(node);
		node.style.display = "none";
	},

	toggle: function (node) {
		node = this.getById(node);
		if (node.style.display == "none") {
			node.style.display = "block";
			return true;
		} else {
			node.style.display = "none";
			return false;
		}
	},

	getViewport: function () {
		var w = 0;
		var h = 0;
		if (this.browser.mozilla) {
			w = document.documentElement.clientWidth;
			h = window.innerHeight;
		} else {
			if (!this.browser.opera && window.innerWidth) {
				w = window.innerWidth;
				h = window.innerHeight;
			} else {
				if (!this.browser.opera && document.documentElement.clientWidth) {
					var w2 = document.documentElement.clientWidth;
					if (!w || w2 && w2 < w) {
						w = w2;
					}
					h = document.documentElement.clientHeight;
				} else {
					if (document.body.clientWidth) {
						w = document.body.clientWidth;
						h = document.body.clientHeight;
					}
				}
			}
		}
		return {width: w, height: h};
	},

	getScroll: function () {
		var top = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
		var left = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
		return {top: top, left: left, offset: {x: left, y: top}};
	},

	setPositivePixelValue: function (node, selector, value) {
		if (isNaN(value)) {
			return false;
		}
		node.style[selector] = Math.max(0, parseInt(value)) + "px";
		return true;
	},

	setSize: function (node, args, box) {
		node = this.getById(node);
		var ret = {};
		if (typeof args.width != "undefined") {
			ret.width = this.setPositivePixelValue(node, "width", args.width);
		}
		if (typeof args.height != "undefined") {
			ret.height = this.setPositivePixelValue(node, "height", args.height);
		}
		return ret;
	},

	getSize: function (node, box) {
		if (typeof box == "undefined") {
			box = this.BOX_ALL;
		}
		node = this.getById(node);
		var cs = this.getComputedStyle(node);
		var r = {width: node.offsetWidth, height: node.offsetHeight};

		if (!(box & this.BOX_PADDING)) {
			r.width -= this.toPixelValue(node, cs.paddingRight) + this.toPixelValue(node, cs.paddingLeft);
			r.height -= this.toPixelValue(node, cs.paddingTop) + this.toPixelValue(node, cs.paddingBottom);
		}
		if (!(box & this.BOX_BORDER)) {
			r.width -= this.toPixelValue(node, cs.borderRightWidth) + this.toPixelValue(node, cs.borderLeftWidth);
			r.height -= this.toPixelValue(node, cs.borderTopWidth) + this.toPixelValue(node, cs.borderBottomWidth);
		}
		if (box & this.BOX_MARGIN) {
			r.width += this.toPixelValue(node, cs.marginRight) + this.toPixelValue(node, cs.marginLeft);
			r.height += this.toPixelValue(node, cs.marginTop) + this.toPixelValue(node, cs.marginTopBottom);
		}
		return r;
	},

	getBox: function (node, box) {
		if (typeof box == "undefined") {
			box = this.BOX_ALL;
		}
		node = this.getById(node);
		var cs = this.getComputedStyle(node);
		var r = {width: 0, height: 0};

		if (box & this.BOX_PADDING) {
			r.width += this.toPixelValue(node, cs.paddingRight) + this.toPixelValue(node, cs.paddingLeft);
			r.height += this.toPixelValue(node, cs.paddingTop) + this.toPixelValue(node, cs.paddingBottom);
		}
		if (box & this.BOX_BORDER) {
			r.width += this.toPixelValue(node, cs.borderRightWidth) + this.toPixelValue(node, cs.borderLeftWidth);
			r.height += this.toPixelValue(node, cs.borderTopWidth) + this.toPixelValue(node, cs.borderBottomWidth);
		}
		if (box & this.BOX_MARGIN) {
			r.width += this.toPixelValue(node, cs.marginRight) + this.toPixelValue(node, cs.marginLeft);
			r.height += this.toPixelValue(node, cs.marginTop) + this.toPixelValue(node, cs.marginTopBottom);
		}
		return r;
	},

	setPosition: function (node, args) {
		node = this.getById(node);
		var ret = {};
		var sc = this.getScroll();
		if (typeof args.left != "undefined") {
			ret.width = this.setPositivePixelValue(node, "left", args.left+sc.left);
		}
		if (typeof args.top != "undefined") {
			ret.height = this.setPositivePixelValue(node, "top", args.top+sc.top);
		}
		return ret;
	},

	getOpacity: function (node) {
		node = this.getById(node);
		if (this.browser.ie) {
			var opac = (node.filters && node.filters.alpha && typeof node.filters.alpha.opacity == "number" ? node.filters.alpha.opacity : 100) / 100;
		} else {
			var opac = node.style.opacity || node.style.MozOpacity || node.style.KhtmlOpacity || 1;
		}
		return opac >= 0.999999 ? 1 : Number(opac);
	},

//	clearOpacity: function (node) {
//		node = typeof node == "string" ? document.getElementById(node) : node;
//		var ns = node.style;
//		if (this.browser.ie) {
//			try {
//				if (node.filters && node.filters.alpha) {
//					ns.filter = "";
//				}
//			} catch (e) {}
//		} else {
//			if (this.browser.moz) {
//				ns.opacity = 1;
//				ns.MozOpacity = 1;
//			} else {
//				if (this.browser.safari) {
//					ns.opacity = 1;
//					ns.KhtmlOpacity = 1;
//				} else {
//					ns.opacity = 1;
//				}
//			}
//		}
//	},
//
//	setOpacity = function (node, opacity, dontFixOpacity) {
	setOpacity: function (node, opacity) {
		node = this.getById(node);
//		if (!dontFixOpacity) {
//			if (opacity >= 1) {
//				if (this.browser.ie) {
//					this.clearOpacity(node);
//					return;
//				} else {
//					opacity = 0.999999;
//				}
//			} else if (opacity < 0) {
//				opacity = 0;
//			}
//		}
		if (this.browser.ie) {
			if (node.nodeName.toLowerCase() == "tr") {
				var tds = node.getElementsByTagName("td");
				for (var x=0; x < tds.length; x++) {
					tds[x].style.filter = "Alpha(Opacity=" + opacity * 100 + ")";
				}
			}
			node.style.filter = "Alpha(Opacity=" + opacity * 100 + ")";
		} else {
			if (this.browser.moz) {
				node.style.opacity = opacity;
				node.style.MozOpacity = opacity;
			} else {
				if (this.browser.safari) {
					node.style.opacity = opacity;
					node.style.KhtmlOpacity = opacity;
				} else {
					node.style.opacity = opacity;
				}
			}
		}
	},

	removeChildNodes: function(node) {
		node = this.getById(node);
		while (node.hasChildNodes()) {
			node.firstChild.parentNode.removeChild(node.firstChild);
		}
	}
};

(function () {
	var dua = navigator.userAgent;
	var dav = navigator.appVersion;
	Html.os.mac = dua.indexOf("Macintosh") >= 0;
	Html.os.win = dua.indexOf("Windows") >= 0;
	Html.os.linux = dua.indexOf("X11") >= 0;
	Html.browser.opera = dua.indexOf("Opera") >= 0;
	Html.browser.khtml = (dav.indexOf("Konqueror") >= 0) || (dav.indexOf("Safari") >= 0);
	Html.browser.safari = dav.indexOf("Safari") >= 0;
	var geckoPos = dua.indexOf("Gecko");
	Html.browser.mozilla = Html.browser.moz = (geckoPos >= 0) && (!Html.browser.khtml);
	if (Html.browser.mozilla) {
		Html.browser.geckoVersion = dua.substring(geckoPos + 6, geckoPos + 14);
	}
	Html.browser.ie = (document.all) && (!Html.browser.opera);
	Html.browser.ie50 = Html.browser.ie && dav.indexOf("MSIE 5.0") >= 0;
	Html.browser.ie55 = Html.browser.ie && dav.indexOf("MSIE 5.5") >= 0;
	Html.browser.ie60 = Html.browser.ie && dav.indexOf("MSIE 6.0") >= 0;
	Html.browser.ie70 = Html.browser.ie && dav.indexOf("MSIE 7.0") >= 0;

	for (var i in Html) {
		if (i.substring(0, 4)=="BOX_") {
			Html.BOX_ALL |= Html[i];
		}
	}
})();
