// AtmosphereInterfaceAddon v.1.3
// 
// Rolu, www.roce.org, CR 2003-jan-16, LU 2003-june-14
//
// Additional code by M.Judd
//
// Easy access to the MTS plugin object from within Atmo.
//
MTSPlugin.prototype.pluginObjects=new Array();
MTSPlugin2=MTSPlugin;
MTSPlugin=function(file,width,height,bkey,alt,parmsc)
{
    this.realMTSPlugin=new MTSPlugin2(file,width,height,bkey,alt,parmsc);
    this.pluginObjects.push(this.realMTSPlugin);
    this.name=this.realMTSPlugin.name;
    this.LN=this.realMTSPlugin.LN;
    this.ImageLink=this.realMTSPlugin.ImageLink;
}
MTSPlugin.prototype=MTSPlugin2.prototype;

function injectPluginIndices()
{
    var i;
    
    for(i=0;i<MTSPlugin.prototype.pluginObjects.length;i++)
    {
        MTSPlugin.prototype.pluginObjects[i].sendJS("pluginIndex="+i);
    }  
}

function getMTSPluginObject(index)
{
    return MTSPlugin.prototype.pluginObjects[index];
}

MTSPlugin.prototype.getPluginObject=function()
{
    var pn=this._mtsPID(), pobj;

    if(VET_Mac&&VET_IE)
    {
        // Sorry, doesn't work here.
    }
    else
    {
        if(VET_Win&&VET_IE)
            pobj=eval('document.all.'+pn);
        else
        {
            if(this.LN==VET_Ln)
                pobj=eval('document.embeds.'+pn)
            else
            {
                if(VET_Gecko)
                    pobj=eval('document.embeds.'+pn)
                else
                    pobj=eval('document.'+this.LN+'.document.embeds.'+pn)
            }
        }
    }
    
    return pobj;
}

//
// Javascript injector.
//
MTSPlugin.prototype.sendJS=function(ajs)
{
    var vpc=this.getPluginObject(),
        eajs=ajs;

    // Prepare string.
    eajs="eval(\"" + eajs.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/'/g, "\\\'").replace(/\r\n/g, "\\n").replace(/\n/g, "\\n").replace(/\r/g, "\\n") + "\");";

    if(VET_Mac&&VET_IE)
    {
        // Sorry, doesn't work here.
    }
    else
    {
        if(VET_Win&&VET_IE)
            vpc.PluginCommand(eajs, 0, 0);
        else
            vpc.DoCommand(eajs);
    }
}
// ======================================================================================
var gTimer = null;
var TimerCount = 0;
var ScreenCount = 1;
var vpc
var AtmoLoadDone = false;
var openscreen = false;
var firstscreen = false;
var not_done = true;
var pics_set = false;
var active_page = "";
var flash_control = false;

function Do_Shift(picnum) 
{
if (!pics_set) 
	{
	pics_set = true;
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" id="map" width="100% " height="100%" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="map.swf" /><param name="loop" value="false" /><param name="play" value="false" /><param name="scale" value="exactfit" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><embed src="map.swf" loop="false" play="false" quality="high" bgcolor="#000000" width="100%" height="100%" swLiveConnect=true id="map" name="map" align="middle" scale="exactfit" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>	');
	myflash = eval("document.map");
	vpc = vmp.getPluginObject();
	active_page = vpc;
	}
if (picnum == 0) // Shift to title pic
	{
	if (!not_done) 
		{
		parent.Left.left.GotoFrame(picnum);
		parent.Top.tvtop.GotoFrame(picnum);
		parent.Bottom.tvbottom.GotoFrame(picnum);
		parent.Right.right.GotoFrame(picnum);
		}
    active_page.style.position="absolute";
    active_page.style.left="-500%";
	myflash.style.position="static";
	myflash.style.left="0%";
	active_page = myflash;
	}
if (picnum == 1 && not_done) // Shift to Opening pic
	{
	parent.World.map.GotoFrame(picnum);
	parent.Left.left.GotoFrame(picnum);
	parent.Top.tvtop.GotoFrame(picnum);
	parent.Bottom.tvbottom.GotoFrame(picnum);
		if (!flash_control)
		{
		parent.Right.right.GotoFrame(picnum);
		}
	active_page = myflash;
	}
if (picnum == 2 && not_done) // Shift to Car.jpg
	{
	parent.World.map.GotoFrame(picnum);
	parent.Left.left.GotoFrame(picnum);
	parent.Top.tvtop.GotoFrame(picnum);
	parent.Bottom.tvbottom.GotoFrame(picnum);
	if (!flash_control)
		{
		parent.Right.right.GotoFrame(picnum);
		}
	active_page = myflash;
	}
if (picnum == 3 && not_done) // Shift to Plugin
	{
	not_done = false;
	parent.Left.left.GotoFrame(9);
	parent.Top.tvtop.GotoFrame(9);
	parent.Bottom.tvbottom.GotoFrame(9);
	if (!flash_control)
		{
		parent.Right.right.GotoFrame(8);
		}
	if (flash_control)
		{
		parent.Right.right.GotoFrame(9);
		}
	active_page.style.position="absolute";
	active_page.style.left="-500%";
	vpc.style.position="static";
	vpc.style.left="0%";
	active_page = vpc;
	flash_control = true;
	}
if (picnum == 4 && not_done) // Shift to Flash Map
	{
	parent.World.map.GotoFrame(8);
	parent.World.map.play();
	parent.Left.left.GotoFrame(12);
	parent.Top.tvtop.GotoFrame(12);
	parent.Bottom.tvbottom.GotoFrame(12);
	if (!flash_control)
		{
		parent.Right.right.GotoFrame(12);
		}
	active_page.style.position="absolute";
	active_page.style.left="-500%";
	myflash.style.position="relative";
	myflash.style.left="0%";
	active_page = myflash;
	}
}

