//Images that we need to preload, nav hover states
var imgs = ['link-1-home-on', 'link-2-about-on', 'link-3-campus-on',
            'link-4-scrapbook-on', 'link-5-academics-on', 'link-6-philanthropy-on',
            'link-7-recruitment-on', 'link-8-social-events-on', 'link-9-officers-on'];

var picSpeed = 800;

function doContent(name, clear)
{
    if ($('.ngg-galleryoverview').html() != null) {
        $('.ngg-galleryoverview').after('<div id="'+name+'-content"></div>');
    } else {
        $('.page-header').after('<div id="'+name+'-content"></div>');
    }
    var innerContent = $('#content > *');
    innerContent.each(function(i){
        if ($(this).attr('class') != 'page-header'
            && $(this).attr('class') != 'ngg-galleryoverview'
            && $(this).attr('id') != name+'-content') {
            $('#'+name+'-content').html($('#'+name+'-content').html() + '<'+this.nodeName+'>' + $(this).html() + '</'+this.nodeName+'>');
            $(this).remove();
        }
        if (i == innerContent.length && clear) {
            $('#'+name+'-content').append('<br style="clear: both" />');
        }
    });
}

function hoverOn(that, name)
{
    $(that).attr('class',  $(that).attr('class').replace('sprite-'+name, 'sprite-'+name+'-on'));
}

function hoverOut(that, name)
{
    $(that).attr('class',  $(that).attr('class').replace('sprite-'+name+'-on', 'sprite-'+name));
}

function startRotation()
{
    var next = $('.ngg-gallery-thumbnail:visible');

    $(next).fadeOut(picSpeed, function(){
        if ($(next).next().attr('class') == 'ngg-gallery-thumbnail') {
            $(next).next().fadeIn(picSpeed);
        } else {
            $('.ngg-gallery-thumbnail:first').next().fadeIn(picSpeed);
        }
    });
    
    setTimeout('startRotation();', 5000);
}

$(function(){
    //Transverse to put the polaroid and content in 2 diff div's
    if ($('.ngg-galleryoverview').html() != null) {
        doContent('right', true);
    } else {
        doContent('inner', false);
    }

    //Make inner-content scrollable
    if ($('.ngg-galleryoverview').html() != null) {
        $('#right-content').jScrollPane();
    } else {
        $('#inner-content').jScrollPane();
    }

    //Nav Hover
    $('#main-nav li').hover(
        function() {
            hoverOn(this, 'nav');
        }, function() {
            hoverOut(this, 'nav');
        }
    );

    //Polaroid Nav Hover
    $('#polaroid-nav-prev').hover(
        function() {
            hoverOn(this, 'prev');
        }, function() {
            hoverOut(this, 'prev');
        }
    );
    $('#polaroid-nav-next').hover(
        function() {
            hoverOn(this, 'next');
        }, function() {
            hoverOut(this, 'next');
        }
    );

    //Show the first thumbnail in the list
    $('.ngg-gallery-thumbnail:first').show();
    
    //Handle for the Prev Button
    $('#polaroid-nav-prev').click(function(){
        var next = $('.ngg-gallery-thumbnail:visible');

        $(next).fadeOut(picSpeed, function(){
            if ($(next).prev().attr('class') == 'ngg-gallery-thumbnail') {
                $(next).prev().fadeIn(picSpeed);
            } else {
                $('.ngg-gallery-thumbnail:last').prev().fadeIn(picSpeed);
            }
        });

        return false;
    });

    setTimeout('startRotation();', 5000);
    
    //handle for the Next button
    $('#polaroid-nav-next').click(function(){
        var next = $('.ngg-gallery-thumbnail:visible');

        $(next).fadeOut(picSpeed, function(){
            if ($(next).next().attr('class') == 'ngg-gallery-thumbnail') {
                $(next).next().fadeIn(picSpeed);
            } else {
                $('.ngg-gallery-thumbnail:first').next().fadeIn(picSpeed);
            }
        });

        return false;
    });
});