addEvent(window, 'load',SetListeners);//イベントリスナセット
addEvent(window, 'load',extractBlockquoteCitations);//引用もと表記
addEvent(window, 'load',TableSummary);//テーブルのサマリー表記
//addEvent(window, 'load',CommentCoockieSet);//クッキーをフォームにセット


function extractBlockquoteCitations() {	//引用タグ内に引用元を明示。
   if(!document.getElementsByTagName){return;}

    var quotes = document.getElementsByTagName('blockquote');
    for (var i =  quotes.length -1; i > -1; i--) {
       var cite = quotes[i].getAttribute('cite');
       var title = quotes[i].getAttribute('title');
       if (cite && title) {		
          var a = document.createElement('a');
          a.setAttribute('href', cite);
          a.setAttribute('title', title);
          a.appendChild(document.createTextNode(title));
          var p = document.createElement('cite');
          p.appendChild(a);
          quotes[i].appendChild(p);
       }
       else if(title){
          var a = document.createElement('span');
          a.appendChild(document.createTextNode(title));
          var p = document.createElement('cite');
          p.appendChild(a);
          quotes[i].appendChild(p);
	   }
	}
}



function TableSummary() {//テーブルのキャプションをtfootに挿入。
   if(!document.getElementsByTagName){return;}

   var table = document.getElementsByTagName('table');
   for (var i =  table.length -1; i > -1; i--) {
      var summary = table[i].getAttribute('summary');
      if (summary) {
	     var thead = table[i].getElementsByTagName('thead').item(0);
	     var col = thead.getElementsByTagName('th').length;
         var tfoot = document.createElement('tfoot');
	     var trs = document.createElement('tr');
	     var tds = document.createElement('td');
         tds.setAttribute('colSpan',col);
         tds.appendChild(document.createTextNode(summary));
         trs.appendChild(tds);
         tfoot.appendChild(trs);
         table[i].appendChild(tfoot);
	  }
   }
}



//aタグにイベントリスナセット
function SetListeners() {
   if(!document.getElementsByTagName){return;}
   var a = document.getElementsByTagName('a');
   for (var i =  a.length -1; i > -1; i--) {
      var link = a[i];
      var href = link.href;
      var rel = String(link.getAttribute('rel'));
      if (href){
 	     if(window.name == "OpenWin"){
			if (href.match(/(\.gif$|\.jpg$)/)) continue;
			var script = link.getAttribute('onclick');
			if(!script){
 			link.onclick = function(){return false;}
		   	addEvent(link, 'click', ParentWindowOpen ,false);
			}
		 }else{
			if (href.match(/^http:\/\/((saishiki|clap)\.sho-da\.net\/(module\/related|resBBS)\/|mobile.surpara.com\/spi\/list.php)/)) {//関連記事と拍手返信+サーパラサンプルのリンク
 				link.onclick = function(){return false;}
				addEvent(link, 'click', OpenNewWindow ,false);
			}
			else if (href.match(/(\.gif$|\.jpg$)/)) {//画像へのリンク
				if(rel==null){link.setAttribute('rel','lightbox2');}
				else{link.setAttribute('rel','lightbox2['+rel+']');}
				link.onclick = function () {myLightbox.start(this); return false;}//lightbox2をセット
			 }
	  	}
	  }
   }
}


//formのクッキー
/*function CommentCoockieSet(){
   if(!document.getElementsByTagName){return;}
	
	var author = get_cookie('author');
	author = unescape(author);
	var email = get_cookie('email');
	email = unescape(email);
	var url = get_cookie('url');
	url = unescape(url);
	var cookie = get_cookie('cookie');
	var form = document.getElementsByTagName('form');
	for (var i =  form.length -1; i > -1; i--) {
		if(form[i].getAttribute('class')){var form_name = form[i].getAttribute('class');}else{return;}
		if(form_name.match(/CommentForm/)){
			var input = form[i].getElementsByTagName('input');
			for (var j =  input.length -1; j > -1; j--) {
				var input_name = input[j].getAttribute('name')
				if(input_name.match(/author/)){input[j].value=author;}
			    else if(input_name.match(/email/)){input[j].value=email;}
			    else if(input_name.match(/url/)){input[j].value=url;}
			    else if(input_name.match(/bakecookie/)){
					input[j].setAttribute('type','checkbox');
					input[j].setAttribute('id','cookie'+i);
					var li = input[j].parentNode;
					var label = document.createElement('label');
					label.setAttribute('for','cookie'+i);
					label.appendChild(document.createTextNode('Cookie?'));
					li.insertBefore(label,li.firstChild);
					if(cookie == 1){input[j].setAttribute('checked','checked');}

				}
			}
		    addEvent(form[i], 'submit', CookieSetReset ,false);

		}
	}
}


function CookieSetReset(e){
   if(!document.getElementsByTagName){return;}
var target_node = GetTargetNode(e);
var input_name;
   var input = target_node.getElementsByTagName('input');
   for (var i =  input.length -1; i > -1; i--) {
	   input_name = input[i].getAttribute('name');
	   if(input_name.match(/author/)){var author=input[i].value;}
	   else if(input_name.match(/email/)){var email=input[i].value;}
	   else if(input_name.match(/url/)){var url=input[i].value;}
	   else if(input_name.match(/bakecookie/) && input[i].checked){var cookie = 1;}
	   else{var cookie = 0;}
   }
   if(cookie == 1){
    author = escape(author);
    email = escape(email);
    url = escape(url);
	set_cookie('author',author);
	set_cookie('email',email);
	set_cookie('url',url);
	set_cookie('cookie',cookie);
   }else{
	set_cookie('author','');
	set_cookie('email','');
	set_cookie('url','');
	set_cookie('cookie','');
   }
}
*/

