// JavaScript Document


var rotate=0;
var aroundCursor=180;
var degreeX = 1;
var degreeY = 1;
var positionofX;
var positionofY;

var moonsPozReativeWithCursor = new Array(2);
  //  moonsPozReativeWithCursor[0] = 500;
  //  moonsPozReativeWithCursor [1] = 500;
var moonsRotation = new Array(2);
  //  moonsRotation[0] = 500;
 //   moonsRotation[1] = 500;
    
function moonOrbit(evnt){//grab event from mousedown
    if( !evnt ){
          positionofX = window.event.screenX;
          positionofY = window.event.screenY;
         appollo(positionofX,positionofY);
        
           }
        else{
        
         var pozX = document.body.scrollLeft+evnt.screenX;
         var pozY = document.body.scrollTop+evnt.screenY;
         positionofX = evnt.screenX;
         positionofY = evnt.screenY;
         moonsPozReativeWithCursor.splice(0,2,positionofX,positionofY);
         moonsRotation.splice(0,2,positionofY,positionofX);
         appollo(positionofX, positionofY,pozX,pozY);
        
           }
         }
    
       function appollo(positionofX,positionofY,pozX,pozY){ 
            aroundCursor+=5;
            var moonObject = window.document.getElementById("moon");
            moonObject.style.visibility = "visible";
            moonObject.style.zIndex="1000000000";
            rotateByDegree(degreeX, degreeY, moonObject, positionofX, positionofY ,pozX,pozY);
			clearTimeout("rotateByDegree()");
             if(rotate <= 2){
        rotate++;
        } 
         } 
              
    //--------------------------------------------------
    
    function rotateByDegree( degreeX, degreeY, moonObject, positionofX, positionofY,pozX,pozY ){ 
        moonObject = window.document.getElementById("moon");
        moonObject.style.top =  moonsRotation[0]+"px";
        moonObject.style.left = moonsPozReativeWithCursor[0]+"px";
         if(positionofX || positionofY === "NaN" ){
        var AsinFromPozX = Math.sin( degreeX / 180 * 3.141 );
        var AcosFromPozY = Math.cos( degreeY / 180 * 3.141 );
        var Sum_sin = Math.abs(( positionofY-positionofX) + (rotate*AsinFromPozX ) );
        var Sum_cos = Math.abs( ( positionofX-positionofY) + (rotate * AcosFromPozY ));
        degreeX +=Sum_sin;
        degreeY += Sum_cos;
        positionofX = degreeX;
        positionofY = degreeY; 
        moonsRotation.splice( 0, 2, positionofX, positionofY );
		moonsPozReativeWithCursor.splice( 0, 2, positionofX, positionofY)
		positionofX = moonsRotation[rotate];
		positionofY = moonsPozReativeWithCursor[rotate];
        }
        
      if( aroundCursor >= 180 ){
      aroundCursor = 0;
     }
   if( rotate >= 2 ){
      rotate = 0;
   } 
   setTimeout( "rotateByDegree()", 25500 );
   
  }               
           

