Limb.Class('imgGallery',
{
  __construct: function(imgViewBlock, previewList, arrows)
  {
    this.imgViewBlock = jQuery(imgViewBlock);
    this.previewList = jQuery(previewList);
    this.rightArrow = jQuery(arrows.rightArrow);
    this.leftArrow = jQuery(arrows.leftArrow);
    this.loader = jQuery('.loading');
    if(!jQuery('.img_block')[0])
      return;
    this.currentImgId = jQuery('.img_block')[0].id.replace('img_block_', '');
    this.currentImg = jQuery('#current_img_' + this.currentImgId);
    this.currentThumbnailImg = jQuery('#image_thumbnail_' + this.currentImgId);

    this.prepareArrows();
    this.highLightPreViewImg();
    this.setEvents();
  },

  loadImg: function (id)
  {
    var that = this;

    if (id)
      this.currentImgId = id;
    this.currentImg = jQuery('#current_img_' + this.currentImgId);
    this.currentThumbnailImg = jQuery('#image_thumbnail_' + this.currentImgId);

    this.highLightPreViewImg();
    this.prepareArrows();

    var currentImgBlock = this.currentImg.parent('.img_block');
    jQuery('.img_block').hide().removeClass('current_img');
    this.loader.hide();

    if(currentImgBlock[0])
    {
      currentImgBlock.show().addClass('current_img');
      window.location.hash = this.currentImgId;
    }
    else
    {
      this.loader.show();
      jQuery.ajax({
        type: 'GET',
        dataType: 'html',
        url: '/photogallery/image/'+id,
        success:function(data){
          that.imgViewBlock.append(data);
          if(jQuery.browser.opera)
            that.showImg(id);
        }
      });
    }
  },

  highLightPreViewImg: function()
  {
    this.previewList.find('li').removeClass('active');
    this.currentThumbnailImg.parent('li').addClass('active');
  },

  prepareArrows: function()
  {
    var prev_image = this.currentThumbnailImg.parent('li').prev('li');
    if(!prev_image[0])
      this.leftArrow.hide();
    else
      this.leftArrow.attr('href', prev_image.children('a').attr('href')).show();

    var next_image = this.currentThumbnailImg.parent('li').next('li');
    if(!next_image[0])
      this.rightArrow.hide();
    else
      this.rightArrow.attr('href', next_image.children('a').attr('href')).show();
  },

  showPrevImg: function()
  {
    var prev_image = this.currentThumbnailImg.parent('li').prev('li');
    if(!prev_image[0])
      return false;
    var id = prev_image.find('a')[0].id.replace('image_thumbnail_', '');
    this.loadImg(id);

  },

  showNextImg: function()
  {
    var next_image = this.currentThumbnailImg.parent('li').next('li');
    if(!next_image[0])
      return false;
    var id = next_image.find('a')[0].id.replace('image_thumbnail_', '');
    this.loadImg(id);
  },

  showImg: function(id)
  {
    if (id.replace('current_img_', '') != this.currentImgId)
    {
      jQuery('#current_img_'+id.replace('current_img_', '')).parent('div.img_block').removeClass('current_img');
      return;
    }

    this.currentImgId = id.replace('current_img_', '');
    this.currentImg = jQuery('#current_img_'+this.currentImgId);
    this.currentThumbnailImg = jQuery('#image_thumbnail_' + this.currentImgId);
    this.loader.css({width: this.currentImg[0].width, height:this.currentImg[0].height, display: 'none'});
    this.currentImg.parent('div.img_block').show();
    window.location.hash = this.currentImgId;
  },

  setHotKeys: function (e)
  {
    if (!e) e = window.event;
    var k = e.keyCode;
    if (e.ctrlKey)
    {
      if (k == 37) imgGallery.showPrevImg(); // Ctrl+Left
      if (k == 39) imgGallery.showNextImg(); // Ctrl+Right
    }
  },

  setEvents: function()
  {
    var that = this;

    this.previewList.find('a').click(function(){
      var id = this.id.replace('image_thumbnail_', '');
      that.loadImg(id);
      return false;
    });
    jQuery(this.rightArrow).click(function(){that.showNextImg();return false;});
    jQuery(this.leftArrow).click(function(){that.showPrevImg();return false;});
    document.onkeydown = that.setHotKeys;
  }
});


jQuery(document).ready(function(){
  imgGallery = new imgGallery('#img_view','#work_list', {rightArrow:"#right_arrow",leftArrow:"#left_arrow"});
});

