var oR2S = new R2S();

function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime()/100000;
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
JSONscriptRequest.prototype.removeScriptTag = function () {
	if (this.scriptObj) {
        this.headLoc.removeChild(this.scriptObj);
        this.scriptObj = null;
	}
}

JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}

/*----------------------------------------------*/

function R2S() {
	this.cols = Array('uri','date','title','desc');
	if (!r2s.id)
	  alert('R2S Error! :: ID設定がありません');
	if (!r2s.type) r2s.type= 1;
	if (!r2s.row) r2s.row= 10;
	if (!r2s.fs) r2s.fs= 10;
	if (!r2s.width) r2s.width=250;
	//if (!r2s.height) r2s.height=200;
	if (!r2s.descWidth) r2s.descWidth=300;
	if (!r2s.color) r2s.color='#0000CC';
	if (!r2s.bgColor) r2s.bgColor='#FFFFFF';
	if (!r2s.bdColor) r2s.bdColor='#666666';
	//if (!r2s.title_len) r2s.title_len=256;
	if (!r2s.text_len) r2s.text_len=256;
	if (!r2s.dSprt) r2s.dSprt='/';
	if (!r2s.bdStyle) r2s.bdStyle='solid';
	if (!r2s.bdWidth) r2s.bdWidth=1;
	this.uri = 'http://satesys.serio.ne.jp/jsonp/r2s/'+r2s.id+'?';
	this.oJsr = new JSONscriptRequest(this.uri);
}

