﻿
function HeadlineRotatorControl(ParentElement, Interval, FadePeriod)
{
    // ControlsVisible options
    this.CONTROLS_VISIBLE_ALWAYS = 0;
    this.CONTROLS_VISIBLE_NEVER = 1;
    this.CONTROLS_VISIBLE_ON_MOUSEOVER = 2;

    // default values
    this.Interval = 10000; // 10 seconds
    this.FadePeriod = 2000; // 2 seconds
    this._ControlsVisible = this.CONTROLS_VISIBLE_ON_MOUSEOVER;

    // private members
    this.ParentElement = null;
    this.Headlines = new Array();
    this.CurrentHeadlineIndex = -1;
    this.ContentArea = null;
    this.Controls = null;
    this.ControlsElement = null;
    this.PreviousHeadlineButton = null;
    this.NextHeadlineButton = null;
    this.PauseRotationButton = null;
    this.HeadlineTitle = null;

    this.constructor = function(ParentElement, Interval, FadePeriod)
    {
        this.ParentElement = ParentElement;

        if (Interval != undefined)
        {
            this.Interval = Interval;
        }
        if (FadePeriod != undefined)
        {
            this.FadePeriod = FadePeriod;
        }

        this.Build();
    };

    this.ControlsVisible = function(ControlsVisible)
    {
        if (ControlsVisible == undefined)
        {
            // get
            return this._ControlsVisible;
        }
        else
        {
            // set
            switch (ControlsVisible)
            {
                case this.CONTROLS_VISIBLE_ALWAYS:
                    {
                        this.ShowControls();
                    }
                    break;
                case this.CONTROLS_VISIBLE_NEVER:
                    {
                        this.HideControls();
                    }
                    break;
                case this.CONTROLS_VISIBLE_ON_MOUSEOVER:
                    {
                        // hide for now, until the mouse hovers over it
                        this.HideControls();
                    }
                    break;
                default:
                    {
                        // erroneous value - ignore
                        ControlsVisible = this._ControlsVisible;
                    }
                    break;
            }
            this._ControlsVisible = ControlsVisible;
        }
    };

    this.ShowControls = function()
    {
        this.Controls.Show();
        //this.Controls.FadeIn(500);
    };

    this.HideControls = function()
    {
        this.Controls.Hide();
        //this.Controls.FadeOut(100);
    };

    this.OnMouseOver = function()
    {
        if (this.ControlsVisible() == this.CONTROLS_VISIBLE_ON_MOUSEOVER)
        {
            this.ShowControls();
        }
        this.ContentArea.className = 'HeadlineRotatorContentAreaHover';
        this.ControlsElement.className = 'HeadlineRotatorControlsHover';
    };

    this.OnMouseOut = function()
    {
        if (this.ControlsVisible() == this.CONTROLS_VISIBLE_ON_MOUSEOVER)
        {
            this.HideControls();
        }
        this.ContentArea.className = 'HeadlineRotatorContentArea';
        this.ControlsElement.className = 'HeadlineRotatorControls';
    };

    this.AddHeadline = function(Title, Link, RootElement)
    {
        var NewHeadline = new Headline(this.ContentArea, Title, Link, RootElement, this.FadePeriod);
        if (this.Headlines.length == 0)
        {
            NewHeadline.Show();
        }
        else
        {
            NewHeadline.Hide();
        }
        this.Headlines.push(NewHeadline);
        RootElement.style.display = '';
    };

    this.Start = function()
    {
        this.NextHeadline();
        this.StartRotationThread();
        this.PauseRotationButton.className = 'HeadlineRotatorButton';
    };

    this.Stop = function()
    {
        this.StopRotationThread();
        this.PauseRotationButton.className = 'HeadlineRotatorButtonPaused';
    };

    this.PreviousHeadline = function()
    {
        this.Rotate(-1);
    };

    this.NextHeadline = function()
    {
        this.Rotate(1);
    };

    this.TogglePause = function()
    {
        if (this.RotationThread != null)
        {
            this.Stop();
        }
        else
        {
            this.Start();
        }
    };

    this.Rotate = function(Direction)
    {
        this.ResetRotationThread();

        if (this.CurrentHeadlineIndex >= 0 && this.CurrentHeadlineIndex < this.Headlines.length)
        {
            this.Headlines[this.CurrentHeadlineIndex].FadeOut();
        }

        this.CurrentHeadlineIndex += Direction;
        if (this.CurrentHeadlineIndex >= this.Headlines.length)
        {
            this.CurrentHeadlineIndex = 0;
        }
        else if (this.CurrentHeadlineIndex < 0)
        {
            this.CurrentHeadlineIndex = this.Headlines.length - 1;
        }

        if (this.CurrentHeadlineIndex >= 0 && this.CurrentHeadlineIndex < this.Headlines.length)
        {
            this.Headlines[this.CurrentHeadlineIndex].FadeIn();

            this.SetHeadlineTitle(this.Headlines[this.CurrentHeadlineIndex]);
        }
    };

    this.StopRotationThread = function()
    {
        clearTimeout(this.RotationThread);
        this.RotationThread = null;
    };

    this.StartRotationThread = function()
    {
        var _this = this;
        this.RotationThread = setTimeout(function() { _this.NextHeadline(); }, this.Interval);
    };

    this.ResetRotationThread = function()
    {
        if (this.RotationThread != null)
        {
            this.StopRotationThread();
            this.StartRotationThread();
        }
    };

    this.SetHeadlineTitle = function(Headline)
    {
        if (this.HeadlineTitle.firstChild != null || this.HeadlineTitle.firstChild != undefined)
        {
            this.HeadlineTitle.removeChild(this.HeadlineTitle.firstChild);
        }
        if (Headline.Title != undefined && Headline.Title != null)
        {
            this.HeadlineTitle.appendChild(document.createTextNode(Headline.Title));
        }
        if (Headline.Link != undefined && Headline.Link != null)
        {
            this.HeadlineTitle.href = Headline.Link;
        }
    };

    this.Build = function()
    {
        this.ContentArea = document.createElement('div');
        this.ControlsElement = document.createElement('div');
        this.PreviousHeadlineButton = document.createElement('a');
        this.NextHeadlineButton = document.createElement('a');
        this.PauseRotationButton = document.createElement('a');
        this.HeadlineTitle = document.createElement('a');

        this.ParentElement.appendChild(this.ContentArea);
        this.ParentElement.appendChild(this.ControlsElement);
        this.ControlsElement.appendChild(this.NextHeadlineButton);
        this.ControlsElement.appendChild(this.PauseRotationButton);
        this.ControlsElement.appendChild(this.PreviousHeadlineButton);
        this.ControlsElement.appendChild(this.HeadlineTitle);

        this.ParentElement.className = 'HeadlineRotator';
        this.ContentArea.className = 'HeadlineRotatorContentArea';
        this.ControlsElement.className = 'HeadlineRotatorControls';
        this.PreviousHeadlineButton.className = 'HeadlineRotatorButton';
        this.NextHeadlineButton.className = 'HeadlineRotatorButton';
        this.PauseRotationButton.className = 'HeadlineRotatorButton';
        this.HeadlineTitle.className = 'HeadlineRotatorTitle';

        this.PreviousHeadlineButton.href = 'javascript:';
        this.NextHeadlineButton.href = 'javascript:';
        this.PauseRotationButton.href = 'javascript:';
        var _this = this;
        this.PreviousHeadlineButton.onclick = function() { _this.PreviousHeadline(); };
        this.NextHeadlineButton.onclick = function() { _this.NextHeadline(); };
        this.PauseRotationButton.onclick = function() { _this.TogglePause(); };

        this.PreviousHeadlineButton.appendChild(document.createTextNode('<'));
        this.PauseRotationButton.appendChild(document.createTextNode('||'));
        this.NextHeadlineButton.appendChild(document.createTextNode('>'));
        this.HeadlineTitle.appendChild(document.createTextNode(''));

        this.Controls = new ElementFader(this.ControlsElement);

        this.ParentElement.onmouseover = function() { _this.OnMouseOver(); };
        this.ParentElement.onmouseout = function() { _this.OnMouseOut(); };
        if (this.ControlsVisible() != this.CONTROLS_VISIBLE_ALWAYS)
        {
            this.HideControls();
        }
    };

    this.constructor(ParentElement, Interval, FadePeriod);
};

