//#####################################################
//# The Viewpoint MTS3Interface												#
//# ------------------------------------------------- #
//# This file makes creating and interacting with a   #
//# Viewpoint VET object generic across OS/Platform.  #
//# ------------------------------------------------- #
//# Original Author: Ales Holecek, 1999/2000          #
//# Maintained by  : Derek Davies, 2001-2004          #
//#                  Jeremy Young, 2005-2006          #
//#                  Derek Davies, 2007               #
//#                  Daniel/Daimen 2007               #
//# ------------------------------------------------- #
//# Last Change : Derek Davies, May 2007              #
//#               Major overhaul/optimization, add    #
//#               full comments, new OSX plugin,      #
//#####################################################
//#####################################################
//
//Note:  This is the verbose/commented version. You can use this version while you
//       are developing/testing if you choose. The only thing we do to this file to
//       produce the uncommented version, is to find all instances of // and delete to
//       end of line. Other than that, there's no other changes.
//Note2: URL's that appear in this file have their two // characters broken up to
//       avoid being seen as a comment and getting destroyed e.g.: "http:/"+"/blabla"
//
VET_IfVer="05.00.02.29";
//
//Legacy variables. These are vars that we no longer use in the regular operation of
//the MTS3Interface. They've been moved to this section and commented out. Normally
//we would just delete them, but considering this is a public file, we don't know if
//some sites have made use of them. So we leave them here, commented-out. Uncomment
//any that you need. It would be a good idea to stop using them because we may finally
//completely remove them from the interface in a future version.
//
//VET_Java=navigator.javaEnabled();
//VMPSupport;
//VET_StartGid=0;
//VET_Premium=true;
//VET_Hff=1;
//VET_NN4=VET_NN&&VET_Bv==4;
//VET_NN6=VET_NN&&(parseInt(navigator.vendorSub)==6);
//VET_CS=VET_Ag.indexOf('cs 2000 7')!=-1;
//VET_AppVer=parseFloat(navigator.appVersion);
//VET_NNtrgr="0";
//VET_Cv=0;
//if(!window.VET_WindowID)VET_WindowID=(new Date()).getTime()+"_"+Math.floor(Math.random()*10000)
//VET_Mili=function(){var v=new Date();return v.getTime()}
//VET_Rand=function(f){return Math.floor(Math.random()*f);}
//VET_Tr="Trigger";

VET_ch=VET_Mc=null;
VET_Ap=navigator.appName;
VET_Bv=parseInt(navigator.appVersion.substring(0,1));
VET_Ag=navigator.userAgent.toLowerCase();
VET_IE=VET_Ag.indexOf("msie")!=-1&&(VET_Bv>=4)&&VET_Ag.indexOf("opera")==-1;
VET_IE6=(VET_IE&&(VET_Ag.charAt(VET_Ag.indexOf('msie')+5)*1>=6));//require at least IE6 now
VET_Firefox=VET_Ag.indexOf('firefox')!=-1;
//VET_CS7 = no need to sniff for Compuserve 2000 v7 as it will match VET_Firefox
VET_Safari=((VET_Ag.indexOf("safari/")!=-1)&&(parseInt(VET_Ag.substring(VET_Ag.indexOf("safari/")+7,VET_Ag.length))>=100));
VET_NN=VET_Ap.indexOf("Netscape")!=-1;
VET_Gecko=VET_Ag.indexOf('gecko')!=-1;
VET_AOL=VET_Ag.indexOf('aol')!=-1&&(VET_Ag.charAt(VET_Ag.indexOf('aol')+4)*1>=7);
VET_NN7=(VET_NN&&(parseInt(navigator.vendorSub)>=7));
//VET_NN8 = no need to sniff for Netscape 8. In IE mode it'll match VET_IE6 and in FF mode it'll match VET_Firefox
VET_XP=VET_Ag.indexOf("nt 5.1")!=-1;
VET_Vista=VET_Ag.indexOf("nt 6.")!=-1;
VET_Win=navigator.platform=="Win32";
VET_Mac=VET_Ag.indexOf('mac')!=-1;
//Default GUID is used first in all cases
//store the GUID we'll use in the VET_AxID variable
VET_AxID="03F998B2-0E00-11D3-A498-00104B6EB52E";
//This alternate GUID can be used if primary not installed, but only if VET_AltPlugin==1
VET_AxSecID="1B00725B-C455-4DE6-BFB6-AD540AD427CD";
VET_Mim="application/x-mtx";//default it to mtx
VET_Dbg=VET_Sdr=0;
VET_AltPlugin=0;
VET_TopURL="";
VET_Dv="http:/"+"/www.viewpoint.com/installer/";//default to viewpoint neinstaller directory
VET_Re=VET_Dv+'index.html?';
VET_Rtyp=1;
VET_Ic="ISceneComponent";
VET_Cf="ComponentFileName";
VET_Sc="SceneComponent.mtc";
VET_Bk="BroadcastKeyFileURL";
//minimum version of VMP Host required to work with Vista (any earlier versions
//of the VMP will have problems on Vista)
VET_VistaHostMin=50659328;

