// Version 3.0.7.33
var isIE4 = navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion.substring(0,1)) >= 4;
var isNN = (navigator.appName.indexOf("Netscape") != -1);
var isIE  = (navigator.appName.indexOf("Microsoft") != -1);
var agt=navigator.userAgent.toLowerCase();
var isWin = agt.indexOf('win') != -1;
var isMac = agt.indexOf('mac') != -1;
var mtsMIMEtype	= "application/x-mtx";
var mPN = "MetaStream 3 Plugin"; // mtsPluginName
var mtsActiveX = "AxMetaStream.MetaStreamCtl";
var _mtsinstalled = true;
var mtsTimer;
var cur_version;
var hFF = 0; // Hidden Form Flag
var NNtrigger = "1";
var propURL;
var _mtsGlobalNNString = "";

var _mtsconsole	= null;
var _mtsdebug = "notok";
var _mtsdefaultpid = "MetaStreamCtl";

var _mtsGlobalAltCont = "";
var static_redirector_flag = 0;
MTSPlugin.prototype.genid = 0;

var mts_global_url = mts_global_name = mts_global_prop = "";

if (isMac)
	propURL = "http://developer.viewpoint.com/download/vet_install_popup_mac.html";
else
{
	mts_global_url = "http://developer.viewpoint.com/download/vet_install_popup_ns.html";
	mts_global_name = "vet_install";
	mts_global_prop = "width=470,height=370,toolbar=no,location=no,resizable=no";
	propURL = "Javascript:MTSopenBrWindow(mts_global_url, mts_global_name, mts_global_prop)";
}

function MTSDebugger	(on)
{
	_mtsdebug = on ? "ok" : "notok";
}

function MTSConsole 	(msg)
{
	_mtsConsole();
}

function MTSopenBrWindow(url,name,feat)
{
	window.open(url,name,feat);
}

function IsMTSInstalled	()
{ 
	if (isIE4) 
	{ 
		if (isMac) 
		{ 
			mPN = "MetaStream3"; 
			_mtsinstalled = mimeAndPluginReady(mtsMIMEtype, mPN); 
		} 
		else
			_mtsinstalled = testAx();
	} 
	else 
	{ 
		if (isMac) 
			mPN = "MetaStream3";
		else 
			mPN = "MetaStream 3 Plugin";
		_mtsinstalled = mimeAndPluginReady(mtsMIMEtype, mPN); 
	} 
	return _mtsinstalled;
}

function testAx		()
{
	if( ie_detect(mtsActiveX) == 0)
		return true;
	else 
		return false;
}

function mimeAndPluginReady(mimetype,pluginName)
{
	for(var i = 0;i < navigator.plugins.length; i++)
	{
		if(navigator.plugins[i].name.toLowerCase() == pluginName.toLowerCase())
		{
			for (var j = 0 ; j < navigator.plugins[i].length; j++)
			{
				var mimeObj = navigator.plugins[i][j];
				if (mimeObj.enabledPlugin && (mimeObj.type == mimetype))
					return true;
			}
			return false;
		}
	}
	return false;
}

MTSPlugin.prototype.createParams = function (parmsIn)
{
	this.newParamString = "";
	var cMV = "50332936";
	var gMV = "50332496";
	this.LN = "none";
	var parms = "";
	if (parmsIn != null)
	{
		var tA  = parmsIn.split(";");
		for (var i=0; i<tA.length; i++)
		{
			if (tA[i] != "")
			{
				var tV = tA[i].split("=");
				re = / /g;
				tV[0] = tV[0].replace(re, "");
				if (tV[0].toLowerCase() == "genieminimumversion")
				{
					tV[1] = tV[1].replace(re, "");
					gMV=tV[1];
				}
				else if (tV[0].toLowerCase() == "componentminimumversion")
				{
					tV[1] = tV[1].replace(re, "");
					cMV=tV[1];
				}
				else if (tV[0].toLowerCase() == "layer")
				{
					tV[1] = tV[1].replace(re, "");
					this.LN = tV[1];
				}
				else if (tV[0].toLowerCase() == "nntrigger")
				{
					tV[1] = tV[1].replace(re, "");
					NNtrigger = tV[1];
				}
				else if (tV[0].toLowerCase() == "imagelink")
				{
					tV[1] = tV[1].replace(re, "");
					this.ImageLink = tV[1];
				}
				else if (tV[0].toLowerCase() == "basehref")
				{
					tV[1] = tV[1].replace(re, "");
					if (tV[1] == "1")
						this.newParamString += "basehref" + "=" + MTSGetBaseHREF() + ";";
				}
				else if (tV[0].toLowerCase() == "popup")
				{
					tV[1] = tV[1].replace(re, "");
					this.popUp = tV[1];
				}
				else
				{
					var idx = 1;
					var token1 = tV[idx-1];
					var token2 = tV[idx];
					idx++;
					while (tV[idx] != null)
					{
						token2 += "=" + tV[idx];
						idx++;
					}
					//re2 = /\"/g;
					//token2 = token2.replace(re, '----');
					parms += AddParm (token1, token2);
					this.newParamString += token1 + "=" + token2 + ";";
				}
			}
		}
	}
	parms += AddParm ("ComponentMinimumVersion", cMV);
	parms += AddParm ("GenieMinimumVersion", gMV);
	this.newParamString += "parentlocation=" + parent.location;
	return parms;
}

