isInt = function( i ) { return ( ( i % 1 ) == 0 )? i : false; };

var animatingInit = ( isInt( theTimer ) ) ? theTimer : false;

if( $( '#animation' ).length )
{
    animatingInit = initAnimations( 3000, false, $( '#animation' ), true );
}

if( $( '#animation-basic' ).length )
{
    animatingInit = initAnimations( 3000, true, $( '#animation-basic' ), true );
}

if( $( '#animation-noloop' ).length )
{
    animatingInit = initAnimations( 3000, false, $( '#animation-noloop' ), false );
}


var theTimer;

function initAnimations( speed, basic, obj, loop )
{
    clearTimeout( theTimer );
    
    var imageArray = new Array();
    var currentImage = 0;
    
    var animatingAtm = false;
    
    var fadeImagein;
    
    var btnBack;
    var btnForward;
    
    var _basic = basic;
    var _loop = loop;
    var _already = false;
    
    animationObj = obj;
    navigationObj = $( '#animation-navigation' );
    navigationController = $( '#animation-skip' );
    
    pauseSpeed = speed;
    fadeSpeed = speed / 6;

    function initNavigation()
    {
        $.each( imageArray,
            function( index, value )
            {                
                clickableObject = $( '<a></a>' );
                clickableObject.attr( 'id', index );

                if( index == 0 )
                {
                    clickableObject.addClass( 'current' );
                }
                                
                navigationObj.append( clickableObject );

            }
        );
        
        $( 'a', navigationObj ).live( 'click',
            function()
            {
                if( ! fadeIn( $( this ).attr( 'id' ) ) )
                {
                    return false;
                }
            }
        );
        
        navigationObj.show();
        
        if( navigationController.length )
        {
            btnBack = $( '<a class="btnBack"></a>' );
            btnForward = $( '<a class="btnForward"></a>' );
            
            navigationController.append( btnBack );
            navigationController.append( btnForward );
                   
            $( '.btnBack' ).live( 'click',
                function()
                {
                    if( currentImage == 0 )
                    {
                        fadeIn( ( imageArray.length - 1 ) );
                    }
                    else
                    {
                        fadeIn( ( currentImage - 1 ) );
                    }
                }
            );

            $( '.btnForward' ).live( 'click',
                function()
                {
                    if( currentImage == ( imageArray.length - 1 ) )
                    {
                        fadeIn( 0 );
                    }
                    else
                    {
                        fadeIn( ( currentImage + 1 ) );
                    }
                }
            );
            
            navigationController.show();
        }
    }
    
    function fadeNext()
    {
        currentImage = parseInt( currentImage );
        
        nextImage = currentImage + 1;

        if( nextImage >= imageArray.length )
        {
            if( _loop == false )
            {
                _already = true;
                return fadeIn( 0 );
            }
            
            nextImage = 0;
        }
        
        if( _already == false )
        {
            fadeIn( nextImage );       
        }
    }
    
    function fadeIn( fadeInNum )
    {
        if( animatingAtm != false )
        {
            return false;
        }
        
        animatingAtm = true;
        
        clearTimeout( theTimer );

        $( animationObj ).css( 'background', 'url( ' + imageArray[ fadeInNum ] + ' )' );

        fadeImagein.delay( fadeSpeed / 2.5 ).fadeOut( fadeSpeed,
            function()
            {
                $( this ).attr( 'src', imageArray[ fadeInNum ] );
            }
        );
        
        currentImage = parseInt( fadeInNum );
        
        $( 'a', navigationObj ).removeClass( 'current' );
                
        fadeImagein.delay( fadeSpeed / 2.5 ).fadeIn( 0,
            function()
            {
                animatingAtm = false;

                theTimer = setTimeout( function() { fadeNext(); }, pauseSpeed );
            }   
        );
        
        $( 'a[id="' + currentImage + '"]', navigationObj ).addClass( 'current' );
                
    }

    function init()
    {
        animationObj.children( 'img' ).each(
            function()
            {
                imageArray.push( $( this ).attr( 'src' ) );
            }
        );
        
        animationObj.empty();
        
        fadeImagein = $( '<img />' );
        fadeImagein.attr( 'src', imageArray[ 0 ] );
        
        currentImage = 0;
        
        animationObj.append( fadeImagein );
        
        theTimer = setTimeout(
            function()
            {
                fadeNext();
            }, pauseSpeed
        );
        
        if( _basic == false )
        { 
            initNavigation();
        }
        
        return theTimer;
    }
    
    
    return init();
}