// Safari & CSS: Safari won't instantiate us now if there's css includes
// beneath us, which are not cached (IOW the DOM is not valid yet).  So, We're
// going to have to check again before "unsupported"... link, redirection, etc.
// Because we may acutally be running... and we don't want the NEInstaller
// popping up on us accidentally, and I'd rather not reload if we don't have to.
VET_PageLoaded = false;
VET_CheckAgainAfterPageLoad = false;
VET_MTSUnsupported = function()
{
    VET_PageLoaded = true;
    MTSUnsupported();
}
//if (VET_Safari && document.addEventListener)
//    document.addEventListener("DOMContentLoaded", VET_MTSUnsupported, false);

//kick off the starting sequence number for unique instance names on this page
//this is used in conjunction with the string "MetaCtl" to produce unique names
//for each instance, e.g., MetaCtl0, MetaCtl1, MetaCtl2 etc. This was a random
//number during the lifetime of the custom OSX scripting method, now it can be
//returned to a simple incrementing number starting at zero as it used to be.
MTSPlugin.prototype.genid=0;

//callback function so VMP can alert interface if a component required is not available
RequiredVersionsUnsupported=function(ctlName)
{
	var reloadPage = true;
	var vmpObj = document.getElementById(ctlName);
	if (vmpObj != null)
	{
		if (VET_Win && VET_IE)
		{
			if (typeof vmpObj.PluginCommand != "undefined")//how can we be at this point and it not be valid?!
			{
				//only redirect if the content GUID is not primary GUID in the current player
				//(eg: only if content == viewpoint AND vmp == WL)
				//viewpoint vmp may have two "primary guids" so it can be at 2nd place too
				var guid = vmpObj.PluginCommand("GetResourceString('GUID')", 0 ,0);
				if (guid != "" && guid.indexOf(VET_AxID) != -1 )
				{
					//just unhide the scene in case parts of scene were loaded!
					vmpObj.PluginCommand("HideScene(false)", 0 ,0);
					reloadPage = false;
				}
			}
		}
		else
		{
			if (typeof vmpObj.DoCommand == "function")//how can we be at this point and it not be valid?!
			{
				//only redirect if the content MIME is not primary MIME in the current player
				//(eg: only if content == viewpoint AND vmp == WL)
				//we dont care about IE because we know we will always call the right player, 
				//assume that primary MIME is listed first (unlike GUID, see above)
				var supportedMimes = vmpObj.DoCommand("GetResourceString('MIMEType')");
				if (supportedMimes != "" && supportedMimes.indexOf(VET_Mim) == 0)
				{
					//don't do anything if the MIME matches the MIME of the player, because then the newly installed player would probably mess up also!
					//just unhide the scene in case parts of scene were loaded!
					vmpObj.DoCommand("HideScene(false)");
					reloadPage = false;
				}
			}	
		}
	}

	if (reloadPage == false)
	{
		return;
	}

	//either redirect to NEinstaller, or write link to page, as if VMP is not installed
	//note: only do this if the mtx requested does not match the primary mime of the player
	//that failed, eg, only if mime is mtx, and player is non-viewpoint 
    MTSUnsupported();
}

//this function checks if we can detect this OS to be MAC OS X
Check_OSX=function()
{
	//if not even a mac, then it certainly can't be mac os x can it
	if(!VET_Mac)
		return false;
	//safari and other gecko browsers will indicate OS X with "mac os x")
	if(VET_Ag.indexOf("mac os x")!=-1)
		return true;
	//Mac/IE doesn't indicate OSX in the useragent, but it does have a unique
	//plugin called "default plugin carbon.cfm" in its navigator plugins array
	//which you don't get on OS8/9 so it must be OSX :)
	var n = navigator.plugins;
	if(n)
	{
		for(var i = 0; i < n.length; i++)
		{
			if(n[i].name.toLowerCase() == "default plugin carbon.cfm")
				return true;
		}
	}
	
	//none of the methods of indication worked, this must be Mac OS8 or 9
	return false;
}

//Put the result of a call to Check_OSX into a global variable. This means we
//won't have to repeatedly follow the logic inside the function. We do it once.
VET_MacOSX=Check_OSX();

//actively check if the VMP is currently installed by trying to create an instance of it
//if you want to skip this check (for example if you're debugging the VMP itself) then
//after you include MTS3Interface, just put VMPInstalled=function(){return true;} in a
//script block. That will override this function completely.
//An alternate method of skipping the VMP sniffing instance is to add MTS3Debug=true
//to the query string for the page.
VMPInstalled=function()
{
	//if we're in debug mode, don't even check, just set VET_IsIn to true
	if(window.location.search.indexOf("MTS3Debug=true") > -1)
		VET_IsIn = true;
	//if we didn't already check if VMP is installed, then check now
	if(typeof window.VET_IsIn == "undefined" || window.VET_IsIn == null)
		VET_IsIn = (VET_Win && VET_IE) ? VET_Ax() : VET_Np();
	//the global is now set, return it also for anyone checking the return code
	return VET_IsIn;
}

VMPSupported=function()
{
	//TODO: Should we just return true hard-coded here and let vmpSupport.js override this if it has any major live issues?
	if(VET_Win)
		return (VET_IE6 || VET_Firefox || VET_NN7 || VET_AOL);
	if(VET_MacOSX)
		return (VET_IE6 || VET_Firefox || VET_Safari);
	return false;
}

//Message that we'll alert if an attempt at running the install is done
//without an internet connection. In reality it just means that the
//vmpSupport.js file wasn't yet loaded.
VET_Local='You are not online.\n\nViewpoint Media Player can not auto-install without an internet connection.';

