function addOnLoadEvent(fn)
{
	if (window.addEventListener) 
	{
		window.addEventListener('load', fn, false);
		return true;
	}
	else if (window.attachEvent) 
	{
		var r = window.attachEvent("onload", fn);
		return r;
	}
	else 
	{
		return false;
	}
}

function instantBig()
{
	if (document.getElementById('homeinteractive')) 
		document.getElementById('homeinteractive').height = 292;
}

function instantSmall()
{
	if (document.getElementById('homeinteractive')) 
		document.getElementById('homeinteractive').height = 146;
}

function makeBig()
{
	if (document.getElementById('homeinteractive')) 
		new Effect.Tween('homeinteractive', Number(document.getElementById('homeinteractive').height), 292, {
			duration: 1.0
		}, 'height');
}

function makeSmall()
{
	if (document.getElementById('homeinteractive')) 
		new Effect.Tween('homeinteractive', Number(document.getElementById('homeinteractive').height), 146, {
			duration: 1.0
		}, 'height');
}

function input_wipe(o, d)
{
	if (o.value == d) 
		o.value = '';
}

function dodgeUpImageMaps()
{
	var i, j, k;
	var imgs = document.getElementsByTagName('IMG');
	var imga = [];
	for (i = 0; i < imgs.length; ++i) 
	{
		if (imgs[i].getAttribute('usemap') && imgs[i].getAttribute('usemap').substring(0,1)=='#') 
		{
			imga.push(imgs[i]);
		}
	}
	for (i = 0; i < imga.length; ++i) 
	{
		var mapname = imga[i].getAttribute('usemap').substring(1);
		imga[i].parentNode.style.position = 'relative';
		var divp = document.createElement('div');
		var img2 = document.createElement('img');
		imga[i].parentNode.insertBefore(divp, imga[i]);
		divp.style.position = 'relative';
		divp.style.height = imga[i].height + 'px';
		divp.appendChild(imga[i]);
		img2.src = imga[i].src
		img2.style.position = 'absolute';
		img2.style.zIndex = '2';
		imga[i].style.position = 'absolute';
		imga[i].src='/gch_assets/images/clear.gif';
		imga[i].style.zIndex = '4';
		divp.appendChild(img2);
		var map = document.getElementsByName(mapname);
		if (map.length) 
		{
			var areas = map[0].getElementsByTagName('AREA');
			for (j = 0; j < areas.length; ++j) 
			{
				var area = areas[j];
				if (area.getAttribute('shape').toLowerCase() == 'rect') 
				{
					var coords = area.attributes.coords.nodeValue.split(',');
					for (k = 0; k < coords.length; ++k) 
					{
						coords[k] = parseInt(coords[k]);
					}
					var px = (coords[0] < coords[2]) ? coords[0] : coords[2];
					var py = (coords[1] < coords[3]) ? coords[1] : coords[3];
					var pw = ((coords[0] < coords[2]) ? -1 : 1) * (coords[0] - coords[2]);
					var ph = ((coords[1] < coords[3]) ? -1 : 1) * (coords[1] - coords[3]);
					var divo = document.createElement('div');
					divo.style.position = 'absolute';
					divo.style.top = py + 'px';
					divo.style.left = px + 'px';
					divo.style.width = pw + 'px';
					divo.style.height = ph + 'px';
					divo.style.opacity = '0.3';
					divo.style.display='none'
					divo.style.border = '1px solid white';
					divo.style.zIndex = '3';
					divo.id = 'areaover'+i+'_'+j;
					area.forid = 'areaover'+i+'_'+j;
					area.onmouseover=function(){var o=document.getElementById(this.forid);if (o){o.style.display='block'}};
					area.onmouseout=function(){var o=document.getElementById(this.forid);if (o){o.style.display='none'}};
					imga[i].parentNode.appendChild(divo);
				}
			}
		}
	}
}

function initSlideshows()
{
	if ((document.getElementById('slideshowa')))
	initSlideshow(document.getElementById('slideshowa').childNodes, 5000);
}