function AddParm (name, value)
{
	if (isIE && isWin)
		return MTSParm (name, value);
	else
		return (name + " = " + "'" + value + "' ");
}

function GetCookie (name)
{
	var mC = " " + document.cookie + ";";
	var sN = " " + name + "=";
	var sOC = mC.indexOf(sN);
	var eOC;
	if (sOC != 1)
	{
		sOC += sN.length;
		eOC = mC.indexOf (";", sOC);
		return (unescape (mC.substring(sOC, eOC)));
	}
}

function SetCookie (name, value)
{
	document.cookie = name + "=" + escape(value);
}

function ClearCookie (name)
{
	var eD = new Date ();
	eD.setTime (eD.getTime() - (3 * 24 * 60 * 60 * 1000));
	document.cookie = name + "=ImOutOfHere; expires=" + eD.toGMTString();
}

function MTSGetBaseHREF ()
{
	var tempVar = "mtsEmpty.html";
	var linkCount = document.links.length;
	document.write("<a href='"+ tempVar +"'></a>");
	var theLink = document.links[linkCount].toString();
	if (tempVar == theLink)
		theLink = "";
	else
	{
		var re = /mtsEmpty.html/g;
		theLink = theLink.replace(re,"");
	}
	return theLink;
}

function MTS_IsString(valP)
{
	if (valP == "")
		return true;
	for (var i=0; i<valP.length; i++)
	{
		if (((valP.charAt(i) < "0") || (valP.charAt(i) > "9")) && (valP.charAt(i) != ".") && (valP.charAt(i) != "-"))
			return true;
	}
	return false;
}

function MTSWrapValue (value)
{
	var returnString = value.toString();
	if (MTS_IsString(returnString)) 
		returnString = "'" + returnString + "'";
	return returnString;
}

