Array.prototype.reset = function()
{
	return this[0];
};

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


(function($) 
{
	$.fn.pngFix = function(settings) 
	{
		return this.each( function() 
		{
			if (navigator.appName.toString().toLowerCase().indexOf('explorer') != -1)
			{
				$(this).attr('writing-mode', 'tb-rl');
				$(this).css('background-image', 'none');
				$(this).css('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + F.getDomain() + '/templates/images/transparent.png",sizingMethod="scale")');
			}
		});
	};
	
	$.fn.animateNestification = function(options) 
	{
		/* Plugin options*/
		var defaults = 
		{
			effect: 'slideDown',
			speed: 200
		};
		
		var options = $.extend({}, defaults, options);

	    return this.each(function() 
	    {	
	    	switch(options.effect)
	    	{
	    		case 'slideUp':
	    			$(this).slideUp(options.speed);
	    			break;
	    		case 'slideDown':
	    			$(this).slideDown(options.speed);
	    			break;
	    		case 'fadeIn':
	    			$(this).fadeIn(options.speed);
	    			break;
	    		case 'fadeOut':
	    			$(this).fadeOut(options.speed);
	    			break;	
	    	}
	    });
	}; 
	
	$.fn.nestify = function(options) 
	{
		/* Plugin options*/
		var defaults = 
		{
			speed: 200,
			effect: 'slideDown'
		};
		
		var options = $.extend({}, defaults, options);
		
	    return this.each(function() 
	    {	
	    	$('ul',$(this)).css({display: "none"});
	    	$("li",$(this)).hover(function()
			{
				$(this).find('a.primary').addClass('selected');
				$(this).find('ul:first').css(
					{
						visibility: "visible", 
						display: "none"
					}).animateNestification(
						{
							effect: options.effect,
							speed: options.speed
						});
			},function()
			{
				$(this).find('a.primary').removeClass('selected');
				$(this).find('ul:first').css({visibility: "hidden"});
			});
	    });
	}; 
	
	/* Clocks */
	$.fn.clocks = function(options)
	{
		var defaults = 
		{
			fadeSpeed: 500,
			height: 100 + '%',
			width: 100 + '%',
			version: 9
		};

		var options  = $.extend({}, defaults, options);
		
		return this.each(function()
		{	
			var so_clock = new SWFObject(options.swf, "so_clock", options.width.toString(), options.height.toString(), options.version.toString(), "#ffffff");
			
			var t = new Date();
			
			var dtstr = t.getHours() + ':' + t.getMinutes() + ':' + t.getSeconds();
			

			so_clock.addVariable("gmt_time", dtstr.toString());
			so_clock.addVariable("cities", "София:+0:Лондон:-2:Ню Йорк:-7:Токио:+6");
			so_clock.addParam("wmode", "transparent");
			so_clock.write($(this).attr('id').toString());
		});
		
		/* Change */
		$.fn.changer = function(src)
		{
			var container = this;
			
			var defaults = 
			{
				fadeSpeed: 500
			};
	
			var options  = $.extend({}, defaults, options);
			
			var changeTopic = function()
			{
				var no = $(this).text();
				
				$(this).parents().filter('.pagination').find('a').removeClass('active');
				
				$(this).addClass('active');
				
				$(container).find('li').hide();
				
				$(container).find('li').eq(parseInt(no)-1).fadeIn(options.fadeSpeed);
				
			};
			
			var loadDefaultTopic = function()
			{
				$(container).find('li').eq(0).fadeIn(options.fadeSpeed);
			};
			
			
			return this.each(function()
			{	
				$('.pagination a',this).each(function(index)
				{
					if (0 === index)
					{
						$(this).addClass('active');
						
						loadDefaultTopic();
					}
					$(this).bind('click',changeTopic);
				});
			});
		};
	
	};
	
	$.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: F.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);
