$(document).ready(function(){
  $("#navigation li").navigation({colourize:false});
  $("a#more_mini").click(function(){
    var h = $("div#mini_copy").height();
    $("#mini_content").animate({top : "-" + h + "px"}, 3000);
    $("a#more_mini").hide();
    $("a#back_mini").show();
  });
  $("a#back_mini").click(function(){
    $("#mini_content").animate({top : "0px"}, 3000);
    $("a#back_mini").hide();
    $("a#more_mini").show();
  });
  
  $("a#more").click(function(){
    var h = $("div#body_copy").height();
    var content = $("#content");
    if (content.position().top == 0){
      content.animate({top : "-" + h + "px"}, 3000);
    } else {
      content.animate({top : content.position().top - h + "px"}, 3000);
    }
    if (content.height() - h + content.position().top < h) {
      $("a#more").hide();
      $("a#back").show();
    }
  });
  $("a#back").click(function(){
    $("#content").animate({top : "0px"}, 3000);
    $("a#back").hide();
    $("a#more").show();
  });
});

function more_images($article_id, $page) {
  var n = $("#gallery_list").children('li').size() ; 
  if (n > ($page -1)*12)
  {
    $.ajax({dataType:'script', type:'get', url:'/articles/' + $article_id + '/more_images_loaded/' + $page}); 
    return false;
  }
  else
  {
    $.ajax({dataType:'script', type:'get', url:'/articles/' + $article_id + '/more_images/' + $page}); 
    return false; 
  }
}

function bb_more_images($bb_id, $page) {
  var n = $("#gallery_list").children('li').size();
  if (n > ($page -1)*12)
  {
    $.ajax({dataType:'script', type:'get', url:'/info/' + $bb_id + '/more_images_loaded/' + $page}); 
    return false;
  }
  else
  {
    $.ajax({dataType:'script', type:'get', url:'/info/' + $bb_id + '/more_images/' + $page}); 
    return false; 
  }
}

function update_galleria() {
  var $opts = $.galleria;
  $('#gallery_list').children('li').each(function(i) {
    var _container = $(this);
    var _o = $.meta ? $.extend({},
    $opts, _container.data()) : $opts;
    _o.clickNext = $(this).is(':only-child') ? false: _o.clickNext;
    var _a = $(this).find('a').is('a') ? $(this).find('a') : false;
    var _img = $(this).children('img').css('display', 'none');
    var _src = _a ? _a.attr('href') : _img.attr('src');
    var _title = _a ? _a.attr('title') : _img.attr('title');
    var _loader = new Image();
    //if (_o.history && (window.location.hash && window.location.hash.replace(/\#/, '') == _src)) {
    //    _container.siblings('.active').removeClass('active');
    //    _container.addClass('active');
    //}
    $(_loader).load(function() {
        $(this).attr('alt', _img.attr('alt'));
        var _thumb = _a ? _a.find('img').addClass('thumb noscale').css('display', 'none') : _img.clone(true).addClass('thumb').css('display', 'none');
        if (_a) {
            _a.replaceWith(_thumb);
        }
        if (!_thumb.hasClass('noscale')) {
            var w = Math.ceil(_img.width() / _img.height() * _container.height());
            var h = Math.ceil(_img.height() / _img.width() * _container.width());
            if (w < h) {
                _thumb.css({
                    height: 'auto',
                    width: _container.width(),
                    marginTop: -(h - _container.height()) / 2
                });
            } else {
                _thumb.css({
                    width: 'auto',
                    height: _container.height(),
                    marginLeft: -(w - _container.width()) / 2
                });
            }
        } else {
            window.setTimeout(function() {
                _thumb.css({
                    marginLeft: -(_thumb.width() - _container.width()) / 2,
                    marginTop: -(_thumb.height() - _container.height()) / 2
                });
            },
            1);
        }
        if (_thumb.attr('rel') == undefined)
        {
          _thumb.attr('rel', _src);
        }
        _thumb.attr('title', _title);
        _thumb.click(function() {
            $.galleria.activate(_thumb.attr('rel'));
        });
        _thumb.hover(function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        });
        _container.hover(function() {
            _container.addClass('hover');
        },
        function() {
            _container.removeClass('hover');
        });
        _container.prepend(_thumb);
        _thumb.css('display', 'block');
        _o.onThumb(jQuery(_thumb));

        _img.remove();
    }).error(function() {
        _container.html('<span class="error" style="color:red">Error loading image: ' + _src + '</span>');
    }).attr('src', _src);
});
}