var Galeria = Class.create();

Galeria.prototype = {
  
  initialize : function (opciones)
  {
    this.opciones = Object.extend({
      contenedor : null,
      arriba : null,
      abajo : null,
      previa : { imagen : null, path : null },
      zoom : { contenedor : null, imagen : null, path : null, triggers: [] },
      evento : 'mouseover'
    }, opciones);
    
    this.contenedor = $(this.opciones.contenedor);
    if (this.contenedor)
    {
      Event.observe(this.opciones.arriba, 'click', this.Up.bindAsEventListener(this));
      Event.observe(this.opciones.abajo, 'click', this.Down.bindAsEventListener(this));
      
      this.previa = this.opciones.previa;
      this.zoom = this.opciones.zoom;
      if (this.previa.imagen && this.previa.path)
      {
        cn = this.contenedor.getElementsByTagName('img');
        for (var i = 0; i < cn.length; i++)
        {
          Event.observe(cn[i], this.opciones.evento, this.Preview.bindAsEventListener(this));
        }
      }
      
      if (this.zoom.imagen && this.zoom.path)
      {
        for (var i = 0; i < this.zoom.triggers.length; i++)
        {
          Event.observe(this.zoom.triggers[i], 'click', this.Zoom.bindAsEventListener(this));
        }
        
        Event.observe(this.zoom.imagen, 'click', this.UnZoom.bindAsEventListener(this));
      }
      
    }
  },
  
  Up : function ()
  {
    var c = this.contenedor;
    var cn = c.getElementsByTagName('img');
    var n = cn[cn.length - 1].cloneNode(true);
    
    Event.observe(n, this.opciones.evento, this.Preview.bindAsEventListener(this));
    c.insertBefore(n, cn[0]);
    
    Event.stopObserving(cn[cn.length - 1], this.opciones.evento, this.Preview.bindAsEventListener(this));
    c.removeChild(cn[cn.length - 1]);
  },
  
  Down : function ()
  {
    var c = this.contenedor;
    var cn = c.getElementsByTagName('img');
    var n = cn[0].cloneNode(true);
    
    Event.observe(n, this.opciones.evento, this.Preview.bindAsEventListener(this));
    c.appendChild(n);
    
    Event.stopObserving(cn[0], this.opciones.evento, this.Preview.bindAsEventListener(this));
    c.removeChild(cn[0]);
  },
  
  Preview : function (e)
  {
    var item = Event.element(e);
    var src = item.src.substr(item.src.lastIndexOf('/') + 1);
    
    $(this.previa.imagen).src = this.previa.path + src;
    
    if (this.zoom.imagen && this.zoom.path)
    {
      $(this.zoom.imagen).src = this.zoom.path + src;
    }
    
  },
  
  Zoom : function () { $(this.zoom.contenedor).show(); },
  UnZoom : function () { $(this.zoom.contenedor).hide(); }
  
}