this.imageTip = function () {
  // Tooltip X offset
  var offsetX = 0;
  
  // Tooltip Y offset
  var offsetY = 0;

  // Identify the images to be given treatment
  var $images = $('#tourIcons img');

  // Add tooltip event handlers
  $images.hover(
    function (evt) { showTip(evt); },
    function (evt) { hideTip(evt); }
  );

  function showTip (evt) {
    var image = evt.currentTarget;
    var link = evt.currentTarget.parentNode;
    var caption = (image.alt != '') ? image.alt : '';

    // Clear the alt text to prevent browser tooltip
    $(image).data('oldAlt', $(image).attr('alt'));
    $(image).attr('alt', '');
    
    $('body').append('<p id="tooltip">' + caption + '</p>');
    $('#tooltip')
      .css('top', (evt.pageY - offsetY) + "px")
      .css('left', (evt.pageX + offsetX) + "px")
      .fadeIn('fast');
  }
  
  function hideTip (evt) {
    var image = evt.currentTarget;
    $(image).attr('alt', $(image).data('oldAlt'))
    $("#tooltip").remove();
  }
}

this.lightboxImages = function () {
  // Identify the images to be given treatment
  var $images = $('#tourContent img');
  $images.each(function () {
    var titleText = this.alt;
    var fullUrl = this.src.replace(/\/UserFiles\/Image\/tours\//, '/UserFiles/Image/tours_full/');
    $(this).wrap('<a href="' + fullUrl + '" rel="prettyPhoto[photos]" title="' + this.alt + '"></a>');
  });
}

this.stripImageTitles = function () {
  var $images = $('#tourContent img');
  $images.removeAttr('title');
}

this.addImageClickNote = function () {
  var $images = $('#tourContent img');
  $images.each(function () {
    $(this).hover(
      function (event) {
        $(this).data('oldTitle', $(this).attr('alt'));
        var newTitle = $(this).attr('alt') + ' (click to enlarge)';

        $(this).attr('alt', newTitle);
        $(this).parent().attr('title', newTitle);
      },
      function (event) {
        $(this).attr('alt', $(this).data('oldTitle'));
        $(this).parent().attr('title', $(this).data('oldTitle'));
      }
    );
    
    $(this).click(function (event) {
      $(this).attr('alt', $(this).data('oldTitle'));
      $(this).parent().attr('title', $(this).data('oldTitle'));
    });
  });
}

$(function () {
  // Add click notice to images
  addImageClickNote();

  // Strip title property from images
  stripImageTitles();
  
  // Add lightbox HTML hooks
  lightboxImages();
  
  // Activate tooltips rollovers
  imageTip();
  
  // Activate Lightbox
  $("a[rel^='prettyPhoto']").prettyPhoto({
    counter_separator_label: ' of ',
    showTitle: false,
    theme: 'light_square'
  });
});
