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() {
      if ($(this).attr('rel') == $('#startphoto').attr('value')) {
        gallery_top[$(this).parent().next().attr('rel')] = - gallery_width[$(this).parent().next().attr('rel')] + 350 - parseInt($(this).width()/2);
      }
      gallery_width[$(this).parent().next().attr('rel')] += 1 + $(this).width();
    })
    if (gallery_width[$(this).attr('rel')] <= $('.gallery').width()) {
    } else {
      animate2($(this).attr('rel'));
      $(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').bind('click', function() {
    $('.gallery_wrap a.act').removeClass('act');
    $(this).addClass('act');
    $('#album_content').html('Загружается...').load('/user_gallery.php?' + $('#sentstr').attr('value') + '&photo=' + $(this).attr('rel'));
    return false;
  });
  $('a.prevphoto').bind('click', function() {
    var t = $('.gallery_wrap a.act');
    t.removeClass('act');
    if (t.prev().length > 0) {
      t.prev().addClass('act').trigger('click');
    } else {
      $('.gallery_wrap a:last').addClass('act').trigger('click');
    }
    return false;
  });
  $('a.nextphoto').bind('click', function() {
    var t = $('.gallery_wrap a.act');
    t.removeClass('act');
    if (t.next().length > 0) {
      t.next().addClass('act').trigger('click');
    } else {
      $('.gallery_wrap a:first').addClass('act').trigger('click');
    }
    return false;
  });
  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)
{  
  while (gallery_top[id] > 0) {
      $('#gal'+id+' a.goto:last').insertBefore($('#gal'+id+' a.goto:first')).each(function() {gallery_top[id] -= 1 + $(this).width()});
  }
  while (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'});
}
