/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var slides_scrollbar = function( scrollbar, slides_reel )
{
    // settings
    var minGripWidth = 16;
    var desiredInterestPointInterval = 70;
    var maxnudge = 50;
    var buttonWidth = 36;    
    var windowMaxWidth = 1400;
    var windowMinWidth = 980;
    
    // objects
    var myScrollbar = scrollbar;
    var mySlidesReel = slides_reel;
    var myGrip = {};
    
    // arrays
    var reelInterestPoints = [];
    var scrollbarIntersetZones = [];
    
    // dimensions
    var frameWidth = 0;
    var reelWidth = 0;
    var trackWidth = 0; 
    var gripWidth = 0;
    var availableTrack;
    //var zoneWidth;
    
    // relations
    var frameToReel;
    var trackToReel;
    
    // positions
    var mouseOnGrip = 0;
    
    var activeZone = 0;
    var currentNudge = 0;
    var move_timer;
    /*
     * Private methods
     */
    
    var update_dimensions = function()
    {
        trackWidth = myScrollbar.width();
        frameWidth = mySlidesReel.get_frameWidth();
        reelWidth = mySlidesReel.get_reelWidth();
    }
    
    var update_grip_size = function()
    {
        var gripMarginAndBorder = 2 * ( parseInt( myGrip.css('border-left-width') ) + parseInt( myGrip.css('margin-left') ) );
        availableTrack = trackWidth - gripMarginAndBorder - minGripWidth;
        
        var desiredTrack = (mySlidesReel.get_interest_points().length - 1) * desiredInterestPointInterval;
        if( desiredTrack >= availableTrack )
        {
               gripWidth = minGripWidth;
        }
        else
        {
            availableTrack = desiredTrack;
            gripWidth = trackWidth - availableTrack;
        }
        
        myGrip.css('width', gripWidth - gripMarginAndBorder );
    }
    
    var calculate_interest_zones = function()
    {
        reelInterestPoints = mySlidesReel.get_interest_points();
        var zoneWidth = Math.floor( availableTrack / ( reelInterestPoints.length - 1 ) );
        
        for( var i = 0; i < reelInterestPoints.length; ++i )
        {
            if( i == 0 )
                scrollbarIntersetZones.push({'left': 0, 'center': 0, 'right': Math.floor( zoneWidth / 2 ) - 1 });
            else if( i == reelInterestPoints.length -1 )
                scrollbarIntersetZones.push({   'left': Math.floor( zoneWidth / 2 ) + ( i - 1) * zoneWidth, 
                                                'center': availableTrack,
                                                'right': availableTrack
                                            
                                            });
            else
                scrollbarIntersetZones.push({   'left': Math.floor( zoneWidth / 2 ) + ( i - 1) * zoneWidth, 
                                                'center': Math.floor( zoneWidth / 2 + ( i - 1) * zoneWidth + ( zoneWidth / 2) ),
                                                'right': Math.floor( zoneWidth / 2 + ( i - 1) * zoneWidth + ( zoneWidth - 1 ) )
                                            });
        }
    }
    
    var set_grip_position = function( position )
    {
        if( position < 0 )
            position = 0;
        else if( position > availableTrack )
            position = availableTrack;
        myGrip.css('left', position);
    }
    
    var set_active_interest_zone = function( interestZone )
    {
        if( interestZone < 0 )
            interestZone = 0;
        else if( interestZone >= scrollbarIntersetZones.length )
            interestZone = scrollbarIntersetZones.length - 1;
        
        activeZone = interestZone;
    }
    
    var find_active_zone = function( position )
    {
        var gripPosition = myGrip.position().left;
        for( var i = 0; i < scrollbarIntersetZones.length; ++i )
        {
            if( gripPosition >= scrollbarIntersetZones[i].left &&
                gripPosition <= scrollbarIntersetZones[i].right )
            {
                var nudge = 0;
                var nudgeSpan = 0;
                var nudgeInterval = 0;
                if( gripPosition < scrollbarIntersetZones[i].center )
                {
                    nudgeSpan = scrollbarIntersetZones[i].left - scrollbarIntersetZones[i].center;
                    nudgeInterval = maxnudge / nudgeSpan;
                    nudge = Math.floor( ( gripPosition - scrollbarIntersetZones[i].center ) * nudgeInterval );
                }
                else if( gripPosition > scrollbarIntersetZones[i].center )
                {
                    nudgeSpan = scrollbarIntersetZones[i].center - scrollbarIntersetZones[i].right;
                    nudgeInterval = maxnudge / nudgeSpan;
                    nudge = Math.ceil( ( gripPosition - scrollbarIntersetZones[i].center ) * nudgeInterval );
                }
                activeZone = i;
                currentNudge = nudge;
                break;
            }
        }
    }
    /*
     * Public methods
     */
    var append = function()
    {
        myGrip = $('<div />', {'id': 'grip', 'class': 'grip_inactive'} );
        myGrip.appendTo( myScrollbar );
        
        if( $(window).width() > windowMaxWidth )
            myScrollbar.width( windowMaxWidth - buttonWidth);
        else if( $(window).width() < windowMinWidth )
            myScrollbar.width( windowMinWidth - buttonWidth);
        else
            myScrollbar.width( $(window).width() - buttonWidth);
        
        update_dimensions();
        
        update_grip_size();
        calculate_interest_zones();
        
        myGrip.bind( 'mousedown.grip', function( event )
        {
            // style the grip
            myGrip.removeClass('grip_inactive').addClass('grip_active');
            
            // record mouse position relative to grip
            mouseOnGrip = event.pageX - myGrip.position().left;
            
            move_timer = setInterval( function() { find_active_zone(); mySlidesReel.move_reel( activeZone, currentNudge ); }, 200 );
            $(document).bind( 'mousemove.grip mouseup.grip', function( event )
            {
                if( event.type == 'mousemove')
                {
                    set_grip_position( event.pageX - mouseOnGrip );
                }
                else if( event.type == 'mouseup')
                {
                    clearInterval(move_timer);
                    find_active_zone();
                    animate_to_interest_zone( activeZone );
                    mySlidesReel.move_reel( activeZone );
                    myGrip.removeClass('grip_active').addClass('grip_inactive');
                    $(document).unbind( 'mousemove.grip mouseup.grip' );
                }
                    
            });
            stopEvent( event );
        });
        
        myScrollbar.bind('mousedown.track', function(event)
        {
            set_grip_position( event.pageX - myScrollbar.offset().left );
            find_active_zone();
            animate_to_interest_zone( activeZone );
            mySlidesReel.move_reel( activeZone );
        });
    }
    
    var remove = function()
    {
        myGrip.remove();
    }
    
    var animate_to_interest_zone = function( interestZone )
    {
        set_active_interest_zone( interestZone );
        myGrip.stop();
        myGrip.animate( {'left': scrollbarIntersetZones[ activeZone ].center }, 200 )
    }
    
    var resize = function()
    {
        if( $(window).width() > windowMaxWidth )
            myScrollbar.width( windowMaxWidth - buttonWidth);
        else if( $(window).width() < windowMinWidth )
            myScrollbar.width( windowMinWidth - buttonWidth);
        else
            myScrollbar.width( $(window).width() - buttonWidth);
        update_dimensions();
        update_grip_size();
        calculate_interest_zones();
        animate_to_interest_zone( activeZone );
    }
    
    var get_scrollbar_object = function()
    {
        return myScrollbar;
    }
    
    return {
        append      : append,
        remove      : remove,
        animate_to_interest_zone   : animate_to_interest_zone,
        resize      : resize,
        get_scrollbar_object  : get_scrollbar_object
    }
}

