var pictureid = 0;

function addThumbEffects() {
  pictureid = $('pictureid').value;
  thumblist = $$('.thumb');
  selected = $('pic-' + pictureid);
  thumblist.erase(selected);
  thumblist.setStyle('opacity', 0.3);
  thumblist.each(function(thumb) {
    var eff = new Fx.Tween(thumb, {property:'opacity', wait:false, duration:'short'});
    thumb.addEvents({
      'mouseenter' : function(event) {
        event.stop();
        this.setStyle('opacity', 1.0);
/*        eff.start(1.0);*/
      },
      'mouseleave' : function(event) {
        event.stop();
        eff.start(0.3);
      }
    });
  });
}

window.addEvent('domready', function() {
  var bNew = $('thumbs');
 // var fadeOut = new Fx.Tween(bNew);
//  var fadeIn = new Fx.Tween(bNew, {property:'opacity', wait:false});
  
  var current = parseInt($('currentpage').value);
  var maxpage = parseInt($('maxpages').value);

  var req = new Request.HTML({
    url: 'updatethumb',
    method: 'get',
    onSuccess: function(html) {
      bNew.removeClass('ajax-loading');
      bNew.set('text', '');
      bNew.adopt(html);
      addThumbEffects();
      $('current-page-cnt').set('text', current + 1);
    }
  });

  addThumbEffects();

  $$('.editable').each(function(desc) {
    new mooipe(desc);
  });
  
  $('newdn').addEvent('click', function(e) {
    e.stop();
    current += 1;
    if (current >= maxpage) {
      current = maxpage - 1;
    }
    bNew.empty().addClass('ajax-loading');
    req.send('i=' + current + '&p=' + pictureid);
  });

  $('newup').addEvent('click', function(e) {
    e.stop();
    current -= 1;
    if (current < 0) {
      current = 0;
    }
    bNew.empty().addClass('ajax-loading');
    req.send('i=' + current + '&p=' + pictureid);
  });

  pcform = $('post-comment-form');
  if (pcform) {
    pcform.addEvent('submit', function(e) {
      e.stop();
      var log = $('comment-box').empty().addClass('ajax-loading');
      this.set('send', {
        method: 'get',
        onComplete: function(response) { 
          log.removeClass('ajax-loading');
          log.set('html', response);
        }
      });
      //Send the form.
      this.send();
    });
  }
});


