$(function() {
  // make it fast
  $('#description-box').hide();
  $('#description').hide();
  
  var backendUrl = $('#photostream').attr('action');

  window.busy = false;
  var currentPicture = 0;
  var offsetMax = 0;
  var redirect_to = null;
  var pictures = $('.picture');

  $('#archives').remove();
  $('#gallery')
    .prepend('<a href="#previous" id="prev" class="arrow" title="previous">&larr;</a>')
    .append('<a href="#next" class="arrow" id="next" title="next">&rarr;</a>')
    .append('<div id="roll-info"><p>Some photos contain description.</p><p>Roll over a&nbsp;photo to&nbsp;show it.</p>');
  $('a').focus(function() { return this.blur(); });
  $('#roll-info').hover(function() {
      $(this).addClass('hover');
    }, function() {
      $(this).removeClass('hover');
    });

  $('.picture p').hide().css({ filter: 'alpha(opacity=70)', '-moz-opacity': 0.7, opacity: 0.7 });

  pictures.hover(function(){
    if( $.trim($(this).find('p').html()) ) {
      $(this).find('p').animate({
        height: 'toggle', opacity: 'show'
      });
    }
  },function(){
    $(this).find('p').animate({
      height: 'toggle', opacity: 'hide'
    });
  }).hide();
  pictures.eq(currentPicture).fadeIn('fast');

  var nextPic = function(j) {
    $(j || []).each(function() {
      var newPict = $('.picture:first').clone(true);
      newPict.insertBefore('#next');
      newPict.hide();
      newPict.find('img').remove();
      newPict.prepend('<img src="' + this.src + '" alt="" />');
      newPict.find('p').html(this.description);
      return false;
    });
  }

  var endStream = function(e) {
    if (e.status == 410) {
      redirect_to = e.getResponseHeader('X-Redirect');
    }
  }

  var togglePicture = function(ob, direction) {
    if (window.busy) return;

    ob.blur();
    if (direction == 'next' && $('.picture').size() - 1 == currentPicture) {
      if (redirect_to) {
        window.location = redirect_to;
      }
      return;
    }
    window.busy = true;
    $('.picture').eq(currentPicture).fadeOut('fast');

    if (direction == 'next') {
      ++currentPicture;
      if ($('.picture').eq(currentPicture + 2).length == 0) {
        var offset = currentPicture + 2;
        if (offset > offsetMax) {
          $.ajax({
            url: backendUrl,
            dataType: 'json',
            data: {
              offset: offset,
              limit: 1
            },
            success: nextPic,
            error: endStream
          });
          offsetMax = offset;
        }
      }
      $('#prev').fadeIn('slow');
      $('#roll-info').hide();
    } else {
      if(!--currentPicture) {
        $('#prev').click(function() { return false; }).fadeOut('slow');
      };
    }
    $('.picture').eq(currentPicture).fadeIn('normal', function() { window.busy = false; $(this).css({ filter: 'alpha(opacity=100)', '-moz-opacity': 1, opacity: 1 }); });
  }

  $('a#next, a#prev').click(function(){
    togglePicture(this, $(this).attr('id'));
    return false;
  })

  $('#exhaustion').click(function() {
    $('#description-box').animate({
      opacity: 'show'
    }).find('#description').slideDown(1000);
    return false;
  });

  // introducing lightbox 
  $('#thumbs a').click(function() {
    $(this).lightbox({ start: true, events: false });
    return false;
  });
})

