/**
 * prepare page
 *
 **/
$(document).ready( function ()
{
  // initialize expand links (toggle content visibility)
  var expandLinks = $('#maincontent .expand');
  // add onclick function to each link with css class expand
  expandLinks.each( function()
  {
    $(this).bind (
      "click",
      function()
      {
        // get all hidden elements within the current list element and toggle display
        $(this).parents('li').find('.hidden').each( function()
        {
          $(this).toggle();
        });
        // toggle link arrow
        $(this).toggleClass('open');
        // don't follow href
        return false;
      }
    );
  });
  // adjust column height initially
  resizeCols();

  // create galleries
  // add rel attribute to lightbox links and create lightboxes
  // account for multiple galleries in one page
  $('div.gallery').each(function(index)
  {
    var galleryName = 'gallery_' + index;
    // add attribute opener link
    $(this).prev().children('a.lightbox').attr('rel',galleryName)
    // add attribute remaining links
    $(this).find('a.lightbox').each(function()
    {
      $(this).attr('rel',galleryName);
    });
    createLightbox(index);
  });

  // add last-child pseudo class if not supported
  $("ul#nav li:last-child").addClass('last-child');
  $("#maincontent ul:last-child").addClass('last-child');
  $("#maincontent ul.img-text li p:last-child").addClass('last-child');
  $("#maincontent div.img-text p:last-child").addClass('last-child');
});

// add handler to resize event
$(window).resize( resizeCols );

/**
 * adjust the height of the three columns to fill the browser window's height
 *
 **/
function resizeCols()
{
  // calculate height: window height minus header height
  var contentHeight = $(window).height() - $('#header').height();
  $('#leftcol').css('height', contentHeight);
  $('#centercol').css('height', contentHeight);
  $('#rightcol').css('height', contentHeight);
}

/**
 * create lightbox
 *
 **/
function createLightbox(index)
{
  $("a[rel='gallery_" + index + "']").fancybox({
    'autoScale' : false,
    'overlayOpacity' : '0.8',
    'overlayColor' : '#000',
    'titlePosition'	: 'inside',
    'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
      return '<span id="fancybox-title-inside">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
    }
  });
}