//The three install functions that use the above VET_Local. These functions are
//stubs really. The Interface itself doesn't offer this functionality, these
//functions will be overridden by vmpSupport.js ("the online file") with their
//full functionality (which won't use VET_Local at all). Details of the functions
//and what they do is to be found in the vmpSupport.js file itself.
VMPInstallWithFrame=function(){alert(VET_Local);}
VMPInstallWithPopup=function(){alert(VET_Local);}
VMPInstallURL=function(){return "javascript:alert(VET_Local)";}

//Pretty superfluous function to set the VET_Dbg global, kept for compatibility
MTSDebugger=function(i){VET_Dbg=i;}

//Adaption function that used to be more active, now it just makes the call to the
//implementation function. This would be deleted but it's kept for compatibility
MTSConsole=function(p1,p2){_mtsConsole.apply(this,arguments);}

//another function that is fairly superfluous but is kept for compatibility
MTSopenBrWindow=function(u,n,f){VET_ch=window.open(u,n,f);setTimeout("VET_Ci(1)",2000);}

//This function creates a plugin on a DIV layer for non Windows/IE browsers.
//That plugin is later scripted into to determine if it's a good VMP install
//The function takes a boolean. True means create the plugin on a DIV, false
//means delete it.
//don't change this variable to c.  If you do, you either need to read the code (bad) or put in an unecessary comment (which must be
//then accessed from different places in code), so even with comments stipped out, it's not good
//return null if DoCommand/PluginCommand (for IE) is not defined
VET_TestPlugin=function(create, axID)
{
	if(create)
	{
		//we really want to only use object as it is part of the standard.  but maybe old NN need embed?
		var typeOfElement = "embed";
		if (VET_IE && !VET_Mac)
			typeOfElement = "object";

		if (axID == null)
			axID = VET_AxID;

		try
		{
			//don't try and create vmpTest a second time, check if it already exists
			var vmpTestObj = document.getElementById("vmpTest");
			if (vmpTestObj == null)
			{
				//create the div and put an embed tag on it
				var d = document.createElement("div");
				d.setAttribute("id","vmpTempDiv");
				d.setAttribute("style","position:absolute;top:1px;left:1px;z-index:1000;");
				document.body.appendChild(d);
				var strInnerHTML = "";
				if (typeOfElement == "embed")
					strInnerHTML = '<embed component="' + VET_Ic + '" componentfilename="' + VET_Sc + '" type="' + VET_Mim + '" name="vmpTest" id="vmpTest" TestOnly="true" width="2" height="2" script="true" VMPClassID="{' + axID + '}"></embed>';
				else if (typeOfElement == "object")
				{
					var ch = MTSParm("Component",VET_Ic) + MTSParm(VET_Cf,VET_Sc) + MTSParm("properties","TestOnly=true");
					strInnerHTML = MTSMarkup("object", ch, "name", "vmpTest", "id", "vmpTest", "classid", ("CLSID:" + axID), "width", 2, "height", 2);
				}
				
				document.getElementById('vmpTempDiv').innerHTML = strInnerHTML;
				vmpTestObj = document.getElementById('vmpTest');
				
				//check for scripting capability
				//if not available, then just set the vmp var to null, as we are returning that var from this func, so we can tell if
				//valid, that way.
				if (vmpTestObj != null)
				{
					if (VET_Win && VET_IE)
					{
						if (typeof vmpTestObj.PluginCommand == "undefined")
							vmpTestObj = null;
					}
					else
					{
						if (typeof vmpTestObj.DoCommand != "function")
                            if (VET_MacOSX && VET_Safari && !VET_CheckAgainAfterPageLoad) VET_CheckAgainAfterPageLoad = true; // dd: css issue
                            else vmpTestObj = null;
					}
					
					
					if (VET_Vista && vmpTestObj && !IsHostSupported(vmpTestObj))
					{
						vmpTestObj = null;
					}
				}
			}
		}
		catch(e)
		{
			vmpTestObj = null;
		}
		return (vmpTestObj != null);	
	}
	else
	{
		if(typeof document.getElementById("vmpTempDiv") == "object")
		{
			var t = document.getElementById("vmpTempDiv");
			//remove the DIV from the DOM, let's overwrite the innerHTML first just in case we fail to remove the DIV
            try // I've seen this fail
            {
                t.innerHTML = "";
                t.parentNode.removeChild(t);
            }
            catch (e)
            {
                // not sure what to do here
            }
		}
	}
}