// クッキー汎用スクリプト

function get_cookie(cn) {
   get_data = document.cookie;
   cv = new Array();
   gd = get_data.split(";");
   for (i in gd) {
      a = gd[i].split("=");
      a[0] = a[0].replace(" ","");
      cv[a[0]] = a[1];
   }
   if (cv[cn]) return cv[cn];
   else return "";
}

function set_cookie(cn,val) {
   ex = new Date();
   ex = new Date(ex.getTime() + (1000 * 60 * 60 * 24 * 60));
   y = ex.getYear(); if (y < 1900) y += 1900;
   hms = ex.getHours() + ":" + ex.getMinutes() + ":" + ex.getSeconds();
   p = String(ex).split(" ");
   ex = p[0] + ", " + p[2] + "-" + p[1] + "-" + y + " " + hms + " GMT;";
   document.cookie = cn + "=" + val +"; expires=" + ex;
}

// Copyright (C) 2001-2002 suepon , All rights reserved.
// Script found at http://suepon.com/

/*クッキー汎用スクリプトココマデ*/


function OpenNewWindow(e){//基本小窓表示のリンク。
   var target_node = GetTargetNode(e);
   var URL;
   
   URL = target_node.href;
   var width = "510";
   var height = "520";
   var st = "toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,";
   window.open(URL, "OpenWin", st+"width="+width+",height="+height);
   return false;

}
function ParentWindowOpen(e){ //別窓内からのリンクを親窓もしくは新規窓で開く。
   var target_node = GetTargetNode(e);
   var URL;
  
   URL = target_node.href;
   if ( window.opener ){
	  window.opener.location.href = URL;
      window.opener.focus();
      return false;
   }else{
      window.open(URL,"opener","");
      return false;
   }

}
function ReturnParentWindow(e){ //別窓から復帰。
   var target_node = GetTargetNode(e);
   var URL = "http://saishiki.sho-da.net/";
   var referrer = document.referrer;
   if ( window.name != "OpenWin"){//別窓を開いていないなら
	   if(referrer.match(/^http:\/\/.*\.sho-da\.net\//)){
		   document.location.href = referrer;
	   }else{
		   document.location.href = URL;
	   }
   }else if ( !window.opener || window.opener.closed ){
       window.open(URL,"opener","");
	   window.close();
   }else if (referrer.match(/^http:\/\/.*\.sho-da\.net\//)) {//親ウインドウに自サイトが表示されていれば
	  window.close();
      window.opener.focus();
   }else{
      window.opener.location.href = URL;
	  window.close();
      window.opener.focus();
   }
}

function GetTargetNode(e){//イベントの起こったノードを取得します。
   var target_node;
   if(e.target){
      target_node = e.target;
   }else{
      target_node = e.srcElement;
   }
   if(target_node.nodeType==3){
      target_node = target_node.parentNode;
   }
   return target_node;
}


//イベントリスナ

function addEvent(obj, eventType, func) {
    if (obj.addEventListener) {
        obj.addEventListener(eventType, func, false);
    } else if (obj.attachEvent) {
        /* WinIE5-6用 */
        obj.attachEvent('on' + eventType, func);
    } else {
        /* MacIE5用 */
		obj["on"+eventType] = func;
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function keyClear(){
   var form = document.SEARCH_FORM;
      if ( form.searchword.value == "キーワードを記入" ){
         form.searchword.value = ""; 
   }
}
//thumbpop
function winOpen(url,w,h) {
  var winFig;
  var width = parseInt(w);
  var height = parseInt(h);
  st = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,";

if(url == "SPI"){//SPI配信リストの場合
  st = "toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,";
  window.open("http://mobile.surpara.com/spi/list.php?id=22967", "OpenWin", st+"width="+width+",height="+height);
  return(false);
}

else if(url == "resBBS"){//返信ＢＢＳの場合
  st = "toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,";
  window.open("http://clap.sho-da.net/resBBS/resBBS.cgi?s=1", "OpenWin", st+"width="+width+",height="+height);
  return(false);
}
else if(url == "related"){//関連記事の場合
  var width = "450";
  var height = "520";
  var address = "http://saishiki.sho-da.net/module/related/"+w;
  st = "toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,";
  window.open(address, "OpenWin", st+"width="+width+",height="+height);
  return(false);
}
  winFig = window.open("", "OpenWin", st+"width="+width+",height="+height);
  winFig.document.open();
  winFig.document.writeln("<html>");
  winFig.document.writeln("<head><title>image window</title></head>");
  winFig.document.writeln('<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">');
  winFig.document.writeln('<a href="javascript:void(0);" onClick="self.window.close()"><img src="'+url+'" border="0" /></a>');
  winFig.document.writeln("</body>");
  winFig.document.writeln("</html>");
  winFig.document.close();

  var Win   = navigator.userAgent.indexOf("Win") != -1   ? true : false;
  var Mac   = navigator.userAgent.indexOf("Mac") != -1   ? true : false;

  if(Win) {
    if(navigator.userAgent.indexOf("Gecko")!=-1){
      winFig.location.reload();
    }
  }
  return(false);
}
