﻿
function BottomBarAnimator( id, start_wait, interval )
{
    var m_id = id;
    var m_start_wait = start_wait;
    var m_interval_time = interval;

    var m_looping = true;  // are we looping? will get set to false we a user clicks on a bb item...
    var m_active_idx = 0;  // the currently active bb item...
    var m_timer_id = null; // id of the interval timer

    var m_active_ids = new Array();
    var m_active_titles = new Array();

    var m_saved_img = new Array();

    // process the products...
    $( m_id ).children( '.bottombar-product' ).each( 
    
        function( i, e ) 
        {
            var product_id = e.id.split('_',2)[1];
        
            // add to the active id list...
            m_active_ids.push( product_id );

            // find the link change the href and setup the click event...
            var link = $(e).find('a').get(0)
            
            var link_inner = $(link).html();
            
            $(link).replaceWith( link_inner );
            
            $(e).click( function() { Select( i ); } );
            
            m_saved_img.push( '' );
        } 
    
    );
    
    Update( 0, 0 );
    
    this.Start = function()
    {
        // we start with the first one...
        m_active_idx = 0;
        
        // delay the start of the looping...
        if ( m_looping )
            window.setTimeout( Run, m_start_wait );
    }

    this.StopIt = function() { Stop(); }

    function Stop()
    {
        if ( m_looping )
        {
            m_looping = false;
            if ( m_timer_id != null )
            {
                clearInterval( m_timer_id );
                m_timer_id = null;
            }
        }
    }

    function Select( id )
    {
        Stop();
                        
        Update( m_active_idx, id );
        
        m_active_idx = id;
        
    }

    function Run()
    {
        if ( m_looping )
        {
            // Update( m_active_idx, m_active_idx );
        
            m_timer_id = window.setInterval( Next, m_interval_time );
        }
    }

    function Next()
    {
        if ( m_looping )
        {
            var next_idx = m_active_idx + 1;

            if ( next_idx >= m_active_ids.length )
            {
                next_idx = 0;
            }

            Update( m_active_idx, next_idx );
            
            m_active_idx = next_idx;
        }
    }
    
    function Update( previous, current )
    {
        var bb_prev_id = $( '#bbid_' +  m_active_ids[ previous ] );
        var bb_curr_id = $( '#bbid_' +  m_active_ids[ current ] );

        var prev_normal = $( '.tofade > img:first', bb_prev_id );
         
        if ( previous != current )
        {
            $( 'div.flash-replaced' ).each( function() {
                $(this).html( '' );
                $(this).removeClass( 'flash-replaced' );
            });
            
            $( 'div.videolink > img' ).each( function() {
                $(this).show();
            });
            
            prev_normal.attr( 'src', m_saved_img[ previous ] );
            
            var duration = Math.abs( current - previous );
            
            if ( m_looping )
                duration = duration * 500;
            else
                duration = duration * 150;
                
            var new_offest = -( current * 551 );
            
            if ( m_looping && current < previous )
            {
                $("#product-carousel-wide").css( 'left', new_offest );
            }
            else
            {
            
                $("#product-carousel-wide").animate( { left: new_offest }, 
                                                     duration, 
                                                     'swing', 
                                                     function() {
                                                     
                                                        $( '#bbid_' +  m_active_ids[ current ] ).addClass( 'bbcurrentproduct' );
                                                     
                                                     } );

            }

         
        }
        else
        {
            if ( m_saved_img[ previous ] != "" )
                prev_normal.attr( 'src', m_saved_img[ previous ] );
        }
        
        curr_active = $( '.tohide > img:first', bb_curr_id );
        curr_normal = $( '.tofade > img:first', bb_curr_id );

        m_saved_img[ current ] = curr_normal.attr( 'src' );
        
        curr_normal.attr( 'src', curr_active.attr( 'src' ) );
    }

    return true;
}

$(document).ready(function () {

    var bba = new BottomBarAnimator( '#bottombar-products', 8000, 8000 );
    
    $( 'div.videolink' ).each( function() {
    
        var product_name = $(this).attr( 'id' ).split( '_' )[1];

        var home_image = $( '#homeimg_' + product_name );

        if ( $().flash.hasFlash() )
        {
            $( this ).hover(
                function() { $(home_image).stop();  $(home_image).fadeTo( 500, 0.0 ); },
                function() { $(home_image).stop(); $(home_image).fadeTo( 250, 1.0 ); }
            );

            $( this ).click( function() {

                bba.StopIt();

                $( home_image ).stop()
            
                var home_video = $( '#homevid_' + product_name );

                $( home_video ).flash( { src: '/Media/' + product_name + '/' + product_name + '-01.swf',
                                         width: 384,
                                         height: 302 },
                                       { version: 9,
                                         update: false } );

                $( home_image ).hide();

            } );

        }
        else
        {
            $(home_image).attr( 'src', $(home_image).attr( 'src' ).replace( '-video.', '.' ) );
        }
                
    
    } );
    
    bba.Start();
    
});
