// ----------------------------------------------------------- addEvent(element, type, handler)
function addEvent(element, type, handler) {
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else {
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

// ----------------------------------------------------------- removeEvent(element, type, handler)
function removeEvent(element, type, handler) {
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}
// Private
function handleEvent(event) {
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];
	for (var i in handlers) {
		if (!Object.prototype[i]) {
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}
	if (this.$$handler) this.$$handler = null;
	return returnValue;
}
// Private
function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function() {
	this.returnValue = false;
}
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
}

if (!window.addEventListener) {
	document.onreadystatechange = function() {
		if (window.onload && window.onload != handleEvent) {
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}

matchHeightByClassName = function(classname) {
	var divList = document.getElementsByTagName('div');
	var cn = classname;
	var targetList = new Array();
	var maxHeight = 0;
	var divHeight;
	
	for(i in divList) {
		d = divList[i]
		if(d.className == cn) {
			targetList.push(d);
			if(d.offsetHeight) {
				divHeight = d.offsetHeight;
			} else if(d.style.height) {
				divHeight = d.style.height;
			}
			maxHeight = Math.max(maxHeight,divHeight);
		}
	}
	
	for(i in targetList) {
		d = targetList[i];
		d.style.height = maxHeight +"px";
	}
	targetList =[];
	divList = [];
}

function toggleDisplay(obj,bln) {
	obj.style.display = (bln) ? 'block' : 'none' ;
	obj.style.height = (bln) ? 'auto' : '0px';
}

function getNextSibling(startChild){
	// This function is needed for Firefox as Firefox identifies 
	// whitespaces and linebreaks as text nodes in JavaScript.
	sibling =startChild.nextSibling;
	while(sibling.nodeType!=1){
		sibling = sibling.nextSibling;
	}
	return sibling;
}
function getPreviousSibling(startChild){
	// This function is needed for Firefox as Firefox identifies 
	// whitespaces and linebreaks as text nodes in JavaScript.
	sibling =startChild.previousSibling;
	while(sibling.nodeType!=1){
		sibling = sibling.previousSibling;
	}
	return sibling;
}

function openWindow(url,winName,W,H,X,Y) {
	if(W == undefined) W = 400;
	if(H == undefined) H = 300;
	if(X == undefined) X = screen.width/2-W/2;
	if(Y == undefined) Y = screen.height/2-H/2;
	winprops = 'height='+H+',width='+W+',top='+X+',left='+Y+',scrollbars=yes,location=yes,toolbar=yes,status=yes,resizable=yes';
	win = window.open(url, winName, winprops);
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}


