var PhotoSlide = newClass();
PhotoSlide.prototype = {
  init: function(mainID) {
    var t = this;

    t.width = 83;
    t.N = 7;

    if(mainID != null)
     t.main = $('#'+mainID);
    else
     t.main = $('div.Gallery');

    t.prev = t.main.find('#prev');
    t.next = t.main.find('#next');
    t.photo = t.main.find('#photo');
    t.small = t.main.find('#small');
    t.ScrollThumbs = t.main.find('div.ScrollThumbs');

    t.cnt = t.ScrollThumbs.find('div.Thumb').length;
    t.curOffset = 0;
    t.curPhoto = 0;


    if($("div.LinkPort").size()) {
      $("div.LinkPort").eq(1).find('a').attr("href","/portfolio/graph.php#" + $('.pic_1').parent("div").find(".id").val())
    }

    if($('#name').length > 0 && $('input.name').length > 0)
     t.name = $('#name');

    if(t.cnt <= t.N) {
      t.prev.addClass("GrayPrev");
    } else {
      t.prev.addClass("GrayPrev");
    }

    if(t.cnt == 1)
     t.next.addClass("GrayNext");

    t.prev.click(function(e){
      e.preventDefault();
      e.stopPropagation();

      t.next.removeClass("GrayNext");

      if(t.curOffset > 0) {
        t.ScrollThumbs.animate({left: "+=" + t.width}, 300 );
        t.curOffset--;
      }

      if(t.curPhoto > 0) {
        t.curPhoto--;
        t.show('a.pic_'+t.curPhoto);
      }
      t.proverka();
    });

    t.next.click(function(e){
      e.preventDefault();
      e.stopPropagation();

      t.prev.removeClass("GrayPrev");

      if(t.curOffset < t.cnt - t.N) {
        t.ScrollThumbs.animate({left: "-=" + t.width}, 300 );
        t.curOffset++;
      }

      if(t.curPhoto < t.cnt-1) {
        t.curPhoto++;
        t.show('a.pic_'+t.curPhoto);
      }
      t.proverka();
    });

    t.small.click(function(e){
      e.preventDefault();
      e.stopPropagation();
      t.show('a.'+$(this).attr('class'))
    });

  },
  show: function(id){
    var t = this;
    t.photo.hide();
    t.curPhoto = id.replace("a.pic_","");
    link = t.main.find(id).attr('href');
    t.photo.attr('src', link);
    if(t.name)
      t.name.html($(id).parent('div.Thumb').find('input.name').val());

    t.photo.load(function(){
      t.photo.show();
    });

    t.proverka();
    t.ScrollThumbs.find('div.Thumb').removeClass('Select');
    t.main.find(id).parent('div.Thumb').addClass('Select');
  },

  proverka: function() {
    var t = this;
    if($("div.LinkPort").size()) {
      $("div.LinkPort").eq(1).find('a').attr("href","/portfolio/graph.php#" + $('.pic_' + t.curPhoto).parent("div").find(".id").val())
    }

    if(t.curPhoto == 0)
        t.prev.addClass("GrayPrev");
    else
        t.prev.removeClass("GrayPrev");
    if(t.curPhoto == t.cnt-1)
        t.next.addClass("GrayNext");
    else
        t.next.removeClass("GrayNext");
  }
}


$(document).ready(function(){
  var galley = $('div.Gallery');
  if(galley.length > 1)
     galley.each(function(){
      new PhotoSlide($(this).attr("id"));
     });
  else
     new PhotoSlide(null);
});

