
dom.event.addEventListener(window, 'load', init2);

var thisCookie = ""
var cookie_ary = []

function init2(){
  var URI = "./client/getCheArt.cgi";
  var READYSTATE_COMPLETE = 4;
  var STATUS_OK = 200;

  var request;
  if(window.XMLHttpRequest){
    try{
      request = new XMLHttpRequest();
    } catch (e) {}
  }else if(window.ActiveXObject){
    try{
      request = new ActiveXObject("Msxm12.XMLHTTP");
    } catch (e) {
      try{
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e){}
    }
  }

  var cookieAry = document.cookie.split("; ");
  for(var i = 0; i < cookieAry.length; i++){
    if(cookieAry[i].substr(0,3) == "che"){
      thisCookie = cookieAry[i].split("=")[1]
      if(!thisCookie){thisCookie = '';}
    }
  }
  if(thisCookie.length != 0){
    clearChildNodes(document.getElementById("articles"));
    if (document.URL.charAt(document.URL.length-1).match(/[^//]/)){
      var mode = "search"
      URI = "." + URI
    }else{
      var mode = "index"
    }
    request.open("post", URI, true);
    request.setRequestHeader("Content-Type", "text/plain");
    request.send("ids=" + thisCookie + "&amp;mode=" + mode);

    newobj = new Object;
    request.onreadystatechange = function(){
      if(request.readyState == READYSTATE_COMPLETE){
        if(request.status == STATUS_OK){
          var article = document.getElementById("articles");
          //clearChildNodes(article);
          article.innerHTML = request.responseText;
          cookie_ary = thisCookie.split("-");
          setCheArt_aTag();
          setCheArt_button();
        }else{
        }
      }
    }
  }

}

function setCheArt_aTag(){
  var aTag = document.getElementById("getCheArt").getElementsByTagName("a");
  for(var i = 0; i < aTag.length; i++){
    dom.event.addEventListener(aTag.item(i), 'click', reloadCheArt);
  }
}

function reloadCheArt(){
  setTimeout('init2()', 3000);
}

function setCheArt_button(){
  var button = document.getElementById("getCheArt").getElementsByTagName("button");
  for(var i = 0; i < button.length; i++){
    dom.event.addEventListener(button.item(i), 'click', deleteCheArt);
  }
}

function deleteCheArt(evt){
  var target_obj = dom.event.target(evt);
  var href_str = target_obj.previousSibling.getAttribute("href");
  var this_id = href_str.substring(href_str.indexOf("=")+1, href_str.length) 
  for(var i = 0; i < cookie_ary.length; i++){
    if(cookie_ary[i] == this_id){
      expires_time = new Date();
      day = 24*60*60*1000;
      expires_time.setTime(expires_time.getTime() + day * 7);
      expires_time = expires_time.toGMTString();
      cookie_ary.splice(i,1);
      document.cookie = "che=" + cookie_ary.join("-") + ";expires=" + expires_time + ";path=/;";
      new DeleteObj(target_obj.parentNode, 10).setOpacity();
    }
  }
}

function DeleteObj(t, v){
  this.target = t;
  this.opacity_value = v;
}
DeleteObj.prototype.del = del;
DeleteObj.prototype.setOpacity = setOpacity;
DeleteObj.prototype.setOpacityValue = setOpacityValue;

function del(){
  this.target.style.display = "none";
}

function setOpacity(){
  if(this.opacity_value > 0){
    this.opacity_value--;
    setTimeout(this.setOpacity.bind(this), 80);
    this.setOpacityValue();
  }else{
    this.del();
  }
}

function setOpacityValue(){
    this.target.style.filter = 'alpha(opacity=' + (this.opacity_value * 10) + ')';
    this.target.style.MozOpacity = this.opacity_value / 10;
    this.target.style.opacity = this.opacity_value / 10;
}

function clearChildNodes(elm){
  while(elm.hasChildNodes()){
    elm.removeChild(elm.lastChild);
  }
}

Function.prototype.bind = function(object) {
    var __method = this;
    return function() {
        return __method.apply(object, arguments);
    }
}

