/*
 * Copyright (c) Kazuhiro Iizuka
 * kazuhiro at mail.aimy.jp ( http://aimy.co.jp/ )
 * License: BSD Licence
 */
var EDITOR_PREFIX = "rte";
var EDITOR_IFRAME_PREFIX = "rte_iframe";
var EDITOR_TEXTAREA_PREFIX = "rte_textarea";
var EDITOR_STATUS_PREFIX = "rte_status";
var EDITOR_TOGGLE_FONT_FACE_PREFIX = "rte_toggle_font_face";
var EDITOR_TOGGLE_FONT_SIZE_PREFIX = "rte_toggle_font_size";
var EDITOR_TOGGLE_FONT_COLOR_PREFIX = "rte_toggle_font_color";
var EDITOR_TOGGLE_ICON_PREFIX = "rte_toggle_icon";
var EDITOR_TOGGLE_ICON_MULTI_PREFIX = "rte_toggle_icon_multi";
var EDITOR_TOGGLE_CHAR_PREFIX = "rte_toggle_char";

var EDITOR_BUTTON_PREFIX = "rte_button";
var EDITOR_BUTTON_IMAGE_PREFIX = "rte_button_image";


var RTEditorConfig = {
  notice: {
  "text_mode": "タグモード",
  "html_mode": "プレビューモード(Google Mapなどのタグを貼るには、タグモードで行ってください)"
  },
  menu: [
    [
    {alias:"font_face",name:"フォント",action:"RTEditor.prototype.FontFaceDropDown"},
    {alias:"font_size",name:"文字の大きさ",action:"RTEditor.prototype.FontSizeDropDown"},
    {alias:"bold",name:"太字",action:"RTEditor.prototype.Bold"},
    {alias:"italic",name:"斜体",action:"RTEditor.prototype.Italic"},
    {alias:"underline",name:"下線",action:"RTEditor.prototype.Underline"},
    {alias:"strike_through",name:"打ち消し線",action:"RTEditor.prototype.StrikeThrough"},
    {alias:"fore_color",name:"文字色",action:"RTEditor.prototype.FontColorDropDown"},
    {alias:"|"},

    {alias:"subscript",name:"下付き文字",action:"RTEditor.prototype.Subscript"},
    {alias:"superscript",name:"上付き文字",action:"RTEditor.prototype.Superscript"},


    {alias:"|"},

    {alias:"html_source",name:"ソース",action:"RTEditor.prototype.HtmlSource"}
    ],
    [
    {alias:"pictogram",name:"顔文字",action:"RTEditor.prototype.IconDropDown"},
    {alias:"pictogram",name:"顔文字",action:"RTEditor.prototype.IconMultiDropDown"},
    {alias:"text_letter_omega",name:"記号",action:"RTEditor.prototype.CharDropDown"},
    {alias:"|"},
    {alias:"hyperlink",name:"リンク",action:"RTEditor.prototype.Hyperlink"},
    {alias:"|"},
    {alias:"align_left",name:"左寄せ",action:"RTEditor.prototype.AlignLeft"},
    {alias:"align_center",name:"中央",action:"RTEditor.prototype.AlignCenter"},
    {alias:"align_right",name:"右寄せ",action:"RTEditor.prototype.AlignRight"},
    {alias:"|"},
    {alias:"numbered_list",name:"段落番号",action:"RTEditor.prototype.NumberedList"},
    {alias:"unordered_list",name:"箇条書き",action:"RTEditor.prototype.UnorderedList"},
    {alias:"outdent",name:"引用解除",action:"RTEditor.prototype.Outdent"},
    {alias:"indent",name:"引用",action:"RTEditor.prototype.Indent"},

    {alias:"horizontal_rule",name:"横罫線",action:"RTEditor.prototype.HorizontalRule"}
/*
    ,
    {alias:"|"},
    {alias:"insert_table",name:"テーブル挿入",action:"RTEditor.prototype.InsertTable"}
*/
    ]

  ],
//  color_palette:
  font_face: [
    {"font":"ＭＳ ゴシック","font_name":"ＭＳ ゴシックを選択"},
    {"font":"ＭＳ 明朝","font_name":"ＭＳ 明朝を選択"},
    {"font":"arial","font_name":"Arialを選択"},
    {"font":"comic sans ms","font_name":"Comic Sans MSを選択"},
    {"font":"courier","font_name":"Courierを選択"},
    {"font":"system","font_name":"Systemを選択"},
    {"font":"Tahoma","font_name":"Tahomaを選択"},
    {"font":"times","font_name":"Timesを選択"},
    {"font":"verdana","font_name":"Verdanaを選択"}
  ],
  font_size: [
    {"size":1,"font_name":"サイズ1"},
    {"size":2,"font_name":"サイズ2"},
    {"size":3,"font_name":"サイズ3"},
    {"size":4,"font_name":"サイズ4"},
    {"size":5,"font_name":"サイズ5"},
    {"size":6,"font_name":"サイズ6"},
    {"size":7,"font_name":"サイズ7"}
  ],
  color_palette: [
    ['FFFFFF','F4F4F4','E9E9E9','DFDFDF','D4D4D4','C9C9C9','BFBFBF','B4B4B4','AAAAAA','9F9F9F','8A8A8A','747474','6A6A6A','5F5F5F','555555','000000'],
    ['ffeaea','ffd5d5','ffbfbf','ffaaaa','ff9595','ff8080','ff5555','ff2b2b','ff0000','ea0000','d50000','bf0000','aa0000','950000','800000','6a0000'],
    ['FFF4EA','FFEAD5','FFDFBF','FFD5AA','FFCA95','FFBF80','FFAA55','FF952B','FF8000','EA7500','D56A00','BF6000','AA5500','954A00','804000','6A3500'],
    ['FFFAEA','FFF4D5','FFEFBF','FFEAAA','FFE495','FFDF80','FFD555','FFCA2B','FFBF00','EAAF00','D59F00','BF8F00','AA8000','957000','806000','6A5000'],
    ['FFFFEA','FFFFD5','FFFFBF','FFFFAA','FFFF95','FFFF80','FFFF55','FFFF2B','FFFF00','EAEA00','D5D500','BFBF00','AAAA00','959500','959500','6A6A00'],
    ['FAFFEA','F4FFD5','EFFFBF','EAFFAA','E4FF95','DFFF80','D5FF55','CAFF2B','BFFF00','AFEA00','9FD500','8FBF00','80AA00','709500','608000','506A00'],
    ['F4FFEA','EAFFD5','DFFFBF','D5FFAA','CAFF95','BFFF80','AAFF55','95FF2B','80FF00','75EA00','6AD500','60BF00','55AA00','4A9500','408000','356A00'],
    ['EAFFEF','D5FFDF','BFFFCF','AAFFBF','95FFAF','80FF9F','55FF80','2BFF60','00FF40','00EA3A','00D535','00BF30','00AA2B','009525','008020','006A1B'],
    ['EAFFFA','D5FFF4','BFFFEF','AAFFEA','95FFE4','80FFDF','55FFD5','2BFFCA','00FFBF','00EAAF','00D59F','00BF8F','00AA80','009570','008060','006A50'],
    ['EAFFFF','D5FFFF','BFFFFF','AAFFFF','95FFFF','80FFFF','55FFFF','2BFFFF','00FFFF','00EAEA','00D5D5','00BFBF','00AAAA','009595','008080','006A6A'],
    ['EAFAFF','D5F5FF','BFF1FF','AAECFF','95E8FF','80E2FF','55D9FF','2BCFFF','00C6FF','00B6EA','00A5D5','0095BF','0084AA','007495','006380','00536A'],
    ['EAEFFF','D5DFFF','BFCFFF','AABFFF','95AFFF','809FFF','5580FF','2B60FF','0040FF','003AEA','0035D5','0030BF','002BAA','002595','002080','001B6A'],
    ['EFEAFF','E0D5FF','D1BFFF','C1AAFF','B395FF','A380FF','8455FF','652BFF','4600FF','4100EA','3A00D5','3500BF','2F00AA','290095','230080','1E006A'],
    ['F4EAFF','EAD5FF','DFBFFF','D5AAFF','CA95FF','BF80FF','AA55FF','952BFF','8000FF','7500EA','6A00D5','6000BF','5500AA','4A0095','400080','35006A'],
    ['FAEAFF','F5D5FF','F1BFFF','ECAAFF','E895FF','E280FF','D955FF','CF2BFF','C600FF','B600EA','AA00D5','9500BF','8400AA','740095','630080','53006A'],
    ['FFEAFA','FFD5F4','FFBFEF','FFAAEA','FF95E4','FF80DF','FF55D5','FF2BCA','FF00BF','EA00AF','D5009F','BF008F','AA0080','950070','800060','6A0050'],
    ['FFEAEF','FFD5DF','FFBFCF','FFAABF','FF95AF','FF809F','FF5580','FF2B60','FF0039','EA003A','D50035','BF0030','AA002B','950025','800020','6A001B']
  ],

/*
  // 単一ページにアイコンをすべて表示する場合
  icons: [
    ['emoticon_happy','emoticon_smile','emoticon_surprised','emoticon_wink','emoticon_tongue','emoticon_waii','emoticon_evilgrin','emoticon_grin','emoticon_unhappy','heart','star','anchor','female','male','eye','music','phone','basket','bell','camera'],
    ['arrow_down','arrow_up','arrow_left','arrow_right','arrow_divide','arrow_redo','arrow_rotate_anticlockwise','arrow_rotate_clockwise','arrow_switch','arrow_turn_left','arrow_turn_right','arrow_undo','sport_8ball','sport_basketball','sport_football','sport_golf','sport_raquet','sport_shuttlecock','sport_soccer','sport_tennis'],
    ['email','email_open','telephone','television','new','pencil','attach','hourglass','date','ipod','map','newspaper','printer','money','money_dollar','money_euro','money_pound','money_yen','house','rainbow'],
    ['computer','controller','database','cut','cut_red','cross','error','exclamation','drive','dvd','drive_cd','drive_network','monitor','mouse','photo','film','find','creditcards','cursor','briefcase'],
    ['comment','css','disk_multiple','document','feed','rss','help','html','image','information','keyboard','key','lock','lock_open','magnifier','shield','stop','report','note','package'],
    ['brick','bug','chart_bar','chart_curve','chart_pie','cog','color_swatch','color_wheel','compress','connect','disconnect','contrast','folder','layers','layout','lightbulb','lightbulb_off','lightning','link','lorry'],
    ['bricks','plugin','pill','ruby','page_copy','page_white','page_white_acrobat','page_word','paintbrush','paintcan','palette','picture','pilcrow','script','server','shading','group','status_away','status_busy','status_offline'],
    ['award_star_bronze_3','award_star_gold_1','award_star_gold_2','award_star_gold_3','award_star_silver_1','award_star_silver_2','award_star_silver_3','flag_blue','flag_green','flag_orange','flag_pink','flag_purple','flag_red','flag_yellow','medal_bronze_1','medal_bronze_2','medal_bronze_3','medal_gold_1','medal_gold_2','medal_gold_3'],
    ['medal_silver_1','medal_silver_2','medal_silver_3','sound_mute','sound_none','control_eject','control_eject_blue','control_end','control_end_blue','control_equalizer','control_equalizer_blue','control_fastforward','control_pause','control_pause_blue','control_play','control_play_blue','control_repeat','control_repeat_blue','control_rewind','control_rewind_blue'],
    ['control_start','control_start_blue','control_stop','control_stop_blue','rosette']
  ],
*/

  // 複数ページに渡りアイコンを表示させる場合
  icons: [

    [
  ['emoticon_happy','emoticon_smile','emoticon_surprised','emoticon_wink','emoticon_tongue','emoticon_waii','emoticon_evilgrin','emoticon_grin','emoticon_unhappy','heart','star','anchor','female','male','eye','music','phone','basket','bell','camera'],
  ['arrow_down','arrow_up','arrow_left','arrow_right','arrow_divide','arrow_redo','arrow_rotate_anticlockwise','arrow_rotate_clockwise','arrow_switch','arrow_turn_left','arrow_turn_right','arrow_undo','sport_8ball','sport_basketball','sport_football','sport_golf','sport_raquet','sport_shuttlecock','sport_soccer','sport_tennis'],
  ['email','email_open','telephone','television','new','pencil','attach','hourglass','date','ipod','map','newspaper','printer','money','money_dollar','money_euro','money_pound','money_yen','house','rainbow'],
  ['computer','controller','database','cut','cut_red','cross','error','exclamation','drive','dvd','drive_cd','drive_network','monitor','mouse','photo','film','find','creditcards','cursor','briefcase'],
  ['comment','css','disk_multiple','document','feed','rss','help','html','image','information','keyboard','key','lock','lock_open','magnifier','shield','stop','report','note','package'],
  ['brick','bug','chart_bar','chart_curve','chart_pie','cog','color_swatch','color_wheel','compress','connect','disconnect','contrast','folder','layers','layout','lightbulb','lightbulb_off','lightning','link','lorry'],
  ['bricks','plugin','pill','ruby','page_copy','page_white','page_white_acrobat','page_word','paintbrush','paintcan','palette','picture','pilcrow','script','server','shading','group','status_away','status_busy','status_offline'],
  ['award_star_bronze_3','award_star_gold_1','award_star_gold_2','award_star_gold_3','award_star_silver_1','award_star_silver_2','award_star_silver_3','flag_blue','flag_green','flag_orange','flag_pink','flag_purple','flag_red','flag_yellow','medal_bronze_1','medal_bronze_2','medal_bronze_3','medal_gold_1','medal_gold_2','medal_gold_3'],
  ['medal_silver_1','medal_silver_2','medal_silver_3','sound_mute','sound_none','control_eject','control_eject_blue','control_end','control_end_blue','control_equalizer','control_equalizer_blue','control_fastforward','control_pause','control_pause_blue','control_play','control_play_blue','control_repeat','control_repeat_blue','control_rewind','control_rewind_blue'],
  ['control_start','control_start_blue','control_stop','control_stop_blue','rosette']


    ],
    [
  ['1k/blog/smile','1k/blog/laugh','1k/blog/wink_smile','1k/blog/bigsmile','1k/blog/kiss','1k/blog/wink_kiss','1k/blog/hanikami','1k/blog/cry','1k/blog/goukyuu','1k/blog/auch','1k/blog/angry','1k/blog/kyotokyoto','1k/blog/yokome','1k/blog/yodare','1k/blog/heart_eyes','1k/blog/massao','1k/blog/quake','1k/blog/surprise','1k/blog/vsign','1k/blog/kira_gold','1k/blog/kira_silver'],
  ['1k/hare','1k/ame','1k/kumori','1k/kaminari','1k/yuki','1k/love','1k/onegai','1k/onegai_right','1k/kodoku','1k/gaaan'],
  ['1k/ganba','1k/orooro','1k/oyasumi','1k/punpun','1k/dog_boy','1k/dog_girl','1k/cat'],
  ['1k/apple','1k/mikan','1k/strawberry_mini','1k/strawberry','1k/suika','1k/carrot','1k/kuri','1k/nasu','1k/piman','1k/udon','1k/ramen','1k/katudon'],
  ['1k/curry','1k/mayo','1k/ketchup','1k/cake','1k/ice','1k/pudding','1k/kashiwamochi','1k/beer','1k/juice_blue','1k/juice_pink','1k/juice_green','1k/juice_orange'],
 ['1k/milk','1k/milk_bottle','1k/coffee_milk','1k/coffee','1k/tea','1k/tea_time']

    ],
    [

  ['0yen/orz_blue','0yen/orz_darkblue','0yen/orz_brown','0yen/orz_moji']

    ],
    [

  ['pomo/usa_futu','pomo/usa_niko','pomo/usa_egao','pomo/usa_laugh','pomo/usa_buzen','pomo/usa_angry','pomo/usa_pokan','pomo/usa_po','pomo/usa_cry','pomo/usa_tohoho','pomo/usa_warainaki','pomo/usa_heart_eyes']
    ]

  ],

  char_map: [
    ["&euro;","&lsquo;","&rsquo;","&rsquo;","&ldquo;","&rdquo;","&ndash;","&mdash;","&iexcl;","&cent;","&pound;","&curren;","&yen;","&brvbar;","&sect;"],
    ["&uml;","&copy;","&ordf;","&laquo;","&not;","&reg;","&macr;","&deg;","&plusmn;","&sup2;","&sup3;","&acute;","&micro;","&para;","&middot;"],
    ["&cedil;","&sup1;","&ordm;","&raquo;","&frac14;","&frac12;","&frac34;","&iquest;","&Agrave;","&Aacute;","&Acirc;","&Atilde;","&Auml;","&Aring;","&AElig;"],
    ["&Ccedil;","&Egrave;","&Eacute;","&Ecirc;","&Euml;","&Igrave;","&Iacute;","&Icirc;","&Iuml;","&ETH;","&Ntilde;","&Ograve;","&Oacute;","&Ocirc;","&Otilde;"],
    ["&Ouml;","&times;","&Oslash;","&Ugrave;","&Uacute;","&Ucirc;","&Uuml;","&Yacute;","&THORN;","&szlig;","&agrave;","&aacute;","&acirc;","&atilde;","&auml;"],
    ["&aring;","&aelig;","&ccedil;","&egrave;","&eacute;","&ecirc;","&euml;","&igrave;","&iacute;","&icirc;","&iuml;","&eth;","&ntilde;","&ograve;","&oacute;"],
    ["&ocirc;","&otilde;","&ouml;","&divide;","&oslash;","&ugrave;","&uacute;","&ucirc;","&uuml;","&uuml;","&yacute;","&thorn;","&yuml;","&OElig;","&oelig;"],
    ["&sbquo;","&#8219;","&bdquo;","&hellip;","&trade;","&#9658;","&bull;","&rarr;","&rArr;","&hArr;","&diams;","&asymp;"]

  ]

};

