var jl_mvcmgr = null;
jl_Imagewrapper = function(image,clickhref){

    //now check to see if param 1 is a string or
    // a javascript image

    if(typeof(image.src) !== 'undefined'){

         this.image = image;
            
    }else{

        this.image = new Image();
        this.image.src = image;
          
    }
 
    this.clickhref = clickhref;

}//end imagewrapper ctor

jl_Imagewrapper.prototype.loadcomplete = function(){
	return this.image.completed;
}


jl_Imagewrapper.prototype.imageloadstatus = function(){
   return this.imageloaded;
}

jl_Imagewrapper.prototype.getimagename = function(){
   return this.image.src;
}

jl_Imagewrapper.prototype.getclickhref = function(){
   return this.clickhref;
}

jl_Imagewrapper.prototype.sethtmlimage = function(theid){
    document.getElementById(theid).src = this.image.src;
}

jl_Imagewrapper.prototype.setclickhref = function(theid){
     document.getElementById(theid).href = this.clickhref;
}



jl_Mvcollagemgr = function(linkcolor,tablewidth,tableheight,updaterate){

    this.procstate = 0;
    this.procckload = 0;
    this.procgntble = 1;         
    this.procanimtble = 2;
    this.procabort = 3;
    this.procThandle = null;
    this.procmseconds = 1000;
    this.tablewidth = tablewidth;
    this.tableheight = tableheight;
    this.tablecellwidth = "100";
    this.tablecellheight = "68";
    this.tablesize = tablewidth*tableheight;
    this.imageheight = -1;
    this.imagewidth = -1;
    this.newwinonclick = false;
    this.imagewrapperarray = new Array(0);
    this.visipicref = null;
    this.invisipicref = null;
    this.updaterate = (updaterate * 1000) + .5;
    this.invisiindex = 0;
    this.linkcolor = linkcolor;

}//end mvcollagemgr ctor

jl_Mvcollagemgr.prototype.getimagename = function(index){
    if(index >= this.tablesize){
          //the user requested a table be created
          //that was larger than num images loaded
          index = 0;

    }     
    return this.imagewrapperarray[index].getimagename();
}

jl_Mvcollagemgr.prototype.setimageheight = function(height){
     this.imageheight = height;
}

jl_Mvcollagemgr.prototype.setimagewidth = function(width){
     this.imagewidth = width;
}

jl_Mvcollagemgr.prototype.settablecellwidth = function(width){
    this.tablecellwidth = width;
} 

jl_Mvcollagemgr.prototype.settablecellheight = function(height){
    this.tablecellheight = height;
}

jl_Mvcollagemgr.prototype.getclickhref = function(index){
    if(index >= this.tablesize){
          //the user requested a table be created
          //that was larger than num images loaded
          index = 0;
    }     
    return this.imagewrapperarray[index].getclickhref();
}

jl_Mvcollagemgr.prototype.numimagesloaded = function(){
    return this.imagewrapperarray.length;
}

jl_Mvcollagemgr.prototype.addimagedata = function (image,clickhref){
    var imgwrapper = new jl_Imagewrapper
(image,clickhref);
    this.imagewrapperarray.push(imgwrapper);
}
 
jl_Mvcollagemgr.prototype.sethtmlimage = function(index,theid){
    this.imagewrapperarray[index].sethtmlimage(theid); 
}

jl_Mvcollagemgr.prototype.setclickhref = function(index,theid){
         this.imagewrapperarray[index].setclickhref(theid); 
}

jl_Mvcollagemgr.prototype.setnewwinonclick = function(val){
      this.newwinonclick = val;
}

jl_Mvcollagemgr.prototype.imageldcomplete = function(){
    var i=0;
    for(i=0;i<this.imagewrapperarray.length;i++){
        if(this.imagewrapperarray[i].loadcomplete() === false){
            return false;
        }   
    }
    return true;
}

jl_Mvcollagemgr.prototype.initimageswapper = function(){

    if(this.numimagesloaded() <= this.tablesize){
         return false;
    }

    this.visipicref = new Array(this.tablesize);
    var invisipicrefsize = this.numimagesloaded() - this.tablesize;

    this.invisipicref = new Array(invisipicrefsize);
    var i = 0;

 
    for(i=0;i<this.visipicref.length;i++){
 
         this.visipicref[i] = i;

    }

    for(i=0;i<this.invisipicref.length;i++){
 
         this.invisipicref[i] = i + this.tablesize;

    }


    return true;
}

jl_Mvcollagemgr.prototype.writeheader = function(header){

    var headernode = document.createElement("h3");
    var txtnode = document.createTextNode(header);
    headernode.appendChild(txtnode); 
    jl_ut_getcontentdiv().appendChild(headernode); 

}

