// JavaScript Functions

// add load event function
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// get elements by class name function
function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// pop up window functions
function popUp(winURL) {
	var winl = (screen.width - 500) / 2;
	var wint = (screen.height - 500) / 2;
	var winProps = 'width=500,height=500,left='+winl+',top='+wint;
	window.open(winURL,"popup",winProps);
}

function popup(mypage, myname, w, h, scroll, resize) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resize='+resize+''
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// prepares links based on XML "rel" attribute
function prepareLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		
		// prepare external links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}

		// prepare close window links
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "close") {
			anchor.onclick = function() {
				window.close();
			}
		}
	}
	
	// prepare popup links
	for (var i=0; i<anchors.length; i++) {
		if (anchors[i].getAttribute("rel") == "popup") {
			anchors[i].onclick = function() {
				popUp(this.getAttribute("href"));
				return false;
			}
		}
	}
}

// load events
addLoadEvent(prepareLinks);
