/* JavaScript lib. scripts by martin $ 2005/10/25 18:10:47
*/
var d = document;
var ie = d.selection ? 1 : 0;
var moz = (d.getSelection && !window.opera) ? 1 : 0;
var smilies = {};
var caretSelection = "";

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,l=c.length;i<l;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); }
}

readMore = function(id, self){
 var ob = o(id);
 if(ob.style.display!="block"){
  ob.parentNode.firstChild.title = '記事をたたむ';
  ob.parentNode.firstChild.innerHTML = '&laquo;hide';
  ob.parentNode.firstChild.style.backgroundPosition = "0 100%";
  ob.style.display = "block";
 } else if(ob.style.display=="block") {
  ob.parentNode.firstChild.title = '続きをよむ';
  ob.parentNode.firstChild.innerHTML = '&raquo;more';
  ob.parentNode.firstChild.style.backgroundPosition = "0 0";
  ob.style.display = "none";
 }
}

function toggleMenu(id, ob){
 var p = ob.parentNode.parentNode;
 var t = 'sidebar-'+id;
 for(var i=0,l=p.childNodes.length;i<l;i++){
  if(p.childNodes[i].className && p.childNodes[i].className.indexOf(t) > 0){
   var el = p.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(o('Page1')!=null) ed = o('Page1');
   else if(o('c_com')!=null) ed = o('c_com');
   else ed = d.getElementsByTagName('TEXTAREA')[0];
  }
  if(ie){
   if(!d.selection.createRange()) 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 caretSelection = (ie) ? ed.selected.text : ed.selected;
 },
 set : function(string){
  if(typeof(caretSelection)=='undefined' || typeof(ed)=='undefined') Caret.get();
  if(ed.createTextRange){
   if(caretSelection.length > 0){
    if(caretSelection.length > 0){
     ed.selected.text = string;
     ed.selected.select();
    } else ed.value += string;
   } else {
    ed.focus();
    ed.selected = d.selection.createRange().duplicate();
    ed.selected.text = string;
   }
   if(ed.selected) ed.selected.select();
  } else if(d.getSelection && caretSelection.length>=0 && ed.selectionStart>=0){
    var s = ed.selectionStart;
    ed.value = ed.value.slice(0,s)+ed.value.slice(s).replace(caretSelection, 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');
 
 with (toolTip.style) {
  position = 'absolute';
  backgroundColor =  'ivory';
  border = '1px solid #333';
  padding = '1px 3px 1px 3px';
  font = '500 11px arial';
  zIndex = 10000;
  top = "-100px";
 }
 
 with (shadow.style) {
  position = 'absolute';
  MozOpacity = 0.3;
  MozBorderRadius = '3px';
  background = '#000';
  zIndex = toolTip.style.zIndex - 1;
 }
 
 for(i=0,l=imgs.length; i<l; 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', '');
    _title = _title.replace(/[\r\n]+/g,'<br/>').replace(/\s/g,'&nbsp;');
    if(_title=='') return;
    toolTip.style.left = 20 + e.pageX + 'px';
    toolTip.style.top = 10 + e.pageY + 'px';
    toolTip.innerHTML = _title;
    
    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 = '';
   }
  }
 }
  d.body.appendChild(shadow);
  d.body.appendChild(toolTip);
}

function quotedStyle(elm){
 if(!elm){
  var bq = d.getElementsByTagName("BLOCKQUOTE");
 } else {
  var bq = elm.getElementsByTagName("BLOCKQUOTE");
 }
 for(i=0,l=bq.length;i<l; i++){
  var q = bq[i];
  with (q.style){
   position = "relative";
   padding = "15px 30px 0px 30px";
   height = q.offsetHeight + "px";
  }
  var qtl = d.createElement("SPAN");
  with (qtl.style){
   position = "absolute";
   background = "url(Images/qtl.png) no-repeat 0 0";
   top = left = 0;
   width = height = "24px";
  }
  var qtr = qtl.cloneNode(true);
  var qbl = qtl.cloneNode(true);
  var qbr = qtl.cloneNode(true);
  with (qtr.style){
   background = "url(Images/qtr.png) no-repeat 0 0";
   top = 0;
   left = q.offsetWidth - 12 + "px";
  }
  with (qbl.style){
   background = "url(Images/qbl.png) no-repeat 0 0";
   top = q.offsetHeight - 12 + "px";
   left = 0;
  }
  with (qbr.style){
   background = "url(Images/qbr.png) no-repeat 0 0";
   top = q.offsetHeight - 24 + "px";
   left = q.offsetWidth - 24 + "px";
  }
  q.insertBefore(qtr, q.firstChild); q.insertBefore(qtl, q.firstChild);
  q.insertBefore(qbl, q.firstChild); q.insertBefore(qbr, q.firstChild);
  with (qtl.style){
   position = "absolute";
   background = "url(Images/qtl.png) no-repeat 0 0";
   top = left = 0;
   width = height = "24px";
  }
 }
}

function roundedStyle(elm){
 var elm = elm ? elm : document;
 var rs = d.getElementsByClassName("rounded", elm);
 for(i=0,l=rs.length;i<l;i++){
  var q = rs[i];
  with (q.style){
   position = "relative";
   padding = "6px 6px 6px 50px";
   margin = "20px 10px";
   backgroundColor = "#eee";
   lineHeight = 1.5;
   height = q.offsetHeight + "px";
  }
  var qtl = d.createElement("SPAN");
  with (qtl.style){
   position = "absolute";
   background = "url(Images/rtl.png) no-repeat 0 0";
   top = left = 0;
   width = height = "12px";
  }
  var qtr = qtl.cloneNode(true);
  var qbl = qtl.cloneNode(true);
  var qbr = qtl.cloneNode(true);
  with (qtr.style){
   background = "url(Images/qtr.png) no-repeat 0 0";
   top = 0;
   left = q.offsetWidth - 12 + "px";
  }
  with (qbl.style){
   background = "url(Images/qbl.png) no-repeat 0 0";
   top = q.offsetHeight - 12 + "px";
   left = 0;
  }
  with (qbr.style){
   background = "url(Images/rbr.png) no-repeat 0 0";
   top = q.offsetHeight - 12 + "px";
   left = q.offsetWidth - 12 + "px";
  }
  q.insertBefore(qtr, q.firstChild); q.insertBefore(qtl, q.firstChild);
  q.insertBefore(qbl, q.firstChild); q.insertBefore(qbr, q.firstChild);
  with (qtl.style){
   position = "absolute";
   background = "url(Images/rtl.png) no-repeat 0 0";
   top = left = 0;
   width = height = "12px";
  }
 }
}

var Ajax = {
 request : function(){ var X;
  return (X = window.ActiveXObject) ? 
  new X("Msxml2.XMLHTTP") ? new X("Msxml2.XMLHTTP") : new X("Microsoft.XMLHTTP") :
  new XMLHttpRequest();
 },
 get : function(url, func){
  var r = Ajax.request();
  try {
   r.open("GET", url, true);
   r.onreadystatechange = function(){
    Ajax.debug("Now Loading.. "+ url + " : " + r.readyState);
    if(r.readyState==4){
     if(func){
      func(r.responseText);
     }
     Ajax.debug("");
    }
   }
   r.send(null);
  } catch (e){Ajax.debug(e,1)}
 },
 cloneScript : function(src){
  var id = src.split(/\//).pop().replace(/\./, '_');
  var h = d.getElementsByTagName("head")[0];
  var s = d.createElement("script");
  var r = Ajax.request();
  s.type = "text/javascript";
  s.charset = "euc-jp";
  s.id = id;
  if(!d.getElementById(id)){
   h.appendChild(s);
  } else return true; // cached
  r.open("GET", src, false); // Not async.
  r.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
  r.send(null);
  if(r.readyState==4 && r.status==200|304){ // Opera8.02's "304" is OK
   s.text = r.responseText; // text property!
   return true;
  }
  if(d.getElementById(id)){
   d.getElementById(id).parentNode.removeChild(d.getElementById(id));
   Ajax.debug("404 error : " + src);
  }
  return false;
 },
 debug : function(d, a){!a ? (window.status=d) : alert("Ajax debug: "+d);}
}

comments_in_situ = function(uid, author, ob, e){
 Ajax.cloneScript("ajax.php?type=info");
 var loaded = Ajax.cloneScript('ajax.php?type=json&aim=owner/smiley.ini.php');
 var articleBottom = gc("article-bottom", o("UID"+uid))!=null ?
  gc("article-bottom", o("UID"+uid)) : gc("article-united-bottom", o("UID"+uid));
 var eX = getEventPageX(e);
 if(o("ajaxToolTip")==null){
  toolTip = d.createElement('DIV');
  toolTip.id = "ajaxToolTip";
  with (toolTip.style) {
   position = "absolute";
   backgroundColor =  "ivory";
   border = "1px solid #333";
   padding = "2px 3px";
   width = "140px";
   font = "500 13px MS P Gothic, Osaka";
   textAlign = "center";
   zIndex = 10000;
   top = getEventPageY(e) + "px";
   left = eX + (getClientWidth()/2<eX?-200:200) + "px";
  }
  toolTip.innerHTML = 'コメント読み込み中...';
  d.body.appendChild(toolTip);
 } else {
  toolTip = o("ajaxToolTip");
  toolTip.innerHTML = 'コメント読み込み中...';
  with (toolTip.style){
   display = "block";
   top = getEventPageY(e) + "px";
   left = eX + (getClientWidth()/2<eX?-200:200) + "px";
  }
 }
 var callback = function(data){
  var commentDiv = document.createElement("DIV");
  commentDiv.className = "comment-div";
  commentDiv.style.marginLeft = "13px";
  data = data.replace(
   /\[link:(.+?)\](.+?)\[\/link\]/gm,
   function (s0, s1, s2){
    return '<a href="http://'+s1+'">'+s2+'</a>';
   }
  );
  data = data.replace(/\[q\]`?(.+?)`?\[\/q\]/gm, '<p class="comment-quote">\\$1</p>');
  for (var i in smilies){
   var r = new RegExp(i.toString().replace(/(\W)/, "\\$1"), "g");
   data = data.replace(r, smilies[i]);
  }
  var cmt = '';
  var aj = data.split("\n");
  var ajlen = aj.length;
  if(ajlen > 1){
   var ID = aj[0].split('|')[0];
   for(i=1; i < ajlen-1; i++){
    var a = aj[i].split('|'); // a[1] = submitter
    var d = new Date(a[0]*1000), Y = d.getFullYear(), mn = d.getMonth(), day = d.getDate();
    var h = d.getHours(), mt = d.getMinutes(), s = d.getSeconds();
    h = (h < 10) ? "0"+h : h; mt = (mt < 10) ? "0"+mt : mt; s = (h < 13) ? s+" AM" : s+" PM";
    var u = a[5]!='' ? ' <a href="'+a[5]+'"><img src="Images/home.png" alt="Website" title="Website" class="home-icon" /></a>' : '';
    var cbody = a[3].replace(/`/gm, "<br />");
    if(typeof ADMIN == "undefined") ADMIN = false;
    var cntrl = ADMIN ?
     '<span class="comment-cntrl"><a  href="index.php?mode=edit_comment&amp;UID='+uid+'&amp;CID='+a[0]+'"><img src="Images/edit_comment.png" alt="Edit" title="このコメントの編集" /></a> <a  href="index.php?mode=delete_comment&amp;UID='+uid+'&amp;CID='+a[0]+'" onclick="return my_confirm(\'このコメントを\')"><img src="Images/del_comment.png" alt="Delete" title="このコメントの削除" /></a> ' : '';
    var res = (ADMIN) ?
     '<a href="index.php?mode=res_comment&amp;UID='+uid+'&amp;CID='+a[0]+'"><img src="Images/reply.png" alt="Reply" title="このコメントに返事を書く" /></a></span>' : '';
    cmt += (author==a[1] ? ' <div class="comment-entry-owner">\n' : ' <div class="comment-entry">\n');

    if(author==a[1]){ // owner
     cmt += ('  <p><span class="owner" id="CID'+a[0]+'"><a title="Permalink" href="index.php?UID='+ID+'#CID'+a[0]+'"> <img src="Images/lcomment.png" class="comment-owner" title="Permalink" alt="Owner Comment" /></a> '+a[1]+u+' '+h+':'+mt+':'+s+' '+Y+'/'+mn+'/'+day+'</span></p>\n');
    } else {
     cmt += ('  <p class="comment-index"><span id="CID'+a[0]+'"><a title="Permalink" href="index.php?UID='+ID+'#CID'+a[0]+'">'+i+'.</a></span> '+a[1]+u+' '+h+':'+mt+':'+s+' '+Y+'/'+mn+'/'+day+'</p>\n');
    }
    cmt += ('  <div class="comment-body" style="color:#'+a[2]+';">'+cbody+cntrl+res+'</div>\n');
    cmt += '</div>\n';
    if(i < ajlen-1 ) cmt += '<hr class="comment-separator" />\n';
   }
   cmt += '<div class="commentFormHere"><button type="button" onclick="loadCommentForm('+uid+');return false;" title="コメントする">Add your comment</button></div>\n';
   cmt += '</div><!--#comment-div-->\n';
  }
  commentDiv.innerHTML = cmt;
  articleBottom.appendChild(commentDiv);
  ob.firstChild.data = ob.firstChild.data.replace(/\[\+\]/,"[-]");
  ob.title = "表示したコメントを隠します";
  toolTip.innerHTML = ' コメント読み込み完了！ ';
  setTimeout('toolTip.style.display = "none"', 500);
 } //#callback
 if(articleBottom.innerHTML.match(/comment-div/)){ // already loaded and displayed
  var comment = gc("comment-div", articleBottom);
  toolTip.style.display = "none";
  if(comment.style.display!="none"){
   comment.style.display = "none";
   ob.firstChild.data = ob.firstChild.data.replace(/\[\-\]/,"[+]");
   return;
  } else {
   comment.style.display = "block";
   ob.firstChild.data = ob.firstChild.data.replace(/\[\+\]/,"[-]");
   return;
  }
 }
 if(!loaded){
  return;
 } else {
  Ajax.get('ajax.php?type=text&aim=cmt/'+uid+'.log', callback);
 }
}

function loadCommentForm(uid,ob){
 var loaded1 = Ajax.cloneScript("ajax.php?type=info");
 var loaded2 = Ajax.cloneScript("ajax.php?type=clone&aim=js/comment.js");
 if(loaded1 && loaded2){
  Ajax.get("ajax.php?type=commentform&UID="+uid, function(data){gc("commentFormHere",o("UID"+uid)).innerHTML=data;});
 }
}

function loadArticle(uid, page){
 var type = (page) ? "page" : "article";
 Ajax.get("ajax.php?type="+type+"&UID="+uid+(page?("&page="+page):""), function(data){
  var articleContent = gc("article-content", o("UID"+uid.replace(/d$/,'')));
  articleContent.style.lineHeight = 1.7;
  if(gc("comment-expand", o("UID"+uid))!=null){
   data += '<p class="comment-expand" title="ここでコメントを展開します">'
        + gc("comment-expand", o("UID"+uid)).innerHTML
        + '</p>\n';
  }
  articleContent.innerHTML = data;
  quotedStyle(articleContent);
  roundedStyle(articleContent);
  if(moz) hackFirefoxToolTip();
 });
}

function gc(clsName, pElement){
 var children = pElement.getElementsByTagName("*");
 for(var i=0,l=children.length;i<l;i++){
  if(children[i].className==clsName) return children[i];
 }
 return null;
}

d.getElementsByClassName = function(className, pElement){
 var cls, gets = [];
 var obs = pElement.getElementsByTagName("*");
 for (var i=0,l=obs.length; i < l; i++){
  var cls = obs[i].className.split(/\s+/);
  for (var j=0,k=cls.length; j < k; j++){
   if(cls[j]==className){
    gets.push(obs[i]); break;
   }
  }
 }
 return gets;
}

imagePop = function (e, path, w, h){
 if(typeof imgPop == "undefined") imgPop = null;
 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";
   width = w + "px";
   height = h + "px";
   zIndex = 1000;
   border = "4px groove Teal";
   display = "none";
  }
  d.body.appendChild(imgPop);
  if(imgPop.complete){
   imgPop.style.display = "block";
  } else window.status = "画像読み込み中…";
  addEvent(imgPop, "load", function(){imgPop.style.display="block"; window.status="";});
  addEvent(imgPop, "click", function(){d.body.removeChild(imgPop);imgPop=null;});
  imgPop.title = "マウスクリックで閉じます";
 }
}

initCSSHover = function(){
 if(ie){
  if(o("theme-selector")){
   var root = o("theme-selector");
   addEvent(root, "mouseover", tsOver);
   addEvent(root, "mouseout", tsOut);
  }
 }
}
function tsOver(){this.className+=" over";}
function tsOut(){this.className=this.className.replace(/ over/, "");}
function noCSS(){ var e = d.getElementsByTagName("*");
 for(var i=0,l=d.styleSheets.length;i<l;i++)d.styleSheets.item(i).disabled = true;
 for(i=0,l=e.length;i<l;i++) e[i].style.cssText = "";
}
function addEvent(obj, evType, fn){
 if(!obj["_"+evType]){
  obj["_"+evType] = [];
  if(obj["on" + evType] != null) obj["_"+evType].push(obj["on" + evType]);
  obj["on" + evType] = evokeEvent;
 } else for(var i in obj["_"+evType]) if(obj["_"+evType][i]===fn) return;
 obj["_"+evType].push(fn);
};

function removeEvent(obj, evType, fn){
 if(obj["_"+evType]){
  for(var i in obj["_"+evType]){
   if(obj["_"+evType][i]===fn) delete obj["_"+evType][i];
  }
 }
};

function evokeEvent(e) {
 var e = e || window.event;
 for(var i in this["_"+e.type]) this["_"+e.type][i].apply(this,[e]);
};


function o(id){return d.getElementById(id) ? d.getElementById(id) : null;};

addEvent(window, "load", function (){
 roundedStyle();
 quotedStyle();
 initCSSHover();
 if(moz) hackFirefoxToolTip();
});

