﻿
//news ticker scroller
//**********************************************
var news = {
    FeedHolder: null,
    ItemHeight: 0,
    ItemCount: 0,
    CurrentItemIdx: 0,
    AutoPlayTimer: null,
    AutoPlayInterval: 4000,
    IsBusy: false,
    Init: function(feedId)
    {
        this.FeedHolder = document.getElementById(feedId);
        if (this.FeedHolder)
        {
            //this.FeedHolder.setAttribute("onmouseover", "news.OnMouseOver()");
            //this.FeedHolder.setAttribute("onmouseout", "news.OnMouseOut()");
            this.FeedHolder.onmouseover = function() { news.OnMouseOver(); };
            this.FeedHolder.onmouseout = function() { news.OnMouseOut(); };
            this.getItemHeight();
            this.AutoPlay();
        }
    },
    AutoPlay: function()
    {
        if (this.FeedHolder)
        {
            if (this.AutoPlayTimer) this.Play();
            this.AutoPlayTimer = setTimeout("news.AutoPlay()", this.AutoPlayInterval);
        }
    },
    StopAutoPlay: function()
    {
        clearTimeout(this.AutoPlayTimer);
    },
    Play: function()
    {
        if (this.FeedHolder && !this.IsBusy)
        {
            this.StopAutoPlay();

            if (this.CurrentItemIdx == this.ItemCount - 1)
                this.goToFirst();
            else
            {
                this.animate(0.2, -this.ItemHeight);
                this.CurrentItemIdx++;
            }
        }
    },
    Rewind: function()
    {
        if (this.FeedHolder && !this.IsBusy)
        {
            this.StopAutoPlay();

            if (this.CurrentItemIdx == 0)
                this.goToLast();
            else
            {
                this.animate(0.2, this.ItemHeight);
                this.CurrentItemIdx--;
            }
        }
    },
    AnimationComplete: function()
    {
        this.IsBusy = false;
    },
    OnMouseOver: function()
    {
        this.IsBusy = true;
    },
    OnMouseOut: function()
    {
        this.IsBusy = false;
    },
    animate: function(duration, step)
    {

        this.IsBusy = true;

        var fadeOut = new AjaxControlToolkit.Animation.FadeOutAnimation(this.FeedHolder, 0.05, 20, 0.3, 1, true);
        var fadeIn = new AjaxControlToolkit.Animation.FadeInAnimation(this.FeedHolder, 0.1, 20, 0.3, 1, true);
        var move = new AjaxControlToolkit.Animation.MoveAnimation(this.FeedHolder, duration, 20, 0, step, true, "px");
        var script = new AjaxControlToolkit.Animation.ScriptAction(this.FeedHolder, 0.1, 0, "news.AnimationComplete()");

        var seq = new AjaxControlToolkit.Animation.SequenceAnimation(this.FeedHolder, 0, 20);
        //seq.add(fadeOut);
        seq.add(move);
        //seq.add(fadeIn);
        seq.add(script);
        seq.play();
    },
    goToFirst: function()
    {
        if (this.FeedHolder && !this.IsBusy)
        {
            var duration = 0.2 * this.ItemCount;
            var step = this.ItemHeight * (this.ItemCount - 1);
            this.animate(duration, step);
            this.CurrentItemIdx = 0;
        }
    },
    goToLast: function()
    {
        if (this.FeedHolder && !this.IsBusy)
        {
            var duration = 0.2 * this.ItemCount;
            var step = this.ItemHeight * (this.ItemCount - 1);
            this.animate(duration, -step);
            this.CurrentItemIdx = this.ItemCount - 1;
        }
    },
    getItemHeight: function()
    {
        if (this.FeedHolder)
        {
            var items = this.FeedHolder.getElementsByTagName("div");
            if (items.length > 0)
            {
                this.ItemHeight = items[0].offsetHeight;
                this.ItemCount = 0;
                for (var i = 0; i < items.length; i++)
                {
                    if (items[i].className == "blog_item")
                        this.ItemCount++;
                }
            }
        }
    }
}
//**********************************************