//used by VET_Np and VET_Ax, on Vista, to weed out pre-3.5 hosts
IsHostSupported=function(vmpObject)
{
	//valid object?
	if (vmpObject == null || vmpObject == "undefined")
		return false;

	//vista?
	if (!VET_Vista)
		return true;
		
	var vr = 0;
	if (VET_IE)
	{
		if (typeof vmpObject.PluginCommand != "undefined")
		{
			vr = vmpObject.PluginCommand('GetVersion(Host)', 0, 0);
			//IE Hosts 3.5.0.13 and below *may* return 0 if scene component is there and is loaded by the time this call occurs
			//first, create test vmp old way, which seems to have no problem. if that fails, query SC for version
			if (vr == 0)
			{
				try
				{
					//hosts above 3.5.0.13 should not need to come here. which means, we don't need WL stuff, as there are no
					//old wl hosts!
					var vmpObject2 = new ActiveXObject("AxMetaStream.MetaStreamCtl");
				}
				catch(e)
				{
					if (e.description.indexOf("Automation") != -1)
					{
						vmpObject2 = null;
					}
				}
				
				if (vmpObject2 != null)
				{
					vr = vmpObject2.PluginCommand('GetVersion(Host)',0,0);
					vmpObject2 = null;//destroy temp object
				}
			}
			if (vr == 0)
			{
				//this works for WL too as the function checks the 'internal' name as well, which is constant and is the viewpoint one
				//anyways, if we were instanciated with WL guid, and plugincommand is valid, that means that we are WL, and there are no WL hosts with this problem
				vr = vmpObject.PluginCommand('GetVersion(SceneComponent.dll)', 0, 0);
			}
		}
	}
	else
	{
		if (typeof vmpObject.DoCommand == "function")
			vr = vmpObject.DoCommand('GetVersion(Host)');
	}

	return ((vr == "No base control") ? false : (vr >= VET_VistaHostMin));
}

//this function checks if there's a VMP plugin installed for all except Windows/IE.
//DANIEL: i don't think we need to loop thru plugins...just try to write the plugin and see it succeeds
//this will save us hardcoding (for viewpoint) and passing in (for white label) the plugin name...
VET_Np=function()
{
	var retVal = false;
	//if Gecko, try checking if this is a scriptable plugin
	if(VET_Gecko && (document.body != null))
	{
     //   if (typeof navigatgor != "undefined" && navigator.plugins && navigator.plugins.refresh)
     //       navigator.plugins.refresh(false);  // we migt have just been installed, double check

        // on Mac Safari we're going to itterate through the mime types,
        // the polling causes the browser to prompt when we create embeds.
        // We should probably deprecate the polling method
        // in favor of having the plugin call a callback on us.
        var found = false;
        if (VET_MacOSX && VET_Safari && typeof navigator != "undefined" && navigator.plugins) 
        {
            for (var i = 0; i < navigator.plugins.length; i++)
            {
                    for (var j = 0; j < navigator.plugins[i].length; j++) 
                    {
                        var mim = navigator.plugins[i][j];
                        if (mim.enabledPlugin && (mim.type == VET_Mim))
                        {
                            if (navigator.plugins[i].name.toLowerCase() != "metastream3")  // dd: here we force old mac plugin to install new one!!!
                                found = true;
                        }
                }
            }
        }

        if (!(VET_MacOSX && VET_Safari) || found)
        {
            retVal = VET_TestPlugin(true);   
            if (retVal == true)
            {
                //in half a second delete the VMP instance we used for sniffing
                //ver presumably this was causing problems when done immediately
                setTimeout('VET_TestPlugin(false)',500);
            }
            else
            {
                //plugin object wasn't successfully created, kill layer
                VET_TestPlugin(false);
            }   
        }
	}
	else if(VET_MacOSX && VET_IE6)//not gecko so can't check VMP ver
	{
		retVal = false;
	}
	else
	{
		retVal = true;
	}
	
	return retVal;
}

//This function checks if the activeXcontrol can be instantiated on Windows/IE browsers
VET_Ax=function()
{
	var retVal = VET_TestPlugin(true);
	
	//if it didn't, then we'd normally be ready to return false. First though, make a check
	//whether the alt (secondary) plugin variable is 1. If it is, we can try and use that one.
	//If we can use it, we have to ensure VET_AxID switches to the correct GUID so the constructor
	//uses the right number.
	if (retVal == false && VET_AltPlugin == 1)
	{
		VET_TestPlugin(false);
		retVal = VET_TestPlugin(true, VET_AxSecID);		
		
		if (retVal)
			VET_AxID = VET_AxSecID;
	}
	
	VET_TestPlugin(false);
	return retVal;
}

//This legacy function reports whether the VMP is installed or not.
//This used to be a function that called another function, now it's just a pointer
//NOTE: -  overriding this in your test page to IsMTSInstalled=function(){return true;}
//		   right after you include the MTS3Interface, would totally bypass the
//		   sniffing VMP instance being created.
IsMTSInstalled=VMPInstalled;