R2S.prototype.callFeed = function (channel, item) {
  this.channel = channel;
  this.item = item;
  var cr = document.createElement('div');
  var a = document.createElement('a');
  a.setAttribute('href', 'http://satesys.net/');
  a.setAttribute('target', '_blank');
  a.appendChild(document.createTextNode('R2S by Serio-net...'));
  cr.style.cssText = 'text-align:right;';
//  a.style.color = '#FFFFFF';
  cr.style.fontSize = '9pt';
  cr.style.backgroundColor = r2s.bdColor;
  if(r2s.bdColor.substring(0, 1) == '#') {
    if(r2s.bdColor.length == 4 && parseInt('0X'+r2s.bdColor.substring(1, 2).toUpperCase())+parseInt('0X'+r2s.bdColor.substring(2, 3).toUpperCase())+parseInt('0X'+r2s.bdColor.substring(3, 4).toUpperCase()) > 28)
        a.style.color = '#000000';
	else if(r2s.bdColor.length == 7 && parseInt('0X'+r2s.bdColor.substring(1, 3).toUpperCase())+parseInt('0X'+r2s.bdColor.substring(3, 5).toUpperCase())+parseInt('0X'+r2s.bdColor.substring(5, 7).toUpperCase()) > 447)
        a.style.color = '#000000';
    else
      a.style.color = '#FFFFFF';
//  } else if(r2s.bdColor.substring(0, 3) == 'rgb') {
  } else {
    cr.style.backgroundColor = '#666666';
    a.style.color = '#FFFFFF';
  }
  cr.appendChild(a);
  if (this.custom) {
    var sniNode = document.getElementById('r2s_in');
    if (! sniNode)
      sniNode = this.snNode;
    if (!this.code) this.code = sniNode.innerHTML.replace('%7B','{').replace('%7D','}');
    var html = Array();
    for (var i=0, post; post = this.item[i]; i++) {
      if(r2s.row && i >= r2s.row)
        break;
      html[i] = this.code;
      for (var j=0, col; col = this.cols[j]; j++) {
        html[i] = html[i].replace('{'+col+'}', (col=='date') ? (new Date(post.date*1000)).getYmd(r2s.dSprt) : 
		(col=='title') ? ((r2s.title_len > 0 && post.title.length > r2s.title_len) ? post.title.substring(0, r2s.title_len) + '...' : post.title) : 
		(col=='desc') ? ((r2s.text_len > 0 && post.desc.length > r2s.text_len) ? post.desc.substring(0, r2s.text_len) + '...' : post.desc) : 
		post[col]);
      }
    }
/*
    var bstyle = '';
    if (r2s.height>0) bstyle += 'overflow:auto;';
    bstyle += 'height:' + (r2s.height ? r2s.height-10 : 150) + 'px;';
    sniNode.style.cssText = bstyle;
*/
    sniNode.style.display = 'block';
    sniNode.innerHTML = html.join('');
  } else {
    var box = document.createElement('div');
    for (var i=0, post; post = this.item[i]; i++) {
      if(r2s.row && i >= r2s.row)
        break;
      var line = document.createElement('div');
      var a = document.createElement('a');
      a.style.color = r2s.color ? r2s.color : '#000000';
      a.setAttribute('href', post.uri);
      title = (r2s.title_len > 0 && post.title.length > r2s.title_len) ? post.title.substring(0, r2s.title_len) + '...' : post.title;
      a.innerHTML = '['+ (new Date(post.date*1000)).getYmd(r2s.dSprt) +'] '+ title;

      if (r2s.type == 2) {
        a.setAttribute('name', i);
        a.onmouseover = function(){
          var top = this.offsetTop + Math.round(r2s.fs*1.5);
          document.getElementById('r2s'+this.name).style.top = ((r2s.height && top > (r2s.height*1+10)) ? r2s.height*1+10 : top) + 'px';
          document.getElementById('r2s'+this.name).style.display = 'block';
        };
        a.onmouseout = function(){
          document.getElementById('r2s'+this.name).style.display = 'none';
        }
      }
      line.appendChild(a);
      box.appendChild(line);
      if (r2s.type == 2) {
          var desc = document.createElement('div');
          var dstyle = 'position:absolute; padding:3px;';
          if (r2s.text_align) dstyle += 'text-align:'+r2s.text_align+';';
          desc.style.cssText = dstyle;
          desc.style.backgroundColor = '#EEF2FF';
          desc.style.borderColor = r2s.bdColor;
          desc.style.borderStyle = r2s.bdStyle;
          desc.style.borderWidth = r2s.bdWidth + 'px';
          desc.style.width = r2s.descWidth + 'px';
          desc.style.display = 'none';
          desc.id = 'r2s'+i;
          desc.innerHTML = (r2s.text_len > 0 && post.desc.length > r2s.text_len) ? post.desc.substring(0, r2s.text_len) + '...' : post.desc;
          this.snNode.appendChild(desc);
      }
    }
    var bstyle = '';
    if (r2s.text_align) bstyle += 'text-align:'+r2s.text_align+';';
    if (r2s.height>0) bstyle += 'overflow:auto;';
    bstyle += 'height:' + (r2s.height ? r2s.height-10 : 150) + 'px;';
    box.style.cssText = bstyle;
    this.snNode.appendChild(box);
  }
  this.snNode.style.fontSize = r2s.fs + 'pt';
  this.snNode.style.width = r2s.width + 'px';
  this.snNode.style.backgroundColor = r2s.bgColor;
  this.snNode.style.borderColor = r2s.bdColor;
  this.snNode.style.borderStyle = r2s.bdStyle;
  this.snNode.style.borderWidth = r2s.bdWidth + 'px';
  this.snNode.appendChild(cr);
  this.oJsr.removeScriptTag();
}

R2S.prototype.refresh = function () {
  this.callFeed(this.channel, this.item);
}


Date.prototype.getYmd = function(p) {
  var y = this.getFullYear() % 100;
  if (y < 10) y = '0'+ y;
  var m = this.getMonth() + 1;
  if (m < 10) m = '0'+ m;
  var d = this.getDate();
  if (d < 10) d = '0'+ d;
  return y + p + m + p + d;
};


(function() {
//  if(document.getElementById('r2s_cr'))
    document.getElementById('r2s_cr').style.display = 'none';
  var style = 'position:relative;';
  this.snNode = document.getElementById('r2s_box');
  if (!this.snNode) {
    if (!r2s.noTopMargin) document.write('<br />');
    document.write('<div id="r2s_box" style="'+style+'"></div>');
    oR2S.snNode = document.getElementById('r2s_box');
  } else oR2S.custom = 1;
  oR2S.oJsr.buildScriptTag();
  oR2S.oJsr.addScriptTag();
})();



