var fontClassFilters = [];
var Font = function(){this.init.apply(this, arguments)};
Font.prototype = {
	fontPath: '/static/scripts/', // DEV NOTES: Path to the directory where Flash gradient font file "sifrGradientText.swf" placed
	init: function(swf, options){
		this.swf = this.fontPath + swf;
		this.setOptions(options || {});
		String.prototype.rgbToHex = this.rgbToHex;
		String.prototype.toHex = this.toHex;
		Array.prototype.indexOf = this.indexOf;
	},
	setOptions: function(o){
		this.options = {
			sizeAdjust: o.sizeAdjust || 0,
			color: o.color || o.sColor,
			bgcolor: o.bgcolor,
			width: o.width,
			height: o.height,
			sWmode: o.sWmode || 'transparent',
			tags: o.tags || '',
			classFilter: o.classFilter || ''
		}
		if(o.classFilter) fontClassFilters[fontClassFilters.length] = o.classFilter;
	},
	replace: function(tags){
		var tags = (tags || this.options.tags).split('.');
		if (tags.length != 0) tag = tags[0] + "." + tags[1];
		else tag = tags[0];
		this.replaceTag(tag);
	},
	replaceTag: function(tag){
		this.replaceElements($(tag));
	},
	replaceElements: function(els){
		for(var i=0; el=els[i]; i++)
			this.replaceElement(el);
	},
	replaceElement: function(el){
		var o = this.options;
		if(!this.hasFlash || el.oldHTML || (!o.classFilter && this.hasClassName(el, fontClassFilters)) || (o.classFilter && el.className.indexOf(o.classFilter)==-1) ) return;
		var c = this.options.color || el.style.color || document.defaultView ? document.defaultView.getComputedStyle(el, null).color : el.currentStyle ? el.currentStyle.color : '#000001';
		if(c.indexOf('rgb') > -1) c = '#'+c.rgbToHex();
		else if(c.length == 4) c = '#'+c.charAt(1)+c.charAt(1)+c.charAt(2)+c.charAt(2)+c.charAt(3)+c.charAt(3);
		var width = o.width || (el.offsetWidth + o.sizeAdjust) * .9;//fix for ie floats
		var height = o.height || el.offsetHeight;
		var sVars = 'txt=' + escape(el.innerHTML) + '&amp;textcolor=' + c + '&amp;w=' + width + '&amp;h=' + (height+o.sizeAdjust) + '';
		el.oldHTML = el.innerHTML;
		el.innerHTML = '<embed type="application/x-shockwave-flash" src="' + this.swf + '" quality="best" wmode="' + o.sWmode + '" bgcolor="' + o.bgcolor + '" flashvars="' + sVars + '" width="' + width + '" height="' + height + '" sifr="true"></embed>';
	},
	hasClassName: function(el, classNames) {
		var classNames = el.className.split(' ');
		for(var i=0; cn=classNames[i]; i++)
			if(classNames.indexOf(cn)) return true;
		return false;
	},
	indexOf: function(needle){
		for(var i=0; val=this[i]; i++)
			if(val == needle) return i;
		return -1;
	},
	toHex: function(){//borrowed from mootools.net
		var N = parseInt(this);
		if (N==0 || isNaN(N)) return "00";
		N = Math.round(Math.min(Math.max(0,N),255));
		return "0123456789abcdef".charAt((N-N%16)/16) + "0123456789abcdef".charAt(N%16);
	},
	rgbToHex: function(){//borrowed from mootools.net
		var rgb = this.match(/[rgba]{3,4}\(([\d]{0,3}),[\s]([\d]{0,3}),[\s]([\d]{0,3})\)/);
		return rgb[1].toHex()+rgb[2].toHex()+rgb[3].toHex();
	},
	hasFlash: function(){//borrowed from http://code.google.com/p/doctype/wiki/ArticleDetectFlash
	  function getFlashVersion(desc) {
	    var matches = desc.match(/[\d]+/g);
	    matches.length = 3;  // To standardize IE vs FF
	    return matches.join('.');
	  }
	
	  var hasFlash = false;
	  var flashVersion = '';
	
	  if (navigator.plugins && navigator.plugins.length) {
	    var plugin = navigator.plugins['Shockwave Flash'];
	    if (plugin) {
	      hasFlash = true;
	      if (plugin.description) {
	        flashVersion = getFlashVersion(plugin.description);
	      }
	    }
	
	  } else if (navigator.mimeTypes && navigator.mimeTypes.length) {
	    var mimeType = navigator.mimeTypes['application/x-shockwave-flash'];
	    hasFlash = mimeType && mimeType.enabledPlugin;
	    if (hasFlash) {
	      flashVersion = getFlashVersion(mimeType.enabledPlugin.description);
	    }
	
	  } else {
	    try {
	      // Try 7 first, since we know we can use GetVariable with it
	      var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
	      hasFlash = true;
	      flashVersion = getFlashVersion(ax.GetVariable('$version'));
	    } catch (e) {
	      // Try 6 next, some versions are known to crash with GetVariable calls
	      try {
	        var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
	        hasFlash = true;
	        flashVersion = '6.0.21';  // First public version of Flash 6
	      } catch (e) {
	        try {
	          // Try the default activeX
	          var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
	          hasFlash = true;
	          flashVersion = getFlashVersion(ax.GetVariable('$version'));
	        } catch (e) {
	          // No flash
	        }
	      }
	    }
	  }
	  return hasFlash;
	}()
}
var gradientText = new Font('sifrGradientText.swf', {sizeAdjust:0, tags:'span.sifruss'});
$(document).ready(function() {
	gradientText.replace(); // Activation of Sifruss gradient font
});