//This function parses the parameters that are grouped into param 6 of the constructor call
MTSPlugin.prototype.createParams=function(p)
{
	this.props="";
	var cMV="50333440",gMV="50333440",hMV="50333440",parms="";
	//parse the xmltext parameter and put it into the xmltext tag and then remove from the main param 6 string
	if(p!=null)
	{
		//xso = offset of xmltext
		var xso=p.indexOf("xmltext");
		//if xmltext found in param 6 of constructor call
		if(xso!=-1)
		{
			//op (original param 6 string) = p
			//xs (xmlstart) = p.indexOf xmltext + 7
			var op=p,xs=xso+7;
			//starting parsing after the xmltext keyword and white space
			//loop till a "<" is found in the string, setting xs (xmlstart)
			while((xs<p.length)&&(p.substring(xs,xs+1)!="<"))
				xs++
			//now we're pointing at the first "<"
			if(xs<p.length)
			{
				//xe (xmlend) = xs (xmlstart) initially. xc (xmlcount) = 0 initially
				var xe = xs, xc = 0;
				//now loop from xs, updating xe until
				while(xe<p.length)
				{
					//get next character, updating xe. We're looking for an ending semi-colon
					//but we might encounter an escaped character like &amp; so if we see an
					//ampersand, then we allow one free semi-colon to get through. As soon
					//as we find a semi-colon that's not related to an ampersand, then we
					//break the loop because we found the end.
					var chr = p.substring(xe++,xe);
					if(chr == "&")
						xc = 2;
					else if(chr==";")
					{
						if(xc>0)
							xc--;
						else
						{
							xe--;
							break;
						}
					}
				}
				//the string we want is from xs (xmlstart) to xe (xmlend)
				var xt=p.substring(xs,xe);
				parms+=AddParm("xmltext",xt);
				//now that we extracted the xmltext field from param 6,
				//it's time to REMOVE it from the p (param 6) string var
				//account for the trailing semi-colon
				if(op.substring(xe,xe+1) == ";")
					xe++;
				//reconstruct param 6 p var using op var from zero to xmltext offset
				p=op.substring(0,xso);
				//then add everything after the end of the xmltext
				if(xe<op.length-1)
					p+=op.substring(xe,op.length);
			}
		}
	}
	
	//now we removed xmltext from the param 6 string, we process the rest of the sub-params in it
	if(p!=null)
	{
		//split param 6 into an array, using ; as the split delimiter
		var tA=p.split(";");
		//iterate the array and deal with each found parameter
		for(var i=0;i<tA.length;i++)
		{
			if(tA[i]!=""){var tV=tA[i].split("=");re= / /g;
				tV[0]=tV[0].replace(re,"");var tv0l=tV[0].toLowerCase();
				if(tv0l=="genieminimumversion"){tV[1]=tV[1].replace(re,"");gMV=tV[1];}
				else if(tv0l=="hostminimumversion"){tV[1]=tV[1].replace(re,"");hMV=tV[1];}
				else if(tv0l=="componentminimumversion"){tV[1]=tV[1].replace(re,"");cMV=tV[1];}
				else if(tv0l=="classid"){tV[1]=tV[1].replace(re,"");VET_AxID=tV[1];}
				else if(tv0l=="mime"){tV[1]=tV[1].replace(re,"");VET_Mim="application/x-" + tV[1];}
				else if(tv0l=="nei"){tV[1]=tV[1].replace(re,"");VET_Dv=tV[1];VET_Re=VET_Dv+'index.html?';}
				else if(tv0l=="layer"){}
				else if(tv0l=="componentname"){tV[1]=tV[1].replace(re,"");VET_Ic=tV[1];}
				else if(tv0l=="imagelink"){tV[1]=tV[1].replace(re,"");this.ImageLink=tV[1];}
				else if(tv0l=="altplugin"){tV[1]=tV[1].replace(re,"");if(tV[1]=="1"||tV[1]=='true')VET_AltPlugin=1;else VET_AltPlugin=0;}
				else if(tv0l=="basehref"){tV[1]=tV[1].replace(re,"");if(tV[1]=="1")this.props+="basehref"+"="+VET_href()+";";}
				else if(tv0l=="topurl"){tV[1]=tV[1].replace(re,"");if(VET_TopURL=="")VET_TopURL=tV[1];}
				//if it's not one of the expected params, we blindly construct the Param assuming it's in the format name=value
				else{var idx=1,t1=tV[idx-1],t2=tV[idx];idx++
					while(tV[idx]!=null){t2+="="+tV[idx];idx++;}
					parms+=AddParm(t1,t2);
					this.props+=t1+"="+t2+";";
				}
			}
		}
	}
	//set the main 3 minimum versions. If they weren't specified in the 6th param,
	//then use the default values that were set at the top of this function
	parms+=AddParm("HostMinimumVersion",hMV);
	parms+=AddParm("ComponentMinimumVersion",cMV);
	parms+=AddParm("GenieMinimumVersion",gMV);
	parms+=AddParm("VMPClassID",("{"+VET_AxID+"}"));
	parms+=AddParm("PageURL",window.location.href);
	this.props+="parentlocation="+self.location.href+";";
	this.props+="mts3interfaceversion="+VET_IfVer+";";
	this.props+="referrer="+(window.VET_Referrer?window.VET_Referrer:window.location.href)+";";
	this.props+="name="+ this.name +";";
	this.props+="starthidden=" + true +";";//if we want the scene to be initiall hidden, if components are not immediately avail/in local UVL
	return parms;
}
	//utility function for creating the param/tags that make up an object/embed tag
AddParm=function(n,v){if(VET_IE&&VET_Win)return MTSParm(n,v);else return (n+"="+"'"+v+"' ");}
	//general purpose cookie functions
GetCookie=function(a){var e,m=" "+document.cookie+";",N=" "+a+"=",s=m.indexOf(N),r="";if(s!=-1){s+=N.length;e=m.indexOf(";",s);r=unescape(m.substring(s,e))}return r}
SetCookie=function(n,v){document.cookie=n+"="+escape(v)}
ClearCookie=function(n){var e=new Date();e.setTime(e.getTime()-(3*24*60*60*1000));document.cookie=n+"=ImOutOfHere; expires="+e.toGMTString()}
	//This function calculates what the base href value is when the basehref param is used in the object/embed tag.