var RTEButton = Class.create();
RTEButton.prototype = {
  over: "_over",
  file_extension: ".gif",
  initialize: function(editor_id,option){
    this.editor_id = editor_id;
    this.id = editor_id+"_"+option.alias;
    this.image_path = RTEditor.prototype.image_path;
    this.option = option;

    this.Instantiate();
  },
  Instantiate: function(){
    var html = '<div id="'+EDITOR_BUTTON_PREFIX+'_'+this.id+'" class="'+EDITOR_BUTTON_PREFIX+'" ';
    html += 'ondragstart="return false;">';
    html += '<img id="'+EDITOR_BUTTON_IMAGE_PREFIX+'_'+this.id+'" src="'+this.image_path+this.option.alias+this.file_extension+'" alt="'+this.option.name+'" title="'+this.option.name+'" />';

    html += '</div>';

    if(true){
      document.write(html);
      // ボタンのエレメントを格納する
      this.button = $(EDITOR_BUTTON_PREFIX+'_'+this.id);
      this.button_image = $(EDITOR_BUTTON_IMAGE_PREFIX+'_'+this.id);

      Event.observe(this.button,'mouseover',this.MouseOver.bindAsEventListener(this),false);
      Event.observe(this.button,'mouseout',this.MouseOut.bindAsEventListener(this),false);
      Event.observe(this.button,'mousedown',this.Click.bindAsEventListener(this),false);
//      Event.observe(this.button,'click',this.Click.bindAsEventListener(this),false);
      Event.observe(this.button,'click',function(evt){}.bindAsEventListener(this),false);


    }else{
      return html;
    }
  },
  MouseOver: function(evt){
    this.button_image.src = this.image_path+this.option.alias+this.over+this.file_extension;
  },
  MouseOut: function(evt){
    this.button_image.src = this.image_path+this.option.alias+this.file_extension;
  },
  Click: function(evt){
    this.option.action(this.editor_id,this,evt);
  }
};

