
// Use traditional event model whilst support for event registration
// amongst browsers is poor.
window.onload = init;

function init()
{
	styleSheetLoad();
	pdfLoad();
}

function pdfLoad()
{
	BrowserDetect.init();
	
	// initialise link behaviour
	if (BrowserDetect.browser == -1)
	{
		// leave default link behaviour intact for unknown browsers
	}
	else if (BrowserDetect.browser == "Explorer" && BrowserDetect.version > 6)
	{
		// leave default link behaviour intact for IE7
		// IE7 does not load new windows correctly using window.open (behaviour by design?)
	}
	else
	{
		initLinks(true);
	}
}

/*
class:		BrowserDetect
credits:	Peter-Paul Koch, www.quirksmode.org/js/detect.html
changelog:	changed unknown values to return -1 instead of string values
*/
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || -1;
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| -1; // UTLUK: modified this line
		this.OS = this.searchString(this.dataOS) || -1;
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
	
/*
methods:	initLinks, openWindow
credits:	W3C, http://www.w3.org/TR/2006/WD-WCAG20-TECHS-20060427/#SCR24
			Roger Johansson, http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/
modified:	amalgamation of above scripts
*/
function initLinks(bUsePopUp)
{
	// check browser support for required DOM methods
	if (!document.getElementsByTagName || 
		!document.createElement || 
		!document.appendChild || 
		!document.createTextNode)
	{
		return false;
	}
	
	var strWarning = " (opens in a new window)";
	var arrAnchors = document.getElementsByTagName("a");
	var objAnchor;
	var objWarning;

	for (var i = 0; i < arrAnchors.length; i++)
	{
		objAnchor = arrAnchors[i];
		if (objAnchor.className == "PDF")
		{
			// add warning to link text
			objWarning = document.createElement("em");
			objWarning.appendChild(document.createTextNode(strWarning));
			objAnchor.appendChild(objWarning);
			
			if (bUsePopUp)
			{
				// register event handlers
				objAnchor.onclick = function(event){return openPopUp(this, event);}
				// UAAG requires that user agents handle events in a device-independent manner
				// but only some browsers do this, so add keyboard event to be sure
				objAnchor.onkeypress = function(event){return openPopUp(this, event);}
			}
			else
			{
				// use target to open new window
				objAnchor.target = "_blank";
			}
		}
	}
	
	arrAnchors = null;
	objAnchor = null;
	objWarning = null;
}

function openWindow(objAnchor, objEvent, bPopUp)
{
	var iKeyCode;
	var bSuccess = false;

	// If the event is from a keyboard, we only want to open the
	// new window if the user requested the link (return or space)
	if (objEvent && objEvent.type == "keypress")
	{
		if (objEvent.keyCode)
		{
			iKeyCode = objEvent.keyCode;
		}
		else if (objEvent.which)
		{
			iKeyCode = objEvent.which;
		}
	
		// If not carriage return or space, return true so that the user agent
		// continues to process the action
		if (iKeyCode != 13 && iKeyCode != 32)
		{
			return true;
		}
	}
	
	bSuccess = window.open(objAnchor.href);

	// If the window did not open, allow the browser to continue the default
	// action of opening in the same window
	if (!bSuccess)
	{
		return true;
	}

	// The window was opened, so stop the browser processing further
	return false;
}

function openPopUp(objAnchor, objEvent, bPopUp)
{
	var iKeyCode;
	var bSuccess = false;

	// If the event is from a keyboard, we only want to open the
	// new window if the user requested the link (return or space)
	if (objEvent && objEvent.type == "keypress")
	{
		if (objEvent.keyCode)
		{
			iKeyCode = objEvent.keyCode;
		}
		else if (objEvent.which)
		{
			iKeyCode = objEvent.which;
		}
	
		// If not carriage return or space, return true so that the user agent
		// continues to process the action
		if (iKeyCode != 13 && iKeyCode != 32)
		{
			return true;
		}
	}
	
	bSuccess = window.open(objAnchor.href, "_blank", "location=no,status=yes,resizable=yes,scrollbars=yes");

	// If the window did not open, allow the browser to continue the default
	// action of opening in the same window
	if (!bSuccess)
	{
		return true;
	}

	// The window was opened, so stop the browser processing further
	return false;
}





// Style switcher code
function doStyleSheetSelection()
{	
	if (!document.getElementById)
	{
		alert("Your browser does not support viewing options feature.");
	}
	else
	{
		var styleSheetTitle;
		
		// get selected theme
		if (document.getElementById("theme-contrast").checked)
		{
			styleSheetTitle = "High contrast theme";
		}
		else
		{
			styleSheetTitle = "Standard theme";
		}
		
		// get selected text size
		if (document.getElementById("text-large").checked)
		{
			styleSheetTitle += " + large text";
		}
		else if (document.getElementById("text-medium").checked)
		{
			styleSheetTitle += " + medium text";
		}
		else
		{
			styleSheetTitle += " + small text";
	
		}

		setActiveStyleSheet(styleSheetTitle);
	}
}

function loadStyleSheetSelection()
{
	if (document.getElementById("theme-contrast")) // Check that we're on the theme selection page
	{
		try
		{
			
			var activeStyleSheet = getActiveStyleSheet().split(" + ");
			var theme = activeStyleSheet[0];
			var textSize = activeStyleSheet[1];
			


			// display selected theme
			if (theme == "High contrast theme")
			{
				document.getElementById("theme-contrast").checked = true;
			}
			else
			{
				document.getElementById("theme-standard").checked = true;
			}
			
			// display selected text size
			if (textSize == "large text")
			{
				document.getElementById("text-large").checked = true;
			}
			else if (textSize == "medium text")
			{
				document.getElementById("text-medium").checked = true;
			}
			else
			{
				document.getElementById("text-small").checked = true;
			}

		 createCookie("style", title, 365);
		
		}
		catch (er)
		{
			document.getElementById("theme-standard").checked = true;
			document.getElementById("text-small").checked = true;
		}
	}
}

/*
Source: http://www.alistapart.com/articles/alternate/
*/

function setActiveStyleSheet(title) 
{


	if ((title == "undefined")||(title == "null"))
	return false;
else
	{
	

		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
		{
    			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
			{
				
      				a.disabled = true;
      				if(a.getAttribute("title") == title)
				{ 
					a.disabled = false;
					//alert(a.getAttribute("title"))
				}
			}
		}
	}
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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 

	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;
}



window.onunload = function(e) {
if (document.getElementById("theme-contrast"))
	{
  	var title = getActiveStyleSheet();
	if(title==null)
		title="Standard theme + small text"
	createCookie("style", title, 365);
}
}


function styleSheetLoad() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  loadStyleSheetSelection();
	
}
// Make sure page loads new style before laoding default

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

