var is_ie6 = document.all && (navigator.userAgent.toLowerCase().indexOf("msie 6") != -1);
//Function getElementsByClass
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/**
	Cross Browser Event Listener fix
	--- These event handler functions are VITAL to the rest of this file ---
	Written by Peter Wilkinson of http://dynamic-tools.net
	Feel free to use or modify this script for any purpose.  I'd appreciate you leaving
	this header in though.
	Thank you, Peter Wilkinson, you are a god among men
*/
function addEvent(elem, eventType, handler)
{
	if (!elem.eventHandlers) elem.eventHandlers = [];
	if (!elem.eventHandlers[eventType])
	{
		elem.eventHandlers[eventType] = [];
		if (elem['on' + eventType]) elem.eventHandlers[eventType].push(elem['on' + eventType]);
		elem['on' + eventType] = handleEvent;
	}
	elem.eventHandlers[eventType].push(handler);
}
function removeEvent(elem, eventType, handler)
{
	var handlers = elem.eventHandlers[eventType];
	for (var i in handlers) if (handlers[i] == handler) delete handlers[i];
}
function handleEvent(e)
{
	var returnValue = true;
	if (!e) e = fixEvent(event);
	var handlers = this.eventHandlers[e.type]
	for (var i in handlers)
	{
		this.$$handleEvent = handlers[i];
		returnValue = !((returnValue && this.$$handleEvent(e)) === false);
	}
	return returnValue;
}

function fixEvent(event)
{
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};

fixEvent.preventDefault = function() {
	this.returnValue = false;
};

fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

//PDF Link Labeler
function pagemodifications () {
	links = document.getElementsByTagName('A');
	for (i in links) {
		link = links[i];
		if (link.href) {
			if (link.href.indexOf('.pdf') != -1) {
				link.innerHTML += '&nbsp;<img src="/media/pdf-icon.png" alt="PDF Download" align="top" style="float:none;border:0;">';
			}
			if (link.className.indexOf('dropdown') != -1) {
				link.href = '#';
			}
		}
	}
}
addEvent(window,'load',pagemodifications);

//Carousel
carouselControl = function (obj,tid) {
	//alert('building carousel control');
	this.obj = obj;
	this.obj.className = 'carousel-activated';
	this.cycletime = this.obj.getAttribute('data-cycletime');
	if (this.cycletime == null | this.cycletime == '') {
		this.cycletime = '5000';
	}
	items = this.obj.getElementsByTagName('LI');
	this.items = new Array();
	for (i in items) {
		if (items[i].parentNode == this.obj) {
			this.items[this.items.length] = items[i];
		}
	}
	this.currentitem = -1;
	this.tid = tid;
		
	this.itemCycle = function () {
		var cycletime = this.cycletime;;
		if (this.currentitem !== -1) {
			this.currentitem++;
			if (this.currentitem >= this.items.length) {
				this.currentitem = 0;
			}
			this.itemSelect(this.currentitem);
		}else {
			this.currentitem = 0;
			this.itemSelect(this.currentitem);
		}
		if (this.items[this.currentitem].getAttribute('data-cycletime')) {
			cycletime = this.items[this.currentitem].getAttribute('data-cycletime');
		}
		//alert('cycled to '+this.currentitem);
		setTimeout('carouselControls['+this.tid+'].itemCycle()',cycletime);
	}
	this.itemSelect = function (itemid) {
		for (i in this.items) {
			if (i == itemid) {
				this.currentitem = i;
				//this is the item to display, display it and resize this.obj to match
				this.items[i].style.visibility = 'visible';
				this.items[i].style.zIndex = '1';
				//set the innerHTML to force a restart of any animated content
				//only works outside IE6
				//only needs to be done for items containing an OBJECT tag
				if (this.items[i].getElementsByTagName('OBJECT').length > 0) {
					if (!is_ie6 | 1==1) {
						this.items[i].innerHTML = this.items[i].innerHTML;
					}
				}
			}else {
				//this is not the item to display, hide it
				if (this.items[i].style) {
					this.items[i].style.visibility = 'hidden';
					this.items[i].style.zIndex = '0';
				}
			}
		}
	}
	
	this.itemCycle();
}
var carouselControls = new Array();
function makecarousels () {
	uls = getElementsByClass('carousel',null,'ul');
	for (i in uls) {
		carouselControls[carouselControls.length] = new carouselControl(uls[i],i);
	}
}
addEvent(window,'load',makecarousels);

//-------------------------------------------------------
// BELOW HERE IS ONLY FOR INTERNET EXPLORER 6 BUG FIXES |
//-------------------------------------------------------
if (is_ie6) {
	//load png fix for tiling
	document.write('<script src="/scripts/iepngfix_tilebg.js" type="text/javascript" language="javascript"></script>');
	
	//Make dropdowns windowed objects for IE6
	//Keeps dropdowns above form fields by placing an invisible iframe behind them.
	function ieDropDownWindower () {
		var html1 =
			"<ul><li><iframe style=\"position: absolute; display: block; " +
			"z-index:-1; width: 100%; height: 250px; top: 0; left: 0;" +
			"filter: mask(); background-color: #ffffff;border:2px solid #ccc;\"></iframe></li></ul>";
		var html2 = 
			"";
		objs = getElementsByClass('windowed',null,'div');
		for (i in objs) {
			tobj = objs[i];
			//alert(tobj.innerHTML);
			//tobj.style.position = 'relative';
			//tobj.style.float = 'right';
			tobj.innerHTML = html1+tobj.innerHTML+html2;
			//var olddisplay = tobj.style.display;
			//tobj.style.display = 'none';
			//tobj.style.display = olddisplay;
		}
	}
	addEvent(window,'load',ieDropDownWindower);

	//Menu Rollover Fix for old IE
	function oldiefixes_init () {
		menulinks = document.getElementById('headerTabs').getElementsByTagName('LI');
		for (i in menulinks) {
			link = menulinks[i];
			link.onmouseover = simulatedhover_rollover;
			link.onmouseout = simulatedhover_rollout;
		}
		menulinks = document.getElementById('headerTop').getElementsByTagName('LI');
		for (i in menulinks) {
			link = menulinks[i];
			link.onmouseover = simulatedhover_rollover;
			link.onmouseout = simulatedhover_rollout;
		}
		inputs = document.getElementsByTagName('INPUT');
		for (i in inputs) {
			el = inputs[i];
			if (el.type == 'checkbox') {
				el.style.width = '90px';
			}
		}
	}
	function simulatedhover_rollover () {
		this.className = 'over';
	}
	function simulatedhover_rollout () {
		this.className = '';
	}
	addEvent(window,'load',oldiefixes_init);

	//IE6 Grey Box on embeds fix
	function ieUpdate () {
		objects = document.getElementsByTagName("object");
		for (var i = 0; i < objects.length; i++)
		{
		    objects[i].outerHTML = objects[i].outerHTML;
		}
	}
	addEvent(window,'load',ieUpdate);
}