/*-------------------------------------------------
Title:		Logica3 JavaScript functions
Author:		John Reed, john@studibonito.com
Updated:	June 26 2007
------------------------------------------------- */


// 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 function
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);
}

// 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;
			}
		}
	}
}


// function to rotate background image on news landing page
function newsRotator() {
	if(!document.getElementById) return false;
	var news = document.getElementById('news');
	var curImg = Math.floor(Math.random() * imageArray.length);
	
	news.style.backgroundImage = 'url(/assets/img/news/'+imageArray[curImg]+')';
}


// load events
addLoadEvent(prepareLinks);