/*-------------------------------------------------
Title:		SAJ JavaScript functions
Author:		John Reed, jreed@cre8media.com
Updated:	Dec 21 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();
		}
	}
}

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

// function to find Y position of a specific element
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) {

		// the bottom of the wrapper and its y-position
		var bottom = document.getElementById('bottom');
		var bottomTop = findPosY(bottom);

		// the screen height and wrapper height
		var screenHeight = window.innerHeight;
		var wrapHeight = bottomTop;

		if(screenHeight > wrapHeight) {

			// set variables for content columns and footer
			var wrap = document.getElementById('wrap');		
			var content = document.getElementById('content');
			var sidebar = document.getElementById('sidebar');
			var footer = document.getElementById('footer');

			var contentTop = findPosY(content);
			var footerTop = findPosY(footer);

			// the header height
			var headerHeight = contentTop;

			// the footer height
			var footerHeight = bottomTop - footerTop;

			// the target height for the content column
			var contentHeight = screenHeight - footerHeight - headerHeight;
	
			// set the heights
			content.style.height = contentHeight + 'px';
			sidebar.style.height = contentHeight + 'px';
		}
	}
}

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