/*
	sky.ticker.js
	2007-11-13 ~ 2007-11-14 

	Seo, Jaehan <daddyofsky@gmail.com>

    @dependence sky.base.js
*/

    var ticker = Class({
        init : function(options) {
            this.playing = false;
            this.timer = null;
			Class.extend(this, {
                object : null,
                child : null,
                size : 0,
                amount : 1,
                delay : 50,
                pause : 2000
			}, options);

            // TODO : validate

            // init
            this.object = Element.extend(this.object);
            this.child = Element.extend(this.child || this.object.childNodes[0]);

            this.oSize = this.object.getSize();
            this.cSize = this.child.getSize();

            // TODO : variation
            var child = this.child.cloneNode(true);
            child.style.marginTop = -1;
            this.object.appendChild(child);

            // event
            this.onMouseEvent = this.onMouse.bindForEvent(this);
            $l(this.object, 'mouseover', this.onMouseEvent);
            $l(this.object, 'mouseout', this.onMouseEvent);

            // trigger
            this.onPlay = this.play.bind(this);
            setTimeout(this.onPlay, this.pause);
        },
        onMouse : function(evt) {
            switch (evt.type.toLowerCase()) {
                case 'mouseover' : this.stop(); break;
                case 'mouseout' : this.play(); break;
            }
        },
        play : function() {
            this.playing = true;
            this.scroll();
        },
        stop : function() {
            this.playing = false;
            if (this.timer) clearTimeout(this.timer);
        },
        scroll : function() {
            if (!this.playing) return;

            // TODO : variation

            // scroll up
            if (this.object.scrollTop >= this.cSize.height) {
                this.object.scrollTop -= this.cSize.height;
            }
            var to = this.object.scrollTop + this.amount;
            var gap = to % this.size;
            if (gap < this.amount) {
                this.object.scrollTop += (gap ? gap : this.amount);
                this.timer = setTimeout(this.onPlay, this.pause);
            } else {
                this.object.scrollTop += this.amount;
                this.timer = setTimeout(this.onPlay, this.delay);
            }
        }
    });

