
var FlashText = new Class({

  initialize: function() {
    this.flashfont = '/resources/flash/text/text_myriadprolight.swf';
    this.selectors = [];
    this.autoload = false;
    window.addEvent('load', this.loaded.bind(this));
  },

  replaceElement: function(el) {
    if (!window.hasFlash) return;
    el = $(el);
    if (!el) return;
    this.createObject(el);
  },

  replaceElements: function(selectors, customfont) {
    this.customfont = customfont;
    this.selectors = selectors;
    if (!this.autoload) {
      this.doReplace();
    }
  },
  
  loaded: function() {
    if (!this.autoload) return;
    this.doReplace();
  },

  doReplace: function() {
    if (window.hasFlash) {
      $$(this.selectors).each(this.replaceElement.bind(this));
    }
  },

  encode_entities: function(s) {
    var result = '';
    for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      result += {'<':'&lt;', '>':'&gt;', '&':'&amp;', '"':'&quot;', '+':'%2B'/*, ' ':'%20'*/}[c] || c;
    }
    return result;
  },
  
  createObject: function(el) {
    el = $(el);
    if (!el) return;
    if (el.hasClass('replaced') || el.hasClass('skip-flash')) return;

    var flashfont = this.flashfont;
    if (this.customfont) {
      flashfont = this.customfont;
    }

    var color = el.getStyle('color').replace('#', '');
    var a = el.getElement('a');
    var color_link = a ? a.getStyle('color').replace('#', '') : '';
    var html = el.innerHTML.trim();
    html = html.replace(/<script.*?>[\s\S]*?<\/.*?script>/ig, '').replace(/\r|\n/ig, '');
    var text = this.encode_entities(html.strip_tags('<a><b><br>').replace(/&nbsp;/ig, ' ').replace(/\xA0/ig, ' '));
    
    var delta = 0;
    var width = el.offsetWidth;
    var height = el.offsetHeight + delta;
    
    var align = el.getStyle('text-align');
    if (el.hasClass('right-aligned')) {
      align = 'right';
    }
    if ((align != 'left') && (align != 'right')) {
      align = 'left';
    }
    
    var bottom_delta = 0;

    if ((width == 0) || (height == 0)) return;

    var flash = '<object';
    var css = 'width: ' + width + 'px; height: ' + height + 'px';
    var props = {};
    if (window.ie) {
      props = {
        'classid': 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
        'codeBase': 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
        'style': css
      };
    } else {
      props = {
        'type': 'application/x-shockwave-flash',
        'data': flashfont,
        'style': css
      };
    }
    $H(props).each(function(value, key) {
      flash += ' ' + key + '="' + value + '"';
    });
    flash += '>';

    var params = {
      'allowScriptAccess': 'sameDomain',
      'allowFullScreen': 'false',
      'movie': flashfont,
      'quality': 'high',
      'bgcolor': '#ffffff',
      'wmode': 'transparent',
      'FlashVars': 'text=' + text + /*'&size=' + size +*/ '&amp;color=' + color + '&amp;color_link=' + color_link + '&text_align=' + align
    };
    
    var params_html = '';
    $H(params).each(function(value, key) {
      params_html += '<param name="' + key + '" value="' + value + '" />';
    });
    flash += params_html;
    flash += '</object>';
    
    flashhtml = flash;
    
    flash = new Element('div').addClass('flashtext flashtext-' + el.get('tag')).setStyles({
      display: 'none',
      margin: el.getStyle('margin'),
      padding: el.getStyle('padding'),
      width: width,
      height: height
    }).setStyle('margin-bottom', el.getStyle('margin-bottom').toInt() - bottom_delta);
    flash.injectBefore(el);
    flash.innerHTML = flashhtml; // Don't use setHTML, its don't remove 'Click-to-activate'

    flash.setStyle('display', '');
    el.setStyle('display', 'none').addClass('replaced');
    return flash;
  }

});

var hasFlash = function(){
  var nRequiredVersion = 9;
        
  if (Browser.Engine.trident && navigator.appVersion.indexOf("Windows") > -1) {
    document.write('<script language="VBScript"\> \non error resume next \nhasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & ' + nRequiredVersion + '))) \n</script\> \n');
    /*     If executed, the VBScript above checks for Flash and sets the hasFlash variable. 
            If VBScript is not supported it's value will still be undefined, so we'll run it though another test
            This will make sure even Opera identified as IE will be tested */
    if (window.hasFlash != null) {
      return window.hasFlash;
    };
  };
        
  if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) {
     var flashDescription = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description;
     var ver = flashDescription.match(/\d{1,2}\.\d/);
     return ver ? (ver[0].toInt() >= nRequiredVersion) : false;
  };
        
  return false;
}();

var flashtext = new FlashText();

