/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 mercoledì 27 dicembre 2006 0.18.26
 HAPedit 3.1.11.111
 - - - - - - - - - - - - - - - - - - - - - - - */
/*border-width: 0px 0px 112px 162px"*/


/*

1- una delle domande fondamentali è: disegnare gli oggetti e muoverli oppure
   fre un framebuffer da sostituire ogni x millisecondi?
   occorre sperimentare entrambe e scegliere la + veloce


*/

function isNull(val){return(val==null);} 

function isFunction(a) {
return typeof a == 'function';
}


  
  function TPoint(x,y)
  {
    this.x=x;
    this.y=y;
    this.name="punto";
    this.color="#000";
    this.point=null;
 
    this.trace = function() {

    point = document.createElement("div");
    point.innerHTML="<b>"+this.name+"</b>";
    point.setAttribute("style","position: absolute; left:"+this.x+"px; top:"+this.y+"px;");
    document.getElementById("container").appendChild(point);
}    
    this.tracePoint = function(width) {
  
    if  (isNull(width)) this.width=2;
    this.width=width;
    
    if  (isNull(this.zindex)) this.zindex=0;
     
    //SPURPOZ!!!
	//this.color="#ffffff";
     
    this.point = document.createElement("div");
    this.point.setAttribute("style","z-index:"+this.zindex+"; position: absolute; background:"+this.color+"; width:"+width+"px; height:"+width+"px;   left:"+this.x+"px; top:"+this.y+"px;");
	//this.point.innerText="A";
    document.getElementById("container").appendChild(this.point);
    }     

 /*ATTENZIONE ALLA GESTIONE DELLA MEMORIA: POTREBBE RIMANERE APPESO AL DOM*/
/*
    this.Pulsa = function(color) {
    //this.width--;
    if (this.width>0)
      {
      this.point.setAttribute("style","position: absolute; left:"+this.x+"px; top:"+this.y+"px; background:"+this.color+"; width:"+width+"px; height:"+width+"px;");
      document.getElementById("container").appendChild(point);
      }
    }    
 */


  };
  
  
  function TLine(p0,p1)
  {
 //lavorare su numeroparams se a zero then non afr nulla e crea funzione per aggiungere i punti
  this.p0=p0;
  this.p1=p1;
  this.incr=3;
  this.name="";
   
  with (Math)
    {
      incrx= p1.x - p0.x ;
      incry= p1.y - p0.y ;
      
      signx=1;
      if (incrx!=0)  signx=(incrx/abs(incrx));
      signy=1;    
      if (incry!=0)  signy=(incry/abs(incry));
  
                 
  
      this.seno = sqrt(pow(incry,2)/(pow(incry,2)+pow(incrx,2)))*signy ;
      this.cose = sqrt(pow(incrx,2)/(pow(incrx,2)+pow(incry,2)))* signx;
      this.ipotenusa = sqrt(pow(incry,2)+pow(incrx,2));
 

   //document.getElementById("checkitout").innerHTML="<div>this.seno:"+this.seno+"</div>";
 
      
    }

    this.calcsin = function(len) {
 


    }


 
  
    this.trace = function() {
    
    spanline = document.createElement("span");
    spanline.setAttribute("name","spanline");
    
   // p0.trace(); TEST
   // p1.trace(); TEST

    
    for (var i=0; i<this.ipotenusa; i+=this.incr )
    {
    
    point = document.createElement("div");
    point.setAttribute("style", "color=#000; position: absolute;top: "+Math.ceil(i*this.seno+this.p0.y)+"px; left:"+Math.ceil(i*this.cose+this.p0.x)+"px; width:1px; height:1px;background:#000;" );
    //point.innerHTML=this.name;
    spanline.appendChild(point); 
    
    }
    document.getElementById("container").appendChild(spanline); 
    //document.getElementById("ELEMINSPAN").innerHTML="<div>"+document.getElementById("container").innerHTML+"</div>";
   // alert (document.getElementById("container").firstChild.name);
                
                
    }
    
   
    this.remove = function() {
   //this.parentNode.removeChild(this)
    
    
    var spanlines = document.getElementsByTagName("span"); 
    document.getElementById("ELEMINSPAN").innerHTML="<div>ELEMENTI SPAN:"+spanlines.length+"</div>";
    for (var i = 0; i < spanlines.length; i++) {
      
            document.getElementById("container").removeChild(spanlines[i]); 
            }        
         
                
    }
      

  };
  
  
  /* dal traceline, come modello
  
   function main()
  { 
  var p0=new TPoint(20,200);
  var p1=new TPoint(400,400);
 
   var l1 =new TLine(p0,p1);
    l1.trace();
 
 };

*/



 function point(namePath)
 {
 this.id=namePath;
 this.x=0;
 this.y=0;
 this.z=0;


 /*OCCORRERA' INTRODURRE IL SUPPORTO MATRICIALE*/

 this.ruotax=function(varalfa,xasse,zasse)
 {
  alfa=0.1;
  if (!isNull(varalfa)) alfa=varalfa;  
 xasse=400;
 yasse=300;

 valx=this.x-xasse;
 valy=this.y-yasse;


 
  this.x=valx*Math.cos(alfa) - valy*Math.sin(alfa)+xasse;
 this.y=valx*Math.sin(alfa) + valy*Math.cos(alfa)+yasse;

 };

 this.ruotay=function(varbeta,xasse,yasse)
 {
  beta=0.01;
  if (!isNull(varbeta)) beta=varbeta;
 xasse=400;
 zasse=0;

 valx=this.x-xasse;
 valz=this.z-zasse;
 
 this.x=valx*Math.cos(beta) + valz*Math.sin(beta)+xasse;
 this.z=valz*Math.cos(beta) - valx*Math.sin(beta)+zasse;

 };


};


 function poly(namePath)
 {
 this.id=namePath;
 this.a=new point(this.name+".a");
 this.b=new point(this.name+".b");
 this.c=new point(this.name+".c");
 this.where = null;

 
 this.trace=function()
     {
     
         var spanlines = document.getElementsByTagName("span");
            
            /*** ELIMINAZIONE *///
            
            
            /*
            document.getElementById("ELEMINSPAN").innerHTML="<div>ELEMENTI SPAN:"+spanlines.length+"</div>"; 
            for (var i = 0; i < spanlines.length; i++) {
            
            spanlines[i].setAttribute("style", "border:solid 2px red;");
            setTimeout(spanlines[i].parentNode.removeChild(spanlines[i]), 100);
            }        
         
          */
          document.getElementById ("container").innerHTML="";
           
          //  if (isFunction(l_ab)) l_ab.remove(  );   ?????????????????????????????????
          //  if (isFunction(l_bc))l_bc.remove(   );
          //  if (isFunction(l_ac))l_ac.remove(  );      
           
             var pa=new TPoint(this.a.x, this.a.y);//REMOVECOMMENT this.a.x,this.a.y);
             var pb=new TPoint(this.b.x,this.b.y);//REMOVECOMMENT this.b.x,this.b.y);
             var pc=new TPoint(this.c.x,this.c.y);//REMOVECOMMENT this.c.x,this.c.y);
             
             pa.name="A";
             pb.name="B";
             pc.name="C";
             
           /*    pa.trace();
             pb.trace();
             pc.trace();           
         */
         
            var l_ab =new TLine(pa,pb);
            var l_bc =new TLine(pb,pc);
            var l_ac =new TLine(pa,pc);   
            
            l_ab.name="ab";
            l_bc.name="bc";
            l_ac.name="ac";
      
            l_ab.trace( this.where );   
            l_bc.trace( this.where );
            l_ac.trace( this.where );   

            document.getElementById("checkitout").innerHTML="<div>this.b.x:"+Math.ceil(this.b.x)+"  this.b.y:"+Math.ceil(this.b.y)+"</div>";         
        
     };

      this.ruotax= function(alfa)
     {

     this.a.ruotax();
     this.b.ruotax();
     this.c.ruotax();

     };

      this.ruotay= function(alfa)
     {

     this.a.ruotay();
     this.b.ruotay();
     this.c.ruotay();

     };


 };