var RTEditor = Class.create();
RTEditor.prototype = {
  editor_map: {},
  editor_button_map: {},
  editor_drop_down_map: {},
  text_mode: false,
  image_path: "",
  pictogram_dir: "pictogram/",
  pictogram_path: "",

  initialize: function(id,config){
    this.id = id;
    if( this.instantiated ){
      return;
    }
    this.config = Object.extend({
      width: "100%",
      height: "220px",
      image_path: "images/wysiwyg/",
      iframe_css: "/css/rteditor_iframe.css",
      beforeunload: "このページから移動すると、編集内容が保存されませんが、移動してよろしいですか？"
    },config||{});
    if( this.config.mode == "text" ){
      this.text_mode = true;
    }
    this.image_path = this.config.image_path;
    RTEditor.prototype.image_path = this.config.image_path;
    this.pictogram_path = this.image_path+this.pictogram_dir;
    document.write('<div class="'+EDITOR_PREFIX+'">');
    this.CreateMenu();
    this.InitDropDown();

    var html = '<iframe id="'+EDITOR_IFRAME_PREFIX+"_"+this.id+'" class="'+EDITOR_IFRAME_PREFIX+'" style="background-color:#fff;border:solid 1px #000;width:'+this.config.width+';height:'+this.config.height+';" src="about:blank" allowtransparency="true" frameborder="0"></iframe>';
    html += '<textarea id="'+EDITOR_TEXTAREA_PREFIX+"_"+this.id+'" class="'+EDITOR_IFRAME_PREFIX+'" style="padding: 2px;border:solid 1px #333;width:'+this.config.width+';height:'+this.config.height+';overflow:auto;"></textarea>';
    html += '</div>';
//alert(html);

    document.write(html);

    this.DesignModeOn();
    if( this.text_mode ){
      $(EDITOR_IFRAME_PREFIX+"_"+this.id).hide();
      $(EDITOR_STATUS_PREFIX+'_'+this.id).update(RTEditorConfig.notice.text_mode);
      $(EDITOR_BUTTON_IMAGE_PREFIX+'_'+this.id+'_html_source').src = this.image_path+"html_source_check.gif";

    }else{
      $(EDITOR_TEXTAREA_PREFIX+"_"+this.id).hide();
      $(EDITOR_STATUS_PREFIX+'_'+this.id).update(RTEditorConfig.notice.html_mode);
      $(EDITOR_BUTTON_IMAGE_PREFIX+'_'+this.id+'_html_source').src = this.image_path+"html_source.gif";

    }


    this.instantiated = true;
    RTEditor.prototype.editor_map[this.id] = this;
  },
  GetInstance: function(editor_id){
    return RTEditor.prototype.editor_map[editor_id];
  },
  CreateMenu: function(){
    var html = '<div class="rte_tool_bar">';
    var oRTEButton,button;
    this.editor_button_map[this.id] = {};
    for(var i=0;i<RTEditorConfig.menu.length;i++){
      html += '<table cellpadding="0" cellspacing="0" border="0"><tr>';
      for(var j=0;j<RTEditorConfig.menu[i].length;j++){
	button = RTEditorConfig.menu[i][j];
	if( button.alias=='|' ){
	  html += '<td class="rte_divider"></td>';
	}else{
	  // 絵文字は、複数ページ対応は、対応しているボタンのみ表示
	  if( button.alias == "pictogram" ){
	    if( typeof(RTEditorConfig.icons[0][0])=="object" ){
	      if( button.action!="RTEditor.prototype.IconMultiDropDown" ){
		continue;
	      }
	    }else{
	      if( button.action!="RTEditor.prototype.IconDropDown" ){
		continue;
	      }
	    }
	  }



	  html += '<td><script>';
	  html += 'var oRTEButton = new RTEButton("'+this.id+'",{alias:"'+button.alias+'",name:"'+button.name+'",action:'+button.action+'});';
	  if( this.text_mode && button.alias=="html_source" ){
	    html += 'oRTEButton.option.alias = "html_source_check";';
	  }
	  html += '</script></td>';

  //	oRTEButton = new RTEButton(this.id,button);
  //	html+= this.CreateButton(RTEditorConfig.menu[i][j]);
  //	html+= new RTEButton(this.id,RTEditorConfig.menu[i][j]).Instantiate();
  //	html += oRTEButton.Instantiate();

	  this.editor_button_map[this.id][button.alias] = oRTEButton;
	}
      }
      html += '</tr></table>';

    }
    html += '</div>';
    html += '<div style="width:'+this.config.width+'" id="'+EDITOR_STATUS_PREFIX+'_'+this.id+'" class="'+EDITOR_STATUS_PREFIX+'"></div>';
//alert(html);
    document.write(html);


  },
  DesignModeOn: function(){
    var doc = this.GetDocument();

/*
if(typeof(doc)=="undefined"){
alert("doc="+doc);
// TODO:Safari....
var html = [];
var theFrame = this.GetComposition();
var i=0;
// dir,lang,className,innerHTML,innerText,outerHTML
with(theFrame){
//  lang;
//  dir;
//  className;
//  innerHTML;
  innerText;
//  outerHTML;
}
for(o in theFrame){
  if( typeof(theFrame[o])=="function" ) continue;
  theFrame[o];
  html.push(o);
  if( i>20 ){ break; }
  i++;
}
//alert(theFrame+","+theFrame.document);
alert(i+" = "+html.join(","));
doc = theFrame.document;
}
*/

    var html = '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>blank page</title><link rel="stylesheet" type="text/css" href="'+this.config.iframe_css+'" /></head>';
    html += '<body style="font-size:100%;margin:2px"></body></html>';
    try{
      doc.designMode = 'on';
      doc.open();
      doc.write(html);
      doc.close();
    }catch(e){
      alert("DesignModeOn:"+e.message);
    }

    // エディタ本体がクリックされたときは、メニューを隠す
    Event.observe(doc,'click',this.Click.bindAsEventListener(this),false);
    Event.observe(doc,'keydown',this.KeyDown.bindAsEventListener(this),false);
    Event.observe($(EDITOR_TEXTAREA_PREFIX+"_"+this.id),'keydown',this.KeyDown.bindAsEventListener(this),false);

    // Textareaにもイベントを割り当てる
//    Event.observe($(EDITOR_TEXTAREA_PREFIX+"_"+this.id),'keydown',this.KeyDownTextarea.bindAsEventListener(this),false);




    // FFで生成されるタグをIEやOperaに近づける
    if( Prototype.Browser.Gecko ){
      doc.execCommand("styleWithCSS",false,false);
    }
  },
  GetIframe: function(){
//alert("GetIframe:"+EDITOR_IFRAME_PREFIX+"_"+this.id);
    if( document.all ){ // IE
      return eval(EDITOR_IFRAME_PREFIX+"_"+this.id);
    }else{ // Mozilla,Opera,Safari
      return document.getElementById(EDITOR_IFRAME_PREFIX+"_"+this.id);
    }
  },
  GetComposition: function(){
    var theFrame = this.GetIframe();
/*
    if( theFrame.contentWindow ){ // IE,Opera?
      return theFrame.contentWindow;
    }else if( theFrame.contentDocument ){ // FF,Safari
      return theFrame.contentDocument;
    }else{
      return theFrame;
    }
*/
    return theFrame.contentWindow?theFrame.contentWindow:theFrame;
  },
  GetDocument: function(){
    var theFrame = this.GetComposition();
    if( typeof(theFrame.document)=="undefined" ){
      // for Safari
      theFrame.innerText;
    }
    return theFrame.document;
  },
  GetWindow: function(){
    return this.GetComposition().contentWindow;
  },
  KeyDown: function(evt){
    this.changed = true;

/*
    var range = this.CreateRange();
    var oRange = editor.GetSelection();
$('debug').innerHTML+="[KeyDown]"+range+","+oRange+","+evt;
    var elements = [];
    do{
      elements.push(element);
      element = element.parentNode;
    }while(element);
$('debug').innerHTML+=elements+"<br>";
$('debug').innerHTML+="<br>";
*/
  },
  KeyDownTextarea: function(evt){
//alert("KeyDownTextarea:"+evt.keyCode+Event.KEY_RETURN);
    if( evt.keyCode == Event.KEY_RETURN && !evt.shiftKey ){
      this.SetSelectTextTextarea("<br />\n");
      Event.stop(evt);
    }
  },
  BeforeUnload:function(){
    Event.observe(window,"beforeunload",this.BeforeUnloadExecute.bindAsEventListener(this),true);
  },
  StopBeforeUnload:function(){
//alert("StopBeforeUnload"+this.BeforeUnloadExecute);
    Event.stopObserving(window,"beforeunload",this.BeforeUnloadExecute.bindAsEventListener(this),true);
    this.changed = false; // stopObservingが利いていないようだ。
  },
  BeforeUnloadExecute: function(evt){
//alert("BeforeUnloadExecute:"+this.id);
    if( this.changed ){
      return evt.returnValue = this.config.beforeunload;
    }
    return true;
  },
/*
  Change: function(evt){
$('debug').innerHTML+="[onchange]<br>";
  },
*/
  Click: function(evt){
//$('debug').innerHTML+=Event.element(evt)+"<br>";
    this.HideAllDropDowns(this.id);
  },
/*
  KeyDown: function(evt){
    if( evt.keyCode == Event.KEY_RETURN && !evt.shiftKey ){
      var range = document.selection.createRange();
      range.pasteHTML("<br>");
      Event.stop(evt);
    }
  },
*/

  GetHTML: function(){
    if( this.text_mode ){
      return $(EDITOR_TEXTAREA_PREFIX+"_"+this.id).value;
    }else{
      var html = this.GetDocument().body.innerHTML;
//      if( html=="<br>" ){ html = ""; }
      if( Prototype.Browser.IE ){
//alert("[B1]"+html);
	// TODO:ここの対処法はAd-hoc...
	html = html.replace(/\r\n/g,"\n");
//	html = html.replace(/<!-- lf -->/g,"\n");


	html = html.replace(/<!-- lf -->\n/g,"\n");
	html = html.replace(/<!-- lf -->/g,"\n");
//alert("[B2]"+html);
	// Pタグを変換する
	html = html.replace(/<p(\s+[^>]*)>/gi, "<div$1>" );
	html = html.replace(/<p\s*>/gi, "<div>" );
	html = html.replace(/<\/p>/gi, "</div>" );
	html = html.replace(/&nbsp;/gi, " " );
	html = html.replace(/<div> <\/div>/gi, "<br>" );

      }else if( Prototype.Browser.WebKit ){
	// Safariがつけるタグを除去する
	html = html.replace(/<SPAN class="Apple-style-span"/ig,"<SPAN");
	html = html.replace(/<BR class=".*-block-placeholder">/ig,"<BR />");
	// TODO:Submit時に取り除く
  //      html = html.replace(/ class="safari_a" onclick="return false;"/ig,"");


  /*
	var spans = this.GetDocument().getElementsByTagName("span");
	for(var i=0;i<spans.length;i++){
	  if( Element.hasClassName(spans[i],'Apple-style-span') ){
	    Element.removeClassName(spans[i],'Apple-style-span');
	  }
	}
  */
      }
  //alert("GetHTML:"+html+","+this.GetDocument().body.innerHTML);

      return html;
    }
  },
  SetHTML: function(str){
    var theDoc = this.GetDocument();
//alert("[SetHTML]"+theDoc+","+theDoc.body);

    if( theDoc && theDoc.body ){
//      if( !document.all ){ theDoc.designMode='off'; }
//alert("SetHTML:"+str);

      $(EDITOR_TEXTAREA_PREFIX+"_"+this.id).value = str;
      // TODO:IEだと改行がなくなってしまう。
      // http://d.hatena.ne.jp/brazil/20060917/1158465641
      if( Prototype.Browser.IE ){

//theDoc.body.appendChild(theDoc.createTextNode("[R]\r[N]\n[NR]\n\r[RN]\r\n"));
//theDoc.body.appendChild(theDoc.createTextNode('[R]\r[N]\n[NR]\n\r[RN]\r\n'));
//alert(theDoc.body.innerHTML);

//alert("[A1]"+str);
//	str = str.replace(/\r\n/g,"\n");
	str = str.replace(/\n/g,"<!-- lf -->");
//alert("[A2]"+str);

      }
      theDoc.body.innerHTML = str;
      if( !document.all ){ theDoc.designMode='on'; }
    }
  },
  GetText: function(){
    // IE,Opera,Safari
    if( this.GetDocument().body.innerText ){
      return this.GetDocument().body.innerText;
    }else{
      return this.GetDocument().body.textContent;
    }
  },
  SetText: function(text){
    // IE,Opera,Safari
    if( this.GetDocument().body.innerText ){
      this.GetDocument().body.innerText = text;
    }else{
      this.GetDocument().body.textContent = text;
    }
  },
  InsertHTML: function(id,html){
    var editor = RTEditor.prototype.GetInstance(id);
    this.Format(editor,"inserthtml",html);
  },
  Focus: function(){
    if( this.text_mode ){
      $(EDITOR_TEXTAREA_PREFIX+"_"+this.id).focus();
    }else{
      this.GetComposition().focus();
    }
  },
  Format: function(editor,cmd,opt){
    cmd = cmd.toLowerCase();
    if( typeof(editor)!='object' ){
      editor = RTEditor.prototype.GetInstance(editor);
    }
    editor.HideAllDropDowns(editor.id);
    editor.Focus();

    if( editor.text_mode ){
// 選択範囲の取得
//alert("str="+str);
      var str = editor.GetSelectTextTextarea();
      if( !str ){
	str = "&nbsp;";
      }
      // 指定されたタグを貼り付ける
      if( cmd=="bold" ){
	str = "<strong>"+str+"</strong>";
      }else if( cmd=="italic" ){
	str = "<em>"+str+"</em>";
      }else if( cmd=="underline" ){
	str = "<u>"+str+"</u>";
      }else if( cmd=="strikethrough" ){
	str = "<strike>"+str+"</strike>";
      }else if( cmd=="fontname" ){
	str = '<font face="'+opt+'">'+str+'</font>';
      }else if( cmd=="fontsize" ){
	str = '<font size="'+opt+'">'+str+'</font>';
      }else if( cmd=="forecolor" ){
	str = '<font color="#'+opt+'">'+str+'</font>';
      }else if( cmd=="insertimage" ){
	str = '<img src="'+opt+'" />';
      }else if( cmd=="inserthtml" ){
	str = opt;
      }else if( cmd=="createlink" ){
	str = '<a href="'+opt+'">'+str+'</a>';
      }else if( cmd=="indent" ){
	str = '<blockquote dir="ltr" style="margin-right:0px;">'+str+'</blockquote>';
      }else if( cmd=="outdent" ){
      }else if( cmd=="subscript" ){
	str = '<sub>'+str+'</sub>';
      }else if( cmd=="superscript" ){
	str = '<sup>'+str+'</sup>';
      }else if( cmd=="inserthorizontalrule" ){
	str = '<hr />';
      }else if( cmd=="justifyleft" ){
	str = '<div align="left">'+str+'</div>';
      }else if( cmd=="justifycenter" ){
	str = '<div align="center">'+str+'</div>';
      }else if( cmd=="justifyright" ){
	str = '<div align="right">'+str+'</div>';
      }else if( cmd=="insertorderedlist" ){
	str = '<ol><li>'+str+'</li></ol>';
      }else if( cmd=="insertunorderedlist" ){
	str = '<ul><li>'+str+'</li></ul>';
      }else{
//	alert("タグモードでは、この機能はサポートされていません。"+cmd+","+opt);
	alert("タグモードでは、この機能はサポートされていません。");
      }
      editor.SetSelectTextTextarea(str);

    }else{
      if( Prototype.Browser.IE && cmd=="inserthtml" ){
	var range = this.CreateRange();
//alert(Object.toJSON(range));
	range.pasteHTML(opt);
	return;
      }
//alert("execCommand("+cmd+","+opt+")");
      try{
	if( !this.GetDocument().execCommand(cmd,false,opt) ){
	  switch(cmd){
	  case 'inserthtml':
	    this.GetDocument().body.innerHTML += opt;
	    break;
	  case 'insertimage':
	    this.GetDocument().body.innerHTML += '<img src="'+opt+'" />';
	    break;
	  case 'createlink': // Safari...
	    this.GetDocument().body.innerHTML += '<a href="'+opt+'" class="safari_a" onclick="return false;" target="_blank">'+this.GetSelectText()+'</a>';
	    break;
	  default:
	    alert("お使いのブラウザでは、この機能はサポートされていません。");
	  }
	}
      }catch(e){
	alert("Format"+e.message);
      }
    }
  },
  // Range
  CreateRange: function(){
    var doc = this.GetDocument();
    var win = this.GetWindow();
    if( doc.createRange ){
      return doc.createRange();
    }else if( doc.selection.createRange ){ // IE
      return doc.selection.createRange();
    }
    return null;
  },
  GetSelection: function(){
    if( window.getSelection ){ // FF & Opera
      var oWindow = this.GetComposition();
      if( oWindow.getSelection().getRangeAt ){
	return oWindow.getSelection().getRangeAt(0);
      }else if( oWindow.document.createRange ){ // Safari
	var oRange = oWindow.getSelection();
	return oRange;
      }
    }else if( document.getSelection ){
alert("[GetSelection:A2]");
    }else if( document.selection && document.selection.createRange ){
      return this.CreateRange();
    }
    return "";
  },
  GetSelectText: function(){
    if( this.text_mode ){
      return this.GetSelectTextTextarea();
    }
    var oSelection = this.GetSelection();
//if(document.all){ alert("GetSelectText:"+Object.toJSON(oSelection)); }
//alert("GetSelectText:"+oSelection);
    if( typeof oSelection.htmlText == "string" ){
      return oSelection.htmlText;
    }else if(oSelection ){
      return oSelection;
    }
    return "";
  },
  GetSelectTextTextarea: function(){
    var str;
    var e = $(EDITOR_TEXTAREA_PREFIX+"_"+this.id);
    if(document.selection){
      str = document.selection.createRange().text;
    }else if(window.getSelection){
      var start = e.selectionStart;
      var end = e.selectionEnd;
      str = e.value.substring(start,end);
    }
    return str;
  },
  SetSelectTextTextarea: function(str){
    var e = $(EDITOR_TEXTAREA_PREFIX+"_"+this.id);
    if(document.selection){
//      if( document.selection.createRange().text ){
	document.selection.createRange().text = str;
//      }else{
//	e.value += str;
//      }
    }else if(window.getSelection){
      var start = e.selectionStart;
      var end = e.selectionEnd;
      e.value = e.value.substring(0,start)+str+e.value.substring(end);
    }
  },

  // Execute
  FontFace: function(id,select){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,"fontname",select);
  },
  FontSize: function(id,select){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,"fontsize",select);
  },
  Bold: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,"bold",null);
  },
  Italic: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,"italic",null);
  },
  Underline: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,"underline",null);
  },
  StrikeThrough: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,"strikethrough",null);
  },
  FontColor: function(id,type,color){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,type,color);
  },

  Icon: function(id,src){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(editor,"insertimage",src);
  },
  Char: function(id,c){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"inserthtml",c);