jl_Mvcollagemgr.prototype.gencollage = function(){
        

     var irow=0;
     var icol=0; 
     var idnum = 0;
          
     for(irow=0;irow<this.tableheight;irow++){
             
         jl_ut_getcontentdiv().appendChild(document.createElement("br"));
 
         for(icol=0;icol<this.tablewidth;icol++){

              var aAnchor = document.createElement("a");
              var aHtmlImage = document.createElement("img");

              if(this.newwinonclick){
                  aAnchor.target = "_blank";
              } else {
                  aAnchor.target = "_self";
              }  

              aAnchor.style.color = this.linkcolor;
              aAnchor.id = "mvchref" + String(idnum); 
              aAnchor.href = this.getclickhref(idnum); 
              aHtmlImage.id = "mvcimg" + String(idnum); 

              if(this.imageheight !== -1){
                  aHtmlImage.height = this.imageheight;
              } 

              if(this.imagewidth !== -1){
                  aHtmlImage.width = this.imagewidth;
              }
 
              aHtmlImage.src = this.getimagename(idnum); 
              aAnchor.appendChild(aHtmlImage);

              jl_ut_getcontentdiv().appendChild(aAnchor);
              idnum++;  

          }

              
     }

     return true;
}

jl_Mvcollagemgr.prototype.killanimthread = function(){
    window.clearTimeout(this.procThandle);
}

jl_Mvcollagemgr.prototype.procstartrun = function(mseconds){

    this.procstate = this.procckload;
    this.procmseconds = mseconds;

    if(this.procThandle !== null){
        window.clearTimeout(this.procThandle);
        this.procThandle = null;
    }
        
    jl_mvcprocrun();
}


jl_Mvcollagemgr.prototype.procrun = function(){
    
    switch(this.procstate){

        case this.procckload:

               if(this.imageldcomplete() === true){
                   this.procstate = this.procgntble;  
               }
               this.procmseconds = 500; 
               break;

        case this.procgntble:
                   
               if(this.initimageswapper() === false){
                   this.procmseconds = 500; 
                   return;
               }
                       
               if(this.gencollage() === false){
                   jl_ut_getcontentdiv().innerHTML += "<p>Not enough images loaded to create table. Must be more images loaded than size of table.</p>";

                    this.procstate = this.procabort;
                    return;

               }
                     

               this.procstate = this.procanimtble;
               this.procmseconds = this.updaterate; 
               break;

        case this.procanimtble:
              
               this.tableupdater(0); 
               break;

    }
}


jl_Mvcollagemgr.prototype.tableupdater = function(){

    if(this.invisipicref === null) return;
 
     var visiindex = jl_ut_randomize(0,(this.visipicref.length - 1));

     var invisiindex = this.invisiindex;

     var imgid = "mvcimg" + String(visiindex);
     var imgwrap = this.imagewrapperarray[this.invisipicref[invisiindex]]; 

     var clickhrefid = "mvchref" + String(visiindex);
     imgwrap.sethtmlimage(imgid);
     imgwrap.setclickhref(clickhrefid);

     //swap visible and invisible references
     var oldvisiref = this.visipicref[visiindex];
     this.visipicref[visiindex] = this.invisipicref[invisiindex];
     this.invisipicref[invisiindex] = oldvisiref;

     this.invisiindex +=  1;
     if(this.invisiindex >= this.invisipicref.length){
         this.invisiindex = 0;
     }

}

jl_Mvcollagemgr.prototype.abortthread = function(){

    return (this.procstate === this.procabort);       

}

jl_Mvcollagemgr.prototype.initthreadtimer = function(){

    this.procThandle = window.setTimeout(jl_mvcprocrun,this.procmseconds);

}



jl_mvcprocrun = function(){

     if(jl_mvcmgr === null){
         return;
     }
    
     if(jl_mvcmgr.abortthread() === false){
         
         jl_mvcmgr.procrun();
         jl_mvcmgr.initthreadtimer();
             
     }
},

jl_createmvcmgr = function(linkcolor,tablewidth,tableheight,updaterate){

      if(jl_mvcmgr=== null){
          jl_mvcmgr = new jl_Mvcollagemgr(linkcolor,tablewidth,tableheight,updaterate);
      }else{

         jl_mvcmgr.killanimthread();
         //just reinit it for now.
         jl_mvcmgr = new jl_Mvcollagemgr(linkcolor,tablewidth,tableheight,updaterate);

      } 
 
      return jl_mvcmgr;

}

