
url = '';
window_title = '';
contents_title = '';
contents_subtitle = '';
description = '';

function bookmarkGetInfoInContents() {
  url = window.location;

  window_title = document.title;
  window_title = window_title.replace(/ - Enterprise Watch$/, '');
  window_title = window_title.replace(/^【PC Watch】/, '');
  window_title = window_title.replace(/ - デジカメ Watch$/, '');
  window_title = window_title.replace(/ - AV Watch$/, '');
  window_title = window_title.replace(/ - GAME Watch$/, '');
  window_title = window_title.replace(/ - ケータイ Watch$/, '');
  window_title = window_title.replace(/ - INTERNET Watch$/, '');
  window_title = window_title.replace(/ -BB Watch$/, '');
  window_title = window_title.replace(/^窓の杜 - /, '');
  window_title = window_title.replace(/^ROBOT-(.*)-(.*)-/, '');
  window_title = window_title.replace(/ - 家電Watch$/, '');
  window_title = window_title.replace(/^Car Watch /, '');
  window_title = window_title.replace(/^[ ]+|[ ]+$/g, '');

  var html_src = document.body.innerHTML;
  html_src.match(/<!-- title -->(.*)<!-- \/title -->/im);
  contents_title = RegExp.$1;
  html_src.match(/<!-- subtitle -->([\s\S]*)<!-- \/subtitle -->/im);
  contents_subtitle = RegExp.$1;
  contents_subtitle = contents_subtitle.replace(/[\r\n]/im, ' ');

  var i=0;
  var class_name;
  var p_elements;
  var div_elements = document.getElementsByTagName("div");
  var len = div_elements.length;
  for(i = 0; i < div_elements.length; i++){
    class_name = div_elements[i].getAttribute('class');
    if(class_name == null) class_name = div_elements[i].getAttribute('className');
    if(class_name != 'mainContents') continue;

    p_elements = div_elements[i].getElementsByTagName('p');
    if(p_elements.length > 0){
      description = p_elements[0].textContent;
      if(description == null){
        description = p_elements[0].innerText;
      }
      description = strip_tag(description);
      break;
    }
  }
}

function strip_tag(in_str)
{
  in_str = in_str.replace(/[\r\n]/mg, '');
  in_str = in_str.replace(/>(.*?)</mg, '>\n$1\n<');
  in_str = in_str.replace(/<("[^"]*"|'[^']*'|[^'">])*>/mg, '');
  in_str = in_str.replace(/^[ ]+|[ ]+$/g, '');
  return in_str;
}

function bookmarkAddTwitter(){
  bookmarkGetInfoInContents();
  var bookmarkurl ='http://twitter.com/home?status='+encodeURIComponent(window_title + ' ' + url);
  window.open(bookmarkurl,'_blank');
}

function bookmarkAddGoogle(){
  bookmarkGetInfoInContents();
  var bookmarkurl = 'http://www.google.com/bookmarks/mark?op=edit&bkmk='+encodeURIComponent(url)+'&title='+encodeURIComponent(window_title);
  window.open(bookmarkurl,'_blank');
}

function bookmarkAddDelicious(){
  bookmarkGetInfoInContents();
  var bookmarkurl = 'http://del.icio.us/post?url='+encodeURIComponent(url)+'&title='+encodeURIComponent(window_title);
  window.open(bookmarkurl,'_blank');
}

function bookmarkAppendHatena(){
  bookmarkGetInfoInContents();
  var bookmarkurl = 'http://b.hatena.ne.jp/append?'+url;
  window.open(bookmarkurl,'_blank');
}

function bookmarkEntryHatena(){
    bookmarkGetInfoInContents();
    var bookmarkurl = 'http://b.hatena.ne.jp/entry/'+url;
    window.open(bookmarkurl,'_blank');
}

