var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	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: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			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.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

function GameCoreWeb3DPlayer( widthTag, heightTag, contentTag)
{
    BrowserDetect.init();

	document.write("<div id=\"gc_plugin\" style=\"width: " + widthTag + "px; height: " + heightTag + "px; border: 0px;\">\n");

	if (DetectGameCoreWebPlayer())
	{
		if (BrowserDetect.browser == "Firefox" && BrowserDetect.OS == "Windows") {
			if (BrowserDetect.version >= 3) {
					document.write("<embed id=\"embed1\" type=\"application/heliosx-gamecore-plugin\" width=" + widthTag + " height=" + heightTag + " content=\"" + contentTag + "\"></embed>");
			}
			else {
					document.write("Please upgrade your version of Firefox to <a href=\"http://www.mozilla.com\">the latest verison.</a>");
			}
		}	
		else if (BrowserDetect.browser == "Explorer" && BrowserDetect.OS == "Windows") {
			if (BrowserDetect.version >= 7) {
					document.write("<OBJECT classid=\"CLSID:A42CFC06-46F5-4183-AB7C-BC79698190C5\" width=" + widthTag + " height=" + heightTag + ">\n");
					document.write("<PARAM name=\"type\" value=\"application/heliosx-gamecore-plugin\"/>\n");
					document.write("<PARAM name=\"content\" value=\"" + contentTag + "\"/>\n");
					document.write("</OBJECT>\n");
			} else {
				document.write("Please upgrade your version of Internet Explorer to <a href=\"http://www.microsoft.com/windows/internet-explorer/default.aspx\">the latest verison.</a>");
		
			}
			
		}	
		else if (BrowserDetect.browser == "Safari" && BrowserDetect.OS == "Windows") {
			if (BrowserDetect.version >= 4) {
					document.write("<OBJECT classid=\"CLSID:A42CFC06-46F5-4183-AB7C-BC79698190C5\" width=" + widthTag + " height=" + heightTag + ">\n");
					document.write("<PARAM name=\"type\" value=\"application/heliosx-gamecore-plugin\"/>\n");
					document.write("<PARAM name=\"content\" value=\"" + contentTag + "\"/>\n");
					document.write("</OBJECT>\n");
			} else {
				document.write("Please upgrade your version of Internet Explorer to <a href=\"http://www.microsoft.com/windows/internet-explorer/default.aspx\">the latest verison.</a>");
		
			}
		}
		else if (BrowserDetect.browser == "Chrome" && BrowserDetect.OS == "Windows") {
			if (BrowserDetect.version >= 4) {
					document.write("<OBJECT classid=\"CLSID:A42CFC06-46F5-4183-AB7C-BC79698190C5\" width=" + widthTag + " height=" + heightTag + ">\n");
					document.write("<PARAM name=\"type\" value=\"application/heliosx-gamecore-plugin\"/>\n");
					document.write("<PARAM name=\"content\" value=\"" + contentTag + "\"/>\n");
					document.write("</OBJECT>\n");
			} else {
				document.write("Please upgrade your version of Chrome to <a href=\"http://www.google.com/chrome\">the latest verison.</a>");
		
			}
			
		}	
		else if (BrowserDetect.browser == "Opera" && BrowserDetect.OS == "Windows") {
			if (BrowserDetect.version >= 9) {
						document.write("<embed id=\"embed1\" type=\"application/heliosx-gamecore-plugin\" width=" + widthTag + " height=" + heightTag + " content=\"" + contentTag + "\"></embed>");
			} else {
				document.write("Please upgrade your version of Opera to <a href=\"http://www.opera.com/\">the latest verison.</a>");
		
			}
			
		}	
		else if (BrowserDetect.browser == "Firefox" && BrowserDetect.OS == "Mac") {
			if (BrowserDetect.version >= 3) {
					document.write("<embed id=\"embed1\" type=\"application/heliosx-gamecore-plugin\" width=" + widthTag + " height=" + heightTag + " content=\"" + contentTag + "\"></embed>");
			}
			else {
					document.write("Please upgrade your version of Firefox to <a href=\"http://www.mozilla.com\">the latest verison.</a>");
			}
		}	
		else if (BrowserDetect.browser == "Safari" && BrowserDetect.OS == "Mac") {
			if (BrowserDetect.version >= 4) {
					document.write("<embed id=\"embed1\" type=\"application/heliosx-gamecore-plugin\" width=" + widthTag + " height=" + heightTag + " content=\"" + contentTag + "\"></embed>");
			} else {
				document.write("Please upgrade your version of Safari to <a href='http://www.apple.com/safari/'>the latest verison.</a>");
				
			}
		}	
		else  {
			if (BrowserDetect.OS == "Windows") {
				document.write("Please install the <a href=\"http://www.mozilla.com\">the latest version of Firefox</a> to use this application.");
			} else if (BrowserDetect.OS == "Mac") {
				document.write("Please install the <a href=\"http://www.apple.com/safari/\">the latest verison of Safari</a> to use this application");
			} else {
				document.write("Your browser is not supported by this plugin.");
			}
		}
	}
	else
	{
		document.write('<a href="http://www.web3d.com/downloads/download.html"><img src=http://www.web3d.com/downloads/Get_Web3D.png alt="Get GameCore Web 3D Player!" border="0" /></a>');
    	setTimeout('AutomaticReload()', 500)
	}
	document.write("</div>");
}


function DetectGameCoreWebPlayer () 
{
	var tPresent = false;
 if (BrowserDetect.browser == "Explorer" && BrowserDetect.OS == "Windows") 
//	if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1) 
	{
		//window.alert("inside explorer");
		
		// GEKIDO: Modified this ONE line of code to make the plugin work in IE
		tPresent = true; //DetectGameCoreActiveX();
	}
	else 
	{
		if (navigator.mimeTypes && navigator.mimeTypes["application/heliosx-gamecore-plugin"]) 
		{
			if (navigator.mimeTypes["application/heliosx-gamecore-plugin"].enabledPlugin && navigator.plugins && navigator.plugins["GameCore 2.0 Web Player"]) 
			{
				tPresent = true;	
			}
		}	
	}
	return tPresent;	
}

function AutomaticReload () 
{
	navigator.plugins.refresh();
	
	// do we have the plugin installed yet?
	if ( DetectGameCoreWebPlayer())
	{
		// plugin available, reload page to display the plugin
		window.location.reload();
	}
	else
	{
		// wait and check later
    	setTimeout('AutomaticReload()', 500)
	}
}
