/**
 * @author Dmitry Grigoriev, dimgel.com
 * @license GPL2, or contact me for commercial usage
 */

function dimIsIE() {
	return !!document.all;
}
function dimIsFF() {
	return !!(document.getElementById && !document.all);
}

function dimGet(id, d) {
	if (!d) {
		d = document;
	}
	return dimIsIE() ? d.all[id] : d.getElementById(id);
}

function dimParent(e) {
	return e ? e.parentNode : null;
}
function dimIsDescendant(anc, desc) {
	var x = desc;
	while(true) {
		x = dimParent(x);
		if (!x) {
			return false;
		}
		if (x == anc) {
			return true;
		}
	}
}

function dimX(e) {
	return e.offsetLeft;
}
function dimY(e) {
	return e.offsetTop;
}
function dimWidth(obj) {
	return parseInt(obj.offsetWidth + '');
}
function dimHeight(obj) {
	return parseInt(obj.offsetHeight + '');
}

function dimWindowWidth() {
	return parseInt((dimIsFF() ? window.innerWidth : document.body.clientWidth) + "");
}
function dimWindowHeight() {
	return parseInt((dimIsFF() ? window.innerHeight : document.body.clientHeight) + "");
}
function dimWindowScrollX() {
	return document.body.scrollLeft;
}
function dimWindowScrollY() {
	return document.body.scrollTop;
}

function dimCenterObject(obj) {
	obj.style.left = (dimWindowWidth() - dimWidth(obj)) / 2 + dimWindowScrollX();
	obj.style.top = (dimWindowHeight() - dimHeight(obj)) / 2 + dimWindowScrollY();
}

function dimEventTarget(ev) {
	return dimIsIE() ? window.event.srcElement : ev.target;
}

function dimSetSelectValue(sel, value) {
	for (var i = 0; i < sel.length; i++) {
		if (sel.options[i].value == value) {
			sel.selectedIndex = i;
			return true;
		}
	}
	return false;
}

function dimLoadBrowserCSS(href) {
	var u = navigator.userAgent;
	var s = null;
	if (/Opera/.test(u))  s = "opera";
	else if (/Konqueror/.test(u))  s = "konqueror";
	else if (/MSIE/.test(u))  s = "ie";
	else if (/Firefox/.test(u))  s = "firefox";

	if (s) {
		document.writeln("<link rel='stylesheet' type='text/css' href='" + 
				href.replace(/browser/, s) + "'>");
	}
}


function dimTrim(s) {
	return s.replace(/(^\s+)|(\s+$)/g, "");
}