function Headline(ParentElement, Title, Link, RootElement, FadePeriod)
{
    this.ParentElement = null;
    this.Title = null;
    this.Link = null;
    this.Content = null;

    this.constructor = function(ParentElement, Title, Link, RootElement, FadePeriod)
    {
        this.ParentElement = ParentElement;
        this.Title = Title;
        this.Link = Link;

        RootElement.parentNode.removeChild(RootElement);
        this.ParentElement.appendChild(RootElement);
        this.ParentElement.style.position = 'relative';
        RootElement.style.position = 'absolute';
        RootElement.style.top = 0;
        RootElement.style.left = 0;

        this.Content = new ElementFader(RootElement, FadePeriod);
    };

    this.FadeOut = function(FadePeriod)
    {
        this.Content.Element.style.zIndex = 1;
        this.Content.FadeOut(FadePeriod);
    };

    this.FadeIn = function(FadePeriod)
    {
        this.Content.Element.style.zIndex = 2;
        this.Content.FadeIn(FadePeriod);
    };

    this.Show = function()
    {
        this.Content.Element.style.zIndex = 2;
        this.Content.Show();
    }

    this.Hide = function()
    {
        this.Content.Element.style.zIndex = 1;
        this.Content.Hide();
    }

    this.constructor(ParentElement, Title, Link, RootElement, FadePeriod);
};