VET_href=function()
{
	//If the browser is Gecko then we can do this easily by just getting the BASE element.
	if(VET_Gecko){
		var ob=document.getElementsByTagName('BASE');
		return (ob&&ob.length)?ob[0].href:"";
	}
		//Otherwise we need to write a dummy A HREF link to mtsEmpty.html and then we
		//retrieve the .href property for that link from document.links and strip out
		//the mtsEmpty.html and what's left is the base HREF.
	var t="mtsEmpty.html",d="document.",c,l;
	eval("c="+d+"links.length");
	var y="write(\"<a href='\"+t+\"'></a>\")";
	eval(d+y);
	eval("l="+d+"links[c].href");
	if(t==l)l="";
	else{var re= /mtsEmpty.html/g;
		l=l.replace(re,"");
	}
	return l;
}
MTS_IsString=function(s){if(s=="")return true;for(var i=0;i<s.length;i++){if(((s.charAt(i)<"0")||(s.charAt(i)>"9"))&&(s.charAt(i)!=".")&&(s.charAt(i)!="-"))return true}return false}
MTSWrapValue=function(v){var rs=v.toString();if(MTS_IsString(rs))rs="'"+rs+"'";return rs}
//Constructor for creating VMP instances.
//NOTE: You can create aliases of this function by just doing this:
//		WLCustom=MTSPlugin;
//		That makes that variable a function pointer and can be used in the
//		same way as MTSPlugin, e.g.: var wlc=new WLCustom(6 params);

function MTSPlugin(fi,wi,hi,bk,alt,par)
{
	//get the URL of the page
	try
	{
		VET_TopURL = top.location.href;
	}
	catch(e) {}
	//at some point we stopped supporting param 5 and hard-code it to popup
	alt="popup";
	//We used to do an alert here but that's a bad idea. Safer to just MTSConsole when > 6 params provided
	if(arguments.length>6)
		MTSConsole("Error: MTSPlugin function takes up to 6 parameters. You supplied "+arguments.length+".");
	//start out with "" for str
	var str="";
	//name of this plugin will be MetaCtln where n is the current index number which
	//is incremented by 1 for each VMP instance created by the constructor (note that
	//sniffing instances don't count).
	var na=this.name="MetaCtl"+MTSPlugin.prototype.genid.toString();
	//This static class property controls the number of times we've instantiated
	//a plugin on this web page. It's used to number the instances uniquely as it's
	//appended to the name "MetaCtl" at object creation time. Now we've used the
	//current genid value, it's time to increment it for the next call to MTSPlugin
	MTSPlugin.prototype.genid++;
	//assume there'll be no image link to click on when no VMP installed. This might
	//be overwritten by the createParams method.
	this.ImageLink = "0";
	//parse the 6th constructor parameter and extract the various params that might be found within
	var parms = this.createParams(par);
	
	//before we check if VMP is installed, see if we are loaded from NEInstaller.  it's possible that we are prompting for NEInstaller,
	//eventhough the VMP is installed and can render (part of) the scene.  no reason to have VMP at that point, cuz NEInstaller theoretically
	//is there when there is no VMP (and invalid/insufficient VMP is equivalent)
	var skipVMP = false;
	{
		var refURL = (document.referrer && document.referrer != null) ? document.referrer : "";
		if (refURL != "")
		{
			var refURLArray = refURL.split('&');
			if (refURLArray.length > 1)
			{
				var refURLArray2 = refURLArray[0].split('?');
				if ( (refURLArray2.length > 0) && ((refURLArray2[0] + '?') == VET_Re) && (refURLArray[1] == window.location.href))
				{
					skipVMP = true;
				}
			}
		}
	}
	
	if (skipVMP == true)
		return;
	
	//Check if we have a valid VMP installed
	IsMTSInstalled();

	//if Win/IE and VMP then we always do the same thing
	if(VET_IsIn)
	{
		//Windows/IE, so make an OBJECT tag
		if(VET_Win && VET_IE)
		{
			var ch = "";
			if(fi != "")
				ch = MTSParm("Source",fi);
			ch += MTSParm("Component",VET_Ic) + MTSParm(VET_Cf,VET_Sc) + MTSParm(VET_Bk,bk);
			ch += parms;
			ch += MTSParm("properties",this.props);
			//i dont think we want to have to support the cab file any more...
			//var cbURL = "https:/"+"/components.viewpoint.com/MTSInstallers/MetaStream3.cab"+"?url="+(window.VET_Referrer?window.VET_Referrer:window.location.href)+"#Version=3,0,2,62";
			str = MTSMarkup("object",ch,"id",na,"name",na,"classid",("CLSID:"+VET_AxID),"width",wi,"height",hi/*,((MTSPlugin.prototype.genid==1)?"codebase":"dummy"),cbURL*/);
		}
		else //else make an embed tag
		{
			//TODO: Why do we switch to universal bkey for Mac only?
			if(VET_Mac)
				bk = "http:/"+"/www.viewpoint.com/installer/UniversalBroadcastKey.mtx";
			str = MTSMarkup("embed","<!-- -->","component",VET_Ic,"componentfilename",VET_Sc,"source",fi,"type",VET_Mim,"width",wi,"height",hi,"script",true,"name",na,"id",na,VET_Bk,bk,"properties",this.props,"instancename",na,"PluginURL","",parms);
		}
	}
	else
	{
        MTSUnsupported();
	}
    
	//if we have some tags to write into the page
	if(str != "")
	{
		//write either an object or an embed tag or an <a href> link into the page
		document.write(str);
		//and echo to console if debug is enabled
		MTSConsole(str,na);
	}
}