function initSlideshow(a, tout)
{
	var i, ao = [];
	for (i = 0; i < a.length; ++i) 
	{
		if (typeof a[i] == 'string' && document.getElementById(a[i])) 
		{
			ao.push(document.getElementById(a[i]));
		}
		else if (typeof a[i] == 'object' && a[i].nodeType==1)
		{
			console.log(a[i])
			ao.push(a[i]);
		}
	}
	// Internet Explorer doesn't support opacity (or alpha filter) on PNG images. Even IE8 is afflicted!
	// Therefore, all IE users will just get images flicking to each other, while Firefox users will get it fading between them.
	// Internet Explorer also doesn't support setTimeout properly, so using a "closure".
	if (isIE)
	{
		for (i=1; i<ao.length; ++i)
		{
			ao[i].style.display='none'
		}
		setTimeout(function(){doSlideshowIE(1, ao, tout)},tout);
	}
	else
	{
		setTimeout(doSlideshow, tout, 0, ao, tout);
	}
}
function doSlideshow(idx, ao, tout)
{
	var p = ao[idx].parentNode;
	newidx = idx+1;
	if (newidx>=ao.length)
	{
		newidx=0;
	}
	p.insertBefore(ao[idx], p.firstChild);
	new Effect.Opacity(ao[idx], { from: 0.0, to: 1.0, duration: 0.5, delay: 0 });
	new Effect.Opacity(ao[newidx], { from: 1.0, to: 0.0, duration: 1 });
	setTimeout(doSlideshow, tout, newidx, ao, tout);
}
function doSlideshowIE(idx, ao, tout)
{
	var p = ao[idx].parentNode;
	newidx = idx+1;
	if (newidx>=ao.length)
	{
		newidx=0;
	}
	p.insertBefore(ao[idx], p.firstChild);
	ao[idx].style.display='block';
	ao[newidx].style.display='none';
	setTimeout(function(){doSlideshowIE (newidx, ao, tout)},tout);
}

function verify_form(verify_object)
{
	var er = '', o;
	for (var i in verify_object) 
	{
		if ((o = document.getElementById(i))) 
		{
			if (o.value == '') 
			{
				er += 'Please enter value for ' + verify_object[i] + "\n";
			}
		}
		else 
		{
			er += 'Value not found for ' + verify_object[i] + "\n";
		}
	}
	if (er != '') 
	{
		alert(er);
	}
	return (er == '');
}

function externalLinks()
{
	if (!document.getElementsByTagName) 
		return;
	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++) 
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
			anchor.target = "_blank";
	}
}

function finished_loading()
{
	if (o = document.getElementById('please_wait')) 
	{
		o.style.display = 'none';
	}
}


function make_rounded(clname)
{
	var divs = document.getElementsByTagName('DIV');
	var mkrn = new Array();
	var cllist = new Array('c', 't', 'l', 'b', 'r', 'bl', 'br', 'tr', 'tl', 'ie_bug');
	var x;
	
	for (x in divs) 
		if (divs[x].className == clname) 
			mkrn[mkrn.length] = divs[x];
	
	for (x = 0; x < mkrn.length; x++) 
	{
		var o = mkrn[x];
		if (!o || !o.parentNode) 
			continue;
		
		var i;
		var t = document.createElement('div');
		var c = t;
		
		t.className = clname + '_container';
		o.className = clname + '_internal';
		
		for (i = 0; i < 10; i++) 
		{
			n = document.createElement('div');
			n.className = cllist[i];
			c.appendChild(n);
			c = n;
		}
		o.parentNode.insertBefore(t, o);
		c.appendChild(o);
		
	}
}

addOnLoadEvent(externalLinks);
addOnLoadEvent(dodgeUpImageMaps);
addOnLoadEvent(initSlideshows);


