/*** map rollover popups function in modern browsers via CSS only;
    however, IE5, IE5.5, IE6 require this Javascript in order to function ***/

function startlist() {
	if (document.all&&document.getElementById) {
		if (document.getElementById("map")) {
			navRoot = document.getElementById("map");
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
		if (document.getElementById("smallmap")) {
			navRoot2 = document.getElementById("smallmap");
			for (i=0; i<navRoot2.childNodes.length; i++) {
				node2 = navRoot2.childNodes[i];
				if (node2.nodeName=="LI") {
					node2.onmouseover=function() {
						this.className+=" over";
					}
					node2.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	}
}

var objPopupWindow;
var temp;
var allPhotos;

function popup(thisPhoto,allPhotos) {
title = document.title;

	/*
	var img = new Image();
	var width, height = 0;
	img.src = thisPhoto.href;


	img.onload = function()
	{
		width = img.width;
		height = img.height;
		alert(width + 'x' + height);
	}
	*/

	// load variable with new content ready for display.
	var newContent = "";
	newContent += '<table id="popup" cellspacing="0" cellpadding="12" border="0" width="100%" height="100%" style="background:url(img/bg_header.gif) repeat-x -24px -12px; font-size:100%;">';
	newContent += '	<tr>';
	newContent += '		<td height="78" style="color:#ffffff; padding-left:80px;" valign="top">';
	newContent += '			<a href="#" style="float:right; margin:0em 1em; color:#ffffff;" onclick="window.opener.focus(); window.close();">[ close window ]<\/a>';
	newContent += '			<span id="alt">'+thisPhoto.getElementsByTagName('IMG')[0].alt+'<\/span><\/td>';
	newContent += '	<\/tr><tr>';
	//newContent += '		<td style="text-align:center;" height="100%" valign="middle"><img id="src" src="'+thisPhoto.href+'" alt="'+thisPhoto.getElementsByTagName('IMG')[0].alt+'" title="" width="'+width+'" height="'+height+'"><\/td>';
	newContent += '		<td style="text-align:center;" height="100%" valign="middle"><img id="src" src="'+thisPhoto.href+'" alt="'+thisPhoto.getElementsByTagName('IMG')[0].alt+'" title="" ><\/td>';
	newContent += '	<\/tr><tr>';
	newContent += '		<td style="text-align:right;" id="navPhotos">';
	for (i=1;i<=numPhotos;i++) {
		 if (thisPhoto.href == allPhotos.getElementsByTagName('A')[i-1].href) {
			newContent += '<input type="text" id="valPhoto" value="'+i+'">'
		}
	}
	
	// Previous
	newContent += '&lt; <a href="#" target="popup" onclick="temp=(document.getElementById(\'valPhoto\').value>1)?parseInt(--document.getElementById(\'valPhoto\').value):1; document.getElementById(\'src\').src=allPhotos.getElementsByTagName(\'A\')[temp-1].href; document.getElementById(\'src\').alt=allPhotos.getElementsByTagName(\'IMG\')[temp-1].alt; document.getElementById(\'alt\').firstChild.nodeValue=allPhotos.getElementsByTagName(\'IMG\')[temp-1].alt; return false;"> previous </a>';
	
	for (i=1; i<=numPhotos; i++) {
		if (i==1) newContent += ' &nbsp; [ ';
		newContent += '<a href="'+allPhotos.getElementsByTagName('A')[i-1].href+'" target="popup" onclick="document.getElementById(\'valPhoto\').value='+i+'; document.getElementById(\'src\').src=\''+allPhotos.getElementsByTagName('A')[i-1].href+'\'; document.getElementById(\'src\').alt=\''+allPhotos.getElementsByTagName('IMG')[i-1].alt+'\'; document.getElementById(\'alt\').firstChild.nodeValue=\''+allPhotos.getElementsByTagName('IMG')[i-1].alt+'\'; return false;"> '+i+' </a>';
		if (i==numPhotos) { newContent += ' ] &nbsp; ' } else { newContent += ' | ' }
	}
	
	// Next
	newContent += ' <a href="#" target="popup" onclick="temp=(document.getElementById(\'valPhoto\').value<numPhotos)?parseInt(++document.getElementById(\'valPhoto\').value):numPhotos; document.getElementById(\'src\').src=allPhotos.getElementsByTagName(\'A\')[temp-1].href; document.getElementById(\'src\').alt=allPhotos.getElementsByTagName(\'IMG\')[temp-1].alt; document.getElementById(\'alt\').firstChild.nodeValue=allPhotos.getElementsByTagName(\'IMG\')[temp-1].alt; return false;"> next </a> &gt;';
	
	newContent += '		<\/td>';
	newContent += '	<\/tr>';
	newContent += '<\/table>';
	// Open popup window if not already open.
	// Check the window opener and the popup window object variable to determine the state of the popup window.
	// Also, since the user can close the popup window with the 'x' button in the window frame
	// (and there is no way of capturing this event specifically) we have to perform a objPopupWindow.closed check.
	if (objPopupWindow && !objPopupWindow.closed) {
		objPopupWindow.document.body.innerHTML = newContent;
	}
	else {
		// popup window isn't open, so launch it.
		objPopupWindow = window.open('','popup','width=660,height=420,scrollbars,status');
		objPopupWindow.document.write('<html>');
		objPopupWindow.document.write('<head>');
		objPopupWindow.document.write('<title>'+title+'<\/title>');
		objPopupWindow.document.write('<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8">')
		objPopupWindow.document.write('<link rel="stylesheet" type="text\/css" href="inc\/style.css">');
		objPopupWindow.document.write('<script>allPhotos = window.opener.document.getElementById("photos"); numPhotos = allPhotos.getElementsByTagName("A").length;</script>');
		objPopupWindow.document.write('<\/head>');
		objPopupWindow.document.write('<body>');
		objPopupWindow.document.write(newContent);
		objPopupWindow.document.write('<\/body>');
		objPopupWindow.document.write('<\/html>');
		objPopupWindow.document.close();
	}
	// focus on open popup window.
	objPopupWindow.focus();
}

window.onerror = function() {
    window.status = 'Error on page.';
}

function attachFocus() {
	if (this.className.indexOf(" focus")<0) {
		this.className+=" focus";
	}
}

function attachBlur() {
	if (this.className.indexOf(" focus")>0) {
		this.className = this.className.substring(0,this.className.indexOf(" focus"));
	} else {
		this.className.replace(" focus","");
	}

	if (this.tempOnBlur) {
		// call existing onBlur event.
		this.tempOnBlur();
	}
}

function msgStatus() {
	if (this.tagName=="INPUT") {
		switch(this.type) {
			case 'image': // form button is an <input type="image"> (graphic);
				if (this.getAttribute('src').indexOf('_a.')!=-1) {
					this.src = this.getAttribute('src').replace('_a.','_b.');
					txtStatus=this.getAttribute('alt');
				}
				if (this.getAttribute('src').indexOf('-a.')!=-1) {
					this.src = this.getAttribute('src').replace('-a.','-b.');
					txtStatus=this.getAttribute('alt');
				}
				break;
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
			case 'file': // form button is an <input type="file"> (webtext);
				this.className="bnover";
				txtStatus=this.getAttribute('value');
				break;
			default:
				break;
		}
	} else {
		if (this.name) {
			for (i=0;i<document.getElementsByName(this.name).length;i++) {
				if (document.getElementsByName(this.name)[i]!=this) {
					if (document.getElementsByName(this.name)[i].tagName=='A') {
						if (document.getElementsByName(this.name)[i].getElementsByTagName('img').length!=0) {
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('_a.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('_a.','_b.');
							}
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('-a.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('-a.','-b.');
							}
						} else {
							document.getElementsByName(this.name)[i].className="rollover";
						}
					}
				}
			}
		}
		if (this.innerText) {
			txtStatus=this.innerText;
		} else {
			if (this.text) txtStatus=this.text;
		}
		if (this.getElementsByTagName('img').length!=0) {
			txtStatus=this.getElementsByTagName('img')[0].getAttribute('alt');
			if (this.getElementsByTagName('img')[0].src.indexOf('_a.')!=-1) {
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_a.','_b.');
			}
			if (this.getElementsByTagName('img')[0].src.indexOf('-a.')!=-1) {
				this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('-a.','-b.');
			}
		} else {
			if (this.tagName=="AREA") {
				txtStatus=this.getAttribute('alt');
			}
		}
	}
	// txtStatus=toTitleCase(txtStatus);
	window.status=toTitleCase(txtStatus); return true;
}

function msgStatusOff() {
	txtStatus=window.defaultStatus;
	if (this.tagName=="INPUT") {
		switch(this.type) {
			case 'image': // form button is an <input type="image"> (graphic);
				if (this.getAttribute('src').indexOf('_b.')!=-1) {
					this.src = this.getAttribute('src').replace('_b.','_a.');
				}
				if (this.getAttribute('src').indexOf('-b.')!=-1) {
					this.src = this.getAttribute('src').replace('-b.','-a.');
				}
				break;
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
				this.className="bnout";
				break;
			default:
				break;
		}
	} else {
		if (this.name) {
			for (i=0;i<document.getElementsByName(this.name).length;i++) {
				if (document.getElementsByName(this.name)[i]!=this) {
					if (document.getElementsByName(this.name)[i].tagName=='A') {
						if (document.getElementsByName(this.name)[i].getElementsByTagName('img').length!=0) {
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('_b.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('_b.','_a.');
							}
							if (document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.indexOf('-b.')!=-1) {
								document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src = document.getElementsByName(this.name)[i].getElementsByTagName('img')[0].src.replace('-b.','-a.');
							}
						} else {
							document.getElementsByName(this.name)[i].className="";
						}
					}
				}
			}
		}
	}
	if (this.getElementsByTagName('img').length!=0 && this.getElementsByTagName('img')[0].src.indexOf('_b.')!=-1 && this.getElementsByTagName('img')[0].name.indexOf('img_')==-1) {
		this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('_b.','_a.');
	}
	if (this.getElementsByTagName('img').length!=0 && this.getElementsByTagName('img')[0].src.indexOf('-b.')!=-1 && this.getElementsByTagName('img')[0].name.indexOf('img_')==-1) {
		this.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src.replace('-b.','-a.');
	}
	window.status=txtStatus; return true;
}

function bodyResize() {
	window.location.href = window.location.href;
}

function msgStatusAllLinks() {
	document.body.onresize=bodyResize;
	for(i=0; i<document.links.length; i++) {
		if (!document.links[i].onmouseover) {
			document.links[i].onmouseover=msgStatus;
			document.links[i].onmouseout=msgStatusOff;
		}
	}
	for(i=0; i<document.getElementsByTagName('input').length; i++) {
		switch(document.getElementsByTagName('input')[i].type) {
			case 'image': // form button is an <input type="image"> (graphic);
			case 'button': // form button is an <input type="button"> (webtext);
			case 'submit': // form button is an <input type="submit"> (webtext);
			case 'reset': // form button is an <input type="reset"> (webtext);
				document.getElementsByTagName('input')[i].onmouseover=msgStatus;
				document.getElementsByTagName('input')[i].onmouseout=msgStatusOff;
				break;
			case 'text':
			case 'password':
				document.getElementsByTagName('input')[i].onfocus=attachFocus;
				
				// Add element's existing onblur event (if it has one) before adding a new onBlur event 'attachBlur'
				// This ensures that any existing onBlur event the element has is still executed, as well as the 'attachBlur' event.
				// Check if IE or Netscape and use appropriate syntax accordingly.
				if (navigator.appName == 'Microsoft Internet Explorer') {
					if (document.getElementsByTagName('input')[i].onblur) {
						// Assign existing onBlur to a temp event 'tempOnBlur' - Later executed in attachBlur function.
						document.getElementsByTagName('input')[i].tempOnBlur = document.getElementsByTagName('input')[i].onblur;
					}
					// attach new onblur event. This will cause the input field colour to change.
					document.getElementsByTagName('input')[i].onblur=attachBlur;
				}
				else {
				
					// attach existing onblur event.
					document.getElementsByTagName('input')[i].addEventListener('blur',attachBlur,false);
				}		
				break;
			default:
				break;
		}
	}
	for(i=0; i<document.getElementsByTagName('textarea').length; i++) {
		document.getElementsByTagName('textarea')[i].onfocus=attachFocus;
		document.getElementsByTagName('textarea')[i].onblur=attachBlur;
	}
}

// toTitleCase
function toTitleCase(strString) {
	// strString = fnTrim(strString);
	var firstSpace = strString.indexOf(' ');
	var currentWord;
	strString = strString.charAt(0).toUpperCase()+strString.substring(1,strString.length);
	if (firstSpace > -1) strString=strString.substring(0,firstSpace+1) + toTitleCase(strString.substring(firstSpace+1,strString.length));
	return strString;
}
