var gallery_top = new Object();
var gallery_dir = new Object();
var gallery_width = new Object();
$(document).bind('ready', function() {    
  $('.gallery_up').bind('click', function() {
    gallery_top[$(this).attr('rel')] += 100;
    animate2($(this).attr('rel'), 1);
    return false;
  }).bind('mouseover', function() {
    gallery_dir[$(this).attr('rel')] = 1;
  }).bind('mouseout', function() {
    gallery_dir[$(this).attr('rel')] = 0;
  }).each(function() {
    gallery_width[$(this).attr('rel')] = 0;
    gallery_dir[$(this).attr('rel')] = 0;
    gallery_top[$(this).attr('rel')] = 0;
    $(this).parent().find('a.goto').each(function() {
      gallery_width[$(this).attr('rel')] += 1 + $(this).width();
    });    
    if (gallery_width[$(this).attr('rel')] <= $('.gallery').width()) {
    } else {
      $(this).show().next().show();
    }
  });
  $('.gallery_dn').bind('click', function() {
    gallery_top[$(this).attr('rel')] -= 100;
    animate2($(this).attr('rel'), 1);
    return false;
  }).bind('mouseover', function() {
    gallery_dir[$(this).attr('rel')] = -1;
  }).bind('mouseout', function() {
    gallery_dir[$(this).attr('rel')] = 0;
  });
  $('a.goto').not('.nopos').bind('click', function() {
    $(this).attr('href', $(this).attr('href').replace(/&start_pos=.*/, '') + '&start_pos=' + gallery_top[$(this).attr('rel')]); 
  })
  //animate2($(this).attr('rel'));
  window.setInterval(function() {
    $('.gallery_up').each(function() {
      if (gallery_dir[$(this).attr('rel')] != 0) {
        gallery_top[$(this).attr('rel')] += 5 * gallery_dir[$(this).attr('rel')];
        animate2($(this).attr('rel'), 0);
      }
    });
  }, 40);
});
function animate2(id, ani)
{
  if (gallery_top[id] > 0) {
      $('#gal'+id+' a.goto:last').insertBefore($('#gal'+id+' a.goto:first')).each(function() {gallery_top[id] -= 1 + $(this).width()});
  }
  if (gallery_top[id] < -gallery_width[id] + $('.gallery').width()) {
      $('#gal'+id+' a.goto:first').insertAfter($('#gal'+id+' a.goto:last')).each(function() {gallery_top[id] += 1 + $(this).width()}); 
  }
  $('#gal'+id+'').css({left: gallery_top[id] + 'px'});
}
