var expressSwfFile = 'http://www.ismedia.jp/common/files/expressInstall.swf';
var baseSwfFile = 'http://www.ismedia.jp/common/images/v1/flash/font/beautifulJapanese.swf';
function replaceFontsClass(_class,_tag,targetSwf,flaWidth,fontSize,fontLineHeight,charLimit,fontColorNormal,fontColorLink,fontColorHover){
 var targets = document.getElementsByClassName(_class);
 for(var i=0;i<targets.length;i++){
  targets[i].id = _class + 'replaceFontsClass' + i;
  var _id = targets[i].id;
  replaceFonts(_id,_tag,targetSwf,flaWidth,fontSize,fontLineHeight,charLimit,fontColorNormal,fontColorLink,fontColorHover);
 }
}
function replaceFonts(_id,_tag,targetSwf,flaWidth,fontSize,fontLineHeight,charLimit,fontColorNormal,fontColorLink,fontColorHover){
 var obj = $(_id);
 var swfdivid = '';
 var anchor;
 var tValue = '';
 var tHref = 'none';
 var tTarget = 'none';
 if ( _tag==null || _tag=='' ){
  swfdivid = _id + '-replacefont';
  tValue = obj.innerHTML;
  if (tValue.match(/<a/gi)) {
   anchor = obj.getElementsByTagName('a')[0]; 
   tValue = anchor.innerHTML;
   tValue = tValue.replace(/\n/ig,''); 
   tValue = tValue.replace(/\r/ig,''); 
   tValue = tValue.replace(/<br*>/ig,'\n'); 
   tValue = tValue.replace(/<\/?[^>]+>/gi,''); 
   tHref = anchor.getAttribute('href');
   tTarget = anchor.getAttribute('target');
   if (tTarget == null ) {tTarget = 'none';}
  } else {
   tValue = tValue.replace(/\n/ig,''); 
   tValue = tValue.replace(/\r/ig,''); 
   tValue = tValue.replace(/<br*>/ig,'\n'); 
   tValue = tValue.replace(/<\/?[^>]+>/gi,''); 
  }
  writeSWF(obj,targetSwf,swfdivid,flaWidth,fontSize,fontLineHeight,charLimit,fontColorNormal,fontColorLink,fontColorHover,tValue,tHref,tTarget);
 } else {
  var nodes = $(_id).getElementsByTagName(_tag);
  for (var i=0; i<nodes.length; i++) {
   swfdivid = _id + '-replacefont-' + _tag + '-' + i;
   obj = nodes[i];
   tValue = obj.innerHTML;
   if (tValue.match(/<a/gi)) {
    anchor = obj.getElementsByTagName('a')[0]; 
    tValue = anchor.innerHTML;
    tValue = tValue.replace(/\n/ig,''); 
    tValue = tValue.replace(/\r/ig,''); 
    tValue = tValue.replace(/<br*>/ig,'\n'); 
    tValue = tValue.replace(/<\/?[^>]+>/gi,''); 
    tHref = anchor.getAttribute("href");
    tTarget = anchor.getAttribute('target');
    if (tTarget == null ) {tTarget = 'none';}
   } else {
    tValue = tValue.replace(/\n/ig,''); 
    tValue = tValue.replace(/\r/ig,''); 
    tValue = tValue.replace(/<br*>/gi,'\n'); 
    tValue = tValue.replace(/<\/?[^>]+>/gi,'');
   }
   writeSWF(obj,targetSwf,swfdivid,flaWidth,fontSize,fontLineHeight,charLimit,fontColorNormal,fontColorLink,fontColorHover,tValue,tHref,tTarget);
  }
 }
}
function writeSWF(_obj,targetSwf,_swfdivid,flaWidth,fontSize,fontLineHeight,charLimit,fontColorNormal,fontColorLink,fontColorHover,tValue,textHref,textTarget ){
 var flaHeight = calFlaHeight(tValue,charLimit,fontSize,fontLineHeight);
 var encodeTextValueUnit = encodeValue(tValue);
 var flashvars = {};
 flashvars.textValue = encodeTextValueUnit;
 flashvars.textHref = textHref;
 flashvars.textTarget = textTarget;
 flashvars.fontSize = fontSize;
 flashvars.fontLineHeight = fontLineHeight;
 flashvars.targetSwf = targetSwf
 flashvars.flaHeight = flaHeight;
 flashvars.flaWidth = flaWidth;
 flashvars.fontColorNormal = fontColorNormal;
 flashvars.fontColorLink = fontColorLink;
 flashvars.fontColorHover = fontColorHover;

 var params = {};
 params.allowScriptAccess = 'always';
 params.quality = 'high';
 params.wmode = 'transparent';
 params.bgcolor = '#ffffff';
 params.scale = 'noscale';
 params.salign = 'lt';

 var attributes = {};
 attributes.width = flaWidth; 
 attributes.height = flaHeight; 
 attributes.id = _swfdivid + '-swf'; 
 if(swfobject.hasFlashPlayerVersion('9.0.0')) {
   _obj.innerHTML = '<div id="' + _swfdivid + '"></div>';
   swfobject.embedSWF(baseSwfFile,_swfdivid,flaWidth,flaHeight,'9.0.0',expressSwfFile,flashvars,params,attributes);
 }
 return;
}
function calFlaHeight( textValue, charLimit, fontSize, fontLineHeight ){
 var fontSize = Number(fontSize);
 var fontLineHeight = Number(fontLineHeight);
 var rowHeight = fontSize + fontLineHeight;
 var len = 0;
 var textValue2 = escape(textValue);
 var rows = 1;
 var crlfCount = 0;
 var crlfText = "";
 var rHeight = 0;
 for (var i=0; i<textValue2.length; i++,len++) {
  if (textValue2.charAt(i) == '%') {
   crlfText = textValue2.charAt(i+1) + textValue2.charAt(i+2);
   if ( (crlfText == "0D") || (crlfText == "0A") ) {
    crlfCount++;
    if (len >= charLimit){
     rows = rows + Math.floor(len/charLimit);
    }
    len = 0;
   }
   if (textValue2.charAt(++i) == "u") {
    i += 3;
    len++;
   }
   i++;
  }
 }
 if (len >= charLimit){
  rows = rows + Math.floor(len/charLimit);
 }
 rHeight = rowHeight * (rows + crlfCount);
 return rHeight;
}

