/*
  targetpref = ID prefix, eg; ROTATE would target ROTATE_ROTATOR, ROTATE_IMG etc..
  imagesdir = root dir for images to be pulled from, will prepend all imgs
  aImageUrls = an array of JSON objects array( { imgsrc: str, caption: str, url: str} )
  rotateMS = number of milliseconds between rotating to next image
*/
function ld_imagefader(targpref, imagesdir, aImageUrls, rotateMS)
{
  return {

    aImageUrls : aImageUrls,
    aImages : null,
    iImagesLoaded : 0,
    iTotalImages : 0,
    iCurrImgIdx : 0,
    sImagesDir : imagesdir,
    iRotateMs : rotateMS,
    sTargetPrefix : targpref,
        
    setup : function()
    {
      $("#"+this.sTargetPrefix+"_ROTATOR").myobj=this;
      //alert(this.aImageUrls[0].imgsrc);
      this.iImgagesLoaded=0;
      this.aImages=new Array();
      this.iTotalImages = this.aImageUrls.length;
      for(a in this.aImageUrls)
      {
        var img = new Image();
        img.myobj=this;
        img.test=123;
        img.onload = this.imageLoaded;
//        img.src = './images/splash/'+this.aImageUrls[a].imgsrc;
        img.src = this.sImagesDir + this.aImageUrls[a].imgsrc;
		n=this.aImages.length;
        this.aImages[n] = img;
        //document.write(this.aImages[n].src + '<br/>');
      }
    },
    
    imageLoaded : function()
    {
      // 'this' is now the image object that invoked the event
      // not the ld_imagefader object !!
      this.myobj.iImagesLoaded++;
      if (this.myobj.iImagesLoaded>=this.myobj.iTotalImages)
      {
        abc=this.myobj;
        setTimeout('abc.rotate()', abc.iRotateMs);
        //this.myobj.rotate();
      }
    },
    
    rotate : function()
    {
      //alert('rotate');
      repl=document.getElementById(this.sTargetPrefix+"_ROTATOR");
      document.getElementById(this.sTargetPrefix+"_ROTATOR").myobj=this;
//      $("#"+this.sTargetPrefix+"_ROTATOR").fadeOut('slow', this.fadeBackIn);
      bgimg=document.getElementById(this.sTargetPrefix+"_IMG_BG");
      fgimg=document.getElementById(this.sTargetPrefix+"_IMG");
      bgimg.src=fgimg.src;
      this.fadeBackIn2();
    },

    fadeBackIn2 : function() 
    {
      // 'this' is now the image object that invoked the event
      // not the ld_imagefader object !!
      this.iCurrImgIdx++;
      if (this.iCurrImgIdx>this.aImages.length-1) this.iCurrImgIdx=0;
      document.getElementById(this.sTargetPrefix+'_CAPTION').innerHTML=this.aImageUrls[this.iCurrImgIdx].caption;
      //document.getElementById(this.sTargetPrefix+'_ROTATOR').onclick = function() { document.location='http://'+this.aImageUrls[this.iCurrImgIdx].url; }
      
      $("#"+this.sTargetPrefix+"_ROTATOR").hide();
      document.getElementById(this.sTargetPrefix+'_IMG').src=this.sImagesDir+this.aImageUrls[this.iCurrImgIdx].imgsrc;
      $("#"+this.sTargetPrefix+"_ROTATOR").fadeIn('slow');
      //window.status = 'curr idx : ' + this.iCurrImgIdx;
      // set time out to when we want to do the next transition
      abc = this;
      setTimeout('abc.rotate()', this.iRotateMs);
    },

    fadeBackIn : function() 
    {
      // 'this' is now the image object that invoked the event
      // not the ld_imagefader object !!
      this.myobj.iCurrImgIdx++;
      if (this.myobj.iCurrImgIdx>this.myobj.aImages.length-1) this.myobj.iCurrImgIdx=0;
      document.getElementById(this.myobj.sTargetPrefix+'_CAPTION').innerHTML=this.myobj.aImageUrls[this.myobj.iCurrImgIdx].caption;
      document.getElementById(this.myobj.sTargetPrefix+'_ROTATOR').onclick = function() { document.location='http://'+this.myobj.aImageUrls[this.myobj.iCurrImgIdx].url; }
      document.getElementById(this.myobj.sTargetPrefix+'_IMG').src=this.myobj.sImagesDir+this.myobj.aImageUrls[this.myobj.iCurrImgIdx].imgsrc;
      $("#"+this.myobj.sTargetPrefix+"_ROTATOR").fadeIn('slow');
      //window.status = 'curr idx : ' + this.iCurrImgIdx;
      // set time out to when we want to do the next transition
      abc = this.myobj;
      setTimeout('abc.rotate()', this.myobj.iRotateMs);
    }

  };
}