/*--------------------------------------------------------------*/
// HTML TABLE SORTER
// OBJECT ORIENTED JAVASCRIPT IMPLEMENTATION OF QUICKSORT
// @author	Terrill Dent 
// @source	http://www.terrill.ca
// @date	August 28th, 2006
// Modified for eDTM by Ben Hood for Beyondedge
/*--------------------------------------------------------------*/
function TSorter()
{
	var table = Object;
	var trs = Array;
	var ths = Array;
	var curSortCol = Object;
	var prevSortCol = -1;
	var sortType = Object;
	var sortIcon = document.createElement('IMG');
	
	function get()
	{
	}
	
	function getCell(index)
	{
		return trs[index].cells[curSortCol]
	}
	
	/*----------------------INIT------------------------------------*/
	// Initialize the variable
	// @param tableName - the name of the table to be sorted
	// @param sort_table - if undefine or true then sorting will be enabled
	//                   - set to false to only put grey lines
	/*--------------------------------------------------------------*/
	this.init = function(tableName, sort_enable)
	{
		if (sort_enable == undefined) 
			sort_enable = true;
		
		if (typeof tableName == 'object') 
			table = tableName;
		else 
			table = document.getElementById(tableName);
		
		if (sort_enable && table && table.tHead) 
		{
			ths = table.tHead.getElementsByTagName("th");
			for (var i = 0; i < ths.length; i++) 
			{
				ths[i].onclick = function()
				{
					sort(this);
				}
			}
		}
		redoRows(true);
		return true;
	};
	
	this.sort_id = function(idTH, decr)
	{
		var o = document.getElementById(idTH);
		if (o) 
		{
			sort(o);
			if (decr) 
				sort(o);
		}
	}
	
	/*----------------------SORT------------------------------------*/
	// Sorts a particular column. If it has been sorted then call reverse
	// if not, then use quicksort to get it sorted.
	// Sets the arrow direction in the headers.
	// @param oTH - the table header cell (<th>) object that is clicked
	/*--------------------------------------------------------------*/
	function sort(oTH)
	{
		curSortCol = oTH.cellIndex;
		sortType = oTH.abbr;
		trs = table.tBodies[0].getElementsByTagName("tr");
		
		//set the get function
		setGet(sortType)
		
		oTH.appendChild(sortIcon);
		
		// if already sorted just reverse
		if (prevSortCol == curSortCol) 
		{
			sortIcon.src = (sortIcon.src == ASSETS_URL + 'images/icons/sort-down.gif') ? ASSETS_URL + 'images/icons/sort-up.gif' : ASSETS_URL + 'images/icons/sort-down.gif';
			//			oTH.className = (oTH.className != 'ascend' ? 'ascend' : 'descend' );
			reverseTable();
		}
		// not sorted - call quicksort
		else 
		{
			sortIcon.src = ASSETS_URL + 'images/icons/sort-up.gif';
			//			oTH.className = 'ascend';
			//			if(ths[prevSortCol].className != 'exc_cell'){ths[prevSortCol].className = '';}
			quicksort(0, trs.length);
			reverseTable();
		}
		prevSortCol = curSortCol;
		redoRows();
	}
	
	function redoRows(redoCols)
	{
		if (redoCols == undefined) 
			redoCols = false;
		trs = (redoCols) ? table.getElementsByTagName("tr") : table.tBodies[0].getElementsByTagName("tr");
		var i, j, rj;
		var ri = (redoCols) ? 0 : 1;
		var trsc = trs.length;
		for (i = 0; i < trs.length; i++) 
		{
			ri = 1 - ri;
			
			var ncn = 'd_r_' + ri;
			
			if (!trs[i].className) 
				trs[i].className = ncn;
			else if (trs[i].className.match(/d_r_(.)/)) 
				trs[i].className = trs[i].className.replace(/d_r_./, ncn);
			else 
				trs[i].className = trs[i].className + ' ' + ncn;
			
			rj = 1;
			if (redoCols && trs[i].childNodes) 
				for (j = trs[i].childNodes.length - 1; j >= 0; j--) 
				{
					if (trs[i].childNodes[j] &&
					(trs[i].childNodes[j].tagName == 'TD' ||
					trs[i].childNodes[j].tagName == 'TH')) 
					{
						rj = 1 - rj;
						if (trs[i].childNodes[j].className == undefined || trs[i].childNodes[j].className == '') 
							trs[i].childNodes[j].className = 'd_c_' + rj;
					}
				}
		}
		if (!table.className) 
			table.className = 'sorted_table';
	}
	
	/*--------------------------------------------------------------*/
	// Sets the GET function so that it doesnt need to be 
	// decided on each call to get() a value.
	// @param: colNum - the column number to be sorted
	/*--------------------------------------------------------------*/
	function setGet(sortType)
	{
		switch (sortType)
		{
			case "link_column":
				get = function(index)
				{
					return getCell(index).firstChild.firstChild.nodeValue;
				};
				break;
			default:
				get = function(index)
				{
					var g1c = getCell(index);
					var g2c = g1c.getAttribute('sort');
					return g2c ? g2c : g1c.firstChild ? g1c.firstChild.nodeValue : '';
				};
				break;
		};
			}
	
	/*-----------------------EXCHANGE-------------------------------*/
	//  A complicated way of exchanging two rows in a table.
	//  Exchanges rows at index i and j
	/*--------------------------------------------------------------*/
	function exchange(i, j)
	{
		if (i == j + 1) 
		{
			table.tBodies[0].insertBefore(trs[i], trs[j]);
		}
		else if (j == i + 1) 
		{
			table.tBodies[0].insertBefore(trs[j], trs[i]);
		}
		else 
		{
			var tmpNode = table.tBodies[0].replaceChild(trs[i], trs[j]);
			if (typeof(trs[i]) == "undefined") 
			{
				table.appendChild(tmpNode);
			}
			else 
			{
				table.tBodies[0].insertBefore(tmpNode, trs[i]);
			}
		}
	}
	
	/*----------------------REVERSE TABLE----------------------------*/
	//  Reverses a table ordering
	/*--------------------------------------------------------------*/
	function reverseTable()
	{
		for (var i = 1; i < trs.length; i++) 
		{
			table.tBodies[0].insertBefore(trs[i], trs[0]);
		}
	}
	
	/*----------------------QUICKSORT-------------------------------*/
	// This quicksort implementation is a modified version of this tutorial: 
	// http://www.the-art-of-web.com/javascript/quicksort/
	// @param: lo - the low index of the array to sort
	// @param: hi - the high index of the array to sort
	/*--------------------------------------------------------------*/
	function quicksort(lo, hi)
	{
		if (hi <= lo + 1) 
			return;
		
		if ((hi - lo) == 2) 
		{
			if (get(hi - 1) > get(lo)) 
				exchange(hi - 1, lo);
			return;
		}
		
		var i = lo + 1;
		var j = hi - 1;
		
		if (get(lo) > get(i)) 
			exchange(i, lo);
		if (get(j) > get(lo)) 
			exchange(lo, j);
		if (get(lo) > get(i)) 
			exchange(i, lo);
		
		var pivot = get(lo);
		
		while (true) 
		{
			j--;
			while (pivot > get(j)) 
				j--;
			i++;
			while (get(i) > pivot) 
				i++;
			if (j <= i) 
				break;
			exchange(i, j);
		}
		exchange(lo, j);
		
		if ((j - lo) < (hi - j)) 
		{
			quicksort(lo, j);
			quicksort(j + 1, hi);
		}
		else 
		{
			quicksort(j + 1, hi);
			quicksort(lo, j);
		}
	}
}

function boxIt()
{
	make_rounded('greyboxme');
	// opera had troubles with the scrolly thing after rounded
	// so just dont round it in opera!
	if (typeof opera == 'undefined') 
		make_rounded('borderme');
}

//if (window.attachEvent) window.attachEvent("onload", boxIt)
//else window.onload = boxIt;