//This function is called when a click-to-install alt content triggers it
mtsClick=function()
{
	//check if another instance on the page has already started an install
	var t = GetCookie(this.name);
	//tell all other instances that an instance has already started an install
	for(var i = 0; i < MTSPlugin.prototype.genid; i++)
	{
		//set a session cookie for each instance with the value 1
		SetCookie("MetaCtl" + i,"1");
	}
	//if no other instance already triggered an install then this one can do it
	//there's extra protection anyway built into MTSJumpToRedirector against
	//multiple installs.
	if(t!="1")
		MTSJumpToRedirector();
}

MTSPlugin.prototype.Console=function(m){_mtsConsole(m,this.name)}
MTSPlugin.prototype.Execute=function(){return this._mtsCall.apply(this,arguments)}
MTSPlugin.prototype.SetProperty=function(n,p,v,t,o){if(arguments.length==5)return this.Execute("SetProperty",n,p,MTSWrapValue(v),t,o);else return this.Execute("SetProperty",n,p,MTSWrapValue(v),t)}
MTSPlugin.prototype.GetProperty=function(n,p,t){return this.Execute("GetProperty",n,p,t)}
MTSPlugin.prototype.TriggerAnim=function(a){return this.Execute("TriggerAnimation",VET_An(a))}
MTSPlugin.prototype.ReverseAnim=function(a){return this.Execute("ReverseAnimation",VET_An(a))}
MTSPlugin.prototype.StopAnim=function(a){return this.Execute("StopAnimation",VET_An(a))}
MTSPlugin.prototype.StartAnim=function(a){return this.Execute("StartAnimation",VET_An(a))}
MTSPlugin.prototype.ResetAnim=function(a){return this.Execute("ResetAnimation",VET_An(a))}
MTSPlugin.prototype.ToggleCollapse=function(n){this.SetProperty(n,'clps',this.GetProperty(n,'clps')==0?1:0)}
MTSPlugin.prototype.SetCollapsed=function(n,v){return this.SetProperty(VET_In(n),'clps',MTSWrapValue(v),'mts_int')}
MTSPlugin.prototype.ToggleVisible=function(n){this.SetProperty(n,'visb',this.GetProperty(n,'visb')==0?1:0)}
MTSPlugin.prototype.SetVisible=function(n,v){return this.SetProperty(VET_In(n),'visb',MTSWrapValue(v),'mts_int')}
MTSPlugin.prototype.ClearScene=function(){return this.Execute("ClearScene")}
MTSPlugin.prototype.TogglePano=function(n){this.SetProperty('MTSScene','paac',this.GetProperty('MTSScene','paac')==0?1:0)}
MTSPlugin.prototype.Render=function(){return this.Execute("Render")}
MTSPlugin.prototype.LoadMTX=function(p){return this.Execute("LoadMTX",p)}
MTSPlugin.prototype.ResetCamera=function(){this.SetProperty('MTSScene','rstc','0')}
MTSPlugin.prototype.GetVer=function(c){return this.Execute("GetVersion",c)}
MTSPlugin.prototype.GetLastErrCode=function(){return this.Execute("GetLastErrCode")}
MTSPlugin.prototype.GetLastPluginErr=function(t){return this.Execute("GetLastPluginErr",t)}
MTSPlugin.prototype.SetPluginErr=function(t,v){return this.Execute("SetPluginErr",t,v)}
MTSPlugin.prototype.GetAllPluginErrFor=function(t){return this.Execute("GetAllPluginErrFor",t)}
MTSPlugin.prototype.ClearAllPluginErrs=function(t){return this.Execute("ClearAllPluginErrs",t)}
MTSPlugin.prototype.ExecuteVETScript=function(vs){return this.Execute("VETScript(" + vs + ")")}
MTSPlugin.prototype.PostEvent=function(n,d){var a=arguments,x=d;for(var i=2;i<a.length;i++)x+=(","+arguments[i]);return this.Execute("PostEvent",n,x)}

	//utility function for building html tags
MTSMarkup=function(tag,c){var a=arguments;
	//if no arguments then you get no HTML
	if(a.length==0)return "";
	//start the HTML with a < and argument zero as the tag name
	var i,s="<"+a[0];
	//loop through all additional arguments in pairs and write as name/value pairs
	for(var i=2;i<a.length;i+=2){
		if(a[i+1]!=null)s+=(" "+a[i]+"=\'"+a[i+1]+"\'");
		else s+=" "+a[i];
	}
	//and finally close the tag, again using argument zero but this time preceded by a / character
	s+=(c.length?(">"+c+"</"+a[0]+">"):("/>"));
	//and return it
	return s;
}

	//Return an OBJECT param tag with the values filled in
MTSParm=function(n,v){return MTSMarkup("param","","name",n,"value",v.toString())}

	//implementation function that writes the debug info into a popup window
