// dipendenze: at_interval
// il plugin si aspetta una serie di li contenenti un a.link e un a con img
/*
 li
 	a.title
 	a
 		img
*/

(function( $ ){
	var methods = {
			init : function( options ) {

				var defaults = {
					'location'         	: 'top',
					'background-color' 	: 'blue',
					'randomPics'		: 10,
					'delay'				: 400,
					'cells'				: 15
				};

				return this.each(function(){
					
					var $this 		= $(this),
						settings 	= jQuery.extend(defaults, options),
						data 		= $this.data('lcd');
					
					if ( ! data ) {						// SINGLETON?
						var _data = {
								elements: [],
								hiding: []
						};
						
						// hover play/pause
						$this.hover(
							function() { var $this = $(this); if($this.data('lcd').isPlay) $this.lcd('pause'); },
							function() { var $this = $(this); if(!$this.data('lcd').isPlay) $this.lcd('play'); }
						);
						
						var count = 0;
						$this.find('li').each(function(key)
						{
							var element = $(this);
							
							// TODO: questa va rimossa rimuovendo l'elemento dal view helper
							// rimuove i link testuali
							element.find('a.link').remove();
							
							var content = element.find('a'); 
							// rimuove il titolo dai link
							content.removeAttr('title');

							if(count < settings.cells){
								_data.elements.push(element);
								
								element.find('img').hide();
								
								// element hover fadeIn/Out
								element.hover(
									function() { $(this).find('img').fadeIn('fast'); },
									function() { $(this).find('img').fadeOut(); }
								);
							}
							else{
								_data.hiding.push(content);
								element.remove();
							}
							
							count++;
						});
						
						// data storage
						$(this).data('lcd', {
							target 		: $this,
							elements	: $(_data.elements),
							hiding		: _data.hiding,
							timer		: false,
							isPlay 		: false,
							stopped		: false,
							randoms		: false,
							delay		: settings.delay
						});
					}
										
					$this.lcd('play');
				});
			},
			
			pause : function( stop ) {
				
				return this.each(function(){
					var $this = $(this),
						data = $this.data('lcd');
					
					data.elements.each(function(){
						var element = $(this);
						
						if(element.data("lcd_toggle"))
							element.data("lcd_toggle").should_stop  = true;
						element.find('img').fadeOut('fast');
					});
					
					data.isPlay = false;
					if(stop) data.stopped = true;
				});
			},
			
			play : function( resume )
			{
				this.each(function(){
					var $this = $(this),
						data = $this.data('lcd');
					
					if(resume) data.stopped = false;
					
					if(data.stopped) return true;
					
					data.elements.each(function(){
						var element = $(this);
						random = 3000 + Math.floor((10000 - 3000 + 1) * (Math.random() % 1));
						element.at_intervals(
							function(){$this.lcd('toggle', element);},
							{ delay: random, name: "lcd_toggle"}
						);
					});
					
					data.isPlay = true;
				});
				
				return this;
			},
			
			toggle : function( element ) {
				var $this 			= $(this),
					data 			= $this.data('lcd'),
					random_fade 	= 500 + Math.floor((2000 - 500 + 1) * (Math.random() % 1)),
					random_change 	= 3000 + Math.floor((20000 - 3000 + 1) * (Math.random() % 1)),
					img 			= element.find('img');
				
				if(element.data("lcd_toggle").started){
					
					element.data("lcd_toggle").should_pause  = true;
					
					if(img.is(':visible'))
					{
						img.fadeOut(random_fade, function(){
							oldImg = $(this).parent();
							oldImg.parent().html(data.hiding.pop());
							data.hiding.unshift(oldImg);
							element.data("lcd_toggle").should_pause  = false;
							element.data("lcd_toggle").delay  = random_change*2;
						});
					}
					else{
						img.fadeIn(random_fade, function(){
							element.data("lcd_toggle").should_pause  = false;
							element.data("lcd_toggle").delay  = random_change;
						});
					}
				}
				else
					element.data("lcd_toggle").started = true;
			}
			
		};
	
	$.fn.lcd = function( method ) {
		if ( methods[method] )
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		else if ( typeof method === 'object' || ! method )
			return methods.init.apply( this, arguments );
		else
			$.error( 'Method ' +  method + ' does not exist on jQuery.lcd' );
	};
})( jQuery );
