/**
 * Scrolling class, scrolls the content of an element
 * @author PeteAllison
 */
/**
 * initial object:
 * If passed a valid element, takes this as the target
 * If passed a hash with .target as a child, process this as the target
 * If passed a hash with .source then uses this as the source for the scroller
 */
// <![CDATA[

var Scroller = new Class({
    Implements: Options,

    version: '2.0.0',

    options: {
        direction: 'up',
        stepSize: 11,
        duration: 900,
        mouseOverPause: false
    },

    scrollPosition: {
        height: 0,
        top: 0,
        container: 0
    },

    state: 'active',

    initialize: function(obj, options){
        this.log('Scroller initialization')
        if (!obj)
            return false;

        if (obj.target)
            this.targetElement = this.getElement(obj.target);
        else
            this.targetElement = this.getElement(obj);

        if (!this.targetElement) {
            this.error('Cannot find target element')
            return false;
        }

        this.setOptions(options);

        if (this.targetElement.get('id') == '')
            this.targetElement.setProperty('id', 'scollingElement')

        this.scrollPosition.container = this.targetElement.getSize().y;

        if (obj.source) {
            this.sourceElement = this.getElement(this.source).clone(true, true);
        } else {
            this.sourceElement = this.targetElement.clone(true, true);
            this.targetElement.set('html', '');
        }

        $try(function(){
            this._style = new Element('style').setProperty('type', 'text/css').inject(document.head);
            var sheet = '#' + this.targetElement.get('id') + '{overflow:hidden;}';
            switch (Browser.Engine.name) {
                case 'trident':
                    this._style.styleSheet.cssText = sheet;
                    break;
                default:
                    this._style.set('text', sheet);
                    break;
            }
        }.bind(this));

        this.targetElement.setStyle('position', 'relative');

        /*this.scroller = new Fx.Scroll(this.targetElement, {
         'onComplete': function() {this.log('fish')}.bind(this),
         'duration': 10000
         });*/
        var scroller = new Element('div', {
            'html': this.sourceElement.get('html'),
            'styles': {
                'position': 'relative'
            }
        }).inject(this.targetElement)
        this.scrollPosition.height = scroller.getSize().y

        this.scroller = new Fx.Tween(scroller, {
            'duration': this.options.duration,
            'transition': 'linear',
            'onComplete': function(){
                this.start();
            }.bind(this)
        });

        if (this.options.mouseOverPause) {
            // Create a overlay
            var temp = this.targetElement.getCoordinates()
            this.overlay = new Element('div', {
                'styles': {
                    'width': temp.width,
                    'height': temp.height,
                    'position': 'absolute',
                    'top': temp.top,
                    'left': temp.left,
                    'background-color': 'transparent',
					'border': '1px solid'
                }
            }).inject(document.body)
            this.overlay.addEvents({
                'mouseover': function(){
                    this.state = 'paused';
                }.bind(this),
                'mouseout': function(e){
                    this.state = 'active';
                    this.start();
                }.bind(this)
            });
        }

        this.start();

        this.log('Scoller initialized');
    },

    start: function(){
        if (this.state == 'active') {
            if (this.scrollPosition.top > this.scrollPosition.height * -1) {
                this.scroller.start('top', this.scrollPosition.top, this.scrollPosition.top -= this.options.stepSize);
            } else {
                this.scroller.set('top', this.scrollPosition.container)
                this.scrollPosition.top = this.scrollPosition.container
                this.start();
            }
        }
    },

    /**
     * Inspects the passed item and retrieves the element associated. It can be
     * passed a string to the ID of an element, a real element
     * @param {mixed} objectReference
     */
    getElement: function(objectReference){
        if ($type(objectReference) == 'element')
            return objectReference;

        return $(objectReference);
    },

    /**
     * Outputs a info message to FireBug
     */
    log: function(){
        if (window.console && console.info)
            console.info.apply(console, arguments);
    },

    /**
     * Outpus an error message to FireBug
     */
    error: function(){
        if (window.console && console.warn)
            console.warn.apply(console, arguments);
    }
})

// ]]>
