﻿function $(id){ return document.getElementById(id) }
function _(evt){ return evt.target || evt.srcElement }


function AccessBest(){
  this.root   = 'http://www2.mukairiku.net/AccessBest/output.cgi';
  this.url    = location.href;
  this.title  = document.title;
  this.id     = 0;
  this.border = '#A3B8CC';
  this.bord   = '#DAE0E6';
  this.color  = '#333333';
  this.size   = 15
  this.width  = 180
  
  this.getList = function(){
    var collurl = this.root+'?id='+escape(this.id)+'&url='+escape(this.url)+'&title='+escape(this.title)+'&color='+escape(this.color)+'&border='+escape(this.border)+'&bord='+escape(this.bord)+'&size='+this.size+'&width='+this.width;
    
    if(window.opera){  // Opera 用のコード
      var img = document.createElement("img");
      img.onerror = function(e){
                      var s     = document.createElement("script");
                      s.type    = "text/javascript";
                      s.charset = "utf-8";
                      s.id      = 'AccessBestCollScript';
                      s.src     = collurl;
                      $("AccessBestContainerScript").appendChild(s); // jsonp is invoked here
                    }; 
      img.width = 0; 
      img.height = 0; 
      img.src = collurl; 
      $("AccessBestContainerScript").appendChild(img); 
      
    }else if(navigator.userAgent.indexOf('Gecko/') != -1){  // Firefox 用のコード
      var script     = document.createElement('script'); 
      script.type    = "text/javascript"; 
      script.charset = 'UTF-8';
      script.id      = 'AccessBestCollScript';
      script.src     = collurl;
      document.getElementsByTagName('head')[0].appendChild(script);
//      $("AccessBestContainerScript").appendChild(script);
      
//      var img    = document.createElement('img');
//      img.width  = 0; 
//      img.height = 0; 
//      img.src    = 'http://www2.mukairiku.net/AccessBest/toumei.GIF'; 
//      img.load   = document.getElementsByTagName('head')[0].appendChild(script);
//      setTimeout($("AccessBestContainerScript").appendChild(img), 10); 
      
//      var func = function(){document.getElementsByTagName('head')[0].appendChild(script);}
//      var listener = function(event){ return func.call(window, event)};
//      window.addEventListener('load', listener, false);
    
    }else{  // その他のブラウザ用コード
      var script     = document.createElement('script'); 
      script.type    = "text/javascript"; 
      script.charset = 'UTF-8';
      script.id      = 'AccessBestCollScript';
      script.src     = collurl;
      document.getElementsByTagName('head')[0].appendChild(script);
    }

  }
  
  return this;
}


function AccessBestCallback(json){
  var border = json[0].border;
  var bord   = json[0].bord;
  var color  = json[0].color;
  var size   = json[0].size;
  var width  = json[0].width;
  
  var html = "";
  $('AccessBestContainerSpread').innerHTML = '';
  html = '<table border="1" style="width:'+width+'; background-color:'+bord+'; border-width:1px; border-color:'+border+'; border-style:solid; border-collapse:collapse; font-size:'+size+'px;"><tr><td colspan="2" align="center" style="background-color:'+border+'"><strong>人気のエントリー</strong></td></tr>';
  for (var i = 1; i < json.length; i++){
    html += '<tr><td>'+i+'</td><td>'+json[i].title+'</td></tr>';
  }
  html += '<tr><td colspan="2" align="center" style="background-color:'+border+'; font-size:11px;"><a href="http://www.mukairiku.net/">www.mukairiku.net</a></td></tr></table>';
  $('AccessBestContainerSpread').innerHTML = html;
}

