var siM = function () {
  var obj = [], scr, spa, img, W, Wi, Hi, wi, hi, Sx, Sy, M;
  var xb = 0, yb = 0, ob =  - 1;
  var Nx = 3; //size grid x 
  var Ny = 3; //size grid y 
  var Tx = 3; // image width 
  var Ty = 3; // image height
  var SP = 1; // velocidad
  var Cobj = function (o, x, y) {
    this.o = o;
    this.ix = Math.min(Nx - Tx, Math.max(0, Math.round(x - (Tx / 2))));
    this.iy = Math.min(Ny - Ty, Math.max(0, Math.round(y - (Ty / 2))));
    this.li = ((this.ix * M + this.ix * Sx) - (x * M + x * Sx)) / SP;
    this.ti = ((this.iy * M + this.iy * Sy) - (y * M + y * Sy)) / SP;
    this.x = x; this.y = y;
    this.l = 0; this.t = 0; this.w = 0; this.h = 0; this.s = -1;
    this.spa = spa[o].style;
    this.img = img[o];
    this.img.center = function() {
      this.style.left = Math.round( -(this.width - Wi) / 2) + "px";
      this.style.top  = Math.round( -(this.height - Hi) / 2) + "px";
    }
    this.img.onclick = function() { obj[o].click(); }
    this.img.onload = function() { this.center(); }
    this.zoom();
  }
  Cobj.prototype.zoom = function() {
    this.f = false;
    this.l += this.li * this.s;
    this.t += this.ti * this.s;
    this.w += wi * this.s;
    this.h += hi * this.s;
    if ((this.s > 0 && this.w < Wi) || (this.s < 0 && this.w > Sx)) {
      var o = this.o;
      setTimeout(function() { obj[o].zoom(); }, 16);
    } else {
      if (this.s > 0) {
        this.l = this.ix * M + this.ix * Sx;
        this.t = this.iy * M + this.iy * Sy;
        this.w = Wi; this.h = Hi;
      } else {
        this.l = this.x * M + this.x * Sx;
        this.t = this.y * M + this.y * Sy;
        this.w = Sx;
        this.h = Sy;
      }
    } 
    this.spa.left = Math.round(this.l) + "px";
    this.spa.top = Math.round(this.t) + "px";
    this.spa.width = Math.round(this.w) + "px";
    this.spa.height = Math.round(this.h) + "px";
    this.spa.zIndex = Math.round(this.w);
  }
  Cobj.prototype.click = function() {
    this.img.center();
    if (this.s > 0) {
      this.s = -1;
      this.zoom();
    } else if (this.s < 0) {
      if (ob >= 0) {
        obj[ob].s =  - 1;
        obj[ob].zoom();
      }
      ob = this.o;
      this.s = 1;
      this.zoom();
    }
  }
  var init = function (Wimg,Himg) {
    scr = document.getElementById("galEXP"); 
    scr.style.width = Wimg + "px"; scr.style.marginLeft = "-" + (Wimg / 2) + "px";
    scr.style.height = Himg + "px"; scr.style.marginTop = "-" + (Himg / 2) + "px";
    spa = scr.getElementsByTagName("span");
    img = scr.getElementsByTagName("img");
    W = scr.offsetWidth; H = scr.offsetHeight; M = W / 40;
    Sx = (W - (Nx - 1) * M) / Nx; Sy = (H - (Ny - 1) * M) / Ny;
    Wi = Tx * Sx + (Tx - 1) * M; Hi = Ty * Sy + (Ty - 1) * M;
    SP = M * Tx * SP;
    wi = (Wi - Sx) / SP; hi = (Hi - Sy) / SP;
     for (var k = 0, i = 0; i < Nx; i++) {
      for (var j = 0; j < Ny; j++) { obj[k] = new Cobj(k++, i, j); }
    } 
  } 
  return { init : init } 
}(); 