_mtsConsole=function()
{
		//get the arguments into a shorter variable name, it pays off after only 2 uses of it
	var a=arguments;
		//get the length of the arguments
	var ct=a.length;
		//if not in debug mode, or no params received, just return and do nothing
	if((VET_Dbg!=1)||(ct==0))return;
		//param 0 should be the message we're meant to output
	var msg=a[0].toString();
		//get the plugin name. if 2 params are received then the second one is the
		//plugin name (e.g. MetaCtl0). If no second param, then use "Default" string
	var pn=(ct==1)?"Default":((a[1].constructor==MTSPlugin)?a[1].name:a[1].toString());
		//if the debug window hasn't been opened yet
	if(!VET_Mc){
			//open it now and keep a handle in the VET_Mc variable
		VET_Mc=window.open("","console","width=700,height=400,resizable,scrollbars");
			//if the window wasn't popup-blocked then we can use it
		if(VET_Mc){
				//write title and opening plaintext tag for win/ie (other browsers don't need it)
			if(VET_Win&&VET_IE)VET_Mc.document.write('<title>debug_output<\/title><plaintext>');
				//and bring the window into focus in case it's hidden behind the main window.
			VET_Mc.focus();
		}
	}

	//as long as the debug window wasn't popup-blocked or closed, we can write to it
	if(!VET_Mc.closed)
	{
			//add a > to the instance name and a space before writing it out
		var s=pn+"> "+msg+"\n"
			//need to write to debug window in different windows for gecko and win/IE
			//IE can accept the string without any processing
		if(VET_Win&&VET_IE)VET_Mc.document.write(s)
		else{
				//otherwise we need to wrap it inside <pre> </pre> tags and replace
				//all < with &lt; and all > with &gt;
			var re= /</g;s=s.replace(re,"&lt;");
			re= />/g;s=s.replace(re,"&gt;");
			VET_Mc.document.write("<pre>"+s+"</pre>")
		}
	}
}
VET_An=function(n){var s='MTSAnimator.';return n.indexOf(s)==0?n:s+n}
VET_In=function(n){var s='MTSInstance.';return n.indexOf(s)==0?n:s+n}

//The main function that scripts into the instance.
MTSPlugin.prototype._mtsCall=function(){
	var ar=arguments,c=ar.length+1,p,d='document.';
	var rg=ar[0]+'(',l=c-2,i,li=c-1,pn=this.name;
	//loop through arguments and add them to the command string separated by commas
	for(var i=1;i<li;i++){rg+=ar[i];if(i<l)rg+=','}rg+=')';
		//Win/IE uses the document.all.MetaCtl0.PluginCommand(cmd,0,0)  format
	if(VET_Win&&VET_IE)p=d+'all.'+pn+'.PluginCommand(\"'+rg+'\",0,0);';
		//where as everything else uses document.embeds.MetaCtl0.DoCommand(cmd) format
	else p=d+'embeds.'+pn+'.DoCommand(\"'+rg+'\");';
		//console it if debug enabled
	MTSConsole(rg,pn);
		//and eval the cmd string
	return eval(p);
}

MTSJumpToRedirector=function(){
		//Check if this global indicates that we've already started an install
	if(VET_Sdr<1){
			//we haven't already started an install, let's make sure this counter
			//now indicates that we have by incrementing it above zero
		VET_Sdr++;
			//get the installer popup cookie which helps to avoid infinite loop install sequences
		var c=GetCookie("VETInstallerPopup");
			//find out if we're not supposed to reload or redirect this page
		var inf=(window.location.search.indexOf("noreloadredir")!=-1);
			//create a date/time object with the value of 30 seconds
		var e=new Date();e.setTime(e.getTime()+30000);
			//if the cookie already exists and the first 10 bytes are AutoReload (or we have "noreloadredir" in the page URL)
		if((c!=""&&c.substring(0,10)=="AutoReload")||inf){
				//and the next 7 bytes of the cookie are "Premium" (or we have "noreloadredir" in the page URL)
			if((c.substring(10,17)=="Premium")||inf)
					//then all we need to do is update this cookie to give it another 30 seconds
				SetCookie("VETInstallerPopup",("AutoReload; expires=" + e.toGMTString()));
			else
				//otherwise clear the cookie now if not premium
				ClearCookie("VETInstallerPopup");
			//either way, we just return and do nothing active
			return;
		}
		//the cookie isn't set so we set it now
		SetCookie("VETInstallerPopup",("AutoReloadPremium; expires="+e.toGMTString()));
		//build up the URL to the frame installer
		var trg=VET_Re+VET_IfVer+"|frame&"+(VET_TopURL!=""?VET_TopURL:window.location.href);
		//and redirect there in a quarter of second
		setTimeout("top.location.href='"+trg+"';",250);
		return;
	}
}

MTSUnsupported=function()
{
    if (VET_CheckAgainAfterPageLoad)
    {
        // dd fix this: see CSS comment at top of file.  We may have to rethink some stuff.
        if (!VET_PageLoaded) return;
        navigator.plugins.refresh(false);
        VET_IsIn = null;
        VET_TestPlugin(false); // destroy test
        if (IsMTSInstalled()) return; // try it again
    }
    MTSJumpToRedirector();
}

//load the vmpSupport.js file. This can be at the viewpoint website for the
//very latest critical updates/patches to the MTS3Interface and to get the
//installation support functions. Alternately, for an offline (e.g. CD-based)
//usage, you can grab the file yourself and put it alongside the MTS3Interface
//on your server. If VMP installation is not required, you may choose to not
//include vmpSupport.js at all. Beware! Notice how the two forward slashes
//after the https are broken up with the + symbol? That's deliberate, to avoid
//comment-stripper programs from seeing that URL and thinking it's a comment.
// DD fix this: this should point to absolute url on our servers!
VET_SupportURL="https:/"+"/components.viewpoint.com/MTSInstallers/vmpSupport.js";
document.write('<script type="text/javascript" src="'+VET_SupportURL+'" id="VMPSupportLib"></script>');

