
if (typeof gallery === 'undefined') {
    gallery = {};
}

gallery.init = function() {
    //merge settings with defaults
    gallery = $.extend({
        'index': 0,
        'duration': 600,
        'nav': '#galleryNav',
        'holder': '#galleryHolder',
        'link': '.galleryLink'
    }, gallery);

    //get the max height of the spacer divs - to be used in the animations
    gallery.origHeight = Math.max.apply(Math, $(gallery.spacer).map(function() { return $(this).height(); }).get()) + 'px';

    //create required elements
    gallery.nav = gallery.createNav();
    gallery.holder = gallery.createHolder();

    //preload image
    gallery.updateImage();

    //bind gallery events
    gallery.nav.find('.navigate .previous').bind('click', function() { gallery.prev(); });
    gallery.nav.find('.navigate .next').bind('click', function() { gallery.next(); });
    gallery.nav.find('.about').bind('click', function() { gallery.toggleInfo(this); });
    gallery.nav.find('.close').bind('click', function() { gallery.hide(); });

    //bind the click event for gallery link
    $(gallery.link).bind('click', function(event) { gallery.show(); });
}

gallery.show = function() {
    $(gallery.link).removeAttr('href');

    //hide content divs (apart from footer and header)
    $(gallery.content).hide();

    //adjust page height to hold large image
    $(gallery.spacer).animate({ 'height': gallery.height }, gallery.duration);
    //show gallery holder & navigation
    gallery.nav.show().animate({ 'opacity': 1 }, gallery.duration);
    gallery.holder.show().animate({ 'opacity': 1 }, gallery.duration, function() {
        //hide and stop banner animation
        banner.hide();
    });

    //change bind the click event for gallery link
    $(gallery.link).unbind('click').bind('click', function() { gallery.hide(); });
}

gallery.hide = function() {
    //show and resume banner animation
    banner.show();

    //hide gallery holder & navigation
    gallery.nav.animate({ 'opacity': 0 }, gallery.duration, function() { $(this).hide(); });
    gallery.holder.animate({ 'opacity': 0 }, gallery.duration, function() { $(this).hide(); });

    //adjust page height to orignal height
    $(gallery.spacer).animate({ 'height': gallery.origHeight }, gallery.duration);

    //show content divs (apart from footer and header)
    $(gallery.content).show();
    //$('#subnav').css({'opacity' : '0.4'}); //temporary, not very good fix, need to improve

    //change bind the click event to the gallery link
    $(gallery.link).unbind('click').bind('click', function() { gallery.show(); });
}

gallery.next = function() {
    //navigate forward through all of the photos...
    if (gallery.index == gallery.images.length - 1)
        gallery.index = 0;
    else
        gallery.index++;
    gallery.updateImage();
}

gallery.prev = function() {
    //navigate backwards through all of the photos...
    if (gallery.index == 0)
        gallery.index = gallery.images.length - 1;
    else
        gallery.index--;
    gallery.updateImage();
}

gallery.toggleInfo = function(element) {
    //get, show & hide the text description for each photo 
    if ($(element).hasClass('less')) {
        $(element).removeClass('less').addClass('more');
        $(element).next('li').slideUp('medium');
    }
    else if ($(element).hasClass('more')) {
        $(element).removeClass('more').addClass('less');
        $(element).next('li').slideDown('medium');
    }
}

gallery.updateImage = function() {
    //update image and meta information
    image = gallery.images[gallery.index];

    if (typeof image !== 'undefined') {
        gallery.holder.css({ 'background-image': 'url(' + ajaxPath + 'resource/' + image.src + ')' });
        gallery.nav.find(".meta .title").text(image.title);
        gallery.nav.find(".meta .desc").html(image.desc);
        //if (image.link) gallery.nav.find(".meta a").show().attr("href", image.link);
        //else gallery.nav.find(".meta a").hide();
    }
}

gallery.createNav = function() {
    element = $(gallery.nav);
    if (!element.is("*")) {
        //draw hidden #galleryNav on page
        element = $('<ul id="' + gallery.nav.substring(1) + '" style="opacity: 0;"></ul>').insertAfter('#breadBar');
        element.append('<li class="navigate"><span class="text">' + morephotos + '</span><span class="arrows"><a class="previous" href="#"><img src="' + ajaxPath + 'images/previous.png" width="27" height="17" alt="Previous" /></a><a class="next" href="#"><img src="' + ajaxPath + 'images/next.png" width="27" height="17" alt="Next" /></a></span></li><li class="about less"><a href="#"><span class="access">' + moretext + '</span>' + aboutimage +'</a></li><li class="photoInfo meta"><h2 class="title"></h2><div class="desc"></div></li><li class="close"><a href="#">' + closegallery +' </a></li>');
        element.hide();
    }
    return element;
}

gallery.createHolder = function() {
    element = $(gallery.holder);
    if (!element.is("*")) {
        //draw hidden #galleryHolder on page
        element = $('<div id="' + gallery.holder.substring(1) + '" style="opacity: 0;"></div>').appendTo('body');
        element.hide();
    }
    return element;
}

$(document).ready(function() { gallery.init(); });

//gallery image size = 1100 x 782
