﻿/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2010-12-20 */

function NewsScroller(opts) {
    this._init(opts);
};

NewsScroller.prototype = {
    opts: null,
    DefaultOptions:
    {
        tagItem: 'LI',
        classItem: null,
        intervalDuration: 5000,
        copies: 3,
        animationFps: 30,
        fadeDuration: 500
    },

    _init: function(opts) {
        var self = this;

        var o = self.opts = dgTools.extend({}, 'container tagItem classItem intervalDuration copies animationFps fadeDuration', opts, self.DefaultOptions);
        o.pause = false;
        o.scrollerLimit = 0;
        o.container = $(o.container);
        o.lineWidth = parseInt(dgTools.Elm.totalWidth(o.container), 10);
        o.lineHeight = parseInt(dgTools.Elm.totalHeight(o.container), 10);
        if (!o.container) return;
        o.containerLi = [];
        for (var j = 0; j < o.container.childNodes.length; j++) {
            var li = o.container.childNodes[j];
            if (li.tagName == o.tagItem.toUpperCase()) {
                if (o.classItem === null || o.classItem === undefined || o.classItem === li.className) {
                    o.containerLi.push(li);
                }
            }
        }

        o.scrollerLimit = o.containerLi.length + (o.containerLi.length * o.copies) + 1;

        var dv = dgTools.Elm.build('div', ['style', 'position:absolute;left:0px;top:0px;overflow:hidden;']);
        dv.style.width = dgTools.Elm.totalWidth(o.container) + 'px';
        var posMode = dgTools.Elm.getRenderedStyle(o.container, 'position');
        if (posMode != 'absolute' && posMode != 'relative') o.container.style.position = 'relative';
        o.containerInner = dv;

        while (o.container.childNodes.length > 0) {
            var el = o.container.childNodes[0];
            o.container.removeChild(el);
            dv.appendChild(el);
            el = null;
        }
        o.container.appendChild(o.containerInner);

        for (var j = 0; j < o.copies; j++) {
            for (var y = 0; y < o.containerLi.length; y++) {
                dv.appendChild(o.containerLi[y].cloneNode(true));
            }
        }

        dgTools.observe(o.container, 'mouseover', function(evt) {
            o.pause = true;
        });
        dgTools.observe(o.container, 'mouseout', function(evt) {
            o.pause = false;
        });

        var go = function(index, position) {
            if (index < o.scrollerLimit) {
                if (!o.pause) {
                    // Scroll down
                    dgTools.easyAnimate(o.containerInner, null, { 'top': -position }, 1000, o.animationFps, null, dgTools.Transitions.linear);
                    // Do this again
                    setTimeout(function() { go(index + 1, position + o.lineHeight); }, o.intervalDuration);
                } else if (o.pause) {
                    // Cursor is over a headline so pause
                    setTimeout(function() { go(index, position); }, o.intervalDuration);
                }
            } else {
                // Last headline has scrolled into view
                // so now loop back to the first headline.

                // Fade out
                dgTools.easyAnimate(o.containerInner, { 'opacity': 1 }, { 'opacity': 0 }, o.fadeDuration, o.animationFps, function() {

                    // Scroll to top
                    dgTools.easyAnimate(o.containerInner, null, { 'top': 0 }, 0, o.animationFps, function() {

                        // Fade in
                        dgTools.easyAnimate(o.containerInner, { 'opacity': 0 }, { 'opacity': 1 }, o.fadeDuration, o.animationFps, function() {
                            // Restart the scoll from the beginning
                            setTimeout(function() { go(2, o.lineHeight); }, o.intervalDuration)

                        }, dgTools.Transitions.linear);

                    },
                    dgTools.Transitions.linear);

                }, dgTools.Transitions.linear);

            }
        }


        dgTools.easyAnimate(o.containerInner, null, { 'top': 0 }, 0, o.animationFps, function() {
            setTimeout(function() { go(2, o.lineHeight); }, o.intervalDuration);
        },
        dgTools.Transitions.linear);

        dv = null; // release memory
    }
};
