$(document).ready(function ($) {
  if($.browser.msie) { ///* Not even IE8 knows about last-child, fix it with jQuery?
    $('div.block-top-gallery:last-child').each(function () {
      $(this).css('marginRight', '0px');
    });
    if($.browser.version == 6) { // IE6 do not know about first-child eather
      $('div.block-top-gallery:first-child').each(function () {
        $(this).css({marginLeft: '0px', width: '174px'}); //IE6 calculates width badly too :-(
      });
    }
  }
  $('.build-first-last').each(function () {
    $($(this).attr('firstChild')).addClass('firstChild');
    $($(this).attr('lastChild')).addClass('lastChild');
  })
  $('.replace-emails').replaceEmails();
  /* we need to init gallery on the page */
  $('div.scrollable-photos-container').each(function () {
    $(this).find('ul').css({height: $(this).find('li:not(.hidden)').outerHeight()});
    $(this).append('<div class="images-numbers"></div>');
    $(this).append('<div class="text-name"></div>');
    $(this).find('li').each(function (i) {
      if(i > 0) {
        $(this).addClass('hidden');
      }
      c = $(this).parents('.scrollable-photos-container').find('.images-numbers');
      t = $(this).find('A').attr('title');
      if(i==0 && t.length > 0) {
        $(this).parents('.scrollable-photos-container').find('.text-name').text(t);
      }

      $(this).find('A').attr('title', '');
      $('<a href="#">'+(i+1)+'</a>').data('title', t).data('image', this).click(function () {
        i = $(this).data('image');
        tc = $(this).parents('.scrollable-photos-container').find('.text-name');
        $(i).siblings(':not(.hidden)').fadeOut('slow', function () {
          $(this).addClass('hidden');
        });
        $(i).fadeIn('slow', function () {
          $(this).removeClass('hidden');
          $(this).parent().animate({height: $(this).outerHeight()});
        });
        t=$(this).data('title');
        if((typeof tc.text() != 'string' || tc.text().length == 0) && typeof t == 'string' && t.length > 0) {
          tc.css({overflow: 'hidden', height: 0}).text(t).slideDown('slow', function () {
            $(this).css({height: 'auto'});
          });
        } else if (typeof tc.text() == 'string' && tc.text().length >= 0 && (typeof t != 'string' || t.length == 0)) {
          tc.text(t).slideUp('slow');
        } else {
          tc.text(t);
        }
        return false;
      }).appendTo(c);
    });
  });
});
(function ($) {$.extend($.fn, {
  replaceEmails: function () {
    $(this).each(function () {
      href = $(this).attr('href');
      if(typeof href != 'undefined' && href.length > 0) {
        $(this).attr('href', href.replace(/ *\( *dot *\) */, '.').replace(/ *\( *at *\) */, '@'));
      }
      $(this).attr('innerHTML', $(this).attr('innerHTML').replace(/ *\( *dot *\) */, '.').replace(/ *\( *at *\) */, '@'));
    })
  }
})}) (jQuery);
