﻿$().ready(function() {
    var Groups =
        [{ gName: 'Home page news items', eName: '.column.first.feature.green-solid'},
        { gName: 'What\'s on Homepage items', eName: '.column.feature.purple-solid'}];

    var tmr;

    $(Groups).each(function(i, item) {
        $.getJSON("ArticleHandler.ashx?Group=" + item.gName, function(data) {
            var items = eval(data);
            if (items == null || items.length == 0)
                return;
            var ItemIndex = 1;
            $.timer(8000, function(timer) {
                timer.stop();
                var newsElement = $(item.eName);
                newsElement.find('img').fadeOut(function()
                {
                    $(this)[0].src = items[ItemIndex].ImageUrl != null ? items[ItemIndex].ImageUrl : null;
                    $(this)[0].alt = items[ItemIndex].Title != null ? items[ItemIndex].Title : null;
                    $(this)[0].title = 'Read the article on: ' + items[ItemIndex].Title != null ? items[ItemIndex].Title : null;
                    $(this).fadeIn();

                    newsElement.find('h3').text(items[ItemIndex].Title);
                    newsElement.find('p').text(items[ItemIndex].Summary).css({ 'max-height': '110px', 'min-height': '110px', 'height': '110px' }).append(' <a href="index.aspx?articleid=' + items[ItemIndex].ArticleID + '">Read more</a>');
                    newsElement.find('a.itemSelected').removeClass('itemSelected');
                    newsElement.find('ol li:nth-child(' + (ItemIndex + 1) + ') a').addClass('itemSelected');

                    ItemIndex = ItemIndex + 1 >= items.length ? 0 : ItemIndex + 1;
                    timer.reset(timer.interval);
                });
            });
        });
    });
});

jQuery.timer = function(interval, callback) {
    /**
    *
    * timer() provides a cleaner way to handle intervals  
    *
    *	@usage
    * $.timer(interval, callback);
    *
    *
    * @example
    * $.timer(1000, function (timer) {
    * 	alert("hello");
    * 	timer.stop();
    * });
    * @desc Show an alert box after 1 second and stop
    * 
    * @example
    * var second = false;
    *	$.timer(1000, function (timer) {
    *		if (!second) {
    *			alert('First time!');
    *			second = true;
    *			timer.reset(3000);
    *		}
    *		else {
    *			alert('Second time');
    *			timer.stop();
    *		}
    *	});
    * @desc Show an alert box after 1 second and show another after 3 seconds
    *
    * 
    */

    var interval = interval || 100;
    if (!callback)
        return false;

    _timer = function(interval, callback) {
        this.stop = function() {
            clearInterval(self.id);
        };

        this.internalCallback = function() {
            callback(self);
        };

        this.reset = function(val) {
            if (self.id)
                clearInterval(self.id);

            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};