jQuery.preloadImages = function( ) {
    for( var i = 0; i < arguments.length; i++ ) {
        jQuery( "<img>" ).attr( "src", imageDir + arguments[ i ] );
    }
}

function applyHover( hoverName, object ) {
    $( object ).attr( "src", imageDir + "menu-" + hoverName + ".png" );
}

function removeHover( hoverName, object ) {
    $( object ).attr( "src", imageDir + "menuactive-" + hoverName + ".png" );
}

$.fn.infiniteCarousel = function () {
    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
        $slider = $wrapper.find('> ul'),
        $items = $slider.find('> li'),
        $single = $items.filter(':first'),
            
        singleWidth = $single.outerWidth(),
        visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
        currentPage = 1,
        pages = Math.ceil($items.length / visible);

        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect

        $wrapper.scrollLeft(singleWidth * visible);

        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
            n = Math.abs(currentPage - page),
            left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                }
                else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&nbsp;</a><a class="arrow forward">&nbsp;</a>');
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

jQuery(document).ready( function( ) {
    $( '#progress h2' ).click( function( ) {
        $( '#' + $( this ).attr( 'id' ) + 'Category' ).toggle( 750 );
        $( this ).toggleClass( 'active' );
    } );

    $('#progress div a').lightBox( );

    $( '.infiniteCarousel' ).infiniteCarousel( );

    $.preloadImages( "menuactive-news.png", "menuactive-about.png", "menuactive-games.png", "menuactive-employment.png", "menuactive-community.png", "menuactive-contact.png",
                  "2DHighlight.jpg", "3DHighlight.jpg", "animationHighlight.jpg", "menuactive-progress.png" );

    $( "#menu ul li a" ).each(
        function( ) {
            if ( $( "body" ).attr( "class" ) == $( this ).attr( "id" ) ) {
                $( this ).children( "img" ).attr( "src", imageDir + "menuactive-" + $( this ).attr( "id" ) + ".png" );
            }
        }
        );

    $( "#menu ul li a" ).hover(
        function ( ) {
            if ( $( "body" ).attr( "class" ) != $( this ).attr( "id" ) ) {
                $( this ).children( "img" ).attr( "src", imageDir + "menuactive-" + $( this ).attr( "id" ) + ".png" );
            }
        },
        function ( ) {
            if ( $( "body" ).attr( "class" ) != $( this ).attr( "id" ) ) {
                $( this ).children( "img" ).attr( "src", imageDir + "menu-" + $( this ).attr( "id" ) + ".png" );
            }
        }
        );

    $( ".infiniteCarousel .wrapper ul li a" ).each(
        function( ) {
            if ( $( this ).attr( "id" ) == "activeThumbnail" ) {
                $( "img#mainImage" ).attr( "src", $( this ).children( "img" ).attr( "alt" ) );
            }
        }
        );

    $( ".infiniteCarousel .wrapper ul li a" ).hover(
        function ( ) {
            if ( $( this ).attr( "id" ) != "activeThumbnail" ) {
                $( ".infiniteCarousel .wrapper ul li a" ).attr( "id", "" );
                $( this ).attr( "id", "activeThumbnail" );
                $( "img#mainImage" ).attr( "src", $( this ).children( "img" ).attr( "alt" ) );
            }
        },
        function ( ) {
            if ( $( this ).attr( "id" ) != "activeThumbnail" ) {
                $( "img#mainImage" ).attr( "src", $( this ).children( "img" ).attr( "alt" ) );
            }
        }
        );
} );