(function ($) {
  var fontsize = $('.wspans').css('font-size');

  $.fn.nuvutype = function (options) {
    if (options != undefined) {
      if (options.colorhigh == undefined) { options.colorhigh = options.colorhigh; }
      if (options.color1to == undefined) { options.color1to = options.color1to; }
      if (options.colors == undefined) { options.colors = "1"; }
      if (options.typespeed == undefined) { options.typespeed = options.typespeed; }
      if (options.splittype == undefined) { options.splittype = options.splittype; }
    }

    // defaults
    options = $.extend({
      colorhigh: '#ffffff',
      color1to: '#ff3366',
      color1base: '#555555',
      color2to: '#b505f6',
      color2base: '#555555',
      color3to: '#02fb42',
      color3base: '#555555',
      color4to: '#02cffb',
      color4base: '#555555',
      colors: '4',
      delay: 10,
      typespeed: 15,
      easing: 'swing',
      fontsizeto: fontsize,
      trigger: 'true',
      splittype: ' '
    }, options);

    return this.each(function () {
      var $this = $(this);
      var o = $.metadata ? $.extend({}, options, $this.metadata()) : options;
      var words = $this.find('.type').text().split(options.splittype);
			
			for ( x in words ){
				if ( words[x] == ' ') words[x] = "</span><span class='wspans_spacer'>&nbsp;";
			}
      var text = words.join("</span><span class='wspans'>");

      $this.find('.type').html("<span class='wspans'>" + text + "</span>");
      $this.find('.wspans').hide();

      if (options.trigger == 'true') {
        $this.find('.nlink').click(function typeRun() {
          var wspans = $(this).next('.type').children('.wspans').css({ 'opacity': '0' }).delay(o.delay);
          var i = 0;

          (function displayType() {
            wspans.eq(i++).show(1).animate({ 'opacity': '1' }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		        .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		        .animate({ 'color': o.color1to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		        .animate({ 'color': o.color1base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		        .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		        .animate({ 'color': o.color1base }, { duration: o.typespeed, queue: 'false', complete: displayType });
          })();

          if (options.colors == '2' || options.colors == '3' || options.colors == '4') {
            (function displayType2() {
              wspans.eq(i++).show(1).next('span').prev('span').delay(350).animate({ 'opacity': '1' }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
	            .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color2to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color2base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		          .animate({ 'color': o.color2base }, { duration: o.typespeed, queue: 'false', complete: displayType2 });
            })();
          };

          if (options.colors == '3' || options.colors == '4') {
            (function displayType3() {
              wspans.eq(i++).show(1).next('span').prev('span').delay(750).animate({ 'opacity': '1' }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color3to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color3base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		          .animate({ 'color': o.color3base }, { duration: o.typespeed, queue: 'false', complete: displayType3 });
            })();
          };

          if (options.colors == '4') {
            (function displayType4() {
              wspans.eq(i++).show(1).next('span').prev('span').delay(750).animate({ 'opacity': '1' }, { duration: o.typespeed, queue: 'false' })
		          .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color4to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color4base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		          .animate({ 'color': o.color4base }, { duration: o.typespeed, queue: 'false', complete: displayType4 });
            })();
          };
        });
      } else if (options.trigger == 'false') {
        $(document).ready(function() {
          var wspans = $this.children('.type').children('.wspans').css({ 'opacity': '0' }).delay(o.delay);
          var i = 0;
          (function displayType() {
            wspans.eq(i++).show(1).animate({ 'opacity': '1' }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
	          .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		        .animate({ 'color': o.color1to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		        .animate({ 'color': o.color1base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		        .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		        .animate({ 'color': o.color1base }, { duration: o.typespeed, queue: 'false', complete: displayType });
          })();

          if (options.colors == '2' || options.colors == '3' || options.colors == '4') {
            (function displayType2() {
              wspans.eq(i++).show(1).next('span').prev('span').delay(350).animate({ 'opacity': '1' }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color2to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color2base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		          .animate({ 'color': o.color2base }, { duration: o.typespeed, queue: 'false', complete: displayType2 });
            })();
          };
          
          if (options.colors == '3' || options.colors == '4') {
            (function displayType3() {
              wspans.eq(i++).show(1).next('span').prev('span').delay(750).animate({ 'opacity': '1' }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color3to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color3base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		          .animate({ 'color': o.color3base }, { duration: o.typespeed, queue: 'false', complete: displayType3 });
            })();
          };

          if (options.colors == '4') {
            (function displayType4() {
              wspans.eq(i++).show(1).next('span').prev('span').delay(750).animate({ 'opacity': '1' }, { duration: o.typespeed, queue: 'false' })
		          .animate({ 'color': o.colorhigh }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color4to }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'color': o.color4base }, { duration: o.typespeed, easing: o.easing, queue: 'false' })
		          .animate({ 'font-size': o.fontsizeto }, { duration: o.typespeed })
		          .animate({ 'color': o.color4base }, { duration: o.typespeed, queue: 'false', complete: displayType4 });
            })();
          };
        });
      };
    });
  };
})(jQuery);