function MTSPlugin(file, width, height, bkey, altcont, parms)
{
	if (altcont.toLowerCase() == "classic") altcont = "none";
	var str = "none";
	this.name = "MetaCtl" + MTSPlugin.prototype.genid.toString();
	MTSPlugin.prototype.genid++;
	this.LN = "none";
	this.ImageLink = "0";
	var parms = this.createParams(parms);
	if((altcont.length!=0) && (!IsMTSInstalled()))
		str = altcont;
	if ((NNtrigger == "1") && (cur_version == null))
		TriggerJS();
	if ((((str.toLowerCase() == "none") && (!isMac)) || ((GetCookie(this.name) == "1") && (!isMac))) && ((!(this.popUp == "1")) || (str.toLowerCase() == "none"))  || (IsMTSInstalled()) )
	{
		if(isWin && isIE)
		{
			var ch;
			if (file != "")ch=MTSParm("Source", file);
			ch += MTSParm("Component","ISceneComponent")+ MTSParm("ComponentFileName","SceneComponent.mtc")+ MTSParm("BroadcastKeyFileURL",bkey);
			ch = ch + parms;
			ch += MTSParm ("properties", this.newParamString);
			var cbURL = "https://components.viewpoint.com/MTSInstallers/MetaStream3.cab" + "?url=" + window.location.href + "#Version=3,0,2,62"; 
			str = MTSMarkup("object", ch, "id", this.name, "classid", "CLSID:03F998B2-0E00-11D3-A498-00104B6EB52E", "width", width, "height", height, "codebase", cbURL);
			if(_mtsdebug == "ok") MTSConsole(str, this.name);
			document.write (str);
		}
		else
		{
			
			var tag;
			if ((isMac) && (isNN) && (!IsMTSInstalled())) 
				tag = "PLUGINSPAGE";
			else if ((isMac) && (isIE) && (!IsMTSInstalled())) 
				MTSJumpToRedirector (this.LN);
			else 
				tag = "PluginURL";
			str = MTSMarkup("embed","<properties='"+ this.newParamString +"'>","component","ISceneComponent","componentfilename","SceneComponent.mtc","source",file, "type","application/x-mtx","width",width,"height",height,"border",true,"script",true,"name",this.name, tag, propURL, "BroadcastKeyFileURL",bkey, parms);
			if (this.LN != "none")
			{
				if (isIE)
				{
					if(_mtsdebug == "ok") MTSConsole(str, this.name);
					document.write (str);
				}
				if (!isIE)
				{
					_mtsGlobalNNString = str;
					//alert (_mtsGlobalNNString);
					var tempVar2 = "document."+this.LN+".document.write(_mtsGlobalNNString);";
					if(_mtsdebug == "ok") MTSConsole(tempVar2, this.name);
					eval (tempVar2);
				}
			}
			else
			{
				if(_mtsdebug == "ok") MTSConsole(str, this.name);
				document.write (str);
			}
			
			if (GetCookie(this.name+"Trigger") == "1")
			{
				ClearCookie (this.name+"Trigger");
				if (this.popUp != '1')
					TriggerJS();
			}
		}
		if (this.ImageLink == "1")
			ClearCookie (this.name);
	}
	else
	{
		if ( (this.ImageLink != "1" && this.popUp == "1") || ((GetCookie(this.name) == "1") && (this.popUp == "1")) || (str == "popUp") || ( isMac && ((str == "none") || (GetCookie(this.name) == "1")))) 
		{
			if (this.ImageLink == "1")
				ClearCookie (this.name);
			if ((altcont != "none") && (altcont != "popUp"))
			{
				_mtsGlobalAltCont = altcont;
				if ((isNN) && (this.LN != "none")) 
				{
					var outStr = "document."+this.LN+".document.write(_mtsGlobalAltCont);";
					if(_mtsdebug == "ok") MTSConsole(outStr, this.name);
					eval (outStr);
				}
				else 
				{
					if(_mtsdebug == "ok") MTSConsole(altcont, this.name);
					document.write (altcont);
				}
			}
			MTSJumpToRedirector (this.LN);
		}
		else
		{
			
			if (this.ImageLink == "1")
			{
				str = MTSMarkup("A", str, "HREF", "JavaScript:mtsClick();");
			}
			_mtsGlobalAltCont = str;
			if ((isNN) && (this.LN != "none")) 
			{
				var outStr =  "document."+this.LN+".document.write(_mtsGlobalAltCont);";
				if(_mtsdebug == "ok") MTSConsole(outStr, this.name);
				eval (outStr); 
			}
			else 
			{
				if(_mtsdebug == "ok") MTSConsole(str, this.name);
				document.write (str);
			}
		}
	}
	if (isMac && isIE)
	{
		if (hFF == 0)
		{
			document.write ("<div id='HiddenLayer' style='position:absolute; left:-1000px; top:-1000px; width:0px; height:0; z-index:0; visibility: hidden'><form name='MTS3'><input type='text' name='mts3_js_jsfield'></form></div>");
			mtsTimer = setInterval ('ExecValue()', 3);
			hFF = 1;
		}
	}
}

function MTSJumpToRedirector (layerName)
{
	if (static_redirector_flag < 1)
	{
		static_redirector_flag ++;
		var mtsTemp = "var mtsFStr = 'left=' + Math.round((screen.availWidth - 470) / 2) + ',top=' + Math.round((screen.availHeight - 370) / 2) + ',width=' + 470 + ',height=' + 370 + ',toolbar=no,location=no,resizable=no'; var handle = window.open('http://developer.viewpoint.com/download/redirector.html','install_win',mtsFStr); if (isIE) var mtsWindowTimer = setInterval ('mtsChangeStatus()', 500); var MTSTimerFlag=0; function mtsChangeStatus () { if (MTSTimerFlag == 0) {handle.status = window.location.href; MTSTimerFlag++;}}";
		var mtsWS = MTSMarkup("script", mtsTemp, "language", "javascript");
		if(_mtsdebug == "ok") MTSConsole(mtsWS);
		
		if ((isNN) && (layerName != "none")) 
			window.open('http://developer.viewpoint.com/download/redirector.html','install_win', 'width=470,height=370,toolbar=no,location=no,resizable=no');
		else 
			document.write (mtsWS);
	}
}