function Circus(namePath, x, y, z, radius, precision)
 {
 this.id=namePath;
 this.x=x;
 this.y=y;
 this.z=z;
 this.precision=precision;
 this.where = null;
 this.Tpoints= Array();
 this.points= Array();
 this.radius=radius;
 
           
var pointIdentifier=0;
var alpha=0.0 ;
   for ( ; alpha < 2*Math.PI;alpha=alpha+this.precision )
  {
  //document.getElementById("ELEMINSPAN").innerHTML= document.getElementById("ELEMINSPAN").innerHTML+"<div>aaa</div>";
   this.points[pointIdentifier]=new point("PT_"+pointIdentifier);
    this.points[pointIdentifier].x=this.radius*Math.cos(alpha)+ this.x;
   this.points[pointIdentifier].y=this.radius*Math.sin(alpha)+ this.y;
   this.points[pointIdentifier].z=this.z;       
    pointIdentifier++;
  }

 
 
 this.trace=function()
     {
     var pointIdentifier=0;
     var alpha=0.0 ;
          for ( ; alpha < 2*Math.PI;alpha=alpha+this.precision )
          {
           this.Tpoints[pointIdentifier]=new TPoint(this.points[pointIdentifier].x,this.points[pointIdentifier].y);
           this.Tpoints[pointIdentifier].name="("+Math.ceil(this.Tpoints[pointIdentifier].x)+", "+Math.ceil(this.Tpoints[pointIdentifier].x)+") - "+this.x;
           this.Tpoints[pointIdentifier].name=" "+"."+" ";
           if (pointIdentifier%4==0) this.Tpoints[pointIdentifier].name=" "+pointIdentifier+" ";;
           //this.Tpoints[pointIdentifier].tracePoint(Math.log((this.points[pointIdentifier].z+112)/this.points[pointIdentifier].z));
           
           var k=1;
       //    if (this.points[pointIdentifier].x>400)   k=5;
       //    if (this.points[pointIdentifier].y<300)   k=1;
       
       
if (this.points[pointIdentifier].z<-10)   k=2;
if (this.points[pointIdentifier].z<-40)   k=2;
if (this.points[pointIdentifier].z<-60)   k=3;
if (this.points[pointIdentifier].z<-80)   k=4;
if (this.points[pointIdentifier].z<-100)   k=5;

 
 
           this.Tpoints[pointIdentifier].tracePoint(k);
          pointIdentifier++;
          }
          //            document.getElementById("checkitout").innerHTML="";         
     };

      this.ruotax= function(alfa)
     {
       var i=0;     
      for (i=0 ; i< this.points.length; i++)
          {
           this.points[i].ruotax(alfa);
          }

     };

      this.ruotay= function(alfa)
     {
       var i=0;     
      for (i=0 ; i< this.points.length; i++)
          {
           this.points[i].ruotay(alfa);
          }     
     };


 };