function Timer()
{

if(!vmp)
   return;
if (!openscreen) 
	{
	Do_Shift(0);
	openscreen = true;
	vmp.sendJS("if(stageModel.bounds!=undefined) sendJS('AtmoLoadDone=true');");

	}

if(AtmoLoadDone)
  {	

  
  if(!firstscreen)
  	{
  	firstscreen = true;
	Do_Shift(ScreenCount);
	}
  TimerCount++;
  if(TimerCount >= 20)
  	{
			
  	ScreenCount++;
	Do_Shift(ScreenCount);
	TimerCount = 0;
	}
	if (ScreenCount == 2)
	   {
		return;
		}
	  gTimer = window.setTimeout("Timer()",1000);
	}
	else
	  {
	  gTimer = window.setTimeout("Timer()",1000);
	  vmp.sendJS("if(stageModel.bounds!=undefined) sendJS('AtmoLoadDone=true');");
	  }
}

function Restore()
{
	if(document.cookie == '')
		{
		return;
		}
	else
		{
		restoregame = document.cookie;
		restoregame = restoregame.substring(0,37);
		vmp.sendJS("Get_Restore_Data('" + restoregame + "')");
		}
}



//
// Preloader picture support. ///////////////////////////////////////
//

AtmoLoadData=[];
AtmoLoadDone=false;
var flashdone =false;



function makeFlashTrue()
{
	flashdone = true;		
}

function usePreloadPic(vmp, picfile, showtime)
{
    var id, vpc, vpic, ld=new Object();

    if(!vmp)
        return;

    if(typeof(showtime)=="undefined")
        ld.picShowTime=0
    else
        ld.picShowTime=showtime;


/////////////////  new bit for flash from above  /////////////////

		pics_set = true;
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" id="map" width="100% " height="100%" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="map.swf" /><param name="loop" value="false" /><param name="play" value="false" /><param name="scale" value="exactfit" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><embed src="map.swf" loop="false" play="false" quality="high" bgcolor="#000000" width="100%" height="100%" swLiveConnect=true id="map" name="map" align="middle" scale="exactfit" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
		if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) 
		{	
			document.write('<SCRIPT LANGUAGE=VBScript\> \n');
			document.write('on error resume next \n');	
			document.write('Sub map_FSCommand(ByVal command, ByVal args)\n');	
			document.write('  call map_DoFSCommand(command, args)\n');	
			document.write('end sub\n');
			document.write('</SCRIPT\> \n');
		}

		myflash = eval("document.map");
		vpc = vmp.getPluginObject();
		active_page = vpc;
	
	active_page.style.position="absolute";
	active_page.style.left="-500%";
	myflash.style.position="static";
	myflash.style.left="0%";
	active_page = myflash;

    ld.myflashLoaded=false;

    AtmoLoadData[AtmoLoadData.length]=ld;
    
    setTimeout("tryShowPlugins()", 200);
}



function tryShowPlugins()
{
    var i, ld, now;

    for(i=AtmoLoadData.length-1;i>=0;i--)
    {
        ld=AtmoLoadData[i];

        if(ld.picShowTime>0)
        {
            if(!ld.myflashLoaded)
            {

                now=new Date();
                ld.myflashLoadTime=now.getTime();
                ld.myflashLoaded=true;
            }

            if(ld.myflashLoaded)
            {
                now=new Date();
                if((now.getTime()-ld.myflashLoadTime)<ld.picShowTime)
                    continue;
            }
        }

        AtmoLoadDone=false;
	vmp.sendJS("if(stageModel.bounds!=undefined) sendJS('AtmoLoadDone=true');");
        if(AtmoLoadDone && flashdone)
        {
            
         vpc = vmp.getPluginObject();
         // set the world to centre and gets rid of flash   
        active_page = myflash;   
	active_page.style.position="absolute";
	active_page.style.left="-500%";
	vpc.style.position="static";
	vpc.style.left="0%";
	active_page = vpc;     
        AtmoLoadData.splice(i, 1);        

        }
    }

    if(AtmoLoadData.length>0)
        setTimeout("tryShowPlugins()", 200);
}


function hideflash()
{
         vpc = vmp.getPluginObject();
         // set the world to centre and gets rid of flash   
        active_page = myflash;   
	active_page.style.position="absolute";
	active_page.style.left="-500%";
	vpc.style.position="static";
	vpc.style.left="0%";
	active_page = vpc;     
        //AtmoLoadData.splice(i, 1); 	
	
}

function showflash()
{
	// set the world to side and shows flash  
	
	vpc = vmp.getPluginObject();
	active_page = vpc;
	
	active_page.style.position="absolute";
	active_page.style.left="-500%";
	myflash.style.position="static";
	myflash.style.left="0%";
	active_page = myflash;	
	
}


