
///
///
///   tadloader.js
///
///   (c) Sabu Francis, May 21, 2007
///
///   Loads TAD ascii files upto version 5.6
///   This is meant for use of TAD files wherever, usage of Javascript is legal
///   Use <script src="tadloader.js"(this file)...> (or whatever it is that
///   loads a Javascript file in your application) before doing anything else.

///   After you load this Javascript file into your HTML/XHTML/SVG/PDF/SWF
///   You can directly load a TAD ascii file as a Javascript file
///   The following object arrays: atom_objects,conn_objects,envl_objects,
///   art_objects, node_objects,pseudo_objects and an array called "classes" 
///   would be populated for later use by the HTML/XHTML/SVG/PDF/SWF

///   Avoid loading more than one TAD file per application in this manner. This 
///   is because the "inheritance" mechanism may pick up values of ardela props 
///   from some the class of some other file.

///   Version 0.1

///   This version does NOT do pinning OR work out cloned objects


var undefined; ///hmmmm.... 
//see http://www.webreference.com/programming/javascript/gr/column9/index.html


/**********Pius routines*********/

//Pius routine for meshes
function piusa(pno,doubleArr) {


 }

//object ID 
function oida(objid) {


}

//confirmation count
function cfrma(count) {


}

/*************headers, mostly not used **************/
function tvr(versionStr) {

}
function tprj(projInfoList) {


}

function prj(a,b,c) { 


}

function tpos(x,y){


}


//You could use this to zoom to right location
function cv(x1,y1,x2,y2){

}

///These are not present in the ascii version of TAD files
function od (s){ } //office ID
function pd (s) { } //project ID 


/////////helper for objectType

function mobj(oType) {
	return new mObject(oType);	
      }	

function mObject(oType){
      this.objType = oType;
      }


function tadFil(tFl) {
       return new tadFile(tFl);
      }	

function tadFile(tFl){      
       this.tadFilId = tFl;
      }
      


  
 var so = "simple";
 var sl = "sloping";

 
 function mo(oType) { return mobj(oType); }

 function tf(tFl) { return tadFil(tFl); }
 
 
//////////Calculating extents of file
var xmin,ymin,xmax,ymax;

 
//////////helper for point object

function tadpt(x1,y1,c1){
	this.x = x1;
	this.y = y1;
	this.c = c1;

	}

function xyc(x1,y1,c1) {
	var y1a = -1.0 * y1;
	if (xmin == undefined ) { xmin = xmax = x1; ymin = ymax = y1a; } 
	   else
	    {
	      if (x1 < xmin) { xmin = x1; }
	      if (x1 > xmax) { xmax = x1; }
	      if (y1a < ymin) { ymin = y1a; }
	      if (y1a > ymax) { ymax = y1a; }
	    }
	    
	return new tadpt(x1,y1a,c1);
	}

///////////helper for qpreds

function qp(what,val,inhOrNot) {
         this.what = what;
         this.val = val;
         this.inh = inhOrNot;
         }
      

function ht(h,inh) {
	return new qp("ht",h,inh);
	
	
	}
function lvl(l,inh) {
	return new qp("lvl",l,inh);
	
	}
	
function iskindof(k,inh) {
	return new qp("ko",k,inh);
	
	}

function hasa(a,b,c,d,e,f,g ) { }


//////////////////Ground planes

function gp(GpName,Arr,p1,p2,p3,p4,p5,p6) {  }

//////////////////////constructors for objects and classes

function cla(classname,cref,parent_cref,
	     count, 
	     qardelaArr, 
	     NoninheritableStr ,
	     InheritableStr) {
  this.name = classname;
  this.cref = cref;
  this.parentcref = parent_cref;
  this.count = count;
  this.qardelaArr = qardelaArr;
  this.nInhCode = NoninheritableStr;
  this.InhCode = InheritableStr;

}	     


