/**
** this code is used by the index page only
*/

    /**
    ** this function sets the postions of all moons. In order to make them positioned correctly
    ** thier position is calculated based on the browser window size
    */	function displayMessage(msg)	{		//alert("here");		var screenWidth; // width of the browser window		if (document.all)			screenWidth = document.body.clientWidth;		else			screenWidth = window.innerWidth;		var messageboard = document.getElementById("messageboarddiv");		messageboard.style.top = "50px";		messageboard.style.left = screenWidth-350;		var message = document.getElementById("incomingmessage");		message.innerText = msg;		messageboard.style.display="block";	}		function hideMessage()	{		var messageboard = document.getElementById("messageboarddiv");		messageboard.style.display="none";	}
	function positionMoons()
	{

    	var newLeft; // width of the browser window
    	if (document.all)
       		newLeft = document.body.clientWidth;
    	else
       		newLeft = window.innerWidth;

    	Moon1 = document.getElementById("moon1");
    	Moon1.style.top = "220px";
        Moon1.style.left = parseInt(newLeft)/2 - 320 ;
	    Moon2 = document.getElementById("moon2");
	    Moon2.style.top = "500px";
	    Moon2.style.left = parseInt(newLeft)/2 + 300 ;
	    Moon3 = document.getElementById("moon3");
	    Moon3.style.top = "250px";
	    Moon3.style.left = parseInt(newLeft)/2 + 175 ;
	    Moon4 = document.getElementById("moon4");
	    Moon4.style.top = "500px";
	    Moon4.style.left = parseInt(newLeft)/2 - 250 ;
	    Moon5 = document.getElementById("moon5");
	    Moon5.style.top = "225px";
	    Moon5.style.left = parseInt(newLeft)/2 + 290 ;
	    Moon6 = document.getElementById("moon6");
	    Moon6.style.top = "350px";
	    Moon6.style.left = parseInt(newLeft)/2 - 100 ;
	    Moon7 = document.getElementById("moon7");
	    Moon7.style.top = "390px";
	    Moon7.style.left = parseInt(newLeft)/2 - 285 ;
	    Moon8 = document.getElementById("moon8");
	    Moon8.style.top = "450px";
	    Moon8.style.left = parseInt(newLeft)/2 + 150 ;
	    Moon9 = document.getElementById("moon9");
	    Moon9.style.top = "500";
	    Moon9.style.left = parseInt(newLeft)/2 - 50 ;
	    Station = document.getElementById("spacestation");
	    Station.style.top = "600px";
	    Station.style.left = parseInt(newLeft)/2 + 200 ;
	    Comet = document.getElementById("comet");
	    Comet.style.top = "25";
	    Comet.style.left = parseInt(newLeft)/2 + 250 ;
	    CloudCity = document.getElementById("cloudcity");
	    CloudCity.style.top = "250";
	    CloudCity.style.left = parseInt(newLeft)/2 - 25 ;
	    // the following are active (moving) objects)
	    // Turn them off so if the user resizes while they
	    // are in motion, they will just disappear instead
	    // of going to the wrong location
	    SupplyShip = document.getElementById("supplyshipdiv");
	    SupplyShip.style.display="none";
	    PatrolShip = document.getElementById("patrolshipdiv");
	    PatrolShip.style.display="none";
    }

    window.onresize = positionMoons;

    function doSomething()
    {
        if (!objectInMotion && !securityPatrol) // this is true when an object is moving
        {
            // see if we do a supply ship run
            if ((Math.round(Math.random()*10)+1) < 6)
            {
                objectInMotion = true;
                supplyShipRun();
            }
            else if ((Math.round(Math.random()*10)+1) < 4)
            {
            	securityPatrolRun();
            }
        }
        setTimeout('doSomething()',15000); // every 15 seconds, check again
    }


    var objectInMotion = false;
    var securityPatrol = false;

    /**
    ** this function starts a supply ship run.
    ** a supply ship always starts from the left of the screen.
    */
    function supplyShipRun()
    {
		var shipNames = new Array("Icarus", "Bonaventure", "Yangtze Maru", "Turpitz");		var moonNames = new Array("Maximo World","Shelly\'s Moon","Mike\'s Moon","Geek Moon","Religon Moon","TS Moon","Recipe Moon","Genealogy Moon","Gold Moon","Cloud City","space station Shenandoah");        var moonNum = Math.round(Math.random()*10) + 1; // pick a moon at random		//alert("MoonNum:" + moonNum);
		var targetMoon;				var msg;
        if (moonNum <1 || moonNum > 10)
        {
            // moon 11 is for the space station
            // I also check for less than 1 incase there is some calulation I missed
            targetMoon = document.getElementById("spacestation");			msg = "space station Shenandoah";
        }
        else if (moonNum == 10)
        {
            // moon 10 is for the Cloud City
            targetMoon = document.getElementById("cloudcity");			msg = moonNames[moonNum-1];
        }
        else
        {
            targetMoon = document.getElementById("moon" + moonNum); // get moon			msg = moonNames[moonNum-1];
        }
        // target is set based on position of the moon
        var centerFactor;
        if (IE4)
            centerFactor = 2;
        else
            centerFactor = 10;
        targetX = parseInt(targetMoon.style.left) + centerFactor;
        targetY = parseInt(targetMoon.style.top) + 0;

		// make the ship object visibable
        var disObj = document.getElementById("supplyshipdiv");
        disObj.style.display="block";

        // now set the base number for the run
        var startX = -550;  // start is just off screen
        var startY = 300+(Math.round(Math.random()*200)-100);
        var startWidth = 600;
        var startHeight = 200;
        var endWidth = 0;
        var endHeight = 0;
        var delay = 10;
        var speed = 2;
        var divObjectName = "supplyshipdiv"; // firefox is case sensitive
        var picObjectName = "supplyship"; // firefox is case sensitive
		        var shipNum = Math.round(Math.random()*3); // pick a ship at random		//alert("ShipNum:" + shipNum);        dynaText = new movableObj(startX,startY,targetX,targetY,startHeight,startWidth,endHeight,endWidth,delay,speed,divObjectName,picObjectName);				displayMessage("This is cargo ship " + shipNames[shipNum] + " in-route for " + msg + ". Please be aware of incoming traffic.");
        dynaText.startMovement();
    }

    var points = new Array();
    var sizes = new Array();

    function securityPatrolRun()
    {
        securityPatrol = true;
        // get the starting point
        var SpaceStation = document.getElementById("spacestation");
        points[0] = new Array(parseInt(SpaceStation.style.left), parseInt(SpaceStation.style.top));
        points[1] = new Array(parseInt(SpaceStation.style.left),550);
        points[2] = new Array(parseInt(SpaceStation.style.left)-400,200);
        points[3] = new Array(parseInt(SpaceStation.style.left)-500,550);
        points[4] = new Array(parseInt(SpaceStation.style.left)-425,600);
        points[5] = new Array(parseInt(SpaceStation.style.left)+80,150);
        points[6] = new Array(parseInt(SpaceStation.style.left)+110,200);
        points[7] = new Array(parseInt(SpaceStation.style.left)-375,300);
        points[8] = new Array(parseInt(SpaceStation.style.left), parseInt(SpaceStation.style.top));

        sizes[0] = new Array(0,0);
        sizes[1] = new Array(9,40);
        sizes[2] = new Array(10,50);
        sizes[3] = new Array(10,50);
        sizes[4] = new Array(0,0);
        sizes[5] = new Array(0,0);
        sizes[6] = new Array(10,50);
        sizes[7] = new Array(86,400);
        sizes[8] = new Array(0,0);

        var patrolShip = document.getElementById("patrolshipdiv");
        patrolShip.style.display="block";
		displayMessage("ATTENTION. We are performing a security sweep of this sector. Please lower your shields and prepare to be scanned.");
        moveShip(1);

    }

    function moveShip(step)
    {
        if (!objectInMotion)
        {			if (step == 8 && securityPatrol)			{				displayMessage("Sector is clear. You may proceed. Have a nice day.");			}			
            if (step == 9)
            {
                var patrolShip = document.getElementById("patrolshipdiv");
                patrolShip.style.display="none";
                securityPatrol=false;				hideMessage();
                return;
            }
	        curPoint = points[step-1];
	        newPoint = points[step];
	        curSize = sizes[step-1];
	        newSize = sizes[step];

	        var delay = 5;
	        var speed = 6;

	        objectInMotion = true;
	        var dynaText = new movableObj(curPoint[0],curPoint[1],newPoint[0],newPoint[1],curSize[0],curSize[1],newSize[0],newSize[1],delay,speed,'patrolshipdiv','patrolship');
	        dynaText.startMovement();
	        step += 1;
        }		
        setTimeout('moveShip(' + step + ')',5000);
    }




