// ****************************************************************************************
// *  Cogent Software Solutions, Inc.
// *  Copyright (c) 2007-2008 
// *  All Rights Reserved
// ****************************************************************************************/
var timer = null;
var browsername = null;
var browserversion = null;
var browser = null;
var os = null;
var jre = null;
var jremaj = null;
var jremin = null;
var mainApp = null
var checkcount = 0;
var screenWidth = screen.width;
var screenHeight = screen.height;
var winWidth = 0;
var winHeight = 0;
var baseURL = null;
var homeURL = null;
var lastURL = null;
var appName = null;
var archive = null;
var stylesheet = null;
var altpage = null;
var badbrowserpage = null;
var splashpage = null;


function setAlternatePage(page)
{
	this.altpage = page;
}

function setBadBrowserPage(page)
{
	badbrowserpage = page;
}

function setSplashPage(page)
{
	this.splashpage = page;
}

function setStyleSheet(stylesheet)
{
	this.stylesheet = stylesheet;
}

function startGalileo(app)
{
	startGalileo(app, null);
}

function startGalileo(app, archive)
{
	checkcount = 0;

	determineClient();

	testClient();

	appName = app;
	
	archive = "galileo.jar, " + archive;

	if (splashpage != null)
	{
		document.write("<iframe name='splashframe' id='splashframe' frameborder='0' width='100%' height='100%' src='splash.html'></iframe>");
	}
	else
	{
		document.write("<iframe name='splashframe' id='splashframe' frameborder='0' width='100%' height='100%'></iframe>");
	}
	
	document.write("<iframe name='appframe' id='appframe' frameborder='0' width='100%' height='5%' scrolling='no' marginwidth='0px' marginheight='0px'></iframe>");

	document.write("<iframe name='hiddenframe' id='hiddenframe' frameborder='0' width='0%' height='0%' src='galileo.html'></iframe>");

   	var af = parent.appframe;

	af.document.open();
	
	af.document.write(
				   "<applet id='Galileo' name='Galileo'",
			   	   "style='position: absolute; left: 0; top: 0; visibility: visible; '",
			   	   "width='100%' height='100%'",
			   	   "codebase='.'",
			   	   "code='galileo.riaf.app.GApplet.class'",
  				   "archive='" + archive + "'",
   	      		   "param name='java_arguments' value='-Djnlp.packEnabled=true'",
      			   "param name='appname' value='" + appName +"'",
      			   "param name='browserbased' value='true'",
				   "param name='java_arguments' value='-Djnlp.packEnabled=true'",
	      		   "scriptable = 'true'>",
   	      		   "<param name='appname' value='" + appName + "'>",
   	      		   "<param name='browserbased' value='true'>",	   	      		   
   	      		   "<param name='ass' value='" + stylesheet + "'>",
   	      		   "<param name='java_arguments' value='-Djnlp.packEnabled=true'>",
   	      		   "<param name='separate_jvm' value='true'>",
   	      		   "</applet>");
   	      		   
	mainApp = af.document.applets[0];   
	
	if (mainApp == undefined)
	{
		mainApp = af.document.Galileo;
	}
	
	af.document.close();
	
	document.close();
	
	timer = window.setInterval("checkApplet()", 100);
}


function testClient()
{
	if (jre == null && altpage != null && (browsername == "Internet Explorer" || browsername == "Firefox" || browsername == "Chrome" || browsername == "Safari"))
	{
		if (altpage != null)
		{
			window.location = altpage;
		}
		else
		{
	
		}
	}
	else if (jre != null && jremaj < 5)
	{
		if (altpage != null)
		{
			window.location = altpage;
		}
		else
		{

		}
	}
	else if (jre != null)
	{
		if (os == "Windows")
		{
			 testWindows();
		}
		else if (os == "MacOSX")
		{
			testMac();
		}
		else if (os == "Linux")
		{
			testLinux();
		}
		else
		{
	
		}
	}
}



function testWindows()
{
	if (browsername == "Firefox")
	{
		var ffver = browserversion.split(".")[0];
		
		if (ffver == 3)
		{
			if (jremaj < 6 || (jremaj == 6 && jremin < 10))
			{
				window.location = altpage;
			}
		}
		else if (ffver == 2)
		{
			if (jremaj < 5 || (jremaj == 6 && jremin > 2 && jremin < 10))
			{
				window.location = altpage;
			}
		}	
	}
	/*else if (browsername == "Mozilla" || browsername == "Chrome")
	{
		if (jremaj < 5 || (jremaj == 6 && jremin > 2 && jremin < 10))
		{
			window.location = altpage;
		}
	}
	else if (browsername == "Safari" || browsername == "Opera")
	{
		if (badbrowserpage != null)
		{
			window.location = badbrowserpage;
		}
	}*/
}