//////////////////////////////////////////
//SFERA

function Sphaera(namePath, x, y, z, radius, precision)
 {
 this.id=namePath;
 this.x=x;
 this.y=y;
 this.z=z;
 this.precision=precision;
 this.where = null;
 this.circuum= Array();
 this.radius=radius;
 this.pcentro=Array();


 this.tempMaxz=0;
 this.tempMinz=99999;
  
           
var pointIdentifier=0;
var alpha=0.0;
   for ( ; alpha < 2*Math.PI;alpha=alpha+this.precision )
  {
  //alert (alpha+this.precision);
   this.circuum[pointIdentifier]=new Circus("circus_"+pointIdentifier,  this.x,  this.y , this.radius*Math.cos(alpha)+ this.z, this.radius*Math.sin(alpha), precision);
   pointIdentifier++;
  }

 
 this.setCentro=function(color)
 {
 //ARRAY CONTENENTE I PUNTI DELL'ORBITA
 //TODO: DOPO UN PO' DEVO SVUOTARE L'aRRAY ALTRIMENTI SI RIEMPIE ALL' INFINITO
  var ActualIndex= this.pcentro.length;
  this.pcentro[ActualIndex]=new point("PT_Centro");
  this.pcentro[ActualIndex].x=this.radius*Math.cos(alpha)+ this.x;
  this.pcentro[ActualIndex].y=this.radius*Math.sin(alpha)+ this.y-11;
  this.pcentro[ActualIndex].z=this.z;  


  this.cagaFuoriLaTraccia(color);
 }
 

 
 this.cagaFuoriLaTraccia=function(color)
 {
 var k;
 for (k=0; k<this.pcentro.length;k++)
   {
   if (k<13)
    {
     //IL -3 è perchè ho la correzione sulla profondita
     puntoTraccia=new TPoint(this.pcentro[k].x,this.pcentro[k].y);
     puntoTraccia.name="Centro "+k;
     profondita=this.pcentro[k].z;


 var dm="#ffdddd";
    if (profondita<140.0) dm="#ffcccc";
    if (profondita<40.0)   dm="#ffaaaa";
    if (profondita<0.0)   dm="#ff8888";
    if (profondita<-70.0)   dm="#ff4444";
    if (profondita<-100.0)  dm="#ff0000";


      var zidx=Math.ceil(this.pcentro[k].z );
 

    
     if (this.tempMaxz<profondita)this.tempMaxz=profondita;
     if (this.tempMinz>profondita)this.tempMinz=profondita;   
     
     /*ho scoperto che va più o meno da -178 a +177*/

  /*   profondita=Math.ceil(((profondita-220)/320) *100);
     col="0";
     if (profondita<10) col="#ff"+ profondita + "0"+ profondita + "0";
     else
     { 
       col="#ff"+ profondita ;
       col=col+ profondita ;
     }
*/


     
     document.getElementById("ELEMINSPAN").innerHTML= "<div>zind: "+zidx+" x: "+Math.ceil(this.pcentro[k].z)+" PROF: "+profondita+" CLR: "+dm+" LEN: "+this.pcentro.length+" MIN:"+Math.ceil(this.tempMinz)+" MAX:"+Math.ceil(this.tempMaxz)+"</div>";
      
     //puntoTraccia.width= (16-k);
    puntoTraccia.zindex=-zidx+200; 
     puntoTraccia.color = dm;
     puntoTraccia.tracePoint(13-k);
     //alert("cagofiori:" +puntoTraccia.x + " - "+puntoTraccia.y);
    
    }
   else
   {this.pcentro.pop();}
   
 
   
   
   }
   
 }
 
 
 
 
 this.trace=function()
     {
       var i=0;     
      for (i=0 ; i< this.circuum.length; i++)
          {
         
           this.circuum[i].trace();
          }
      };

      this.ruotax= function(alfa)
     {
      var i=0;     
      for (i=0 ; i< this.circuum.length; i++)
          {
           this.circuum[i].ruotax(alfa);
          }
          
      /*GESTIONE CENTRO*/    
      if ( this.pcentro.length>0)
      {
        var k;
         for (k=0; k<this.pcentro.length;k++)
            {
            this.pcentro[k].ruotax(alfa);
            }
      }          

     };

      this.ruotay= function(alfa)
     {
      var i=0;     
      for (i=0 ; i< this.circuum.length; i++)
          {
           this.circuum[i].ruotay(alfa);
          } 
      /*GESTIONE CENTRO*/    
      if ( this.pcentro.length>0)
      {
        var k;
         for (k=0; k<this.pcentro.length;k++)
            {
            this.pcentro[k].ruotay(alfa);
            }
      }            
              
     };


 };




/////////////////////////////////////////////


function CLEAR()
{
document.getElementById("container").innerHTML="";
}


