MTJax.Imager = {
  initialize: function(Ident, Caller, ToThumb)
  {
    if (!arguments[2]) 
      ToThumb = false;
    
    new Request.JSON({
      url: '/plugin/de.masstisch.ajax/Galerie/getImages.php',
      data: 'Search=' + encodeURIComponent(Ident),
      onComplete: this.step2.bind(this)
    }).send();
    
    if ('hid' == Ident.split('_')[0]) 
    {
      this.ScalerOptOptions = 'croph';
    }
    
    this.Img = $('jax_imager_img');
    this.Caller = Caller;
    this.ToThumb = ToThumb;
    
    this.Act = $('jax_imager_act');
    this.Prev1 = $('jax_imager_prev1');
    this.Prev2 = $('jax_imager_prev2');
    this.Next1 = $('jax_imager_next1');
    this.Next2 = $('jax_imager_next2');
    
    this.ArPrev = $('jax_imager_prev');
    this.ArNext = $('jax_imager_next');
    this.ArPrev.onclick = this.down.bind(this);
    this.ArNext.onclick = this.up.bind(this);
    
    this.Act.src = '/media/Image/Icons/blind.gif';
    this.Prev1.src = '/media/Image/Icons/blind.gif';
    this.Prev2.src = '/media/Image/Icons/blind.gif';
    this.Next1.src = '/media/Image/Icons/blind.gif';
    this.Next2.src = '/media/Image/Icons/blind.gif';
    
    this.Prev1.JumpSpan = -1;
    this.Prev2.JumpSpan = -2;
    this.Next1.JumpSpan = 1;
    this.Next2.JumpSpan = 2;
    
    this.Prev1.onclick = this.chgActual;
    this.Prev2.onclick = this.chgActual;
    this.Next1.onclick = this.chgActual;
    this.Next2.onclick = this.chgActual;
    
    this.Prev1.Imager = this;
    this.Prev2.Imager = this;
    this.Next1.Imager = this;
    this.Next2.Imager = this;
  },
  Img: null,
  Caller: null,
  Act: null,
  Prev1: null,
  Prev2: null,
  Next1: null,
  Next2: null,
  Pool: [],
  Pics: [],
  Prevs: [],
  Nexts: [],
  Actua: [],
  onPicture: 1000,
  ScalerOptions: 'w620.h350.fill',
  ScalerOptOptions: 'fill',
  getActualPic: function()
  {
    return this.Pics[(this.onPicture) % this.Pics.length];
  },
  setDirectly: function(URL)
  {
    this.blindPics();
    this.onPicture *= this.Pics.length;
    
    var ProbeB = decodeURIComponent(Util.stripScalerOptions(URL));
    
    for (var i = 0; (Pic = this.Pics[i]); i++) 
    {
      if (Pic.Url != ProbeB) 
        this.onPicture++;
      else 
        break;
    }
    
    
    //        this.sortToPool();
    this.setActual();
  },
  step2: function(Response)
  {
    this.Pics = Response.Pics;
    
    if (Response.DeBug) 
      console.log(Response.DeBug);
    
    this.onPicture *= this.Pics.length;
    
    var ProbeB = decodeURIComponent(Util.stripScalerOptions(this.Img.src));
    
    for (var i = 0; (Pic = this.Pics[i]); i++) 
    {
      //            alert(Pic.Url + ' :: ' + ProbeB);
      if (Pic.Url != ProbeB) 
        this.onPicture++;
      else 
        break;
    }
    
    if (false == this.setActual()) 
      return false;
    
    
    this.Caller.ThumbView_OnPage = 1;
    if (this.ToThumb) 
    {
      this.Caller.switchView_Thumbnails();
    }
    else 
      this.Caller.switchView_Image();
  },
  blindPics: function()
  {
    this.Act.src = '/media/Image/Icons/blind.gif';
    this.Prev1.src = '/media/Image/Icons/blind.gif';
    this.Prev2.src = '/media/Image/Icons/blind.gif';
    this.Next1.src = '/media/Image/Icons/blind.gif';
    this.Next2.src = '/media/Image/Icons/blind.gif';
    this.Img.src = '/media/Image/Icons/blind.gif';
  },
  chgActual: function()
  {
    this.Imager.blindPics();
    this.Imager.onPicture += this.JumpSpan;
    this.Imager.setActual();
  },
  setActual: function()
  {
    var err = null;
    
    try 
    {
      switch (this.Pics.length)
      {
        case 4:
          this.Prev2.style.visibility = 'hidden';
          this.Next2.style.visibility = 'visible';
          this.Prev1.style.visibility = 'visible';
          this.Next1.style.visibility = 'visible';
          this.ArPrev.style.visibility = 'visible';
          this.ArNext.style.visibility = 'visible';
          break;
        case 3:
          this.Prev2.style.visibility = 'hidden';
          this.Next2.style.visibility = 'hidden';
          this.Prev1.style.visibility = 'visible';
          this.Next1.style.visibility = 'visible';
          this.ArPrev.style.visibility = 'visible';
          this.ArNext.style.visibility = 'visible';
          break;
          
        case 2:
          this.Prev2.style.visibility = 'hidden';
          this.Next2.style.visibility = 'hidden';
          this.Prev1.style.visibility = 'hidden';
          this.Next1.style.visibility = 'visible';
          this.ArPrev.style.visibility = 'visible';
          this.ArNext.style.visibility = 'visible';
          break;
          
        case 1:
          this.Prev2.style.visibility = 'hidden';
          this.Next2.style.visibility = 'hidden';
          this.Prev1.style.visibility = 'hidden';
          this.Next1.style.visibility = 'hidden';
          this.ArPrev.style.visibility = 'hidden';
          this.ArNext.style.visibility = 'hidden';
          break;
          
        case 0:
          console.log('Silently ignoring this ...');
          return false;
          break;
          
        default:
          this.Prev2.style.visibility = 'visible';
          this.Next2.style.visibility = 'visible';
          this.Prev1.style.visibility = 'visible';
          this.Next1.style.visibility = 'visible';
          this.ArPrev.style.visibility = 'visible';
          this.ArNext.style.visibility = 'visible';
          break;
      }
      
      this.Prev2.src = Util.setScalerOptions(this.Pics[(this.onPicture - 2) % this.Pics.length].Url, 'w50.h50.' + this.ScalerOptOptions);
      this.Prev1.src = Util.setScalerOptions(this.Pics[(this.onPicture - 1) % this.Pics.length].Url, 'w55.h55.' + this.ScalerOptOptions);
      
      this.Act.src = Util.setScalerOptions(this.Pics[(this.onPicture) % this.Pics.length].Url, 'w60.h60.' + this.ScalerOptOptions);
      
      this.Next1.src = Util.setScalerOptions(this.Pics[(this.onPicture + 1) % this.Pics.length].Url, 'w55.h55.' + this.ScalerOptOptions);
      this.Next2.src = Util.setScalerOptions(this.Pics[(this.onPicture + 2) % this.Pics.length].Url, 'w50.h50.' + this.ScalerOptOptions);
      
      var srcPic = this.Pics[(this.onPicture) % this.Pics.length].Url;
      this.Img.src = Util.setScalerOptions(Util.stripScalerOptions(srcPic), this.ScalerOptions);
      
      this.Caller.LatestIdent = this.Act;
      this.Caller.recreateMenu(this.Pics[(this.onPicture) % this.Pics.length]);
    } 
    catch (e) 
    {
      err = e;
    }
    
    if (err) 
    {
      console.log(err);
    }
    
    return true;
  },
  up: function()
  {
    this.blindPics();
    this.onPicture++;
    this.setActual();
  },
  down: function()
  {
    this.blindPics();
    this.onPicture--;
    this.setActual();
  }
};

MTJax.Imager = new Class(MTJax.Imager);