function mtsClick ()
{
	var tempName ="";
	for (var i=0; i < MTSPlugin.prototype.genid; i++)
	{
		tempName = "MetaCtl" + i.toString();
		SetCookie(tempName, '1');
		if ((isNN)) SetCookie(tempName+"Trigger", '1');
	}
	location.reload();
}

function ExecValue ()
{
	var value = document.MTS3.mts3_js_jsfield.value;
	if (value != "")
	{
		eval(value);
		document.MTS3.mts3_js_jsfield.value = "";
	}
}

function TriggerJS()
{
	NNtrigger = "0";
	var src_path = "https://components.viewpoint.com/MTSInstallers/MetaStream3.jar" + "?url=" + window.location.href;
	var cur_version = 2;
	if (navigator.appName == "Netscape" && parseInt(navigator.appVersion.substring(0,1)) >= 4 && navigator.platform == "Win32" && navigator.javaEnabled())
	{
		trigger = netscape.softupdate.Trigger; if(trigger.UpdateEnabled())
		{
			do_update = true;
			if ( (mimeType = navigator.mimeTypes["application/x-mtx"]) != null && (plugin = mimeType.enabledPlugin) != null)
			{
				descr_str = String(plugin.description);
				lastind = descr_str.lastIndexOf("r");
				if(lastind >=0 )
				{
					vers = parseInt(descr_str.substring(lastind+1).toString());
					if(!isNaN(vers) && vers >= cur_version)
						do_update = false;
				}
			}
			if(do_update)
			{
				trigger.StartSoftwareUpdate(src_path, trigger.DEFAULT_MODE);
			}
		}
	}
}

MTSPlugin.prototype.Console = function(msg)
{
	_mtsConsole(msg, this._mtsPID());
}

MTSPlugin.prototype.Execute = function()
{
	return this._mtsCall();
}

MTSPlugin.prototype.SetProperty =function(name, prop, value, type)
{
	return this.Execute("SetProperty ", name, prop, MTSWrapValue(value), type);
}

MTSPlugin.prototype.GetProperty =function(name, prop, type)
{
	return this.Execute("GetProperty ", name, prop, type);
}

MTSPlugin.prototype.TriggerAnim	= function(anim)
{
	return this.Execute("TriggerAnimation", _anim(anim));
}

MTSPlugin.prototype.ReverseAnim = function(anim)
{
	return this.Execute("ReverseAnimation", _anim(anim));
}

MTSPlugin.prototype.StopAnim	= function(anim)
{
	return this.Execute("StopAnimation", _anim(anim));
}

MTSPlugin.prototype.StartAnim	= function(anim)
{
	return this.Execute("StartAnimation", _anim(anim));
}

MTSPlugin.prototype.ResetAnim	= function(anim)
{
	return this.Execute("ResetAnimation", _anim(anim));
}

MTSPlugin.prototype.ToggleCollapse = function(name)
{
	return this.Execute("ToggleCollapsed",_inst(name));
}

MTSPlugin.prototype.SetCollapsed = function(name, value)
{
	return this.SetProperty(_inst(name), 'clps',MTSWrapValue(value), 'mts_int');
}

MTSPlugin.prototype.ToggleVisible = function(name)
{
	return this.Execute("ToggleVisible", _inst(name));
}

MTSPlugin.prototype.SetVisible = function(name, value)
{
	return this.SetProperty(_inst(name), 'visb', MTSWrapValue(value), 'mts_int');
}

MTSPlugin.prototype.ClearScene = function()
{
	return this.Execute("ClearScene");
}

MTSPlugin.prototype.TogglePano = function(value)
{
	return this.Execute("TogglePano", MTSWrapValue(value));
}

MTSPlugin.prototype.Render = function()
{
	return this.Execute("Render");
}

MTSPlugin.prototype.LoadMTX = function(path)
{
	return this.Execute ("LoadMTX", path);
}

