/*
	www.ChrisBeales.net Javascript - (c) Chris Beales 2006-2010

	Name = formatting.js

	Version date = 08/03/2010
	
	Description = Page formatting scripts
*/

window.onresize = function() {
  runMe();
};


function runMe() {
// check setup cookie ...
	var currentSetup = readCookie('siteSetup');
	if (currentSetup == null) {
		if (sniffBrowserOk() == true) {
			currentSetup = "advanced";
		}
		else {
			currentSetup = "basic";
		}
	}
	// alert(currentSetup);
	if (currentSetup == 'advanced') {
		advancedSetup();
	}
}


function sniffBrowserOk() {
	try {
		var nAgt = navigator.userAgent;
		var browserName  = navigator.appName;
		var fullVersion  = ''+parseFloat(navigator.appVersion);
		var verOffset,ix;

	// In MSIE, the true version is after "MSIE" in userAgent
		if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
			fullVersion = nAgt.substring(verOffset+5);
		}

	// trim the fullVersion string at semicolon/space if present
		if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix);
		if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix);

	// Firefox if stated in user agent
		var foxID = "Firefox";

	// check if recognised browser that can cope with advanced setup ...
		var ieOk = ((browserName == "Microsoft Internet Explorer") && (fullVersion >= 7));
		var foxOk = (nAgt.search(foxID) != -1);
		
		if ((foxOk == true) || (ieOk == true)) {
			return true;
		}		
	}
	catch(err) {		
		txt="There was an error on this page.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);		
	}		
	return false;
}


function switchSetup() {
	try {
		var currentSetup = readCookie('siteSetup');
		if (currentSetup == null) {
			if (sniffBrowserOk() == true) {
				currentSetup = "advanced";
			}
			else {
				currentSetup = "basic";
			}
		}
		
		if (currentSetup == "basic") {
			createCookie('siteSetup', 'advanced', '90');
			advancedSetup();
		}	
		else {	
			createCookie('siteSetup', 'basic', '90');
			window.location.reload();
		}
		
		txt = 'Website is now set up in ' + readCookie('siteSetup') + ' mode. \n\n';
		txt = txt + 'You may need to refresh the page to see the change.'
		alert(txt);	
	}
	catch(err) {		
		txt="There was an error on this page.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);		
	}	
}


function advancedSetup() {
// Change labels if they exist ...
	try {
		var switchElement = document.getElementById('currentSetup');
		switchElement.innerHTML = "Advanced Configuration";
	}
	catch(err) {
		// do nothing
	}		

// do setup ...
	try {
		var myHeight = 0;
		if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
			myHeight = window.innerHeight;
		} else if( document.documentElement && document.documentElement.clientHeight ) {
		//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientHeight;
		}

		topHeight =	0;
		topHeight =	document.getElementById("top").clientHeight;

		//	alert( topHeight + "  " + myHeight);
		if ((myHeight > 100) && (topHeight > 10) && ((topHeight / myHeight) < 0.3)) {
			mainHeight = myHeight - topHeight - 40;
			document.getElementById("main").style.height= mainHeight + "px";
		}
	
	// This is beyond Javascript standard (only allows focus on forms and hyperlinks) works in IE though
		document.getElementById("main").focus();	
	}
	catch(err) {		
		txt="There was an error on this page.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);		
	}	
}


function scrollUp() {
	document.getElementById('main').scrollTop=0;
}


function underlineSwitch(element) {
	var wkElement = document.getElementById(element);	
	wkElement.style.textDecoration = "underline";
	wkElement.style.cursor = "pointer";
}


function normalSwitch(element) {
	var wkElement = document.getElementById(element);
	wkElement.style.textDecoration = "none";
}


function zoomImage(element) {
	var wkElement = document.getElementById(element);
	
	var msg = "Title: '" + wkElement.title + "'\n\n";
	
	if (wkElement.alt == "[i]") {
		alert(msg);
	}
	else {
		msg = msg + "Would you like to open the image in a new page?"

		var lama=confirm(msg);

		if (lama == true) {			
			window.location.assign('#' + wkElement.id);
			window.location.assign(wkElement.src);
		}
	}
}


function outlineImage(element) {
	var wkElement = document.getElementById(element);	

	if ((wkElement.alt == "[i]") || (wkElement.alt == "RSS")) {
		wkElement.style.paddingLeft = "1px";
		wkElement.style.paddingRight = "1px";
		wkElement.style.borderLeft = "1px solid blue";
		wkElement.style.borderRight = "1px solid blue";
	}
	else {
		wkElement.style.padding = "3px";
		wkElement.style.border = "1px solid blue";
	}
}


function resumeImage(element) {
	var wkElement = document.getElementById(element);	
	wkElement.style.border = "none";
	
	if ((wkElement.alt == "[i]") || (wkElement.alt == "RSS")) {
		wkElement.style.paddingLeft = "2px";
		wkElement.style.paddingRight = "2px";		
	}
	else {
		wkElement.style.padding = "4px";
	}
}


function scrollUp() {
	document.getElementById('main').scrollTop=0;
}


// Cookie scripts from 'http://www.quirksmode.org/js/cookies.html'
// ---------------------------------------------------------------
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
