/*
	display.js
	
	Javascripts for ~~ website
*/
/*
	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.
*/

// accommodates different states for the top-level menu image.
// and what the heck, do all the image preloading, too.
function newSfHover() {
	menuext = '_menu';
	overext = '_over';
	piclist = new Array();
	b=0;
	for (var a=0;a<document.getElementById("nav").getElementsByTagName("li").length;a++) {
		thisitem = document.getElementById("nav").getElementsByTagName("li")[a];
		for (var e=0;e<thisitem.getElementsByTagName('img').length;e++) {
			thispic = thisitem.getElementsByTagName('img')[e];
			if (thispic.className.match('rollover')) {
				var thispicsrc = thispic.getAttribute('src');
				var ftype = thispicsrc.substring(thispicsrc.lastIndexOf('.'),thispicsrc.length);
				oversrc = thispicsrc.replace(ftype,overext+ftype);
				menusrc = thispicsrc.replace(ftype,menuext+ftype);
				thispic.setAttribute('osrc',oversrc); // 'over' mode
				thispic.setAttribute('msrc',menusrc); // 'menu' mode
				thispic.setAttribute('xsrc',thispicsrc); // default (the SRC on initial image load)
				thispic.onmouseover = function() { $(this).addClass('mhover'); return; }
				thispic.onmouseout = function() { $(this).removeClass('mhover'); return; }
				piclist[b] = oversrc;
				piclist[b+1] = menusrc;
				b=b+2;
			}
		}
		thisitem.onmouseover = function() {
			$(this).addClass('over'); // requires jQuery
			for (var i=0;i<this.getElementsByTagName('img').length;i++) {
				nowpic = this.getElementsByTagName('img')[i];
				if (nowpic.className.match('rollover')) {
					if (nowpic.className.match('mhover')) {
						nowpic.setAttribute('src',nowpic.getAttribute('osrc'));
					} else {
						nowpic.setAttribute('src',nowpic.getAttribute('msrc'));
					}
					break;
				}
			}
			return;
		}
		thisitem.onmouseout = function() {
			$(this).removeClass('over'); // requires jQuery
			for (var i=0;i<this.getElementsByTagName('img').length;i++) {
				nowpic = this.getElementsByTagName('img')[i];
				if (nowpic.className.match('rollover')) {
					nowpic.setAttribute('src',nowpic.getAttribute('xsrc'));
				}
			}
			return;
		}
	}
	extraPreloader(piclist);
}
function mapKeyer() { // requires jQuery
	if (!document.getElementById('mapmap')) { return; }
	if (!$('#mapmap')) { return; }
	$('#map_legend .none').css({display:'block'});
	$('#mapmap').children('area').mouseup(function(){
			// first, clear the existing caption
			$('#map_legend').children().css({display:'none'});
			// get the coordinate of this area item and put the bullet there.
			// THE BULLET IS BIGGER THAN THE TARGET so values may have to fudge.
			var areaoffset = this.getAttribute('coords').split(',',2);
			var bulletleft = Number(areaoffset[0] - 5);
			var bullettop = Number(areaoffset[1] - 5);
			$('#map_bullet').css({display:'block',top:bullettop+'px',left:bulletleft+'px'});
			// get the relevant legend and display it.
			var mapitem = '.' + this.getAttribute('id');
			$(mapitem).css({display:'block'});
		});
	return;
}
// locks the SUBMIT button until mandatory fields are filled out.
function mandatoryForm() { // requires jQuery
	mandatory = $('#feedbackform .mandatory').length;
	setInterval(function() {
			$('#feedbackform .mandatory .field').filter(function(a){return $(this).attr('value')=='';}).removeClass('filled').prev().removeClass('done');
			$('#feedbackform .mandatory .field').filter(function(a){return $(this).attr('value')!='';}).addClass('filled').prev().addClass('done');
			q = $('#feedbackform .filled').length;
			if ($('#feedbackform .filled').length==mandatory) {
				document.getElementById('Submit').disabled = false;
			} else {
				document.getElementById('Submit').disabled = true;
			}
		},150);
	return;
}
// Convenience method to make login links only appear when asked.
function staffLogin() {
	document.getElementById('stafflogin').onmouseup = function() {
		if (document.getElementById('stafflogin').getElementsByTagName('ul')[0].style.display.match('block')) {
			document.getElementById('stafflogin').getElementsByTagName('ul')[0].style.display = 'none';
		} else {
			document.getElementById('stafflogin').getElementsByTagName('ul')[0].style.display = 'block';
		}
		if (isIE()) {
			setInterval(function() {
					document.getElementById('stafflogin').getElementsByTagName('ul')[0].style.display = 'none';
				},25000); // The links disappear after mumblesomething seconds.
		} else {
			setInterval(function() {
					document.getElementById('stafflogin').getElementsByTagName('ul')[0].style.display = 'none';
				},15000); // The links disappear after fifteen seconds.
		}
	}
}
// Tests for MSIE version. Returns version whole number
// assumes that 5 is only wanted as a flag to reject too-old versions of IE.
// otherwise, 0==FALSE== not MSIE.
// This is more reliable than browser testing, because browsers installed with multi-IE hacks can misidentify themselves.
// It will misidentify if somebody's upgraded jscript by hand, but that's an edge case.
function isIE() {
	/*@cc_on
		if (@_jscript_version < 5.6) { return 5; }
		if (@_jscript_version >= 5.6 && @_jscript_version < 5.7) { return 6; }
		if (@_jscript_version >= 5.7 && @_jscript_version < 5.8) { return 7; }
		if (@_jscript_version >= 5.8 && @_jscript_version < 5.9) { return 8; }
	@*/
	return 0;
}
// preload nondisplayed images needed by various things.
function extraPreloader (piclist) {
	picPre = new Array();
//	imgPath = 'images/';
	imgPath = '';
	for (a=0;a<piclist.length;a++) {
		picPre[a] = new Image();
		picPre[a].src = imgPath + piclist[a];
	}
	return;
}
function launchAll() {
//	if (!document.getElementById()) { return; }
//	initRollovers();
//	if (isIE()) { sfHover(); }
	newSfHover();
	if (document.getElementById('stafflogin')) { staffLogin(); } // should only fire when the Login button is available.
	if (document.getElementById('mapmap')) { mapKeyer(); } // should only fire on projects_map.html
	if (document.getElementById('feedbackform')) { mandatoryForm(); } // should only fire on contact.html
}
window.onload = launchAll;

/* EOF */
