(function(window, $, page) {
	
	page.classes.text_rotator = function(options) {
		
		var o, internal, sequence;
		
		o = $.extend({
			$e:null,
			selector:'',
			specimens:{}
		}, options);
		
		internal = {
			name:'Module.text_rotator',
			$e:(o.$e ? o.$e : $(o.selector)),
			rotators:[],
			controls:(o.controls ? o.controls : null)
		};
		
		function init_row(){
			var $me;
			$me = $(this);

			if(!$me.filter(':visible').length){
				return;
			}

			$me.css({
				opacity:0.0
			});
			$me.wrap("<div></div>");
			switch_text($me, true);

			$me.animate({
				opacity:1.0
			},500);
			
			$me.parent().css({
				'position':'relative',
				'display':'block',
				'height': $me.height(),
				'margin-left:':$me.css('margin-left')
			}).addClass('font-sample-wrapper');

			$me.css({
				position:'absolute',
				top:$me.position().top,
				left:$me.position().left
			});

			$me.addClass('text-rotator-initialized');
		}

		sequence = {
			series: function(elements, fx){
				if(!fx.delay){ return; }
				if(!fx.fn){ return; }
				function callback(){
					if($.isFunction(fx.finished)){
						fx.finished.apply(this);
					}
					if(fx.repeat){
						sequence.series(elements,fx);
					}
				}
				elements.each(function(i,j){
					setTimeout(function(){
						fx.fn.apply(j,[callback]);
					},fx.delay);
				});
			},
			waterfall: function(elements, fx){
				if(!fx.delay && fx.delay !== 0){ return; }
				if(!fx.fn){ return; }

				var index, current_sample, last_element;
				index = 0;
				stopped = false;
				current_sample = null;

				function callback(){
					if($.isFunction(fx.finished)){
						fx.finished.apply(this);
					}
					if(fx.repeat){
						sequence.series(elements,fx);
					}
				}

				function run(){
					if(last_element){ 
						//index = 0;
						//last_element = false;
						if($.isFunction(fx.finished)){
							fx.finished.apply(current_sample,[elements]);
						};
						return;
					}
					current_sample = elements.eq(index);
					setTimeout(function(){
						fx.fn.apply(current_sample,[callback]);
					},fx.delay);
					index++;
					if(index == elements.length){ last_element = true; }
				}
				
				run();
			},
			random: function(elements, fx){
				if(!fx.delay_low && fx.delay_low !== 0){ return; }
				if(!fx.delay_high && fx.delay_high !== 0){ return; }
				if(!fx.fn){ return; }
				elements.each(function(i,j){
					setTimeout(function(){
						fx.fn.apply($(j),[]);
					},NI.math.random(fx.delay_low,fx.delay_high));
				})
			}
		};




		function get_random_specimen(font){
			var index;
			if(!o.specimens[font]){ return false; }
			index = NI.math.round(NI.math.random(0, o.specimens[font].length-1), 0);
			return o.specimens[font][index];
		}

		function switch_text(element, prevent_replace){
			var new_element, my_font, height_swapped;

			my_font = element.attr('data-font');

			if(!my_font || !my_font.length){ return; }

			specimen = get_random_specimen(my_font);
			if(!specimen){ return; }

			if(!prevent_replace){
				new_element = element.clone();
			} else {
				new_element = element;
			}
			
			element.parent().append(new_element);
			new_element.text(specimen).css({
				opacity:0.0,
				fontSize:my_font.split(',')[1]+'px'
			});

			if(!prevent_replace){
				element.animate({
					opacity:0.0
				},750,function(){
					element.remove();
					new_element.parent().css({
						height:new_element.height()
					});
				});
			}
			

			new_element.animate({
				opacity:1.0
			},750, function(){
				new_element.parent().css({
					height:new_element.height()
				});
			});

			return new_element;
		}


		this.visible = function() {

			sequence.series(internal.$e,{
				delay:200,
				fn: function(callback){
					if(!$(this).hasClass('text-rotator-initialized')){
						init_row.call(this);
					} else {
						switch_text($(this));
					}
					if($.isFunction(callback)){
						callback();
					}
				}
			});
		};

		console.log(internal);
		
	};
	
}(this, this.jQuery, this.page));
