$(document).ready(function() { setGallery();
                               setValidator();
                             });
  

function getFancyTitle(title, currentArray, currentIndex, currentOpts)
{
  var curImage = $(currentArray[currentIndex]).attr("href");  
  return $('ul li').has('a[href|="' + curImage + '"]').find(".description").html();
}

function setGallery()
{
  $(".ImageGallery > ul > li.imageframe").each(function(index) { $(this).css("width", ($(this).find("img").outerWidth() + 50) + "px"); });
  $(".ImageGallery > ul > li.imageframe").each(function(index) { $(this).html( '<div class="image-border" \
                                          style="width: ' + ($(this).find("img").outerWidth() + 16) + 'px; \
                                               height: ' + ($(this).find("img").outerHeight() + 16) + 'px;"> \
                                    <div class="edge-top"></div> \
                                    <div class="edge-left"></div> \
                                    <div class="edge-right"></div> \
                                    <div class="image-content"><a rel="' + $(this).find("a").attr("rel") + '" href="' + $(this).find("a").attr("href") + '">'
                                                      + $(this).find("a").html() + '</a>' +
                                  '</div> \
                                    <div class="description">' + $(this).find("div.description").html() + '</div> \
                                    <div class="edge-bottom"></div> \
                                    <div class="corner-bottom-left"></div> \
                                    <div class="corner-bottom-right"></div> \
                                    <div class="corner-top-left"></div> \
                                    <div class="corner-top-right"></div> \
                                  </div>' +
                                  (($(this).find("span.title").length == 1) ?
                                      ('<div class="image-title">'
                                      + $(this).find("span.title").html() + '</div>')
                                      : '')
                                  ); });
  $('.ImageGallery > ul > li a:has(img)').fancybox( {   'showCloseButton'  : true,   'titlePosition'     : 'inside',   'titleFormat'    : getFancyTitle});
  
}


function setValidator()
{
  if ($("#mainForm"))
  {
    $("#mainForm").validate();
  }
}


