DailyPhoto = Class.create();

DailyPhoto.prototype = {
  elemsI: new Array(),
  elemsIC: new Array(),
  initialize: function(node) {
    var params=Utils.getParams(node);
    this.prefix = node.id;
    if (!params["opacity"]) params["opacity"] = 50;
    Element.getElementsByClassName(node,"bg").each(function(el){
        Element.setStyle(el,{opacity: params["opacity"]/100.0});
              el.style.zIndex = 2;
    });
    this.paused=false;
    this.speed = 1*params["speed"]/1000;
    this.foto = 1*params["photo"];
    this.fotoCount = 1*params["photoCount"];
    this.btns = document.getElementById(this.prefix + '_switch').getElementsByTagName('a');
    this.btns[this.foto].className = 'sel';
    this.borderColor=$(this.prefix+"_content").getStyle("borderColor");
    this.surColor=$(this.prefix+"_content").parentNode.getStyle("backgroundColor");
    this.content=$(this.prefix+"_content");
    this.content.setStyle({borderColor:this.surColor});
    $(this.prefix+"_content").setStyle({borderWidth:'2px'});
    Event.observe(node, 'mouseover', this.mouseoverHandler.bindAsEventListener(this));
    Event.observe(node, 'mouseout', this.mouseoutHandler.bindAsEventListener(this));
    var fc=this.fotoCount;
    for (var i = 0; i < fc; i++) {
      var elem = $(this.prefix + '_i' + i);
      this.elemsI[i]=elem;
      elem.style.zIndex = -1*i;
      if(i>0) {
    elem.setStyle({opacity: 0.0})
          Element.removeClassName(elem,"hidden");
      }
      elem = $(this.prefix + '_ic' + i);
      this.elemsIC[i]=elem;
      elem.style.zIndex = -1*i;
      if(i>0) {
    elem.setStyle({opacity: 0.0})
          Element.removeClassName(elem,"hidden");
      }
    }
    var bt=this.btns;
    for (var i = 0; i < bt.length; i++) {
      bt[i].index = i;
      Event.observe(bt[i], 'click', this.changePhoto.bindAsEventListener(this));
    }
    this.timer=setTimeout(this.next.bind(this), (this.speed*1000) + Utils.getTInterval());
    this.working=false;
    Event.observe(window,"unload",this.cleanup.bind(this));
  },
  cleanup: function() {
    this.btns=null;
    this.content=null;
    clearTimeout(this.timer);
  },
  mouseoverHandler: function(){
    this.content.setStyle({borderColor:this.borderColor});
    this.pause();
  },
  mouseoutHandler: function(){
    this.content.setStyle({borderColor:this.surColor});
        //$(this.prefix+"_content").setStyle({borderWidth:'2px'});
    this.release();
  },
  next: function() {
    if (this.working) return;
    this.working=true;
    this.timer=setTimeout(this.next.bind(this), this.speed*1000);
    if (!this.paused) this.forceChange(this.foto + 1);
    this.working=false;
  },
  pause: function(event) { this.paused=true; },
  release: function(event) { this.paused=false; },
  changePhoto: function(event) {
    this.forceChange(Event.element(event).index);
    return false;
  },
  forceChange: function(f) {
    var fr=20;
    var elemsI=this.elemsI;
    var elemsIC=this.elemsIC;
    new Effect.Opacity(elemsI[this.foto], {duration:1.0, from:1.0, to:0.0, fps:fr});
    new Effect.Opacity(elemsIC[this.foto], {duration:1.0, from:1.0, to:0.0, fps:fr});
    this.btns[this.foto].className = '';
    elemsIC[this.foto].style.zIndex = -1*this.foto;
    elemsI[this.foto].style.zIndex = -1*this.foto;
    this.foto = f;
    if (this.foto >= this.fotoCount) this.foto = 0;
    new Effect.Opacity(elemsI[this.foto], {duration:1.0, from:0.0, to:1.0, fps:fr});
    new Effect.Opacity(elemsIC[this.foto], {duration:1.0, from:0.0, to:1.0, fps:fr});
    this.btns[this.foto].className = 'sel';
    elemsI[this.foto].style.zIndex = 1;
    elemsIC[this.foto].style.zIndex = 3;
  }
}
Event.observe(window,'load',function () {
        document.getElementsByClassName('dailyPhoto').each(function (node, ind){
            new DailyPhoto(node);
        });
  });


