ActivityFeed = function(obj, type, visibleEntries)
{
	this.obj			= obj;
	this.type			= type;
	this.entryInterval	= null;
	this.entryDelay		= 5000;
	this.data			= null;
	this.visibleEntries	= visibleEntries;

	this.init = function()
	{
		// Show feeds only in dsktop version
		if (this.obj.parent().is(':visible') && this.data == null) {
			var _this = this;
			$.ajax({
				url: 'lib/FeedReader.php',
				data: {language: language, type: this.type},
				dataType: 'json',
				success: function(data) {
					_this.data = data;
					_this.createEntries();
				}
			});
		}
	}
	
	this.refresh = function()
	{
		this.init();
	}

	this.createEntries = function()
	{
		var _this = this;
		for (var i=0; i<this.data.length; i++) {
			var date  = this.data[i].date;
			var short = this.data[i].short;
			var long  = this.data[i].long;
			var link  = this.data[i].link;
			if (link != '') {
				var startTag = '<a href="'+link+'" target="_blank" class="entry" id="'+(this.type+i)+'">';
				var endTag   = '</a>';
			} else {
				var startTag = '<div class="entry" id="'+(this.type+i)+'">';
				var endTag   = '</div>';
			}
			sidebarEntry  = startTag;
			sidebarEntry += '<div class="date">' + date + '</div>';
			sidebarEntry += '<div class="text">' + short + '</div>';
			sidebarEntry += endTag;
			sidebarEntry = $(sidebarEntry);

			this.obj.find('.scroll').append(sidebarEntry);
			if (i<visibleEntries) {
				sidebarEntry.fadeIn();
			}
		}

		if (this.data.length > 3) {
			this.startAnimation();
			this.obj.mouseover(function() {
				_this.stopAnimation();
			})
			this.obj.mouseout(function() {
				_this.startAnimation();
			})
		}
	}

	this.startAnimation = function()
	{
		var _this = this;
		this.entryInterval = setInterval(function() {
			_this.nextEntry();
		}, this.entryDelay)
	}

	this.stopAnimation = function()
	{
		clearInterval(this.entryInterval);
	}

	this.nextEntry = function()
	{
		var _this = this;
		var currentEntry = this.obj.find('.scroll .entry').eq(0);
		this.obj.find('.scroll .entry').eq(visibleEntries).delay(500).fadeIn();
		currentEntry.animate({opacity: 0}, 250).slideUp(500, function() {
			$(this).css({opacity: 100});
			var nextEntry = $(this).next('.entry');
			$(this).remove().appendTo(_this.obj.find('.scroll'));
		});
	}

	this.init();
}

