/*
	Exclusive news plugin.
	
	@author: Angel Kostadinov  (angel.kostadinov@toxicmedia.bg)
	@copyright: CMSbg (tm)
*/
Array.prototype.reset = function()
{
	return this[0];
};

String.prototype.getAlphas = function()
{
	return this.split(/\s{0}/);
};

(function($) 
{
	$.fn.ticker = function(src)
	{
		var defaults = 
		{
			timeout: 6000,
			refreshTimeout: 60000,
			speed: 50,
			element: null,
			maxStringLength: 60	
		};
		
		var topics = [];
		
		var timer = null;
		
		var options  = $.extend({}, defaults, options);
		
		var goTyping = function()
		{
			if (0 === topics.length) 
			{
				getTopics(goTyping);
			}
			else 
			{
				loadDefaultTopic();
			}
		};
		
		var loadDefaultTopic = function()
		{
			stopTyping();
			
			$('a.pause').removeClass('play');
			
			if (topics.length == 1)
			{
				var t = topics.reset();
				
				writeTopic(t[0].getAlphas(),t[1]);
			}
			else 
			{
				var t = topics.shift();
				
				topics.push(t);
				
				writeTopic(t[0].getAlphas(),t[1]);
			}
		};
		
		var loadPreviousTopic = function()
		{
			stopTyping();
			
			var t = topics.shift();
			
			topics.push(t);
			
			var t = topics.reset();
	
			writeTopic(t[0].getAlphas(),t[1]);
			
		};
		
		var emptyTopics = function()
		{
			$(options.element).text('');
			
			return true;
		};
		
		var splitTopic = function(topic)
		{
			return topic.split(/\s{0}/);
		}
		
		var writeTopic = function(topic, address)
		{
			if (topic.length > 0)
			{
				var c = topic.shift();
				
				$(options.element).attr('href',address + '.html');
				$(options.element).html($(options.element).text().slice(0,-1) + c + '_');
				
				timer = setTimeout(function()
				{
					writeTopic(topic,address);
				},50);
			}
			else 
			{
				$(options.element).html($(options.element).text().slice(0,-1));
				
				timer = setTimeout(goTyping,options.timeout);
			}
		}
		
		var error = function()
		{
			alert('Error while loading topic(s)');
		};
		
		var getTopics = function( callback )
		{
			$.ajax(
			{
			   type: "GET",
			   url: getDomain() + "/libraries/services/topics.php",
			   dataType: 'xml',
			   cache:false,
			   async: true,
			   success: function( response )
			   {
		   			topics.length = null;
		   	
			   		$('topic',response).each(function()
			   		{
			   			var topic = $(this).text();
			   			var address = $(this).attr('address');
			   			
			   			var t = new Array(topic,address)
			   			
			   			topics.push(t);
			   		});
			   		
			   		if (topics.length > 0)
			   		{
			   			timer = setTimeout(goTyping,100);
			   		}
			   }
			 });
		};
		
		var pauseTyping = function( e )
		{
			var element = e.target;
			
			if ($(element).hasClass('play'))
			{
				$(element).removeClass('play');

				loadDefaultTopic();
			}
			else 
			{
				$(element).addClass('play');
				timer = clearTimeout(timer);
			}
		};
		
		var stopTyping = function()
		{
			timer = clearTimeout(timer);
			emptyTopics();
		};
		
		return this.each(function()
		{	
			options.element = this;
			
			$('.ui-controls a.prev').eq(0).bind('click',loadPreviousTopic);
			$('.ui-controls a.pause').eq(0).bind('click',pauseTyping);
			$('.ui-controls a.next').eq(0).bind('click',loadDefaultTopic);
			
			
			goTyping();
		});
	};
})(jQuery);

$(function()
{
	$('div[id=exclusive] div a').ticker();
});