/* 
 * Class to make simple images changes
 * @Author Pavel Osipov
 * @Created 2009/09/24
 */


function imges_changer(imagesSourcesArr, targetImgID, loaderImgPath, trgtToLoaderImgInject){
   loaderImgPath = loaderImgPath;//path for loader animated image
   loaderImgObj = null;
   trgtToLoaderImgInject = trgtToLoaderImgInject;//here will be injected loader image
   loaderInjected = false;
   loaderVisible = false;

   imgsArr = imagesSourcesArr;
   curImgIndex = 0;
   
   targetIMG = null;



  this.init = function(autostart){
    this.injectLoader();

   targetIMG = document.getElementById(targetImgID);   
   if(targetIMG == null){
     alert('Error getting target image element');
     return;
   }
   targetIMG.onload = function(){document.getElementById(loaderImgObj.getAttribute('id')).style.display='none'};
		
		if(autostart){		
    	this.initCycle();
    }

  }

  this.initCycle=function()  {
    if(targetIMG){
      var foo = this;
      setTimeout(function(){foo.initCycle()}, 5000);
      this.switchImage();
    }
  }

 this.switchImage = function(){
   if(++curImgIndex > imgsArr.length - 1){
     curImgIndex = 0;
   }
   this.showLoading(true);   
   targetIMG.src =imagesSourcesArr[curImgIndex];
   
 }



   this.showLoading = function(bFlag){
    if(loaderInjected){      
        if(bFlag){
            loaderImgObj.style.display = '';
        }else{
            loaderImgObj.style.display = 'none';
        }
      }
    }

  this.injectLoader = function(){
    //add loader image to DOM    
    if(loaderImgPath){      
      loaderImgObj = document.createElement('img');
          loaderImgObj.setAttribute('src',loaderImgPath);
          loaderImgObj.setAttribute('alt','Loading..');
          loaderImgObj.setAttribute('title','Loading..');
          loaderImgObj.setAttribute('id','ldr_img');
          loaderImgObj.style.position	= 'fixed';
          loaderImgObj.style.display	= 'none';
          loaderImgObj.style.left		= (this.getClientWidth()/2) - 25 + 'px';
          loaderImgObj.style.top		= 200 + 'px';
          loaderImgObj.style.zIndex	= 3;
      document.getElementById(trgtToLoaderImgInject).appendChild(loaderImgObj);
      loaderInjected = true;
    }
  }


  this.addImg = function(path){
    if(path){
      imgsArr.push(path);
    }
  }




  this.getClientWidth = function(){
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
  }
  this.getClientHeight = function(){
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
  }


}


