/*
	DBD.text-scroller (October 20, 2011) widget for jQuery
	http://distinctbydesign.com
	Copyright (c) 2011 Shannon Davenport
	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), the rights to use, copy, modify, merge, publish,
	distribute the Software, and to permit persons to whom the Software is 
	furnished to do so, subject to the following conditions:
	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function($) {
	$.widget("ui.dbdtextscroller", {
		options: {
			height:200,
			width:300,
			text:[],
		},
		
		properties: { current_text:0},
		
		/*************
       * Initialize *
       **************/
      _create : function(){
         var self = this;
         self._buildScroller();
      },
      
      _buildScroller : function(){
	      var self = this;
	      var s = $('<div/>',{'class':'dbd-ts'});
	      var txt = $('<span/>',{'id':'dbd-ts-txt'}).html(self.options.text[self.properties.current_text]);
	      self.element.append(txt);
		  self._onScroll();
      },
      
      _onScroll:function(){
	        var self = this;
	        var curr_txt = $('#dbd-ts-txt').html();  
	        setTimeout(function(){self._textFade();},self._wordCount(curr_txt)*150);
	  },
	  
	  _textFade:function(){ 
		  var self = this;
		  $('#dbd-ts-txt').fadeOut(1500, function() {
		        	$(this).empty();
			        self.properties.current_text = ((self.properties.current_text+1)>(self.options.text.length-1)?0:self.properties.current_text+1);
			        $(this).html(self.options.text[self.properties.current_text]);
			      	$(this).fadeIn(1500, function(){self._onScroll();});
			    });
	  },
	  
	  _wordCount:function(txt){
		  var words = txt.split(' ');
		  return words.length;
	  }
      
  });
})(jQuery);
		
