﻿/**
 * @author jnf (http://github.com/jnf)
 * @description Extends Scripty to include text blurring effects.
 * @dependency Prototype.js 1.6.1 (http://www.prototypejs.org)
 * @dependency Scriptaculous 1.8.3 (http://github.com/madrobby/scriptaculous)
 * @version 0.1
 *
 */
 
Effect.Blur = function(element, delta, options) {
  var browser = $H(Prototype.Browser).find(function (f) { return true == f[1] })[0]; //determine browser family
  var element = $(element); //extend the element
  options.blurColor = options.blurColor || element.originalColor || element.getStyle('color'); //if a color isn't passed, use the text's original color
  element.originalColor = element.originalColor || options.blurColor || element.getStyle('color'); //prefer cached color
  if ("IE" == browser) { // :(
    var existingRule = element.getStyle('filter');
    var match = existingRule.match(/pixelradius=[\d]+/);
    var existingBlur = match ? match.first().gsub(/\D/,'') * 1 : 0;
    //return tween
    return new Effect.Tween(element, existingBlur, existingBlur + delta, options,
      function(position) {
        var ruleContent = position == 0
          ? "none"
          : "progid:DXImageTransform.Microsoft.blur(pixelradius=" + position + ", enabled='true');";
        this.setStyle("filter: " + ruleContent);
      }
    );
  } else {
    var existingRule = element.getStyle('text-shadow');
    var match = existingRule.match(/[1-9]+[0-9]*px/);
    var existingBlur = match ? match.first().gsub(/\D/,'') * 1 : 0;
    //return tween
    return new Effect.Tween(element, existingBlur, existingBlur + delta, options,
      function(position) {
        this.setStyle({
          color: 'transparent',
          textShadow: '0 0 ' + position + 'px ' + options.blurColor || '#000'
        });
      }
    );
  }
}