function testMac()
{
	if (browsername == "Opera")
	{
		if (badbrowserpage != null)
		{
			window.location = badbrowserpage;
		}
	}
}


function testLinux()
{
	if (browsername == "Firefox")
	{
		var ffver = browserversion.split(".")[0];

		if (ffver == 3)
		{
			if (jremaj < 6 || (jremaj == 6 && jremin < 10))
			{
				window.location = altpage;
			}
		}
		else if (ffver == 2)
		{
			if (jremaj < 5 || (jremaj == 6 && jremin > 2 && jremin < 10))
			{
				window.location = altpage;
			}
		}	
	}
	else if (browsername == "Opera")
	{
		if (badbrowserpage != null)
		{
			window.location = badbrowserpage;
		}
	}
}



function checkApplet()
{
	if (mainApp != null)
	{
	    var af = parent.appframe;
		var isloaded = 0;
		var firsttry = true;
		
		try
		{
			isloaded = mainApp.isLoaded();
		}
		catch(error)
		{
			checkcount++;		
			
			if (checkcount > 200)
			{
				window.clearInterval(timer);
				
				if (altpage == null)
				{
					failedToStart();
				}
				else
				{
					window.location = altpage;	
				}
			}
		}
		
		
		if (isloaded == 1 && firsttry == true)
		{
			try
			{
			    firsttry = false;
			    
				homeURL = window.location.href.replace(/:[\/]*/, "://");
				homeURL = homeURL.replace(/\s/g, "%20");
	
				mainApp.setClientInfo(os, browsername, browserversion, navigator.userAgent);
			
				lastURL = homeURL;
	
				baseURL = homeURL.split("#")[0];
				homeURL = baseURL;
			
				mainApp.startApplication(appName);
			}
			catch(error)
			{
				alert(error);
			}
			
		}
		else if (isloaded == -1)
		{
			window.clearInterval(timer);
			alert("Application failed. See Java console for more information");	
		}
		else if (isloaded == 2)
		{
			window.clearInterval(timer);
			reportWindowSize();
	
			var hash = window.location.hash;
			
			if (hash.length > 0)
			{
				hash = hash.substring(1);
				mainApp.navigateTo(hash);
			}
	
			timer = window.setInterval("checkURL()", 50);
	
			showApp();
		}

	}
}


function failedToStart()
{
	window.document.write("<div style='width: 100%; padding-top: 50px; font-size: 16pt; text-align: center'>");
	window.document.write("The page you are trying to view requires the<br>");
	window.document.write("latest Java Plugin from Sun Microsystems.");
	window.document.write("</div>");
	window.document.close();
}


function showApp()
{
    var sf = document.getElementById("splashframe");
    var af = document.getElementById("appframe");

	if (sf)
	{
    	sf.width = "0";
	    sf.height = "0";
	}

    af.width = "100%";
    af.height = "100%";
}


function determineWindowSize()
{
	if (browsername == "Internet Explorer")
	{
		winWidth  = document.body.clientWidth;
		winHeight = document.body.clientHeight;
	}
	else
	{
		winWidth  = window.innerWidth;
		winHeight = window.innerHeight;
	}
}

function reportWindowSize()
{
	if (mainApp != null)
	{
		determineWindowSize();
		mainApp.setClientWindowSize(winWidth, winHeight);
	}
}