function tobj( classNo,objName,objType,ilist,botArr,topArr,piusParamsArr,helperPos,qardelaArr,
	      selfCode,cloneCode,desc) {
 

 this.cref = classNo;
 this.thename = objName;
 this.objType = objType;
 this.flags = ilist;
 this.botArr = botArr;
 this.topArr = topArr;
 this.piusArr = piusParamsArr;
 this.helperPos = helperPos;
 this.qardelaArr = qardelaArr;
 this.selfCode = selfCode;
 this.cloneCode = cloneCode;
 this.desc = desc;
 

}


function referred(objref, //Cref of dependent object        
                 objname, //Name of dependent object
                 parobjref, //Cref of object from where generated
                 parobjname, //Name of object from where generated
                 genTypeInt, //Generation type see the mk... constants above
                 paramsArr,//Parameters used as per Generation type as follows
                         //and also see predicate getShapeFromOtherObj defined in CREATEOBJ.PRO
                 
                 transArr, 
                 //Extra translation parameters incase the object was individually shifted
                 
                 rotArr 
                 //Extra rotation parameters incase the object was individually rotated
                 )
 {
 
 
 
 }


/////////////////Finding out the "kindOf" object


function traverseAnyQPForVal(qardelaArr,wha) {
    for (var i = 0; i < qardelaArr.length; i++) 
      {
        if (qardelaArr[i].what == wha) {
             
            return qardelaArr[i].val;
          }
      
      }
       return null;
       
   }

function traverseParentClassesForVal(classNo,wha) {
    
     var cla = fetchClassNo(classNo);
 
     for (var i = 0; i < cla.qardelaArr.length; i++) 
      {
        if ((cla.qardelaArr[i].what == wha) && (cla.qardelaArr[i].inh == 1) ) {
             
            return cla.qardelaArr[i].val;
          }
      
      }

     if (cla.cref == 0) //if the class being examined is the current class
        return 4;  //then dont traverse back further
      else 
         return traverseParentClassesForVal(cla.parentcref,wha); 

  }

function fetchClassNo(classNo) {
  for (var i=0; i < classes.length; i++) {
    if (classes[i].cref == classNo) 
    	return classes[i];
   
   }
   alert ("TAD file is corrupt!");
  //Should not reach here!
 }


/************ populate the JS arrays ***********/

classes = new Array ( );
atom_objects = new Array ( ); //1
conn_objects = new Array ( ); //2
envl_objects = new Array ( ); //3
art_objects = new Array ( );  //4
node_objects = new Array ( ); //5
pseudo_objects = new Array ( ); //6

function cl(classname,cref,parent_cref,
	     count, 
	     qardelaArr, 
	     NoninheritableStr ,
	     InheritableStr) {
claObj = new cla(classname,cref,parent_cref,
	     count, 
	     qardelaArr, 
	     NoninheritableStr ,
	     InheritableStr);

classes[classes.length] = claObj;	     

} 


function tob( classNo,objName,objType,ilist,botArr,topArr,piusParams,helperPos,qardelaArr,
	      selfCode,cloneCode,desc) {

aObj = new tobj(classNo,objName,objType,ilist,botArr,topArr,piusParams,helperPos,qardelaArr,
	      selfCode,cloneCode,desc);

var ko = traverseAnyQPForVal(qardelaArr,"ko");

if (ko == null)  {
    var parCla = fetchClassNo(classNo); 
    ko = traverseAnyQPForVal(parCla.qardelaArr,"ko"); }
   
if (ko == null) {
    ko = traverseParentClassesForVal(parCla.parentcref,"ko");

   }

switch(ko) {

   case 1:
   	  atom_objects[atom_objects.length]= aObj;
          break;
    case 2: 
          conn_objects[conn_objects.length]= aObj;
          break;
          
    case 3:
          envl_objects[envl_objects.length]= aObj;
          break;
     
    case 4:
     	  art_objects[art_objects.length]= aObj;
          break;
  
    case 5:
          node_objects[node_objects.length]= aObj;
          break;
          
    default:      
          pseudo_objects[pseudo_objects.length]= aObj;

          
  }

}