MTSPlugin.prototype.ResetCamera	= function()
{
	return this.Execute ("ResetCamera");
}

MTSPlugin.prototype.GetVer	= function(comp)
{
	return this.Execute ("GetVersion", comp);
}

function MTSMarkup (tag, children)
{
	var a=arguments;
	if(a.length == 0)
		return "";
	var str = "<" + a[0];
	var i;
	for(i = 2; i < a.length; i+=2)
	{
		if (a[i+1] != null)
			str += (" "+a[i]+"=\'"+a[i+1]+"\'");
		else
			str += " " + a[i];
	}
	str += (children.length ? (">"+children+"</"+a[0]+">") : ("/>"));
	return str;
}

function MTSParm(name, value)
{
	return MTSMarkup("param","","name", name, "value", value.toString());
}

MTSPlugin.prototype._mtsPID = function()
{
	return this.name;
}

function _mtsConsole()
{
	if(_mtsdebug != "ok")
		return;
	var arg = (arguments.length == 0) ? arguments.caller : arguments;
	var count=arg.length;
	if(count == 0)
		return;
	var msg = arg[0].toString();
	var pname = (count == 1) ? "Default":((arg[1].constructor == MTSPlugin) ? arg[1].name : arg[1].toString());
	if(_mtsconsole == null)
	{
		_mtsconsole = window.open("", "console", "width=300,height=300,resizable,scrollbars");
		_mtsconsole.document.open("text/plain");
		_mtsconsole.focus();
	}
	if(!_mtsconsole.closed)
		_mtsconsole.document.writeln(pname+"> "+msg);
}

function isVPError(inStr)
{
	var pos = -1;
	var lstpos = -1;
	var retVal = 0;
	pos = inStr.indexOf("VP_ErrorCode");
	if(pos>-1)
	{
		pos = inStr.indexOf("(");
		lstpos = inStr.indexOf(")");
		if(pos + lstpos < 0) retVal = -1;
		valueString = inStr.substring(pos+1,lstpos);
		retVal = parseInt(valueString);
	}
	else
		retVal = 0;
	return retVal;
}

function _anim(name)
{
	return name.indexOf('MTSAnimator.') == 0 ? name : 'MTSAnimator.'+name;
}

function _inst(name)
{
	return name.indexOf('MTSInstance.') == 0 ? name : 'MTSInstance.'+name;
}

function _geom(name)
{
	return name.indexOf('MTSGeometry.') == 0 ? name : 'MTSGeometry.'+name;
}

function _mat (name)
{
	return name.indexOf('MTSMaterial.') == 0 ? name : 'MTSMaterial.'+name;
}

MTSPlugin.prototype._mtsCall = function ()
{
	var args = (arguments.length == 0) ? arguments.caller : arguments;
	var count=args.length;
	count++;
	if(count < 2)
	{
		alert('too little arguments for functions call');
		return false;
	}
	var argstr = args[0] + '(';
	var last=count-2;
	var i;
	var lasti=count-1;
	//var pname = args[lasti];
	var pname = this._mtsPID();
	for(i=1;i<lasti;i++)
	{
		argstr+= args[i];
		if(i<last)
			argstr+= ',';
	}
	argstr+=')';
	var pID = pname;
	var replace = /MetaCtl/i;
	pID = pID.replace (replace, "");
	var result;
	if (isMac && isIE)
	{
		var macStr = "MTS3_JS" + pname + ":" + (argstr) + ";";
		var executeString = 'document.MTS3.mts3_js_jsfield.value = macStr;';
		eval(executeString);
		result=document.MTS3.mts3_js_jsfield.value;
		document.MTS3.mts3_js_jsfield.value = "";
		if(_mtsdebug == "ok")
			MTSConsole(macStr);
	}
	else
	{
		var pc;
		if (isWin && isIE)
			pc = 'document.all.'+pname+'.PluginCommand(\"'+argstr+'\",0, 0);';
		else
		{
			if (this.LN == "none")
				pc = 'document.'+pname+'.DoCommand(\"'+argstr+'\");';
			else
				pc = 'document.'+this.LN+ '.document.embeds.' +pname+'.DoCommand(\"'+argstr+'\");';
		}
		result = eval (pc);
		if (_mtsdebug == "ok")
			MTSConsole(argstr, pname);
	}
	return result;
}