function checkURL()
{
	try
	{
		var applocation = mainApp.getAppLocation();
	
		var brwlocation = null;
	
		if (browsername == "Internet Explorer")
		{
			var hf = parent.hiddenframe;
	
			brwlocation = homeURL;
	
			var hash = hf.document.getElementById("history");
	
			if (hash != null)
			{
				brwlocation += "#" + hash.value;
			} 
		}
		else
		{
			brwlocation = window.location.href;
			brwlocation = brwlocation.replace(/\s/g, "%20");
		}
	
		brwlocation = brwlocation.replace(/:[\/]*/, "://");
		applocation = new String(applocation).replace(/:[\/]*/, "://");

		if (applocation != brwlocation)
		{
			var hash = null;
	
			if (brwlocation != lastURL)
			{
				// Address bar was changed
				lastURL = brwlocation;
				var m = lastURL.match(/#(.*)/);
				
				if (m != null && m.length > 1)
				{
					hash = m[1];
	
					if (browsername == "Internet Explorer")
					{	
						window.location.hash = hash;
					}
					
					if (hash.length > 0)
					{
					   mainApp.navigateTo(hash);
					}
					else
					{
					   mainApp.navigateHome();
					}
				}
				else
				{
		  		   if (browsername == "Internet Explorer")
				   {	
					window.location.hash = "";
				   }
	
				   mainApp.navigateHome();
				}
	
			}
			else	
			{
				// Application navigation occurred
				lastURL = applocation;

				var m = lastURL.match(/#(.*)/);
	
				if (m != null && m.length > 1)
				{
					hash = m[1];
					
					window.location.hash = hash;
				
					if (browsername == "Internet Explorer")
					{
						var hf = parent.hiddenframe;
						hf.document.open();
						hf.document.write("<div id='history' value='" + hash + "'/>");
						hf.document.close();
						
					}
				}
				else 
				{
					mainApp.navigateHome();
				}	
			}
		}
	}
	catch(error)
	{
		window.clearInterval(timer);
	}
}


function determineClient()
{
	var browserType = navigator.appName;
	var userAgent = navigator.userAgent;
	
	if (userAgent.indexOf("Windows")!=-1)
	{
	 	os = "Windows";
	}
	else if (userAgent.indexOf("Mac")!=-1) 	
	{
		os = "MacOSX";
	}
	else if (userAgent.indexOf("Linux")!=-1)
	{
		os = "Linux";
	}
	else if (userAgent.indexOf("SunOS")!=-1) 	
	{
		os = "SunOS";
	}
	else
	{
		os = "Unknown";
	}

	if (browserType == "Microsoft Internet Explorer")
	{
		browsername = "Internet Explorer";
		browserversion = parseIeVersion();
		browser = browsername + " " + browserversion;
	}
	else if (userAgent.indexOf("Firefox") != -1)
	{
		parseBrowser("Firefox");
	}
	else if (userAgent.indexOf("Opera") != -1)
	{
		parseBrowser("Opera");
	}
	else if (userAgent.indexOf("Camino") != -1)
	{
		parseBrowser("Camino");
	}
	else if (userAgent.indexOf("Chrome") != -1)
	{
		parseBrowser("Chrome");
	}
	else if (userAgent.indexOf("Safari") != -1)
	{
		parseBrowser("Safari");
	}
	else if (userAgent.indexOf("Konqueror") != -1)
	{
		parseBrowser("Konqueror");
	}
	else if (userAgent.indexOf("Mozilla") != -1)
	{
		parseBrowser("Mozilla");
	}
	else
	{
		browser = "Unknown";
	}
	
	detectJRE();	
}


function parseBrowser(id)
{
	var userAgent = navigator.userAgent;
	var strings = userAgent.split(" ");
	var found = false;
	var bs = "";

	for (var i=0; i<strings.length && found == false; i++)
	{
		if (strings[i].indexOf(id) > -1)
		{
			bs = strings[i];
			found = true;
		}
	}

	if (found == true)
	{
		strings = bs.split('/');
		
		browsername = strings[0];
		browserversion = strings[1];
		browser = browsername + " " + browserversion;
	}
	
	return found;	
}


function parseIeVersion()
{
	var userAgent = 
		navigator.userAgent.substr(navigator.userAgent.indexOf("("), 
								   navigator.userAgent.indexOf(")"));
	var version = null;
	
	var strings = userAgent.split(";");
	
	for (var i=0; i<strings.length; i++)
	{
		if (strings[i].indexOf("MSIE") > -1)
		{
			version = strings[i].split(" ")[2];
		}
	}
	
	return version;
}

function detectJRE() 
{
   	checkPlugins()

	if (jre == null)
	{
	    var list = deployJava.getJREs();
	
		if (list.length > 0)
	    {
	    	jre = list[list.length-1];        
			parseJRE(jre);
	    }
   	}
}


function checkPlugins()
{
	numPlugins = navigator.plugins.length;
	
	if (numPlugins > 0)
	{
		for (i = 0; i < numPlugins; i++) 
		{
			var plugin = navigator.plugins[i];

			var n = plugin.name.match(/Java\(TM\).*/);

			if (jre == null && n != null && n.length > 0)
			{
				n = plugin.name.match(/Java\(TM\) Platform SE (\d+) U(\d+)/);

				if (n != null && n.length > 0)
				{
					jremaj = n[1];
					jremin = n[2];
					jre = "1." + jremaj + "." + jremin;
				}
				else
				{
					n = plugin.name.match(/Java\(TM\) Plug-in (\d+)\.(\d+)\.\d+_(\d+)/);
	
					if (n != null && n.length > 0)
					{
						jremaj = n[2];
						jremin = n[3];
						jre = "1." + jremaj + "." + jremin;
					}
				}
			}
		}
	}
}

function parseJRE(jre)
{
	jremaj = jre.split(".")[1];		
	temp = jre.split(".")[2];
	jremin = temp.split("_")[1];
}


