if (typeof Widget=="undefined") {
  Widget={}
}
Widget.Blender=Class.create();
Widget.Blender.prototype={
  initialize: function(A,C,B) {
    this.img=$(A);
    this.list=C;
    this.options=Object.extend({id:this.img.id,fadeDuration:2.5,displayDuration:2.5,autoSize:false,autoStart:true,noWrap:false,attributes:{},dir:"",beforeBlend:null,startIndex:0},B||{});
    this.index=this.options.startIndex;
    if(this.options.noWrap) {
      this.container=this.img.up()
    } else {
      this.container=new Element("div").setStyle({position:"relative",width:(this.img.width+parseInt(this.img.getStyle("padding-left"))
        +parseInt(this.img.getStyle("padding-right"))+parseInt(this.img.getStyle("border-left-width"))
        +parseInt(this.img.getStyle("border-right-width")))+"px",
      height:(this.img.height+parseInt(this.img.getStyle("padding-top"))
        +parseInt(this.img.getStyle("padding-bottom"))+parseInt(this.img.getStyle("border-top-width"))
        +parseInt(this.img.getStyle("border-bottom-width")))+"px"});
      this.img.up().replaceChild(this.container,this.img);
      this.container.appendChild(this.img)
    } 
    this.loadedObserver=this.loaded.bind(this);
    this.nextObserver=this.next.bind(this);
    this.stopped=true;
    if(this.options.autoStart){
      setTimeout(this.start.bind(this),this.options.displayDuration*1000)
    }
  },
  
  start: function() {
    if(!this.stopped) {
      return
    }
    this.stopped=false;
    this.next()
  },
  
  stop: function() {
    this.stopped=true;
    try{
      clearTimeout(this.timeout)
    } catch(A) {}
    try{
      Effect.Queues.get(this.options.id).each(function(B){B.cancel()})
    } catch(A) {}
    if(this.oldImg) {
      this.container.removeChild(this.oldImg);
      this.oldImg=null
    }
    Element.setOpacity(this.img,1)
  },
  
  next: function(){
    if(this.oldImg) {
      this.container.removeChild(this.oldImg)
    }
    this.oldImg=this.img;
    if(this.stopped||this.list.length==0){return }++this.index;
    if(this.index>=this.list.length) {
      this.index=0
    }
    this.img=new Element("img",this.options.attributes);
    this.img.observe("load",this.loadedObserver);
    this.img.src=this.options.dir+this.list[this.index]
  },
  
  loaded: function() {
    this.img.stopObserving("load",this.loadedObserver);
    if(this.options.autoSize){
      this.resize(this.img)
    }
    this.img.setOpacity(0);
    this.container.appendChild(this.img);
    this.img.setStyle({position:"absolute",top:this.container.getStyle("padding-top"),left:((parseInt(this.container.getStyle("padding-left"))+(this.container.getDimensions().width-this.img.width)/2)+'px')});
    if(typeof this.options.beforeBlend=="function"){
      this.options.beforeBlend(this.oldImg,this.img)
    }
    new Effect.Opacity(this.oldImg,{duration:this.options.fadeDuration,from:1,to:0,queue:{scope:this.options.id}});
    new Effect.Opacity(this.img,{duration:this.options.fadeDuration,from:0,to:1,queue:{scope:this.options.id}});
    this.timeout=setTimeout(this.nextObserver,(this.options.fadeDuration+this.options.displayDuration)*1000)
  },
  
  resize: function(B) {
    var F=this.container.getDimensions();
    F.width-=parseInt(this.container.getStyle("padding-left"))+parseInt(this.container.getStyle("padding-right"))+parseInt(this.container.getStyle("border-left-width"))+parseInt(this.container.getStyle("border-right-width"));
    F.height-=parseInt(this.container.getStyle("padding-top"))+parseInt(this.container.getStyle("padding-bottom"))+parseInt(this.container.getStyle("border-top-width"))+parseInt(this.container.getStyle("border-bottom-width"));
    var C=F.width/B.width;
    var E=F.height/B.height;
    var A=B.width*E;
    var D=B.height*C;
    if(C>E){
      B.width=A;
      B.height=F.height
    } else {
      B.width=F.width;
      B.height=D
    }
  }
};
var Blender=Widget.Blender