var r2s = new Object();
var oNPR = new NPR();
var oR2S = new R2S();



/*------------------------------------------------------------
	JSONP
------------------------------------------------------------*/


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);
}

JSONscriptRequest.prototype.setUrl = function (fullUrl) {
    this.fullUrl = fullUrl;
}



/*------------------------------------------------------------
	Nanoha Portal Reader
------------------------------------------------------------*/


function NPR() {
	this.cols = Array('uri','date','title','desc');
//初期設定
	this.uri = 'http://kaho.itjp.net/nanoha/RSSidx.php'+'?';
	this.oJsr = new JSONscriptRequest(this.uri);
	this.r2sUri = 'http://satesys.serio.ne.jp/jsonp/r2s/';
	this.feeds = new Object;
}


NPR.prototype.readIndex = function (mode) {
  switch (mode) {
    case 'site':
      this.dispIndex('sites');
    break;
    case 'date':
      this.makeUpdIndex();
      this.dispIndex('updidx');
    break;
    default:
      this.callbackfunc = 'this.dispIndex();this.makeHome()';
      oNPR.oJsr.buildScriptTag();
      oNPR.oJsr.addScriptTag();
    break;
  }
}


NPR.prototype.readItem = function (sid) {
  var nd = document.createElement('div');
  this.dispfid = (this.data)[sid].rss[0];
  if (this.feeds[this.dispfid]) {
    this.dispSubject();
  } else {
    this.callbackfunc = 'this.dispSubject()';
    oNPR.oJsr.setUrl(this.r2sUri+(this.data)[sid].id+'-'+(this.data)[sid].rss[0]+'?');
    oNPR.oJsr.buildScriptTag();
    oNPR.oJsr.addScriptTag();
  }
}


NPR.prototype.previewItem = function (sid) {
  if (document.getElementById('pv'+sid).style.display != 'none') {
    document.getElementById('pv'+sid).style.display = 'none';
    return false;
  }
  var nd = document.createElement('div');
  this.dispfid = (this.data)[sid].rss[0];
  if (this.feeds[this.dispfid]) {
    this.dispPreview(sid)
  } else {
    this.callbackfunc = 'this.dispPreview(\''+sid+'\')';
    oNPR.oJsr.setUrl(this.r2sUri+(this.data)[sid].id+'-'+(this.data)[sid].rss[0]+'?');
    oNPR.oJsr.buildScriptTag();
    oNPR.oJsr.addScriptTag();
  }
}


/*----------------------------------------------------------*/


NPR.prototype.makeHome = function (sid) {
  this.updOrder = new Array();
  var i = 0;
  for (var sid in this.data) {
    this.updOrder[i++] = {'sid':this.data[sid].id, 'update':this.data[sid].date};
  }
  this.updOrder.sort(cmp_update);
  this.dispHome();
}

NPR.prototype.makeUpdIndex = function (cls) {
  for (var i=0, line; line = this.index.type[i]; i++) {
    var tmp = new Array();
    for (var j=0, sid; sid = line.sites[j]; j++) {
      if(!(this.data)[sid])
        continue;
      tmp[j] = {'sid':this.data[sid].id, 'update':this.data[sid].date};
    }
    tmp.sort(cmp_update);
    line.updidx = new Array();
    for (var j=0, site; site = tmp[j]; j++) {
      line.updidx[j] = site.sid;
    }
  }
}

NPR.prototype.refresh = function () {
  this.callSite(this.data);
}


/*----------------------------------------------------------*/

NPR.prototype.dispIndex = function (index) {
  if(!index) {index = 'sites'}
  this.npNodeL.innerHTML = '';
  var a = document.createElement('a');
  a.href = '#';
  a.innerHTML = '[新着記事] ';
  a.onclick = function() {oNPR.dispHome(); return false;};
  this.npNodeL.appendChild(a);

  this.npNodeL.appendChild(document.createTextNode('[ '));

  var a = document.createElement('a');
  a.href = '#';
  a.innerHTML = 'サイト順';
  a.onclick = function() {oNPR.readIndex('site'); return false;};
  this.npNodeL.appendChild(a);

  this.npNodeL.appendChild(document.createTextNode(' | '));

  var a = document.createElement('a');
  a.href = '#';
  a.innerHTML = '更新順';
  a.onclick = function() {oNPR.readIndex('date'); return false;};
  this.npNodeL.appendChild(a);
  
  this.npNodeL.appendChild(document.createTextNode(' ]'));

  var ul = document.createElement('ul');
  for (var i=0, line; line = this.index.type[i]; i++) {
    var li = document.createElement('li');
    li.appendChild(document.createTextNode(line.label));
    var ul2 = document.createElement('ul');
    for (var j=0, sid; sid = line[index][j]; j++) {
		if(!(this.data)[sid])
          continue;
	    var li2 = document.createElement('li');
        var a = document.createElement('a');
        a.href = '#';
	    a.appendChild(document.createTextNode((this.data)[sid].title));
        a.name = sid;
        a.onclick = function() {oNPR.readItem(this.name); return false;};
        li2.appendChild(a);
        ul2.appendChild(li2);
    }
    li.appendChild(ul2);
    ul.appendChild(li);
  }
  this.npNodeL.appendChild(ul);
}

var cf = 0;

