$(document).ready(function () {
  $('.tooltipById').each(function () {
    tooltip_id = '#'+$(this).attr('id')+"_tooltip";
    $(this).tooltip({effect: 'slide', tip: tooltip_id, position: 'center right', offset: [0, -5], direction: 'right'});
  });
  if($('.scrollable-photos').length > 0) {
    $('.scrollable-photos').each(function () {
      if($(this).parents('.popup_by_id').length == 0) {
        if(typeof $.fn.jScrollHorizontalPane != 'undefined') {
          $(this).jScrollHorizontalPane({showArrows: true,dragMaxWidth: 90, arrowSize: 11, scrollbarHeight: 11, scrollbarMargin: 11});
          $(this).oneTime(100, function () {
            this.scrollTo('.scrollable-photos .current');
          });
        } else {
          $('.scrollable-photos').jcarousel({itemFallbackDimension: $('.scrollable-photos a:eq(0)').width() | 10, buttonNextHTML: '<a href="#" onclick="return false;">&gt;&gt;</a>', buttonPrevHTML: '<a href="#" onclick="return false;">&lt;&lt;</a>', scroll: 1});
        }
      }
    });
  }
  $('.popup-by-id').each(function (){
    $(this).click(function () {
      arr = $(this).parseClassToAssoc('popup');
      //console.log($('#'+arr['id'].pop()).toggle());
      $('<a href="#'+arr['id'].pop()+'"></a>').fancybox({hideOnContentClick: false, padding: 0, centerOnScroll: true, frameWidth: '450px', frameHeight: '100px', callbackOnComplete: function () {
        o = $('#fancy_content').find('.scrollable-photos');
        o.jScrollHorizontalPane({showArrows: true,dragMaxWidth: 90, arrowSize: 13, scrollbarHeight: 11, scrollbarMargin: 11});
        $('#fancy_content').find('.gallery-image').click(function () {
          str = ($(this).attr('rel')+"").replace(/[\r\n]/, "");
          $(this).attr('onclick', function () {});
          reg = /show_full_photo\(this, '([^\']+)', '([^\']*)', '([^\']*)'\)/
          preg = reg.exec(str);
          show_full_photo(this, preg[1], preg[2], preg[3], $(this).parent().parent().parent().parent());
          return false;
        })
      }}).click();
      return false;
    });
  });
});
function gallery_scroll_to_current_image(parentobj){
  scroll_to = 0;
  if($(parentobj).find('.scrollable-photos').find('.current').length > 0)
    scroll_to = $(parentobj).find('.scrollable-photos').find('.current').position().left - $('.scrollable-photos').find('a.current').width();
  $(parentobj).find('.scrollable-photos').each(function () {
    if(typeof this.scrollTo == 'function')
      this.scrollTo((scroll_to > 0 ? scroll_to : 0))
  });
}
function show_full_photo(thumb_obj, url, name, notes, parentobj) {
  if(typeof parentobj == 'undefined' || parentobj == null) { /* default case */
    parentobj = document;
  } /* else it is defined */
  full_obj = $(parentobj).find('.full-image img');
  orig_src = full_obj.attr('src');
  if(orig_src == url) {
    return true;
  }
  $(full_obj).parent().css({height: $(full_obj).height() + 6, overflow: 'hidden'});
  $(full_obj).fadeOut('slow', function() {
    $(thumb_obj).parent().find('.current').each( function() {
      $(this).removeClass('current');
    });
    $(thumb_obj).addClass('current');
    gallery_scroll_to_current_image(parentobj);
    if(typeof $('.scrollable-photos', parentobj).get(0).scrollTo == 'function')
      $('.scrollable-photos', parentobj).get(0).scrollTo('.scrollable-photos .current', parentobj);
    $(this).attr('src', url).load(function () {
      $(this).fadeIn('slow');
      $(this).parent().stop(true).animate({height: $(this).height()+6});
    });
    o = $('.full-image-description');
    o.css({overflow: 'hidden', height: o.height()})
    $('.full-image-container .image-name', parentobj).html(name);
    $('.full-image-container .image-notes', parentobj).html(notes);
    total_height = 0;
    o.children().each(function each() { if((h=$(this).height())) total_height += h; });
    o.animate({height: total_height});
  });
  return true;
}
