/*------------------------------------------------------------------
Title:		TMS News & Features JavaScript Functions
Author:		John Reed, john@studiobonito.com (unless otherwise noted)
Updated:	June 20 2007
----------------------------------------------------------------- */





// add load event function
// source: http://simonwillison.net/2004/May/26/addLoadEvent/
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
// source: http://www.dustindiaz.com/getelementsbyclass/
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;
}


// funtion to toggle "advanced" and "simple" search
function searchType() {
	if (document.getElementById) {
		var showAdvanced = document.getElementById("showAdvanced");
		var showSimple = document.getElementById("showSimple");
		var advancedSearch = document.getElementById("advancedSearch");
		var simpleSearch = document.getElementById("simpleSearch");
		
		showAdvanced.onclick = function() {
			simpleSearch.style.display = "none";
			advancedSearch.style.display = "block";
			return false;
		}

		showSimple.onclick = function() {
			simpleSearch.style.display = "block";
			advancedSearch.style.display = "none";
			return false;
		}
	}
}


// prepares links based on "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 print links
		if (anchors[i].getAttribute("rel") == "print") {
			anchors[i].onclick = function() {
				window.print();
			}
		}
	}
}


// function to find Y position of a specific element
// source: http://blog.firetree.net/2005/07/04/javascript-find-position/
function findPosY(obj) {
	var curtop = 0;

	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}


// set heights of content columns
function setHeights() {
	if(document.getElementById) {
		
		// set variables for content columns and footer
		var left = document.getElementById('left');
		var center = document.getElementById('center');
		if( document.getElementById('right') != null ) var right = document.getElementById('right');
		var footer = document.getElementById('footer');
		
		// find the Y origin of #left (thus #center & #right) and #footer
		var leftTop = findPosY(left);
		var footerTop = findPosY(footer);
		
		// calculate the height in pixels
		var height = footerTop - leftTop;
		
		// convert height to ems
		var emHeight = height/10;
		
		// set the heights
		left.style.height = emHeight + 'em';
		center.style.height = emHeight + 'em';
		if( document.getElementById('right') != null ) right.style.height = emHeight + 'em';
	}
}

// *** protect our loyal users from spam!
// specify only the tribune.com username
function noSpam(u,t) {
	if(u) {
		if(t) {
			document.write('<a href="mai' + 'lto:' + u + '@' + 'tribu' + 'ne.com">' + t + '</a>');
		} else {
			document.write('<a href="mai' + 'lto:' + u + '@' + 'tribu' + 'ne.com">' + u + '@tr' + 'ibune.com</a>');
		}
	}
}


// load events
addLoadEvent(prepareLinks);
addLoadEvent(setHeights);