
function joint_show()
{
   this.styleObj.visibility="visible";
}

function joint_hide()
{
   this.styleObj.visibility="hidden";
}

function joint_move(posX,posY)
{
   this.x=posX;
   this.y=posY;
   this.styleObj.pixelLeft=posX;
   this.styleObj.pixelTop=posY;
}

function joint_hilight()
{
   this.imageObj.src=imgJointHi.src;
}

function joint_focus()
{
   this.imageObj.src=imgJointFo.src;
}

function joint_clear()
{
   this.imageObj.src=imgJoint.src;
}

function joint(initId)
{
   this.jointId=initId;
   this.styleObj=document.all["joint"+initId].style;
   this.imageObj=document.images["jointImg"+initId];
   this.x=-1;
   this.y=-1;

   this.show=joint_show;
   this.hide=joint_hide;
   this.move=joint_move;
   this.hilight=joint_hilight;
   this.focus=joint_focus;
   this.clear=joint_clear;
}


//######################################################################

var twoPi=(Math.PI)/2;

function section_draw()
{
   eval("section"+this.sectionId+".from='"+(this.startPoint.x+vmlOffsetX)+","+(this.startPoint.y+vmlOffsetY)+"'");
   eval("section"+this.sectionId+".to='"+(this.endPoint.x+vmlOffsetX)+","+(this.endPoint.y+vmlOffsetY)+"'");

   this.recalculate();
}

function section_recalculate()
{
   this.dx=this.endPoint.x-this.startPoint.x;
   this.dy=this.endPoint.y-this.startPoint.y;

   if(this.dx == 0)
   {
      this.length=Math.abs(this.endPoint.y-this.startPoint.y);
      this.force=1.0;
   }
   else
   {
      if(this.dy == 0)
      {
         this.length=Math.abs(this.endPoint.x-this.startPoint.x);
         this.force=0.0;
      }
      else
      {
         this.length=Math.round(Math.sqrt((this.dx*this.dx)+(this.dy*this.dy)));

         var OoverA=this.dx/this.dy;
         this.force=(twoPi-Math.abs(Math.atan(OoverA)))/twoPi;

         if(this.dy<0)
            this.force=-this.force;
      }
   }
}

function section(initId,startPoint,endPoint)
{
   this.sectionId=initId;
   this.startPoint=startPoint;
   this.endPoint=endPoint;
   this.length=0;
   this.force=0;
   this.dx=0;
   this.dy=0;

   this.draw=section_draw;
   this.recalculate=section_recalculate;

   this.recalculate();
}



//######################################################################


function train_setDistance(distance)
{
   this.distance=distance;

   var bounced=true;

   var totalDistance=0;
   for(var i=0;i<maxSections;i++)
   {
      totalDistance+=sectionArray[i].length;
      if(totalDistance>distance)
      {
         bounced=false;
         this.section=i;
         this.sectionDistance=distance-(totalDistance-sectionArray[i].length)
         i=maxSections;
      }
   }
   if(bounced)
   {
      this.section=maxSections-1;
      this.sectionDistance=sectionArray[this.section].length;
   }

   return bounced;
}

function train_calculateDistance()
{
   var calculatedDistance=0;

   for(var i=0;i<this.section;i++)
   {
      calculatedDistance+=sectionArray[i].length;
   }
   calculatedDistance+=this.sectionDistance;
   this.distance=calculatedDistance;

   return this.distance;
}

function train_place(posX,posY)
{
   this.styleObj.pixelLeft=posX;
   this.styleObj.pixelTop=posY;
}

function train_move(delta)
{
   this.setDistance(this.distance+delta);
}


function train(initId)
{
   this.trainId=initId;
   this.distance=0;
   this.section=0;
   this.sectionDistance=0;
   this.styleObj=document.all["train"+initId].style;

   this.setDistance=train_setDistance;
   this.calculateDistance=train_calculateDistance;
   this.place=train_place;
   this.move=train_move;
}
