/*
 editor.js by Masayuki AOKI, martin.
 Last modified :2003.12.04 18:54
*/
var d = document;
var cursorStart = cursorEnd = 0;
var ie = d.selection ? 1 : 0;
var moz = (d.getSelection && !window.opera) ? 1 : 0;

function InitEditor(){
 if(!d.getElementById('editarea')) return;
 d.getElementById('editarea').onclick = function(){
  if(d.getElementById("palette").style.visibility!="hidden"){
   d.getElementById("palette").style.visibility = "hidden";
  }
 }
 divs = d.getElementsByTagName('DIV');
  for (var i=0; i < divs.length; i++) {
   if (divs[i].className == "imgbtn") {
    divs[i].onmouseover = function (){
     this.style.borderColor = '#0000db';
     if(this.id=="color" || this.id=="marker") return;
     else this.style.backgroundColor = '#cacaff';
    }
    divs[i].onmouseout = function(){ 
     this.style.border = 'solid 1px #f5f5f5';
     if(this.id=='color') return;
     else this.style.backgroundColor = '#f5f5f5';
    }
    divs[i].onmousedown = function(){
      this.style.borderColor = '#909090 #f0f0f0 #f0f0f0 #909090';
   }
    divs[i].onmouseup = function(){
     this.style.border = 'solid 1px #c0c0c0';
    }
   }
 }
 if(ie){
  d.getElementById('editarea').onmousedown = function(){cursorStart = getCursorPosition();}
  d.getElementById('editarea').onmouseup = function(){
   if(event.srcElement.id=='editarea'){
    var _cursorEnd = getCursorPosition();
    if(cursorStart - _cursorEnd > 0){
     cursorEnd = cursorStart;
     cursorStart = _cursorEnd;
    } else cursorEnd = _cursorEnd;
   }
  }
 } else if(moz){
  d.getElementById('editarea').onmouseup = function(){
   cursorStart = d.getElementById('editarea').selectionStart;
   cursorEnd = d.getElementById('editarea').selectionEnd;
  }
 } else {
  cursorEnd = d.getElementById('editarea').value.length;
 }
}

function Palette(mode,e){
 var btn = d.getElementsByTagName('td');
 for (var i=0;i<btn.length;i++){
  if(btn[i].className=="color-cell"){
   btn[i].onmouseover = function(){this.style.border="2px solid #1c0574";};
   btn[i].onmouseout = function(){this.style.border="1px solid #ddd"};
   btn[i].onclick = function(){
    selectColor(this.id, mode);
    d.getElementById("palette").style.visibility = "hidden";
   };
  }
 }
 var sT = ie ? document.documentElement.scrollTop : '';
 d.getElementById("palette").style.left = (e.clientX-10) + 'px';
 d.getElementById("palette").style.top = (sT+e.clientY+16) + 'px';
 d.getElementById("palette").style.visibility = "visible";
}
function getSelectedText(){
 var el = d.getElementById('editarea');
 el.focus();
 if(ie){
  selection = d.selection.createRange().text;
  return (selection.length > 0) ? selection : false;
 } else if(moz){//Mozilla
  selection = el.value.substring(el.selectionStart, el.selectionEnd);
  return (selection.length > 0) ? selection : false;
 } else return '';
}

function setRangeSelected(start, end){
 var el = d.getElementById('editarea');
 el.focus();
 if(ie){ // IE
  var rng = el.createTextRange();
  rng.collapse(true);
  rng.moveEnd('character', end);
  rng.moveStart('character', start);
  rng.select();
 } else if(moz){ // Mozilla
  el.setSelectionRange(start, end);
 } else el.select();
}

function replaceSelectedText(replace){
 var el = d.getElementById('editarea');
 var sel = getSelectedText();
 if(sel){
  el.value = el.value.slice(0,cursorStart)+el.value.slice(cursorStart).replace(sel,replace);
 } else {
  el.value = el.value.slice(0,cursorStart)+replace+el.value.slice(cursorStart);
 }
 var br = (ie) ? 
     el.value.slice(0,cursorStart).length - el.value.slice(0,cursorStart).replace(/\n/gm,'').length : 0;
 var pos = cursorStart+(replace).split('').length - br;
 setRangeSelected(pos,pos);
}

function selectColor(color, mode){
 var rep = getSelectedText() ? getSelectedText() : '';
 replaceSelectedText('['+mode+':' + color + ']'+rep+'[/'+mode+']');
 d.getElementById(mode).style.backgroundColor = color;
}

function fontSize(sz){
 var rep = getSelectedText() ? getSelectedText() : '';
 replaceSelectedText('[size:' + sz + ']'+rep+'[/size]');
}

function getCursorPosition(){ // for IE
 var el = d.getElementById('editarea');
 var selection = el.createTextRange(); 
 var point = event.srcElement.createTextRange();
 point.moveToPoint(window.event.x, window.event.y);
 for(var i = 0; i < el.value.length && !point.isEqual(selection); i++){
  if(point) selection.move('character');
 }
 var br = el.value.slice(0,i).length - el.value.slice(0,i).replace(/\n/gm,'').length;
 return i = !point.isEqual(selection) ? 0 : i+br;
}

