﻿NewsSlide = function(_options)
{
    // default configuration properties
    var defaults =
    {
        prevId: 'prevBtn',
        prevText: 'Previous',
        nextId: 'nextBtn',
        nextText: 'Next',
        speed: 800,
        auto: false,
        pause: 2000,
        continuous: false,
        labelId: 'lblNewsText',
        descId: 'lblNewsDesc',
        imageId: 'imgNewsImage'
    };

    var options = $.extend(defaults, _options);

    var data = null;
    var imgObject = null;
    var lblObject = null;
    var lblDesc = null;
    var prevObject = null;
    var nextObject = null;

    var currentNewsObject = null;
    var currentIndex = -1;
    var refreshTimer = null;

    InitMain();

    function InitMain()
    {
        imgObject = $('#' + options.imageId);
        lblObject = $('#' + options.labelId);
        lblDesc = $('#' + options.descId);
        prevObject = $('#' + options.prevId);
        nextObject = $('#' + options.nextId);
        // bind events
        imgObject.click(OpenDetailsUrl);
        prevObject.click(ManualPrevious);
        nextObject.click(ManualNext);
    };

    this.SetData = function(_data)
    {
        data = _data;
        GetNext();
        if (options.auto)
        {
            StartTimer();
        }
    };

    function OpenDetailsUrl()
    {
        if (currentNewsObject != null)
        {
            window.location = currentNewsObject.DetailsUrl;
        }
    };

    function DisplayCurrent()
    {
        if (currentIndex != -1)
        {
            var news = data[currentIndex];
            if (news != null)
            {
                DisplayNewsDetails(news);
            }
        }
    };

    function DisplayNewsDetails(_news)
    {

        currentNewsObject = _news;
        //imgObject.fadeOut('fast');
        imgObject.attr("src", _news.ImgUrl);
        imgObject.attr("title", _news.Text);
        //imgObject.fadeIn('fast');
        lblObject.html(_news.Text);
        lblDesc.html(_news.Desc);
    };

    function StopTimer()
    {
        if (refreshTimer != null)
        {
            window.clearInterval(refreshTimer);
        }
    };

    this.Start = function()
    {
        StartTimer();
    }


    function StartTimer()
    {
        refreshTimer = window.setInterval(GetNext, options.pause);
    };

    function ManualNext()
    {
        StopTimer();
        GetNext();
    };

    function ManualPrevious()
    {
        StopTimer();
        GetPrevious();
    };

    function GetNext()
    {
        if (currentIndex < data.length-1)
        {
            currentIndex++;
        }
        else
        {
            if (options.continuous)
            {
                currentIndex = 0;
            }
        }
        DisplayCurrent();
    };

    function GetPrevious()
    {
        if (currentIndex > 0)
        {
            currentIndex--;
        }
        else
        {
            if (options.continuous)
            {
                currentIndex = data.length;
            }
        }
        DisplayCurrent();
    };
}
