/*
	Additional Javascripts for the Thetford website.
	Provided by Hile Design
*/
/*
	initRollovers(): Generic image rollover script
	Source:
	Standards Compliant Rollover Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
	Modified with info from http://www.wandforge.com/rollover/
	Other modifications per site.
*/
function initRollovers() {
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className.match("rollover")) {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_over'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			aImages[i].setAttribute('osrc', src);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			aImages[i].onmouseover = function() {
				if (this.className.match('rollover')) {
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
			}
			aImages[i].onmouseout = function() {
				if (this.className.match('rollover')) {
					this.setAttribute('src',this.getAttribute('osrc'));
				}
			}
		}
	}
}


/*
	Resource Gallery scripting
	To display and manipulate the tables on the resource gallery pages.
	
*/


// for whatever it's worth, the default sort is alphabetical by name.
function init_arrows() {
	init = true;
	arrowset = [];
	arrowmaker('hd_gt_name_title');
	arrowmaker('hd_gt_num_title');
	init = false;
	// arrows have been written in. now the following has to occur.
	return;
}
function arrowmaker(title) {
	for (var a=0;a<document.getElementsByTagName('th').length;a++) {
		if (document.getElementsByTagName('th')[a].className.match(title)) {
			var thp = document.getElementsByTagName('th')[a].getElementsByTagName('div');
			var th = thp[0];
		}
	}
	var whitedown = '<img src="/Portals/0/resource_gallery/icons/hd_arrow_down_1.gif" title="This column is sorted A-Z or 0-9" class="hd_arrow_down hd_arrow_active" width="7" height="7" />';
	var whiteup = '<img src="/Portals/0/resource_gallery/icons/hd_arrow_up_1.gif" title="This column is sorted Z-A or 9-0" class="hd_arrow_up hd_arrow_active" width="7" height="7" />';
	var blackdown = '<img src="/Portals/0/resource_gallery/icons/hd_arrow_down_0.gif" title="Sort this column A-Z or 0-9" class="hd_arrow_down" width="7" height="7" />';
	var blackup = '<img src="/Portals/0/resource_gallery/icons/hd_arrow_up_0.gif" title="Sort this column Z-A or 9-0" class="hd_arrow_up" width="7" height="7" />';
	if (init && title=='hd_gt_name_title') {
		var arrowinsert = ' ' + whitedown + blackup;
	} else {
		var arrowinsert = ' ' + blackdown + blackup;
	}
	var label = th.innerHTML;
	th.innerHTML = label + arrowinsert;
	for (var a=0;a<th.getElementsByTagName('img').length;a++) {
		arrowset.push(th.getElementsByTagName('img')[a]);
		arrowbutton(th.getElementsByTagName('img')[a]);
	}
	return;
}
function arrowbutton(arrow) { // fires when an arrow is loaded or switches, to make it ready to click.
	// 'up' = click to sort Z-A
	arrow.onmouseup = function() {
		arrowsort(this);
		for (var a=0;a<arrowset.length;a++) {
			if (arrowset[a]!=this) {
				arrowset[a].src = arrowset[a].src.replace('_1.gif','_0.gif');
				arrowset[a].className.replace(' hd_arrow_active','');
				var arrowtitle = arrowset[a].getAttribute('title');
				arrowtitle = arrowtitle.replace('This column is sorted ','Sort this column ');
				arrowset[a].setAttribute('title',arrowtitle);
			} else {
				this.src = this.src.replace('_0.gif','_1.gif');
				this.className = this.className + ' hd_arrow_active';
				var arrowtitle = this.getAttribute('title');
				arrowtitle = arrowtitle.replace('Sort this column ','This column is sorted ');
				this.setAttribute('title',arrowtitle);
			}
		}
	}
	return;
}
function arrowsort(arrow) { // fires when an arrow is clicked. We have to investigate what got clicked and act appropriately.
	if (arrow.className.match(/_up/)) { var dir = 'up'; } else { var dir = 'down'; }
	// get the table ID
	var thid = arrow.parentNode.parentNode.id;
	if (thid.match(/hd_gt_name_/)) { var col = 'name'; } else { var col = 'num'; }
	var thidfinder = 'hd_gt_'+col+'_';
	var tid = thid.replace(thidfinder,'');
	// rowset is globally defined, we can do less data collection.
	// collect the name or num values in each row, and the row it's in.
	var valset = [];
	var head = document.getElementById(tid+'_hed');
	for (var a=0;a<rowset.length;a++) {
		var tds = rowset[a].getElementsByTagName('td');
		for (var b=0;b<tds.length;b++) {
			if (tds[b].className.match('hd_gt_'+col)) {
				// find the column being sorted against, pick the value out of it
				td = tds[b];
				var thisval = td.innerHTML;
				if (!thisval) { thisval = '' }
				// add the to-be-sorted value to the sorting stack, add the row to the soon-to-be-new table
				valset.push(thisval);
				break;
			}
		}
	}
	if (dir=='down') { // does what it says on the tin.
		valset.sort(alpha);
	} else {
		valset.sort(ralpha);
	}
	var newrows = [];
	document.getElementById(tid).innerHTML = ''; // clear out the table...
	document.getElementById(tid).appendChild(head); // ...and replace it with the original header row...
	for (var a=0;a<valset.length;a++) {// ...followed by the new order of the non-header rows.
		for (var b=0;b<rowset.length;b++) {
			var tds = rowset[b].getElementsByTagName('td');
			for (var c=0;c<tds.length;c++) {
				if (tds[c].className.match('hd_gt_'+col)) { // pick the bit to match and sort against
					var td = tds[c];
					if (valset[a]==td.innerHTML) {
						newrows.push(rowset[b]);
						document.getElementById(tid).appendChild(rowset[b]);
						break;
					}
				}
			}
		}
	}
	rowset = newrows;
	altrow();
	// now rewrite the arrow with the new SRC as appropriate...
	return;
}
// paints rows in alternating colors
function altrow() {
	// write hd_gt_light and hd_gt_dark in turn, light first.
	for (var a=0;a<rowset.length;a++) {
		if (Math.floor(a/2)==(a/2)) {
			var row_bg = 'hd_gt_light';
		} else {
			var row_bg = 'hd_gt_dark';
		}
		rowset[a].className = 'hd_gt_row '+row_bg;
	}
	return;
}
// initializes the product rows for sorting purposes.
function rowsetter() {
	rowset = [];
	for (var a=0;a<document.getElementsByTagName('tr').length;a++) {
		if (document.getElementsByTagName('tr')[a].className.match('hd_gt_row')) {
			rowset.push(document.getElementsByTagName('tr')[a]);
		}
	}
	return;
}
// hides the file tables, unhides the 'view files' triggers, loads the 'view files' and 'hide files' triggers.
function tableActions() {
	// hd_gt_view, hd_gt_hide
	for (var a=0;a<rowset.length;a++) {
		var divs = rowset[a].getElementsByTagName('div');
		for (var b=0;b<divs.length;b++) {
			if (divs[b].className.match('hd_gt_view')) {
				divs[b].style.display = 'block';
				divs[b].onmouseup = function() { viewhide(this); }
				var prods = rowset[a].getElementsByTagName('table');
				prods[0].style.display = 'none';
			}
			if (divs[b].className.match('hd_gt_hide')) {
				divs[b].onmouseup = function() { viewhide(this); }
			}
		}
	}
	return;
}
// applies the toggling action to the 'view' and 'hide' buttons.
function viewhide(thing) {
	if (thing.className.match('_hide')) {
		var listid = thing.id.replace('_hide_','_list_');
		var otherid = thing.id.replace('_hide_','_view_');
		var displ = 'none';
	} else {
		var listid = thing.id.replace('_view_','_list_');
		var otherid = thing.id.replace('_view_','_hide_');
		var displ = 'block';
	}
	document.getElementById(listid).style.display = displ;
	document.getElementById(otherid).style.display = 'block';
	thing.style.display = 'none';
	return;
}
function alpha(a,b) {
	// alphabetical order 
	if (a<b) { return -1; }
	if (b<a) { return 1; }
	return 0;
}
function ralpha(a,b) {
	// reverse alphabetical order 
	if (a>b) { return -1; }
	if (b>a) { return 1; }
	return 0;
}

/*
	for launching everything on page load.
*/

function hd_launchAll() {
	if (!document.getElementById) { return; }
	initRollovers();
//	rowsetter();
//	tableActions();
//	altrow();
//	init_arrows();
}
window.onload = hd_launchAll;
