﻿var addEvent = (window.addEventListener) ?
   (function(elm, type, event) {
      elm.addEventListener(type, event, false);
   }) : (window.attachEvent) ?
   (function(elm, type, event) {
      elm.attachEvent('on'+type, event);
   }) :
   (function(elm, type, event) {
      elm['on'+type] = event;
   }) ;

addEvent(window, 'load', function() {
   var as = document.getElementsByTagName('a');
   for(var a, i = 0; a = as[i]; i++) {
      if(a.href.match('^http://(www.)?cadbox.co.jp/') ||
            !a.href.match('^http')
            )
         continue;

      (function(elm) {
         var url = encodeURIComponent(elm.href);
         var xmlhttp = new AJAX.init();
         xmlhttp.callback = function() {
            var response = xmlhttp.getResponse();
            if(!response) return;
            if(response == 'nil') return;

            elm.style.textDecoration = 'line-through';
            //elm.style.backgroundColor = '#FF00FF';

            var nElm;
            if(response == '-') {
               // リンク切れ
               nElm = document.createElement('span');
               nElm.innerHTML = '\u30EA\u30F3\u30AF\u5207\u308C\u3067\u3059';
            } else {
               // もしかして･･･？
               nElm = document.createElement('a');
               nElm.href = response;
               nElm.innerHTML = '\u2192 \u79FB\u8EE2\u306E\u53EF\u80FD\u6027\u304C\u3042\u308A\u307E\u3059';
            }
            elm.parentNode.removeChild(elm);
            //elm.parentNode.insertBefore(nElm, elm.nextSibling);
         }
         xmlhttp.get('/ken-search/link/linkcheck.cgi?url=' + url)
      })(a);
   }
});

var AJAX = {
   init: function(url) {
      this._http = AJAX.object();

      this.get = function(url) {
         this._http.open('GET', url, true);
         this._http.onreadystatechange = this.callback;
         this._http.send('');
      }

      this.getResponse = function() {
         if(this._http.readyState == 4 && (this._http.status == 200 || this._http.status == 304)) {
            return this._http.responseText;
         }
         return;
      }
   },

   object: function() {
      if(document.getElementById) {
         try{
            return new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e){
            try {
               return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E){
               if(typeof XMLHttpRequest != 'undefined') return new XMLHttpRequest;
            }
         }
      }

      return false;
   }
};



