/////////////////////////////
// DOM FUNCTIONS
/////////////////////////////

// cross-platform way to retrieve an element by its ID
function _getElementById (id) {
	if (document.getElementById) {
		// W3C (Gecko, KHTML)
		return document.getElementById(id);
	} else if (document.all) {
		// IE
		return document.all(id);
	} else {
		// unsupported
		return null;
	}
}

// returns the text contents of an element
function getElementText (element) {
	var text = '';
	if (element.childNodes && element.childNodes.length > 0){
		var child;
		for (var i in element.childNodes) {
			child = element.childNodes[i];
			if (child.elementType == 3) {
				text += child.elementValue;
			}
		}
	}
	return text;
}

/////////////////////////////
// EVENT FUNCTIONS
/////////////////////////////

// cross-platform way to handle events
function _addEventListener (element, eventName, handler) {
	if (element.addEventListener) {
		// W3C (newer Gecko, KHTML)
		element.addEventListener(eventName, handler, false);
	} else {
		// older browsers
		var oldHandler;
		eval('oldHandler = element.on' + eventName + ';');
		if (oldHandler) {
			eval('element.on' + eventName + ' = function (e) { oldHandler(e); handler(e); };');
		} else {
			eval('element.on' + eventName + ' = handler;');
		}
	}
}

// cross-platform way to get information about occuring event
function getEventObject (e) {
	if (e) {
		// W3C
		return e;
	} else if (window.event) {
		// IE
		return window.event;
	} else {
		// unsupported
		return null;
	}
}

// cross-platform way to find the object the event occurred on
function getEventTarget (e) {
	e = getEventObject(e);
	var targ;
	if (e.target) {
		targ = e.target;
	} else if(e.srcElement) {
		targ = e.srcElement;
	} else {
		return null;
	}
	// Safari returns the text node instead of the containing element
	if (targ.nodeType == 3) {
		targ = targ.parentNode;
	}
	return targ;
}

// prevent default handling of an event;
// if assigned as an event handler, it causes the event
// to do nothing;
// if called from an event handler, it prevents any other
// handlers/actions from taking place

function preventEventDefault (e) {
	if (e && e.stopPropagation && e.preventDefault) {
		e.stopPropagation();
		e.preventDefault();
	} else if (window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
}

// aborbs the event - prevents event from bubbling up
// parents will not receive the event
// but standard browser actions still take place
function absorbEvent(e){
	if (e) {
		e.stopPropagation();
	} else if (window.event) {
		window.event.cancelBubble=true;
	}
}

// sends the event to parents (allows bubbling)
// but no default browser actions take place
function passEvent (e) {
	if (e) {
		e.preventDefault();
	} else if(window.event) {
		window.event.returnValue = false;
	}
}

/////////////////////////////
// Visibility functions
/////////////////////////////

// shows the element
// works in all CSS-aware browsers
function showElement (e) {
	if (e && e.style) {
		e.style.visibility = 'visible';
		e.style.display = '';
	}
}

// hides the element
// works in all CSS-aware browsers
function hideElement (e) {
	if (e && e.style) {
		e.style.visibility = 'hidden';
		e.style.display = 'none';
	}
}

// determines if the element is currently hidden
// works in all CSS-aware browsers
function isElementVisible (e) {
	return e && e.style && e.style.visibility == 'visible' && e.style.display != 'none';
}

/////////////////////////////
// CSS functions
/////////////////////////////

// method that returns true if the specified CSS class has been assigned to an element
function hasCssClass (element, className) {
	if (element.className == "") {
		return false;
	} else if (element.className == className) {
		return true;
	} else {
		var exp = new RegExp("_" + className + "$", "");
		return element.className.match(exp);
	}
}

// adds a CSS class to an element
function addCssClass (element, className) {
	if (element.className == '') {
		element.className = className;
	} else if (!hasCssClass(element, className)) {
		element.className += '_' + className;
	}
}

// removes a CSS class from an element
function removeCssClass (element, className) {
	var exp = new RegExp("_?" + className + "$", "");
	element.className = element.className.replace(exp, "");
}

/////////////////////////////
// Position functions
/////////////////////////////

// finds the number of pixels between the top of an element
// and the top of the page
function findTop (element) {
	var top = element.offsetTop;
	while (element = element.offsetParent) {
		top += element.offsetTop;
	}
	return top;
}

// finds the number of pixels between the left side of an element
// and the left side of the page
function findLeft (element) {
	var left = element.offsetLeft;
	while (element = element.offsetParent) {
		left += element.offsetLeft;
	}
	return left;
}

_addEventListener(window, 'load', function (e) {
	// ugly hack to make the left bar work in Firefox and IE
	var leftbar = _getElementById('leftbar');
	if (leftbar) {
		if (!document.all) {
			leftbar.style.position = 'absolute';
		}
		leftbar.style.visibility = 'visible';
		leftbar.style.display = 'block';
	}
});
