﻿// JScript File

YAHOO.namespace("Animation");

YAHOO.Animation.Scroll = function(containerId, scrollType, listItemSize) {
    this.Init(containerId, scrollType, listItemSize);
}    

YAHOO.Animation.Scroll.prototype = {

    ContainerId:null,
    MaxDuration:null,
    ScrollType:null,
    ListItemSize:null,
    NumOfListItems:null,
    TotalViews:null,
    MyAnimation:null,
   
    Init:function(containerId, scrollType, listItemSize )
    {
        // set base vars
        this.ContainerId = containerId;
        this.ScrollType = scrollType;
        this.ListItemSize = listItemSize;
        
        // calculate additional vars
        this.NumOfListItems = (document.getElementById(this.ContainerId).getElementsByTagName("li").length);
        this.TotalViews = this.NumOfListItems - 2; // 0 based indicator of possible display views
        this.MaxDuration = this.TotalViews * this.ListItemSize / 105;
        
        // set animation properties
        this.MyAnimation = new YAHOO.util.Anim(this.ContainerId);
        this.SetDuration( true );
        this.MyAnimation.method = YAHOO.util.Easing.easeOut;

    },
    
    
    // support function, gets the current display view (0 based value)
    GetCurrentViewNum:function() {
        var El = this.MyAnimation.getEl();
        switch ( this.ScrollType ) {
            // vertical based scroll
            case "VerticalScroll": 
                return -( this.GetPxValue( El.style.marginTop ) / this.ListItemSize );
            
            // horizontal based scroll
            case "HorizontalScroll": 
                return -( this.GetPxValue( El.style.marginLeft ) / this.ListItemSize );
        }
    
    },
    
    
    // function sets the duration of the animation 
    SetDuration:function( IsScrollAdvancing )
    {
        // set duration to the remaining time left of animation (calculated with the current position)

        // get current view position                
        var El = this.MyAnimation.getEl();
        var CurrentViewNum = this.GetCurrentViewNum();
        var Sec = 0;
        
        // set duration based on scroll advancing or retreating
        if ( IsScrollAdvancing == true ) {
            Sec = this.MaxDuration * ( ( this.TotalViews - CurrentViewNum ) / this.TotalViews );
        } else {
            Sec = this.MaxDuration * ( CurrentViewNum / this.TotalViews );
        }
        
        // make vertical scrolls slower
        if ( this.ScrollType == "VerticalScroll" ) {
            Sec *= 1.5;
        }
        
        this.MyAnimation.duration = Sec;

    },
    
    
    // function sets the attribute value of the animation
    SetAnimationAttribute:function ( IsScrollAdvancing )
    {
        switch ( this.ScrollType ) {
            // vertical based scroll
            case "VerticalScroll": 
                if ( IsScrollAdvancing == true ) {
                    this.MyAnimation.attributes.marginTop = {to: -( this.ListItemSize * this.TotalViews )};
                } else {
                    this.MyAnimation.attributes.marginTop = {to: 0};
                }
                break;
            
            // horizontal based scroll
            case "HorizontalScroll": 
                if ( IsScrollAdvancing == true ) {
                    this.MyAnimation.attributes.marginLeft = {to: -( this.ListItemSize * this.TotalViews )};
                } else {
                    this.MyAnimation.attributes.marginLeft = {to: 0};
                }
                break;
        }
    
    },
    
    
    // function retrieves the integer value from the given string (e.g., "-14px" returns "-14")
    GetPxValue:function( PxValue )
    {
        PxValue = PxValue.replace( /px/, "" );
        if ( PxValue == "" ) return 0;
        else return PxValue;
    },
    
    
    // function stops the animation on whatever frame it is on
    Pause:function()
    {
        this.MyAnimation.stop( false );
    },
    
    
    // function advances the scroll effect
    ScrollAdvance:function()
    {
        // finish animation if it's not yet complete
        this.Pause();
        
        // assign new animation
        this.SetAnimationAttribute( true );
        this.SetDuration ( true );

        // perform animation
        this.MyAnimation.animate();
    },
    
    
    // function retreats the scroll effect
    ScrollRetreat:function()
    {
        // finish animation if it's not yet complete
        this.Pause();

        // assign new animation
        this.SetAnimationAttribute( false );
        this.SetDuration ( false );

        // perform animation
        this.MyAnimation.animate();
    },
    
    
    // function sets advance and retreat effects on buttons
    SetAdvanceAndRetreatOnButtons:function( AnimationInstance, AdvanceButtonId, RetreatButtonId )
    {
        document.getElementById(RetreatButtonId).onclick = function(){ return false; }
        document.getElementById(RetreatButtonId).onmouseover = function(){ AnimationInstance.ScrollRetreat(); }
        document.getElementById(RetreatButtonId).onmouseout = function(){ AnimationInstance.Pause(); }
        document.getElementById(AdvanceButtonId).onclick = function(){ return false; PauseRotation(true);}
        document.getElementById(AdvanceButtonId).onmouseover = function(){ AnimationInstance.ScrollAdvance(); }
        document.getElementById(AdvanceButtonId).onmouseout = function(){ AnimationInstance.Pause(); }
    }    
   
}
