
var fading = 0;
var ImageFader = Class.create();

ImageFader.prototype ={
  initialize: function(classname){
    this.effect = 1;
    this.blendtime = 1;
    this.fading =0;
    this.current_image = 0;
    this.next_image = 1;
    this.image = new Array();
    this.last_image = 0;
    this.class_name=classname?classname:'image';
    if(document.getElementsByClassName(this.class_name)) {
  		this.image = document.getElementsByClassName(this.class_name);
  		for(i=0; i < this.image.length; i++) {
  		  if(this.image[i].visible()){
  		    this.current_image = i;
  		    this.setCursors(i,this.image.length-1);
  		  }
  		}
    }
    	$(this.class_name+'_fader_max').innerHTML = this.image.length;        
  },
  setCursors: function(current, max){
    if(current==0 || current == max){
      if(current==0){
        this.next_image = 1;
        this.last_image = max;
      }
      if(current==max){
        this.next_image = 0;
        this.last_image = max-1;
      }    
    }else{
      this.next_image = current + 1;
      this.last_image = current -1;
    }  
      //$(this.class_name+'_fader_title').innerHTML = this.image[current].readAttribute('title')?this.image[current].readAttribute('title'):this.image[current].readAttribute('alt') ;
    	$(this.class_name+'_fader_current').innerHTML = current+1;  
  },
  nextimage: function(){
    if(fading==0){
      fading = 1;
      window.setTimeout("fading=0;",this.blendtime*700);
    	if(this.effect == 1) { new Effect.Fade(this.image[this.current_image],{duration: this.blendtime}); new Effect.Appear(this.image[this.next_image],{duration: this.blendtime}); }
    	if(this.effect == 2) { new Effect.BlindUp(this.image[this.current_image]); new Effect.BlindDown(this.image[this.next_image]); }
    	this.current_image = this.next_image;
    	this.setCursors(this.current_image, this.image.length-1);
  
    }
  },
  lastimage: function(){
    if(fading==0){
      fading = 1;
      window.setTimeout("fading=0;",this.blendtime*700); 
    	if(this.effect == 1) { new Effect.Fade(this.image[this.current_image],{duration: this.blendtime}); new Effect.Appear(this.image[this.last_image],{duration: this.blendtime}); }
    	if(this.effect == 2) { new Effect.BlindUp(this.image[this.current_image]); new Effect.BlindDown(this.image[this.last_image]); }
    	this.current_image = this.last_image;
    	this.setCursors(this.current_image, this.image.length-1);  	
    }
  }  
}
