/* JavaScript scripts by martin $ 2005/05/08 02:03:06
*/
var d = document;
var ie = d.selection ? 1 : 0;
var moz = (d.getSelection && !window.opera) ? 1 : 0;

function insertLink(target){
 var t = d.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(ie){
  if(data) window.clipboardData.setData('Text', data);
  else window.clipboardData.setData('Text', item.parentNode.childNodes(0).innerText);
 } else return;
}

function getClientWidth(){
 if(self.innerWidth){
  return self.innerWidth;
 } else if(d.documentElement && d.documentElement.clientWidth){
  return d.documentElement.clientWidth;
 } else if(d.body){
  return d.body.clientWidth;
 }
}

function getClientHeight(){
 if(self.innerHeight){
  return self.innerHeight;
 } else if(d.documentElement && d.documentElement.clientHeight){
  return d.documentElement.clientHeight;
 } else if(d.body){
  return d.body.clientHeight;
 }
}

function getDocHeight(){
 var h;
 if(d.documentElement && d.body){
  h = Math.max(
   d.documentElement.scrollHeight,d.documentElement.offsetHeight,d.body.scrollHeight
  );
 } else h = d.body.scrollHeight;
 return (arguments.length==1) ? h + 'px' : h;
}

function getEventPageX(ev){
 if(ev.pageX){
  return ev.pageX;
 } else if(ev.clientX){
  if(d.documentElement && typeof d.documentElement.scrollLeft != "undefined"){
   return d.documentElement.scrollLeft + ev.clientX;
  } else if(d.body && typeof d.body.scrollLeft != "undefined"){
   return d.body.scrollLeft + ev.clientX;
  }
 }
 return 0;
}

function getEventPageY(ev){
 if(ev.pageY){
  return ev.pageY;
 } else if(ev.clientY){
  if(d.documentElement && typeof d.documentElement.scrollTop != "undefined"){
   return d.documentElement.scrollTop + ev.clientY;
  } else if(d.body && typeof d.body.scrollTop != "undefined"){
   return d.body.scrollTop + ev.clientY;
  }
 }
 return 0;
}

function getScrollY(){
 if(d.documentElement && typeof d.documentElement.scrollTop != "undefined"){
  return d.documentElement.scrollTop;
 } else if(d.body && typeof d.body.scrollTop != "undefined"){
  return d.body.scrollTop;
 } else if(typeof window.pageYOffset != "undefined"){
  return window.pageYOffset;
 }
 return 0;
}

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(e){
 var imgs = d.getElementsByTagName('IMG');
 var toolTip = d.createElement('DIV');
 var shadow = d.createElement('DIV');
 d.body.appendChild(shadow);
 d.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 + e.pageX + 'px';
    toolTip.style.top = 10 + e.pageY + '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 = '';
   }
  }
 }
}

function httpRequest(url, info){ // AJAX
 var request, d = document;
 if(window.XMLHttpRequest){
  request = new XMLHttpRequest();
 } else if(window.ActiveXObject){
  try {
   request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(e){
   try {
    request = new ActiveXObject("Microsoft.XMLHTTP");
   } catch(e){
    request = false;
   }
  }
 }
 if(request){
  request.onreadystatechange = function(){
   if(request.readyState==4 && request.status==200){
    d.getElementById(info).appendChild(d.createTextNode(request.responseText));
   }
  }
  request.open("GET", url, true);
  request.send(null);
 }
}

var imgPop = null;

imagePop = function (e, path, w, h){
 if(imgPop==null){
  imgPop = d.createElement("IMG");
  imgPop.src = path;
  with (imgPop.style){
   position = "absolute";
   left = Math.round((getClientWidth()-w) / 2) + "px";
   top = Math.round((getClientHeight()-h) / 2 + getScrollY()) + "px";
   margin = "0";
   zIndex = 1000;
   border = "4px groove Teal";
   display = "none";
  }
  d.body.appendChild(imgPop);
  if(imgPop.complete){
   imgPop.style.display = "block";
  } else window.status = "画像読み込み中…";
  imgPop.onload = function(){imgPop.style.display="block"; window.status="";}
  imgPop.onclick = function(){d.body.removeChild(imgPop);imgPop=null;}
  imgPop.title = "マウスクリックで閉じます";
 }
}

window.onload = function(){
 if(moz) hackFirefoxToolTip();
}
