// Original concept used from Ruby on Rails (http://www.rubyonrails.org)
// Adaptado por http://www.sentidocomun.es

var PlugShow = Class.create();
  PlugShow.prototype = {
    initialize: function(element, options) {
      this.element = $(element);
      this.options = Object.extend({className: this.element.id + '_plug', duration: 2}, options);
      //this.plugs = document.getElementsByClassName(this.options.className, this.element);
      //el getElementsByClassName da problemas en FF3 por eso lo quitamos
      this.plugs=$$("." + this.options.className);
      

      this.preparePlugs();
      this.registerCallback();
    },

    preparePlugs: function() {
      this.currentPlug = this.plugs.first();
      this.element.style.position = 'relative';
      this.element.style.height = this.plugs.max(function(plug) {
        var visible = Element.visible(plug), height;
        Element.setStyle(plug, {position: 'absolute', width: '100%', left: '0px'});
        if (!visible) Element.show(plug);
        //height = Element.getHeight(plug);
        height=alturafotosrotar;
        if (!visible) Element.hide(plug);
        return height;
      }).toString() + 'px';
    },

    nextPlug: function() {
      return this.plugs[(this.plugs.indexOf(this.currentPlug) + 1) % this.plugs.length];
    },

    registerCallback: function() {
      stoprotacion=window.setTimeout(this.tick.bind(this), this.options.duration * 1000);
    },

    tick: function() {
      var currentPlug = this.currentPlug, nextPlug = this.nextPlug();

      new Effect.Parallel([
        new Effect.Fade(currentPlug, {sync: true}),
        new Effect.Appear(nextPlug, {sync: true})
      ], {
        duration: 5,
        afterFinish: (function(effect) {
          this.currentPlug = nextPlug;
          this.registerCallback();
        }).bind(this)
      })
    }
  }

  Event.observe(window, 'load', function() {
    for (i=0;i<plugshowlist.length;i++) {
      alturafotosrotar=plugshowlist[i][1];
      new PlugShow(plugshowlist[i][0]);
    }

  })