function fontProp(tag, nl){
 var nl = nl ? '\n' : '';
 var el = d.getElementById('editarea');
 var rep = getSelectedText() ? getSelectedText() : '';
 replaceSelectedText(nl+'['+tag+']'+rep+'[/'+tag+']'+nl);
}
function createLink(){
 var link = prompt('リンクしたいアドレスをどうぞ: ', 'http://');
 if(link){
  link = link.replace(/^(http\:\/\/)/g,'');
  if(!link.match(/[^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()%]/)){
   if(getSelectedText()){
    replaceSelectedText('[link:'+link+']'+getSelectedText()+'[/link]');
   } else {
    var site = prompt("Input site name: ", "");
    if(site) replaceSelectedText('[link:'+link+']'+site+'[/link]');
   }
  } else link = prompt('Somewhere is wrong with your input URL...', link);
 } else {
  return;
 }
}
function sentenceFormat(f){
 var el = d.getElementById('editarea');
 var rep = getSelectedText() ? getSelectedText() : '';
 if(rep==''){
  replaceSelectedText('['+f+']\n\n[/'+f+']');
 } else {
  if(f==''){
   return;
  } else {
   replaceSelectedText('\n['+f+']'+rep+'[/'+f+']\n');
  }
 }
 d.getElementById('format').selectedIndex = 0;
}

function writeToolBar(){
 createPalette();
 var code =
   '<div id="toolbar">\n'
 + '<table style="margin:auto;" cellpadding="1" cellspacing="0" >\n'
 + '<tr>\n'
 + ' <td>\n'
 + ' <div><strong>文の整形</strong>\n'
 + ' <select id="format" onchange="sentenceFormat(this.options[this.selectedIndex].value)">\n'
 + '  <option value="" selected="selected">下から選択</option>\n'
 + '  <option value="q">引用ブロック</option>\n'
 + '  <option value="pre">整形ブロック</option>\n'
 + ' </select>\n'
 + ' </div>\n'
 + ' </td>\n'
 + ' <td>&nbsp;&nbsp;</td>\n'
 + ' <td>\n'
 + ' <div id="createlink" class="imgbtn"><img src="button/link.png" alt="" title="Insert Link" onclick="createLink()" /></div>\n'
 + '</td>\n'
 + ' <td><div id="bold" class="imgbtn"><img onclick="fontProp(\'b\')" src="button/bold.png" alt="" title="Bold" /></div></td>\n'
 + ' <td><div id="italic" class="imgbtn"><img onclick="fontProp(\'i\')" src="button/italic.png" alt="" title="Italic" /></div></td>\n'
 + ' <td><div id="underline" class="imgbtn"><img onclick="fontProp(\'u\')" src="button/underline.png" alt="" title="Underline" /></div></td>\n'
 + ' <td><div id="del" class="imgbtn"><img onclick="fontProp(\'del\')" src="button/del.png" alt="" title="Del line"></div></td>\n'
 + ' <td><div id="color" class="imgbtn" style="background:#777;"><img src="button/color.png" onclick="Palette(\'color\',event);" alt="" title="Text Color" /></div></td>\n'
 + '</tr>\n'
 + '</table>\n'
 + '</div>\n';
 d.write(code);
}

ch_col = function(col){ document.getElementById('editarea').style.color = '#'+col;}

function createPalette(){
 var colors = [
"#333333","#006633","#333333","#336633","#339933","#33cc33","#663333","#666633","#669933",
"#666666","#006666","#333366","#336666","#339966","#33cc66","#663366","#666666","#669966",
"#999999","#006699","#333399","#336699","#339999","#33cc99","#663399","#666699","#669999",
"#cccccc","#0066cc","#3333cc","#3366cc","#3399cc","#33cccc","#6633cc","#6666cc","#6699cc",
"#ffffff","#0066ff","#3333ff","#3366ff","#3399ff","#33ccff","#6633ff","#6666ff","#6699ff",
"#ff0000","#999900","#cc6600","#cc9900","#cccc00","#ff3300","#ff9900","#ffcc00","#ffff00",
"#0000ff","#999966","#cc6666","#cc9966","#cccc66","#ff3366","#ff9966","#ffcc66","#ffff66",
"#ffff00","#999999","#cc6699","#cc9999","#cccc99","#ff3399","#ff9999","#ffcc99","#ffff99",
"#00ffff","#9999cc","#cc66cc","#cc99cc","#cccccc","#ff33cc","#ff99cc","#ffcccc","#ffffcc",
"#ff00ff","#9999ff","#cc66ff","#cc99ff","#ccccff","#ff33ff","#ff99ff","#ffccff","#ffffff"
 ];

 var cols = '';
 cols += '<table><tr>';
 for (i=1; i<=colors.length;i++){
  cols += '<td id="'+colors[i-1]+'" class="color-cell" style="background:'+colors[i-1]+'"><img src="button/cell.png" /></td>';
  if(i%9==0) cols += '</tr>\n<tr>';
 }
 cols += '</table>\n';
 var p = '<div id="palette" style="\n'
       + '         position:absolute;\n'
       + '         visibility:hidden;\n'
       + '         background-color: Menu;\n'
       + '         border: 2px solid;\n'
       + '         border-color: #f0f0f0 #909090 #909090 #f0f0f0;\n'
       + '         width:176px;"\n'
       + '>\n';
 with(d){
  write(p);
  write(cols);
  write('</div>\n');
 }
}
 window.onload = InitEditor;