function bookmarkAddHatena(){
    bookmarkGetInfoInContents();
    var bookmarkurl = 'http://b.hatena.ne.jp/add?mode=confirm&is_bm=1&title='+encodeURIComponent(window_title)+'&url='+encodeURIComponent(url);
    window.open(bookmarkurl,'_blank');
}

function bookmarkAddHatena_sp(){
    bookmarkGetInfoInContents();
    var bookmarkurl = 'http://b.hatena.ne.jp/bookmarklet.touch?btitle='+encodeURIComponent(window_title)+'&url='+encodeURIComponent(url);
    window.open(bookmarkurl,'_blank');
}

function bookmarkAddLivedoor(){
  bookmarkGetInfoInContents();
  var bookmarkurl = 'http://clip.livedoor.com/clip/add?link='+encodeURIComponent(url)+'&title='+encodeURIComponent(window_title); //+'&notes=&tags=&jump=ref&ie=sjis';
  window.open(bookmarkurl,'_blank');
}

function bookmarkAddYahoo(){
  bookmarkGetInfoInContents();
  var bookmarkurl = 'http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t='+encodeURIComponent(window_title)+'&u='+encodeURIComponent(url)+'&opener=bm&ei=UTF-8';
  window.open(bookmarkurl, 'popup', 'width=550px,height=480px,status=1,location=0,resizable=1,scrollbars=0,left=100,top=50');
}

function bookmarkAddFacebook(){
  bookmarkGetInfoInContents();
  var bookmarkurl = 'http://www.facebook.com/plugins/like.php?href='+encodeURIComponent(url);
  window.open(bookmarkurl,'_blank');
//    var bookmarkurl = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(url);
//    window.open(bookmarkurl ,null ,'width=480px,height=480px,status=1,location=0,resizable=1,scrollbars=0,left=100,top=50');
}

// 20110621: Overwrite SocialBookmark Div:takah-ma
var ajaxReq;
var ajaxTargetElem;

function getSocialBookmark(elemName){
  ajaxTargetElem = document.getElementById(elemName);
  ajaxGet('/include/js/social_bookmark_overwrite_utf8.inc');
}

function ajaxLoadSocialBookmark(elemName){
  m_getSocialBookmark(elemName);
}
function m_getSocialBookmark(elemName){
  ajaxTargetElem = document.getElementById(elemName);
  ajaxGet('/include/js/m.social_bookmark_overwrite.inc');
}

function ajaxGet(url){
  if (window.XMLHttpRequest){
    ajaxReq = new XMLHttpRequest();
  }else{
    if (window.ActiveXObject){
      try{
        ajaxReq = new ActiveXObject("Msxml2.XMLHTTP");
      }catch(e){
        try {
          ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e){
          ajaxReq = null;
        }
      }
    }
  }
  ajaxReq.onreadystatechange = ajaxSetResponseText;
  ajaxReq.open("GET", url, true);
  ajaxReq.send(null);
}

function ajaxSetResponseText(){
  entry_url = getCanonicalHref();
  encode_url = encodeURIComponent(entry_url);
  entry_title = document.title;

  if (ajaxReq.readyState == 4 && ajaxReq.status == 200){
    replaceText = ajaxReq.responseText.replace('ENTRYURL', entry_url);
    replaceText = replaceText.replace('ENTRYTITLE', entry_title);
    replaceText = replaceText.replace('ENCODEURL', encode_url);
    replaceText = replaceText.replace('ENCODEURL', encode_url);

    ajaxTargetElem.innerHTML = replaceText;
//    eval(getElementById("social_bookmark").innerHTML);
  }
}

function getCanonicalHref() {
  links = document.getElementsByTagName("link");
  ret = "";

  for(var i = 0, j = links.length; i < j; i++) {
    if(links[i].rel == "canonical") {
      ret = links[i].href;
    } else {
//      return null;
    }
  }
  return ret;
}

function sleep(sleep_out){
if(flg){ return;}
settimeout("sleep()", sleep_out*1000);
flg=1;
limit = 100000000;
for(i=1; i<limit; $i++){/* 何もしないループ */;}
}


