﻿$(document).ready(function($) {


    $("a[rel='example1']").colorbox();

    if (!$('.gallery').length == 0) {
        $('.gallery li a').append('<span class="galleryOverlayIcon"></span>');
    }

    if (!$('.gallery2').length == 0) {
        $('.gallery2 li a').append('<span class="galleryOverlayIcon"></span>');
    }

    // Custom Lightbox
    $('.customLightbox').click(function(event) {
        event.preventDefault();

        // TO DO:
        // It would be nice if the panel animated between size changes. Suggest hiding the image before loading the new one, 
        // animating the panel then fading in the new image.

        $('body').css('overflow-y', 'hidden');

        $('<div id="lightbox_overlay"></div>').css('top', $(document).scrollTop()).css('opacity', '0').animate({ 'opacity': '0.8' }, 'slow').appendTo('body');
        $('<div id="lightbox_container"><div id="lightbox_close"><a>x Close</a></div><div id="lightBoxMainImgWrap"></div><div id="lightbox_text"></div></div>').hide().appendTo('body');

        // Clone the thumbnail list to our LightBox
        $('.gallery2 ul').clone().appendTo('#lightBoxMainImgWrap');

        // Allow thumbs to change images shown in the panel
        $('#lightbox_container ul li a').click(function(event) {
            event.preventDefault();
            $('#lightBoxMainImg').attr('src', $(this).attr('href'));
            $('#lightbox_text').html('').html('<p>' + $(this).attr('title') + '</p>');
        });

        $('#lightbox_close').click(function(event) {
            event.preventDefault();
            removeLightBox();
        });

        $('#lightbox_text').html('').html('<p>' + $(this).attr('title') + '</p>');

        $('<img id="lightBoxMainImg" />').attr('src', $(this).attr('href')).load(function() { positionLightBoxImage(); }).click(function() { removeLightBox(); }).appendTo('#lightBoxMainImgWrap');

    });


    // Add a class to the active page in the breadcrumb
    $('.breadcrumb a:last').toggleClass('active');

    $(window).bind("load", function() {
        setEqualHeight($(".home_pane_b .home_pane_inner, .home_pane_c .home_pane_inner, .home_pane_d .home_pane_inner, .home_pane_e .home_pane_inner, .home_pane_f .home_pane_inner"));
    });

    // Hide the content box if no content exists

    if (!$('.contentBox').length == 0) {

        if (($('#ctl00_mainPageContent_mainContent2_contentDiv').html().length == 0) && ($('#ctl00_mainPageContent_mainContent3_contentDiv').html().length == 0)) {
            $('.contentBox').hide();
        }
    }
});

function setEqualHeight(columns) {
    var tallestColumn = 0;

    columns.each(function() {
        currentHeight = $(this).height();
        if (currentHeight > tallestColumn) {
            tallestColumn = currentHeight;
        }
    });

    columns.height(tallestColumn);
}

function positionLightBoxImage() {

    if ($('#lightBoxMainImg').width() > 0) {
        lightboxContainerWidth = $('#lightBoxMainImg').width() + 140 + 'px';
    } else {
        lightboxContainerWidth = $('#lightbox_container').width() + 10 + 'px';
    }

    $('#lightbox_container').css('width', lightboxContainerWidth);

    var top = ($(window).height() - $('#lightbox_container').height()) / 2;
    var left = ($(window).width() - $('#lightbox_container').width()) / 2;
    $('#lightbox_container').css({ 'top': top + $(document).scrollTop(), 'left': left }).fadeIn();
};

function removeLightBox() {
    $('#lightbox_overlay, #lightbox_container').fadeOut('slow', function() { $(this).remove(); $('body').css('overflow-y', 'auto'); });
};
