﻿
/****************************************************
* Script: ScrollingDiv
* Date: (November 12th 2008)
* Author: Dimitri Troncquo
* Contact: dimi3.t@gmail.com
* Description: 
* Creates a customizable vertical 
* scroller for div elements
*
* Requirements:
*  - Prototype framework
*  - script.aculo.us framework
*  - acms.js
*  - drk_scrollingdiv.css (or equivalent css)
****************************************************/
var ScrollingDirection = {
    Up: 1, Down: -1
}; 
var ScrollingDiv = Class.create({
  initialize: function(divId, scrollingPixels) {  
    if(divId && scrollingPixels){
        this.div = document.getElementById(divId);
        this.scrollBy = scrollingPixels;
        this.isScrolling = false;
        this.canScroll = true;
        this.pixelsScrolled = 0;
    }
    if(!this.div)  
        alert("Failed to initialize ScrollingDiv");
    else{         
        this.calculateTopBottom();
        //just a little hack to avoid delay on first click:
        this.div.style.top = -10 + "px";
        new Effect.Move(this.div, { x: 0, y: -1, duration: 0.01 });
        new Effect.Move(this.div, { x: 0, y: 1, duration: 0.01 });        
    }
  },
  calculateTopBottom: function(){
    this.absoluteTop = 0;//findPosition(this.div).y;
    this.absoluteBottom = getDimensions(this.div).height - this.scrollBy -10;
  },
  startScroll: function(scrollParameters){
    if(scrollParameters.direction){                   
        this.canScroll = true;
        function doEffectDelegate(me, direction){
            if(!me.isScrolling){
                me.isScrolling = true;
                if((me.pixelsScrolled += (me.scrollBy * direction * -1)) < 0)
                    me.pixelsScrolled = 0;
                switch(direction){
                    case ScrollingDirection.Up:
                        if(me.pixelsScrolled <= me.absoluteTop){  
                           me.toTop();
                           me.isScrolling = false;                                                   
                           return;
                        }
                        break;
                    case ScrollingDirection.Down:                        
                        if(me.pixelsScrolled >= me.absoluteBottom){
                            me.isScrolling = false;
                            return;
                        }
                        break;
                }
                var effect = new Effect.Move(me.div, { x: 0, y: (me.scrollBy * direction), duration: 0.07 });

                function resetSate(currentInstance){                 
		            setTimeout(function() {
		                currentInstance.isScrolling = false;
		                if(currentInstance.canScroll)
                            doEffectDelegate(currentInstance, direction);		                
		            }, effect.options.duration*1000);
		        }
		        resetSate(me);		        
            }            
        }
        doEffectDelegate(this, scrollParameters.direction);
    }
  },
  endScroll: function(scrollParameters){
    this.canScroll = false;
  },
  toTop: function(){
    function resetToTopDelegate(me){
        setTimeout(function() {
            me.div.style.top = -10 + "px"; 
            me.pixelsScrolled = 0;
        }, 75);   
    }
    resetToTopDelegate(this);
  }
  });
