/* JavaScript scripts by martin $ 2005/04/11 07:14:50
*/
var d = document;
var ie = d.selection ? 1 : 0;
var moz = (d.getSelection && !window.opera) ? 1 : 0;

function insertLink(target){
 var t = document.getElementById(target);
 var link = prompt('リンクしたいURLをどうぞ: ', 'http://');
 if(link){
  link = link.replace(/^(http\:\/\/)/g,'');
  if(!link.match(/[^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()%]/)){
   var site = prompt("サイト名をどうぞ ", "");
   if(site) t.value += ('[link:'+link+']' + site + '[/link]');
  } else link = prompt('URLに不適な文字が入っているような...', link);
 } else {
  return;
 }
}

function googleIt(query){
 window.open(encodeURI("http://www.google.com/search?hl=ja&ie=UTF-8&oe=UTF-8&q="+query));
}
function wikipediaIt(query){
 window.open("http://ja.wikipedia.org/wiki/%E7%89%B9%E5%88%A5:Search?search="+encodeURI(query));
}
function ToClipBoard(item,data){
 if(document.all){
  if(data) window.clipboardData.setData('Text', data);
  else window.clipboardData.setData('Text', item.parentNode.childNodes(0).innerText);
 } else return;
}

function getDocHeight(){
 var h;
 if(document.documentElement && document.body){
  h = Math.max(
   document.documentElement.scrollHeight,document.documentElement.offsetHeight,document.body.scrollHeight
  );
 } else h = document.body.scrollHeight;
 return (arguments.length==1) ? h + 'px' : h;
}

my_confirm = function(text,add){
 if(arguments.length==1) text += "削除しますか？";
 if(!confirm(text)) return false;
 else return true;
}

Cookie = { // クッキーの設定，呼び込み，削除
 set : function(name,value,days){
  var exp = "";
  if(days){
   var d = new Date();
   d.setTime(d.getTime()+(days*24*60*60*1000));
   exp = "; expires="+d.toGMTString();
  } else exp = "; expires=Sat, 31-Dec-2005 00:00:00 GMT;";
  document.cookie = name + "=" + escape(value) + exp + "; path=/";
 },
 get : function(name){
  c = document.cookie.split(";");
  for(var i=0;i<c.length;i++){
   index = c[i].indexOf("=");
   if(c[i].substr(0,index)==name||c[i].substr(0,index)==" "+name)return unescape(c[i].substr(index+1));
  }
  return '';
 },
 del : function(name) { Cookie.set(name,'',-1); }
}

toggle = function(el){
 if(el.childNodes[2].style.display=='none'){
  el.firstChild.firstChild.nodeValue = '>>記事をたたむ';
  el.childNodes[2].style.display='block';
 } else if(el.childNodes[2].style.display=='block') {
  el.firstChild.firstChild.nodeValue = '続きをよむ>>';
  el.childNodes[2].style.display='none';
 }
}

function my_menu(id, ob){
 var id = id.toLowerCase();
 for(var i=0;i<d.getElementById(id).childNodes.length;i++){
  if(d.getElementById(id).childNodes[i].className=='box-body'){
   var el = d.getElementById(id).childNodes[i]; break;
  } else el = '';
 }
 if(el=='') return;
 var bgPos = ob.style.backgroundPosition;
 ob.style.backgroundPosition = bgPos=='0px 100%' ? '0px 0px' : '0px 100%';
 ob.title = bgPos=='0px 100%' ? '閉じます' : '展 開！';
 el.style.display = el.style.display=="none" ? 'block' : 'none';
}

Caret = { // マウスで指定したポイントあるいは文字列に要素を挿入
 get : function(){
  if(typeof ed == 'undefined'){
   if(d.getElementById('edit')) ed = d.getElementById('edit');
   else ed = d.getElementById('c_com');
  }
  if(ed.createTextRange){
   ed.focus();
   ed.selected = d.selection.createRange().duplicate();
  } else if(d.getSelection && moz){
   ed.selected = ed.value.substring(ed.selectionStart, ed.selectionEnd);
  } else ed.selected = ' ';
  return sel = (ie) ? ed.selected.text : ed.selected;
 },
 set : function(string){
  if(typeof(sel)=='undefined') Caret.get();
  if(ed.createTextRange){
   if(sel.length > 0){
    if(sel.length > 0){
     ed.selected.text = string; ed.selected.select();
    } else ed.value += string;
   } else {
    ed.selected.text += string;
   }
   ed.selected.select();
  } else if(d.getSelection && sel.length>=0 && ed.selectionStart>=0){
    var s = ed.selectionStart;
    ed.value = ed.value.slice(0,s)+ed.value.slice(s).replace(sel, string);
    ed.setSelectionRange(s+string.length, s+string.length);ed.focus();
  } else ed.value += string;
 }
}

hackFirefoxToolTip = function(){
 var imgs = d.getElementsByTagName('IMG');
 var toolTip = d.createElement('DIV');
 var shadow = d.createElement('DIV');
 document.body.appendChild(shadow);
 document.body.appendChild(toolTip);
 
 with (toolTip.style) {
  position = 'absolute';
  backgroundColor =  'ivory';
  border = '1px solid #333';
  padding = '1px 3px 1px 3px';
  font = '500 11px arial';
  zIndex = 10000;
 }
 
 with (shadow.style) {
  position = 'absolute';
  MozOpacity = 0.3;
  MozBorderRadius = '3px';
  background = '#000';
  zIndex = toolTip.style.zIndex - 1;
 }
 
 for(i=0; i<imgs.length; i++){
  if(imgs[i].getAttribute('title') != null || imgs[i].getAttribute('alt') != null){
   imgs[i].onmouseover=function(e){
    var _title = this.getAttribute('title')!=null ? this.getAttribute('title') : this.getAttribute('alt');

    this.setAttribute('title', '');
    
    toolTip.style.left = 20 + d.documentElement.scrollLeft + e.clientX + 'px';
    toolTip.style.top = 10 + d.documentElement.scrollTop + e.clientY + 'px';
    toolTip.innerHTML = _title.replace(/\r\n/g,'<br />').replace(/\s/g,'&nbsp;');
    
    with (shadow.style){
     width = toolTip.offsetWidth -2 + 'px';
     height = toolTip.offsetHeight -2 + 'px';
     left = parseInt(toolTip.style.left) + 5 + 'px';
     top = parseInt(toolTip.style.top) + 5 + 'px';
    }
   }
   imgs[i].onmouseout=function(){
    this.setAttribute('title', toolTip.innerHTML.replace(/<br \/>/g,'&#13;&#10;').replace(/&nbsp;/g,' '));
    toolTip.style.top = '-1000px';
    shadow.style.top = '-1000px';
    toolTip.innerHTML = '';
   }
  }
 }
}

window.onload = function(){
 if(moz) hackFirefoxToolTip();
}