//    editor.InsertHTML(editor,c);
  },
  Hyperlink: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.HideAllDropDowns(id);
    var selected_text = editor.GetSelectText();
    if( selected_text=='' ){
      alert("リンクさせる部分を選択してください。");
      return;
    }
//alert("selected_text="+selected_text);
//    var oRange = editor.CreateRange();
    if( editor.text_mode ){
      var oRange = editor.GetSelectTextTextarea();
    }else{
      var oRange = editor.GetSelection();
    }



    var oAnchor = null;
    if( oRange!=null ){
      // 選択された部分の親を探す
      // range中に含まれるエレメントに対して、共通の親エレメントを取得する
      var oParent;
      // 親エレメントからaタグを探す。
      if( oRange.parentElement ){ // IE
	oParent = oRange.parentElement();
	while(oParent && oParent.tagName!='A'){ // todo?:toupper
	  oParent = oParent.parentNode;
	}
	oAnchor = oParent;
      }else if( oRange.commonAncestorContainer ){ // FF,Opera
	oParent = oRange.commonAncestorContainer;
	if( oParent != null & oParent.nodeType!=Node.TEXT_NODE ){
	  var oATags = oParent.getElementsByTagName('A');
	  if( oATags && oATags.length>0 ){
	    oAnchor = oATags[0];
	  }
	}
      }
    }
    var link = prompt("リンクしたいURLを入力してください。",oAnchor?oAnchor.href:"http://");
    if( link && link!='http://' ){
      editor.Format(id,"createlink",link);
    }
  },
  AlignLeft: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"justifyleft");
  },
  AlignCenter: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"justifycenter");
  },
  AlignRight: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"justifyright");
  },
  NumberedList: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"insertOrderedList");
  },
  UnorderedList: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"insertUnorderedList");
  },
  Outdent: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"outdent");
  },
  Indent: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"indent");
  },
  Subscript: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"subscript",null);
  },
  Superscript: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"superscript",null);
  },
  HorizontalRule: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.Format(id,"InsertHorizontalRule");
  },
  InsertTable: function(id,options){
    var editor = RTEditor.prototype.GetInstance(id);
    editor.InsertHTML(id,'<table border="1" style="border-collapse:collapse;"><tbody><tr><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table>');
  },

  SetModeNotice: function(button){
//    var button_image = $(EDITOR_BUTTON_IMAGE_PREFIX+'_'+this.id+'_html_source');
    if( this.text_mode ){
      button.button_image.src = RTEditor.prototype.image_path+"html_source.gif";
      button.option.alias = "html_source";

      $(EDITOR_STATUS_PREFIX+'_'+this.id).update(RTEditorConfig.notice.html_mode);
    }else{
      button.button_image.src = RTEditor.prototype.image_path+"html_source_check.gif";
      button.option.alias = "html_source_check";

      $(EDITOR_STATUS_PREFIX+'_'+this.id).update(RTEditorConfig.notice.text_mode);
    }
  },

  HtmlSource: function(id,button,evt){
//alert(id+","+button.button_image.src+","+evt);
    var editor = RTEditor.prototype.GetInstance(id);
    editor.SetModeNotice(button);
    if( editor.text_mode ){ // HTML => TEXT

      // Safariのときは、この順序が重要になる
      $(EDITOR_TEXTAREA_PREFIX+"_"+id).hide();
      $(EDITOR_IFRAME_PREFIX+"_"+id).show();
      editor.SetHTML($(EDITOR_TEXTAREA_PREFIX+"_"+id).value);

    }else{ // TEXT => HTML

      $(EDITOR_TEXTAREA_PREFIX+"_"+id).value = editor.GetHTML();
      $(EDITOR_TEXTAREA_PREFIX+"_"+id).show();
      $(EDITOR_IFRAME_PREFIX+"_"+id).hide();

    }
    editor.text_mode = !editor.text_mode;
  },

  // DropDown
  InitDropDown: function(){
    var html = '<div id="'+EDITOR_TOGGLE_FONT_FACE_PREFIX+'_'+this.id+'" class="rte_selector" style="width:15em; display:none;"></div>';
    html += '<div id="'+EDITOR_TOGGLE_FONT_SIZE_PREFIX+'_'+this.id+'" class="rte_selector" style="width:15em; display:none;"></div>';
    html += '<div id="'+EDITOR_TOGGLE_FONT_COLOR_PREFIX+'_'+this.id+'" class="rte_selector" style="display:none;"></div>';
    html += '<div id="'+EDITOR_TOGGLE_ICON_PREFIX+'_'+this.id+'" class="rte_selector" style="display:none;"></div>';
    html += '<div id="'+EDITOR_TOGGLE_ICON_MULTI_PREFIX+'_'+this.id+'" class="rte_selector" style="display:none;"></div>';
    html += '<div id="'+EDITOR_TOGGLE_CHAR_PREFIX+'_'+this.id+'" class="rte_selector" style="display:none;"></div>';
    document.write(html);

    this.editor_drop_down_map[this.id] = {};
    this.editor_drop_down_map[this.id][EDITOR_TOGGLE_FONT_FACE_PREFIX] = this.GenerateFontFace();
    this.editor_drop_down_map[this.id][EDITOR_TOGGLE_FONT_SIZE_PREFIX] = this.GenerateFontSize();
    this.editor_drop_down_map[this.id][EDITOR_TOGGLE_FONT_COLOR_PREFIX] = this.GenerateColorPicker('forecolor');
    this.editor_drop_down_map[this.id][EDITOR_TOGGLE_ICON_PREFIX] = this.GenerateIcon();
    this.editor_drop_down_map[this.id][EDITOR_TOGGLE_ICON_MULTI_PREFIX] = this.GenerateIconMulti(0);
    this.editor_drop_down_map[this.id][EDITOR_TOGGLE_CHAR_PREFIX] = this.GenerateChar();
  },
  GenerateFontFace: function(){
    var html = '<div onclick="RTEditor.prototype.HideDropDown(\''+this.id+'\',\''+EDITOR_TOGGLE_FONT_FACE_PREFIX+'\');">';
    html += '<table width="100%" cellpadding="0" cellspacing="0">';
    for(var i=0;i<RTEditorConfig.font_face.length;i++){
      html += '<tr><td class="rte_selector_item" ';
      html += 'onmouseover="this.style.backgroundColor=\'#ddd\';" ';
      html += 'onmouseout="this.style.backgroundColor=\'#fff\';" ';
      html += 'onmousedown="RTEditor.prototype.FontFace(\''+this.id+'\',\''+RTEditorConfig.font_face[i]['font']+'\'); return false;" ';
      html += '>';
      html += '<font face="'+RTEditorConfig.font_face[i]['font']+'">'+RTEditorConfig.font_face[i]['font']+'</font>';
      html += '</td></tr>';
    }
    html += '</table></div>';
    return html;
  },
  GenerateFontSize: function(){
    var html = '<div onclick="RTEditor.prototype.HideDropDown(\''+this.id+'\',\''+EDITOR_TOGGLE_FONT_SIZE_PREFIX+'\');">';
    html += '<table width="100%" cellpadding="0" cellspacing="0">';
    for(var i=0;i<RTEditorConfig.font_size.length;i++){
      html += '<tr><td class="rte_selector_item" ';
      html += 'onmouseover="this.style.backgroundColor=\'#ddd\';" ';
      html += 'onmouseout="this.style.backgroundColor=\'#fff\';"';
      html += 'onmousedown="RTEditor.prototype.FontSize(\''+this.id+'\',\''+RTEditorConfig.font_size[i]['size']+'\');" ';
      html += '>';
      html += '<font size="'+RTEditorConfig.font_size[i]['size']+'">'+RTEditorConfig.font_size[i]['font_name']+'</font>';
      html += '</td></tr>';
    }
    html += '</table></div>';
    return html;
  },
  GenerateColorPicker: function(type){
    var html = '<div onclick="RTEditor.prototype.HideDropDown(\''+this.id+'\',\''+EDITOR_TOGGLE_FONT_COLOR_PREFIX+'\');">';
    html += '<table cellpadding="0" cellspacing="0">';
    for(var i=0;i<RTEditorConfig.color_palette.length;i++){
      html += '<tr>';
      for(var j=0;j<RTEditorConfig.color_palette[i].length;j++){
	html += '<td valign="top" bgcolor="#'+RTEditorConfig.color_palette[i][j]+'" style="width:12px;line-height:12px;border:1px solid white;" ';
	html += 'onmouseover="this.style.border=\'1px inset #000\'; return false;" ';
	html += 'onmouseout="this.style.border=\'1px solid #fff\'; return false;" ';
	html += 'onmousedown="RTEditor.prototype.FontColor(\''+this.id+'\',\''+type+'\',\''+RTEditorConfig.color_palette[i][j]+'\'); return false;" ';
	html += '>&nbsp;</td>';
      }
      html += '</tr>';
    }
    html += '</table></div>';
    return html;
  },
  GenerateIcon: function(){
    var icons = RTEditorConfig.icons;
    var html = '<div onclick="RTEditor.prototype.HideDropDown(\''+this.id+'\',\''+EDITOR_TOGGLE_ICON_PREFIX+'\');">';
    for(var i=0;i<icons.length;i++){
      for(var j=0;j<icons[i].length;j++){
	html += '<img style="border:solid 1px #fff;" src="'+this.pictogram_path+icons[i][j]+'.gif" ';
	html += 'onmouseover="this.style.border=\'inset 1px #000\'; return false;"';
	html += 'onmouseout="this.style.border=\'solid 1px #fff\'; return false;"';
	html += 'onmousedown="RTEditor.prototype.Icon(\''+this.id+'\',this.src); return false;" ';
	html += '/>';
      }
      html += '<br />';
    }
    html += '</div>';
//alert(html);
    return html;
  },
  GenerateIconMulti: function(index){
    if( typeof(RTEditorConfig.icons[0][0])!="object" ){
      return "";
    }
    var html = '<div class="multi_icon_pagenate">';
    var icons = RTEditorConfig.icons[index];
    for(i=0;i<RTEditorConfig.icons.length;i++){
      if( i==index ){
	html += '<b>'+(i+1)+'</b> ';
      }else{
	html += '<a href="#" onclick="RTEditor.prototype.ChangePage(\''+this.id+'\',\''+i+'\'); return false;">'+(i+1)+'</a> ';
      }
    }
    html += '</div>';
    html += '<div onclick="RTEditor.prototype.HideDropDown(\''+this.id+'\',\''+EDITOR_TOGGLE_ICON_PREFIX+'\');">';
    for(var i=0;i<icons.length;i++){
      for(var j=0;j<icons[i].length;j++){
	html += '<img style="border:solid 1px #fff;" src="'+this.pictogram_path+icons[i][j]+'.gif" ';
	html += 'onmouseover="this.style.border=\'inset 1px #000\'; return false;"';
	html += 'onmouseout="this.style.border=\'solid 1px #fff\'; return false;"';
	html += 'onmousedown="RTEditor.prototype.Icon(\''+this.id+'\',this.src); return false;" ';
	html += '/>';
      }
      html += '<br />';
    }
    html += '</div>';
    return html;
  },
  ChangePage: function(editor_id,index){
    var editor = RTEditor.prototype.GetInstance(editor_id);
    $(EDITOR_TOGGLE_ICON_MULTI_PREFIX+'_'+editor_id).update(editor.GenerateIconMulti(index));
  },
  GenerateChar: function(){
    var char_map = RTEditorConfig.char_map;
    var html = '<div onclick="RTEditor.prototype.HideDropDown(\''+this.id+'\',\''+EDITOR_TOGGLE_CHAR_PREFIX+'\');">';
    html += '<table>';
    for(var i=0;i<char_map.length;i++){
      html += '<tr>';
      for(var j=0;j<char_map[i].length;j++){
	html += '<td style="border:solid 1px #fff;" ';
	html += 'onmouseover="this.style.border=\'inset 1px #000\'; return false;" ';
	html += 'onmouseout="this.style.border=\'solid 1px #fff\'; return false;" ';
	html += 'onmousedown="RTEditor.prototype.Char(\''+this.id+'\',\''+char_map[i][j]+'\'); return false;" ';
	html += '>'+char_map[i][j]+'</td>';
      }
      html += '</tr>';
    }
    html += '</table></div>';
    return html;
  },



  ToggleDropDown: function(prefix,oButton){
//alert(prefix+'_'+this.id);
    if( Element.visible($(prefix+'_'+this.id)) ){
      this.HideDropDown(this.id,prefix);
    }else{
      this.ShowDropDown(this.id,prefix,oButton);
    }
  },
  HideDropDown: function(id,prefix){
//alert(prefix+'_'+id);
    $(prefix+'_'+id).hide();
  },
  ShowDropDown: function(id,prefix,oButton){
    // 一度すべてのDropDownメニューを非表示にする
    this.HideAllDropDowns(id);
//alert(Object.toJSON(oButton));
    // 表示するための内容を取得する
    var el = this.PrepareDropDownContents(prefix);
    var offset = 0;
    var pos = Position.positionedOffset(oButton.button);
//alert("pos="+pos+","+oButton.button.getHeight());
    el.setStyle({
      "position":"absolute",
      "left":(pos[0]+offset)+'px',
      "top":(pos[1]+oButton.button.getHeight())+"px",
      "zIndex":10,
      "display":"block"
    });
  },
  PrepareDropDownContents: function(prefix){
    var theDiv = $(prefix+'_'+this.id);

    if( theDiv.getAttribute("loaded")===true ){
      return theDiv;
    }
    theDiv.innerHTML = RTEditor.prototype.editor_drop_down_map[this.id][prefix];
//alert(theDiv.innerHTML);
    theDiv.setAttribute("loaded",true);
    return theDiv;
  },
  HideAllDropDowns: function(id){
    var editor = RTEditor.prototype.GetInstance(id);
    for(var prefix in RTEditor.prototype.editor_drop_down_map[this.id]){
      editor.HideDropDown(id,prefix);
    }
  },

  /* ドロップダウンメニューを表示する */
  FontFaceDropDown: function(editor_id,oButton,evt){
    var editor = RTEditor.prototype.GetInstance(editor_id);
//alert("FontFaceDropDown"+editor_id+","+editor.id);
//alert(Object.toJSON(oButton));
    editor.ToggleDropDown(EDITOR_TOGGLE_FONT_FACE_PREFIX,oButton);
  },
  FontSizeDropDown: function(editor_id,oButton,evt){
    var editor = RTEditor.prototype.GetInstance(editor_id);
    editor.ToggleDropDown(EDITOR_TOGGLE_FONT_SIZE_PREFIX,oButton);
  },
  FontColorDropDown: function(editor_id,oButton,evt){
    var editor = RTEditor.prototype.GetInstance(editor_id);
    editor.ToggleDropDown(EDITOR_TOGGLE_FONT_COLOR_PREFIX,oButton);
  },
  IconDropDown: function(editor_id,oButton,evt){
    var editor = RTEditor.prototype.GetInstance(editor_id);
    editor.ToggleDropDown(EDITOR_TOGGLE_ICON_PREFIX,oButton);
  },
  IconMultiDropDown: function(editor_id,oButton,evt){
    var editor = RTEditor.prototype.GetInstance(editor_id);
    editor.ToggleDropDown(EDITOR_TOGGLE_ICON_MULTI_PREFIX,oButton);
  },
  CharDropDown: function(editor_id,oButton,evt){
    var editor = RTEditor.prototype.GetInstance(editor_id);
    editor.ToggleDropDown(EDITOR_TOGGLE_CHAR_PREFIX,oButton);
  }
};
