/*
 * The entire point of this file is to mark the first list item
 * in the navigation list with the ID "first_nav_el", as this
 * seems to be too difficult a task to reliably accomplish in
 * the navigation code. -EN
 *
 * 02-20-2007: 
 *	This file now also tags the first A or STRONG element inside
 *	#first_nav_el as #first_nav_text.
 *
 * 02-27-2007:
 *  This file now also removes much of the right margin on #bannerAndMeat
 *  if there is a wide table inside of #content.
 */

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = function() {
		for (var i = 0; i < arguments.length; i++) {
			this[this.length] = arguments[i];
		}

		return this.length;
	};
}

if (typeof Array.prototype.map == "undefined"){
	Array.prototype.map = function(fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function")
			throw new TypeError();

		var res = new Array(len);
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this)
				res[i] = fun.call(thisp, this[i], i, this);
		}

		return res;
	};
}

function resolve(element) {
	return (typeof element == 'string')
		? ((!!document.getElementById)
			? document.getElementById(element) 
			: document.all[element])
		: element;
}

var SelfPity = {
	addEvent: function(element, name, observer) {
		element = resolve(element);
		
		if (element.addEventListener)
			element.addEventListener(name, observer, false);
		else if (element.attachEvent)
			element.attachEvent('on' + name, observer);
	},
	
	getFirstChild: function(el) {
		if (el == null) return null;
		var tag = (arguments.length > 1)
			? arguments[1]
			: null;
			
		for (var i = 0; i < el.childNodes.length; i++) {
			var node = el.childNodes[i];
			if (node.nodeType == 1 && (tag == null || tag == node.tagName))
				return node;
		}
		
		return null;
	},
	
	findNavListTop: function() {
		var mininav = resolve('minisiteNavigation');
		var list = SelfPity.getFirstChild(mininav, "DIV");
		return SelfPity.getFirstChild(list, "UL");
	},
	
	fixFirstElement: function(list) {
		list = resolve(list);
		
		var li = SelfPity.getFirstChild(list, "LI");
		if (null != li) {
			li.id = "first_nav_el";
			
			var kid = SelfPity.getFirstChild(li, "A") ||
				SelfPity.getFirstChild(li, "STRONG");
			if (null != kid) {
				kid.id = "first_nav_text";
			}
		}
	},
	
	fixWideTable: function() {
		mybannerAndMeat = resolve("bannerAndMeat");
		mycontent = resolve("content");
		mypattern = /\bcontainsWideTable\b/;
		
		for (var i = 0; i < mycontent.childNodes.length; i++) {
			var node = mycontent.childNodes[i];
			
			if (node.nodeType != 1)
				continue;
			
			if (mypattern.test(node.className)) {
				mybannerAndMeat.style.marginRight = '2%';
				resolve('banner').style.marginRight = '90px';
				break;
			}
		}
	},
	
	normalizeColumns: function() {
		var cols = []
		
		for (var i = 0; i < arguments.length; i++) {
			var col = resolve(arguments[i]);
			if (col)
				cols.push(col);
		}
		
		var len = cols.length;
		
		if (len == 1)
			return;
		
		// Set everything to be auto height
		for (var i = 0; i < len; i++) {
			cols[i].style.height = 'auto';
		}
		
		var maxHeight = 0;
		for (var i = 0; i < len; i++) {
			if (cols[i].offsetHeight > maxHeight)
				maxHeight = cols[i].offsetHeight;
		}
		
		
		for (var i = 0; i < len; i++) {
			var col = cols[i];
			
			col.style.height = maxHeight + 'px';
			if (col.offsetHeight > maxHeight) {
				col.style.height =
					(maxHeight - (col.offsetHeight - maxHeight)) + 'px';
			}
		}
	},
	
	normalizeAthleticsColumns: function() {
		SelfPity.normalizeColumns('content', 'navigation', 'related');
	}
};

SelfPity.addEvent(window, 'load',
	function() {
		SelfPity.fixFirstElement(SelfPity.findNavListTop());
		SelfPity.fixWideTable();
		SelfPity.normalizeAthleticsColumns();
	});
SelfPity.addEvent(window, 'resize', SelfPity.normalizeAthleticsColumns);
