// JavaScript Document
function VistaDetect()
{
	//returns true if the operating system is vista, false otherwise
	return (navigator.userAgent.toLowerCase().indexOf("windows nt 6")>-1);
}

function Windows7Detect()
{
	//returns true if the operating system is vista, false otherwise
	return (navigator.userAgent.toLowerCase().indexOf("windows nt 7")>-1);
}

function testall()
{
	CookieJump(); 
	
	
	var thisIsVista = VistaDetect();
	var thisIsWindows7 = Windows7Detect();
	var silverlightIsInstalled=Silverlight.isInstalled("1.0");
	var radioButtons = document.forms['selections'].elements['choices'];

	//alert("Vista=" + thisIsVista);
	//alert("Silverlight=" + silverlightIsInstalled);
	
	if (thisIsVista || thisIsWindows7)
	{
		setCheckedValue(radioButtons,"vista");
		//alert("this is Vista");
	}
	else
	{
		if(silverlightIsInstalled)
		{
			setCheckedValue(radioButtons,"silverlight");
			//alert("this is Silverlight");
		}
		else
		{
			setCheckedValue(radioButtons,"access");
			//alert("this is Accessible Version");
		}
	}


}

function launchTTP2()
{
	var Request = new QSHandler();
	var ttpID = Request.QueryString('id');
	var accessibilityFolder = Request.QueryString('accessfolder');
	var radioButtons = document.forms['selections'].elements['choices'];
	var ttp2version = getCheckedValue(radioButtons);
	var useCookies = document.forms['selections'].elements['cookie'].checked;
	var silverlightURL = "http://ttpdownload.bl.uk/app_files/silverlight/default.html?id=" + ttpID;
	var accessibilityURL = "http://www.bl.uk/onlinegallery/ttp/" + accessibilityFolder + "/accessible/introduction.html";
	var vistaURL = "http://ttpdownload.bl.uk/app_files/xbap/BrowserApp.xbap?id=" + ttpID + "&spread=0";
	
	//alert ("ID = " + ttpID);
	//alert ("Accessibility Folder = " + accessibilityFolder);
	//alert ("Radio Button = " + ttp2version);
	//alert ("Cookies = " + useCookies);
	//alert ("Silverlight URL = " + silverlightURL);
	//alert ("Vista URL = " + vistaURL);
	//alert ("Accessible URL = " + accessibilityURL);
	
	
	if(useCookies)
	{
		setCookie("ttp2version",ttp2version,30);
	}
	
	
	//alert("Cookie=" + getCookie("ttp2version"));
	
	
	
	var agt=navigator.userAgent.toLowerCase();
	this.mac = (agt.indexOf("mac")!=-1);
	this.ie  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	
	var arm_macie = false;
	var arm_offsety = 0;
	var arm_offsetx = 0;
	var xMax=1024;
	var yMax=768;
	
	if((this.mac)&&(this.ie))
	{
		 arm_macie = true;
		 arm_offsety = 100;
		 arm_offsetx = 5;
	}
	
	if (document.all)
	{
		if(arm_macie)
		{
			xMax = screen.width;
			yMax = screen.height;
		}
		else
		{
			xMax = screen.width-8;
			yMax = screen.height-28;
		}
	}
	else
	{
		if (document.layers)
		{
			xMax = window.outerWidth;
			yMax = window.outerHeight;
		}
		else
		{
			xMax = screen.width;
			yMax = screen.height;
		}
	}
	
	
	//alert("x=" + xMax);
	//alert("y=" + yMax);
	
	window.close();
	
	if (ttp2version == 'vista')
	{
		window.open(vistaURL,'index','status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no,history=no,width='+xMax+',height='+yMax+',screenX=0,screenY=0,top=0,left=0');
	}
	else
	{
		if (ttp2version =='silverlight')
		{
			window.open(silverlightURL,'index','status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no,history=no,width='+xMax+',height='+yMax+',screenX=0,screenY=0,top=0,left=0');
		}
		else
		{
			window.open(accessibilityURL);
		}
	}
	
}


function CookieJump()
{
	var ttp2Page = getCookie('ttp2version');
	
	
	if (!(ttp2Page == null))
	{
		window.close();
		var Request = new QSHandler();
		var ttpID = Request.QueryString('id');
		var accessibilityFolder = Request.QueryString('accessfolder');
		var silverlightURL = "http://ttpdownload.bl.uk/app_files/silverlight/default.html?id=" + ttpID;
		var accessibilityURL = "http://www.bl.uk/onlinegallery/ttp/" + accessibilityFolder + "/accessible/introduction.html";
		var vistaURL = "http://ttpdownload.bl.uk/app_files/xbap/BrowserApp.xbap?id=" + ttpID + "&spread=0";
			
		var agt=navigator.userAgent.toLowerCase();
		this.mac = (agt.indexOf("mac")!=-1);
		this.ie  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
		var arm_macie = false;
		var arm_offsety = 0;
		var arm_offsetx = 0;
		var xMax=1024;
		var yMax=768;
		
		if((this.mac)&&(this.ie))
		{
			 arm_macie = true;
			 arm_offsety = 100;
			 arm_offsetx = 5;
		}
		
		if (document.all)
		{
			if(arm_macie)
			{
				xMax = screen.width;
				yMax = screen.height;
			}
			else
			{
				xMax = screen.width-8;
				yMax = screen.height-28;
			}
		}
		else
		{
			if (document.layers)
			{
				xMax = window.outerWidth;
				yMax = window.outerHeight;
			}
			else
			{
				xMax = screen.width;
				yMax = screen.height;
			}
		}
		
		
		if (ttp2Page == "vista")
		{
			window.open(vistaURL,'index','status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no,history=no,width='+xMax+',height='+yMax+',screenX=0,screenY=0,top=0,left=0');
		}
		else
		{
			if (ttp2Page == "silverlight")
			{
				window.open(silverlightURL,'index','status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no,history=no,width='+xMax+',height='+yMax+',screenX=0,screenY=0,top=0,left=0');
			}
			else
			{
				window.open(accessibilityURL);
			}
		}
	}
}




function getCheckedValue(radioObj)
{
	if(!radioObj) 
		return "";
	
	var radioLength = radioObj.length;
	if (radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for (var i = 0; i < radioLength; i++)
	{
		if (radioObj[i].checked)
		{
			return radioObj[i].value;
		}
	}
	return "";
}


function setCheckedValue(radioObj, newValue)
{
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for (var i=0; i < radioLength; i++)
	{
		radioObj[i].checked=false;
		if(radioObj[i].value==newValue.toString())
		{
			radioObj[i].checked = true;
		}
	}
}

function QSHandler() 
{
	var qs = location.search.substr(1).split("&");
	this.data = [];
	for(var i = 0; i < qs.length; i++) this.data[qs[i].split("=")[0]] = qs[i].split("=")[1];
	this.QueryString = function(x)
	{
		return this.data[x];
	};
} 	