NPR.prototype.dispHome = function (sid) {
  this.npNodeU.innerHTML = '';
  this.npNodeD.innerHTML = '';

  var end = 25;
  this.npNodeU.innerHTML = '<h4>Blog新着' + end + '件</h4>日付/タイトルをクリックすると記事の詳細を表示します。<br />日付/タイトル/記事をクリックすると、折りたたみます。<br />全サイトの更新情報は<a href="#bottom">下のアンテナ</a>へどうぞ';
  var dl = document.createElement('dl');
  for (var i=0, sid;( i < end) && (sid = this.updOrder[i].sid); i++) {
    if (this.data[sid].date*1000 > (new Date()).getTime()) {
      end++; continue;
    }
    var dt = document.createElement('dt');
    var a = document.createElement('a');
    a.href = this.data[sid].url;
    a.target = '_blank';
    a.innerHTML = this.data[sid].title;
    a.onclick = function() {cf = 1;};
	var T = new Date(this.data[sid].date*1000);
    dt.appendChild(document.createTextNode('['+ T.getYmd('/') + ' ' + T.getHis() +'] '));
    dt.appendChild(a);
    dt.appendChild(document.createTextNode(' - ' + this.data[sid].headline.replace(/\[RSS\]/, "")));
/*
    var a = document.createElement('a');
    a.href = this.data[sid].url;
    a.innerHTML = this.data[sid].title;
    dt.appendChild(a);
*/
    dt.name = sid;
    dt.onclick = function() {
		if (cf) {cf = 0;}
		else {oNPR.previewItem(this.name);}
	};
    dt.style.backgroundColor = '#EEE';
    dt.onmouseover = function() {this.style.backgroundColor = '#EFF'};
    dt.onmouseout = function() {this.style.backgroundColor = '#EEE'};
    dl.appendChild(dt);
    var dd = document.createElement('dd');
    dd.id = 'pv'+sid;
    dd.style.display = 'none';
    dd.name = sid;
    dd.onclick = function() {
		if (cf) {cf = 0;}
		else {oNPR.previewItem(this.name);}
	};
    dl.appendChild(dd);
  }
  this.npNodeD.appendChild(dl);
}


NPR.prototype.dispSubject = function () {
  this.npNodeU.innerHTML = '';
  this.npNodeD.innerHTML = '';
  var channel = this.feeds[this.dispfid].channel;
  var item = this.feeds[this.dispfid].item;

  var site = (this.data)[channel.sid];
  var h4 = document.createElement('h4');
  var a = document.createElement('a');
  a.href = site.url;
  a.target = '_blank';
  a.innerHTML = site.title;
  h4.appendChild(a);
  this.npNodeU.appendChild(h4);

  var dl = document.createElement('dl');
  for (var i=0, line; line = item[i]; i++) {
    var dt = document.createElement('dt');
    var a = document.createElement('a');
    a.href = line.uri;
    a.innerHTML = line.title;
    a.target = '_blank';
    dt.appendChild(document.createTextNode('['+ (new Date(line.date*1000)).getYmd('/') +'] '));
    dt.appendChild(a);
    dl.appendChild(dt);
    var dd = document.createElement('dd');
    dd.innerHTML = line.desc.replace(/\t/g,"<br />\n");
    dl.appendChild(dd);
  }
  this.npNodeD.appendChild(dl);
}


NPR.prototype.dispPreview = function (sid) {
  var item = this.feeds[this.dispfid].item;
  var dd = document.getElementById('pv'+sid);
  dd.innerHTML = item[0].desc.replace(/\t/g,"<br />\n") + '<br />';
  var a = document.createElement('a');
  a.href = item[0].uri;
  a.target = '_blank';
  a.innerHTML = '[ 記事を読む ]';
  a.onclick = function() {cf = 1;};
  dd.appendChild(a);
  dd.style.display = 'block';

}


/*------------------------------------------------------------*/

NPR.prototype.callSite = function (json) {
  this.data = json;
//  this.oJsr.removeScriptTag();
}


NPR.prototype.callIndex = function (json) {
  this.index = json;
  this.oJsr.removeScriptTag();
  this.callback();
}

/*------------------------------------------------------------*/

NPR.prototype.callback = function () {
  eval(this.callbackfunc+';');
  this.callbackfunc = '';
}


NPR.prototype.sortIndex = function () {
  var obj = this.data;
//alert(obj);
  return obj.sort(cmp_update);
}


function cmp_update(a, b) {
  return b.update - a.update;
}



/*------------------------------------------------------------
	R2S API
------------------------------------------------------------*/


function R2S() {
}

R2S.prototype.callFeed = function (channel,item) {
  oNPR.feeds[channel.pid] = {'channel':channel,'item':item};
  oNPR.callback();
}


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;
};

Date.prototype.getHis = function() {
  var H = this.getHours();
  if (H < 10) H = '0'+ H;
  var i = this.getMinutes();
  if (i < 10) i = '0'+ i;
  var s = this.getSeconds();
  if (s < 10) s = '0'+ s;
  return H + ':' + i + ':' + s;
};



/*------------------------------------------------------------
	Loader
------------------------------------------------------------*/


(function() {
  var style = 'position:relative;';
  document.write('<div id="NPR_box" style="'+style+'"></div>');
  oNPR.npNode = document.getElementById('NPR_box');

  var nd = document.createElement('div');
  nd.id = 'NPR_l';
  nd.style.cssText = 'width:200px;float:left;border:1px solid #000;background-color:#fff;height:600px;overflow:auto;';
  oNPR.npNode.appendChild(nd);
  var nd = document.createElement('div');
  nd.style.cssText = 'margin-left:200px;';
  nd.id = 'NPR_u';
  oNPR.npNode.appendChild(nd);
  var nd = document.createElement('div');
  nd.style.cssText = 'margin-left:200px;';
  nd.id = 'NPR_d';
  oNPR.npNode.appendChild(nd);
  var nd = document.createElement('div');
  nd.style.cssText = 'clear:left';
  oNPR.npNode.appendChild(nd);

  oNPR.npNodeL = document.getElementById('NPR_l');
  oNPR.npNodeU = document.getElementById('NPR_u');
  oNPR.npNodeD = document.getElementById('NPR_d');

  oNPR.readIndex();
})();




