var DISPLAYTABLEROW;
var DISPLAYTABLE;
if (document.all) {
	DISPLAYTABLEROW = 'block';
	DISPLAYTABLE = 'block';
}
else {
	DISPLAYTABLEROW = 'table-row';
	DISPLAYTABLE = 'table';
}

var ie = document.all ? true : false;

function gbid(id) { return document.getElementById(id); }
function dot(msg) { document.body.appendChild(document.createTextNode(msg)); }
function dotr(msg) { document.body.appendChild(document.createElement("div")).innerHTML += msg; }
function dmsg(msg, html) {
	var msgPane = document.getElementById("debuggingMessagePane");
	if (!msgPane) {
		var msgPane = document.createElement("div");
		msgPane.id = "debuggingMessagePane";
		document.body.appendChild(msgPane);
		document.body.style.marginBottom = "50%";
	}
	if (html) {
		msgPane.appendChild(document.createElement("div")).innerHTML += msg;
	}
	else {
		msgPane.appendChild(document.createTextNode(msg));
	}
}

/**
From http://www.dustindiaz.com/getelementsbyclass/
*/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function hasClassName(el, name) {
  var i, list;

  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function addClassName(el, name) {
	if (!el.className) {
		el.className = name;
	}
	else if (el.className.search(name)==-1) {
		el.className += " " + name;
	}
}

function removeClassName(el, name) {
	el.className = el.className.replace(name, '').replace(/^\s*|\s*$/g, '');
}

function firstChild(el) {
	el = el.firstChild;
	while (el) {
		if (typeof(el.tagName)!="undefined") {
			return el;
		}
		el = el.nextSibling;
	}
	return null;
}

function nextSibling(el) {
	while (el.nextSibling) {
		if (typeof(el.nextSibling.tagName)!="undefined") {
			return el.nextSibling;
		}
		el = el.nextSibling;
	}
	return null;
}

function previousSibling(el) {
	while (el.previousSibling) {
		if (typeof(el.previousSibling.tagName)!="undefined") {
			return el.previousSibling;
		}
		el = el.previousSibling;
	}
	return null;
}

function lastChild(el) {
	el = el.lastChild;
	while (el) {
		if (typeof(el.tagName)!="undefined") {
			return el;
		}
		el = el.previousSibling;
	}
	return null;
}

function getContainerWithTagOfClass(node, tagName, className) {
  while (node != null) {
    if (node.tagName != null && node.tagName == tagName &&
			hasClassName(node, className)) {
		return node;
	}
    node = node.parentNode;
  }
  return node;
}

function getContainerWithTagName(node, tagName) {
  while (node != null) {
    if (node.tagName != null && node.tagName.toLowerCase() == tagName) {
			return node;
		}
		node = node.parentNode;
  }
  return node;
}

function getFirstTagChild(el) {
	for (var i=0; i<el.childNodes.length; i++) {
		if (typeof(el.childNodes[i].tagName)!="undefined") {
			return el.childNodes[i];
		}
	}
	return null;
}

/** Removes scrolls of divs, etc. because I'm normally only ever interested
in distance from top of virtual page, not of virtual sections within the virtual page **/
function getElLeft(el) {
	var val = 0;
	while (el /* && !isNaN(el.offsetLeft)*/ ) {
		val += el.offsetLeft;
		if (el && el!=document.body && el.tagName!="HTML" && el.scrollLeft) {
			val -= el.scrollLeft;
		}
		el = el.offsetParent;
	}
	return val;
}

function getElTop(el) {
	var val = 0;
	while (el && !isNaN(el.offsetTop)) {
		val += el.offsetTop;
		if (el && el!=document.body && el.tagName!="HTML" && el.scrollTop) {
			val -= el.scrollTop;
		}
		el = el.offsetParent;
	}
	return val;
}

function getElWidth(el) {
	return el.offsetWidth;
}

function getElHeight(el) {
	return el.offsetHeight;
}

function getElRight(el) {
	return getElLeft(el)+getElWidth(el);
}

function getElBottom(el) {
	return getElTop(el)+getElHeight(el);
}

function getScrollLeft() {
	if (document.documentElement.scrollLeft) { return document.documentElement.scrollLeft; }
	if (document.body.scrollLeft) { return document.body.scrollLeft; }
	else if (window.scrollX) { return window.scrollX; }
	else { return 0; }
}

function getScrollTop() {
	if (document.documentElement.scrollTop) { return document.documentElement.scrollTop; }
	if (document.body.scrollTop) { return document.body.scrollTop; }
	else if (window.scrollY) { return window.scrollY; }
	else { return 0; }
}

function getInnerWindowWidth() {
	if (window.innerWidth) { return window.innerWidth; }
	else if (document.documentElement) { return document.documentElement.clientWidth; }
	else if (document.body.clientWidth) { return document.body.clientWidth; }
	else { return 0; }
}

function getInnerWindowHeight() {
	if (window.innerHeight) { return window.innerHeight; }
	else if (document.documentElement) { return document.documentElement.clientHeight; }
	else if (document.body.clientHeight) { return document.body.clientHeight; }
	else { return 0; }
}

//////////////////////////////////
/// Selections

function range_clearSelection() {
	if (document.selection) {
		document.selection.empty();
	}
}

//////////////////////////////////
///Utilities

///Popups
function popupElement(el, top, left) {
	//alert(top);
	if (typeof(el)=="string") {
		el = document.getElementById(el);
	}
	/*if (el.parentNode!=document.body) {
		el.parentNode.removeChild(el);
		document.body.appendChild(el);
	}*/
	//alert(el.style.visibility);
	el.style.visibility = 'hidden';
	el.style.display = 'block';
	var newTop = top-getScrollTop();
	var newLeft = left-getScrollLeft();
	var elWidth = getElWidth(el);
	var elHeight = getElHeight(el);
	//alert((evt.left+efWidth)+":"+document.body.clientWidth);
	if ( (newLeft+elWidth)>getInnerWindowWidth()) {
		newLeft = left-elWidth;
		if (newLeft<0) {
			newLeft = 0;
		}
	}
	if ( (newTop+elHeight)>getInnerWindowHeight()) {
		newTop = newTop-elHeight;
		if (newTop<0) {
			newTop = 0;
		}
	}
	el.style.top = (getScrollTop() + newTop) + "px";
	el.style.left = (getScrollLeft() + newLeft) + "px";
	el.style.visibility = 'visible';
	//el.style.border = "solid 40px red";
	//alert(el.id);

	///Hack to hide select objects
	if (document.all) {
		var width = getElWidth(el);
		var height = getElHeight(el);
		var els = document.getElementsByTagName('select');
		top = newTop;
		left = newLeft;
		for (var i=0; i<els.length; i++) {
			var selTop = getElTop(els[i])-getScrollTop();
			var selLeft = getElLeft(els[i])-getScrollLeft();
			var selWidth = getElWidth(els[i]);
			var selHeight = getElHeight(els[i]);
			if ((	(top<selTop && top+height>selTop)
					|| (top>selTop && top+height<selTop+selHeight)
					|| (top<selTop+selHeight && top+height>selTop+selHeight))
					&&
					(	(left<selLeft && left+width>selLeft)
					|| (left>selLeft && left+width<selLeft+selWidth)
					|| (left<selLeft+selWidth && left+width>selLeft+selWidth))
					) {
				els[i].style.visibility = 'hidden';
			}
		}
	}
}

function dismissPopup(el) {
	if (typeof(el)=="string") {
		el = document.getElementById(el);
	}
	///Hack to re-show select objects
	var top = getElTop(el);
	var left = getElLeft(el);
	var width = getElWidth(el);
	var height = getElHeight(el);
	var els = document.getElementsByTagName('select');
	for (var i=0; i<els.length; i++) {
		var selTop = getElTop(els[i]);
		var selLeft = getElLeft(els[i]);
		var selWidth = getElWidth(els[i]);
		var selHeight = getElHeight(els[i]);
			if ((	(top<selTop && top+height>selTop)
					|| (top>selTop && top+height<selTop+selHeight)
					|| (top<selTop+selHeight && top+height>selTop+selHeight))
					&&
					(	(left<selLeft && left+width>selLeft)
					|| (left>selLeft && left+width<selLeft+selWidth)
					|| (left<selLeft+selWidth && left+width>selLeft+selWidth))
				) {
			els[i].style.visibility = 'visible';
		}
	}

	///Actual dismiss
	el.style.display = 'none';
}

/** Very crude popup menu stuff **/
var currentMousedMenu = null;
var currentActiveMenu = null;
var dismissPopupTimeout = 1000;
//var mcount = 0;

function popupElementMenu(el, top, left) {
	//dot(mcount+":"+currentActiveMenu+"; "); mcount++;
	if (typeof(el)=="string") {
		el = document.getElementById(el);
	}
	if (currentActiveMenu && currentActiveMenu!=el.id) {
		dismissPopupMenu(currentActiveMenu, true);
	}
	popupElement(el, top, left);
	currentActiveMenu = el.id;
}

function dismissPopupMenu(el, force) {
	if (typeof(el)=="string") {
		el = document.getElementById(el);
		//alert(el);
	}
	if (!force && el.id==currentMousedMenu) {
		return;
	}
	dismissPopup(el);
	if (currentActiveMenu==el.id) {
		currentActiveMenu = 0;
	}
}

/** Should merge with the above 2 functions **/
function hideSelectObjects(elToPopup, ignorePrefix) {
	if (!document.all) { return; }
	///Hack to hide select objects
	var top = getElTop(elToPopup);
	var left = getElLeft(elToPopup);
	var width = getElWidth(elToPopup);
	var height = getElHeight(elToPopup);
	var els = document.getElementsByTagName('select');
	for (var i=0; i<els.length; i++) {
		if (ignorePrefix && els[i].name && String(els[i].name).search(new RegExp("^"+ignorePrefix))!=-1) {
			continue;
		}
		var selTop = getElTop(els[i])-getScrollTop();
		var selLeft = getElLeft(els[i])-getScrollLeft();
		var selWidth = getElWidth(els[i]);
		var selHeight = getElHeight(els[i]);
			//alert(selTop+";"+selLeft+";"+selWidth+";"+selHeight+"\n"
			//	+top+";"+left+";"+width+";"+height+"\n"
			//	+getScrollTop()+";"+getScrollLeft());
			if ((	(top<selTop && top+height>selTop)
					|| (top>selTop && top+height<selTop+selHeight)
					|| (top<selTop+selHeight && top+height>selTop+selHeight))
					&&
					(	(left<selLeft && left+width>selLeft)
					|| (left>selLeft && left+width<selLeft+selWidth)
					|| (left<selLeft+selWidth && left+width>selLeft+selWidth))
				) {
			els[i].style.visibility = 'hidden';
		}
	}
}

function showSelectObjects(elToDismiss, ignorePrefix) {
	if (!document.all) { return; }
	///Hack to re-show select objects
	var top = getElTop(elToDismiss);
	var left = getElLeft(elToDismiss);
	var width = getElWidth(elToDismiss);
	var height = getElHeight(elToDismiss);
	var els = document.getElementsByTagName('select');
	for (var i=0; i<els.length; i++) {
		if (els[i].name && String(els[i].name).search(new RegExp("^"+ignorePrefix))!=-1) {
			continue;
		}
		var selTop = getElTop(els[i])-getScrollTop();
		var selLeft = getElLeft(els[i])-getScrollLeft();
		var selWidth = getElWidth(els[i]);
		var selHeight = getElHeight(els[i]);
			if ((	(top<selTop && top+height>selTop)
					|| (top>selTop && top+height<selTop+selHeight)
					|| (top<selTop+selHeight && top+height>selTop+selHeight))
					&&
					(	(left<selLeft && left+width>selLeft)
					|| (left>selLeft && left+width<selLeft+selWidth)
					|| (left<selLeft+selWidth && left+width>selLeft+selWidth))
				) {
			els[i].style.visibility = 'visible';
		}
	}
}

if(typeof HTMLElement!="undefined" && HTMLElement.prototype.__defineGetter__ != 'undefined'){
	HTMLElement.prototype.__defineGetter__("innerText", function() {
		var tmp = this.innerHTML.replace(/<br>/gi,"\n");
		return tmp.replace(/<[^>]+>/g,"");
	});

	HTMLElement.prototype.__defineSetter__("innerText", function(txtStr){
		var parsedText = document.createTextNode(txtStr);
		this.innerHTML = "";
		this.appendChild( parsedText );
	});

	HTMLIFrameElement.prototype.__defineGetter__("document", function() {
		return this.contentDocument;
	});
}