function encodeValue(tValue){
 var chCode = '';
 var encodeTextValue = '';
 num = '';
 var _encodeTextValueUnit = '';
 for (var ii = 0; ii < tValue.length; ii++) {
  chCode = tValue.charCodeAt(ii);
  if (chCode <= 0x7f) {
   encodeTextValue += tValue.charAt(ii);
  } else if (chCode >= 0x80 && chCode <= 0x7ff) {
   encodeTextValue += String.fromCharCode(((chCode >> 6) & 0x1f) | 0xc0);
   encodeTextValue += String.fromCharCode((chCode & 0x3f) | 0x80);
  } else {
   encodeTextValue += String.fromCharCode((chCode >> 12) | 0xe0);
   encodeTextValue += String.fromCharCode(((chCode >> 6) & 0x3f) | 0x80);
   encodeTextValue += String.fromCharCode((chCode & 0x3f) | 0x80);
  }
 }
 for (var jj = 0; jj < encodeTextValue.length; jj++) {
  chCode = encodeTextValue.charCodeAt(jj);
  if ((chCode >= 0x30 && chCode <= 0x39) || (chCode >= 0x40 && chCode <= 0x5A) ||
   (chCode >= 0x61 && chCode <= 0x7A) || (chCode == 0x2A) ||
   (chCode == 0x2D) || (chCode == 0x2E) || (chCode == 0x5F)) {
   _encodeTextValueUnit += encodeTextValue.charAt(jj);
  } else {
   num = chCode.toString(16).toUpperCase();
   _encodeTextValueUnit += "%" + (num.length == 1 ? "0" + num : num);
  }
 }
 return _encodeTextValueUnit;
}
