/* 文字コードはUTF-8 */
feedreader = function(html_func){
  this.html_func = html_func;
	this.init = function(tid, xml_url){
		this.tid = tid;
		this.xml_url = xml_url;
		this.count = (typeof(arguments[2]) != 'undefined' ? arguments[2] : 10);
		this.link_type = (typeof(arguments[3]) != 'undefined' ? arguments[3] : '_self');
		this.dateobj = new Date();
		this.now = this.dateobj.getTime();
		this.object = document.getElementById(this.tid);
		this.object.innerHTML = '<div style="font-size:12px;color:#bbb;">LOADING...</div>';
    if (typeof(this.html_func) == 'undefined' || typeof(this.html_func) != 'function') {
      this.buildHtml = this.standard_html;
    } else {
      this.buildHtml = this.html_func;
    }
		this.load();
	}
	this.fail = function(){
		this.object.innerHTML = '<div style="font-size:12px;color:#900;">ERROR</div>';
	}
	this.load = function(){
		xmlhttp.res[this.xml_url] = new Object();
		xmlhttp.res[this.xml_url].pointer = this;
		xmlhttp.res[this.xml_url].exec = function(){
			var html = '';
			var root = xmlhttp.res[this.pointer.xml_url].XML.getElementsByTagName('rss');
			if(root.length > 0){
				var items = root.item(0).getElementsByTagName('item');
				if(items.length > 0){
					var count = 0;
					for(var i = 0; i < items.length; i++){
						var item = items.item(i);
						var link = xmlhttp.getNodeValue(item, 'link');
						if(document.URL.indexOf(link) >= 0){
							continue;
						}
						count += 1;
						if(this.pointer.count < count){
							break;
						}
						var title = xmlhttp.getNodeValue(item, 'title');
						var pubDate = xmlhttp.getNodeValue(item, 'pubDate');
						var pubtime = Date.parse(pubDate);
						this.pointer.dateobj.setTime(pubtime);
						var date_str = this.pointer.formatDate(this.pointer.dateobj);
            var class_name = (i == 0 ? 'first' : ((count - i) == 1 ? 'last' : ''));
						if(this.pointer.now - pubtime < 86400000){
							class_name = (class_name != '' ? ' ' : '') + 'new';
						}
						html += this.pointer.buildHtml(item, title, link, class_name, date_str);
					}
				}
			}
			this.pointer.object.innerHTML = html;
		}
		xmlhttp.res[this.xml_url].fail = this.fail;
		xmlhttp.loadDoc(this.xml_url, 'get', null, true, true);
	}
  this.formatDate = function(date) {
    var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		var date_str = year + '.' + (month < 10 ? '0' : '') + month + '.' + (day < 10 ? '0' : '') + day;
    return date_str;
  }
  this.standard_html = function(item, title, link, class_name, date_str) {
    var html = '<dl><dt' + (class_name != '' ? ' class="' + class_name + '"' : '') + '>' + date_str + '</dt><dd><a href="' + link + '" target="' + this.link_type + '">' + title + '</a></dd></dl>';
    return html;
  }
}
feedreader.photo_html = function(item, title, link, class_name, date_str) {
  var description = xmlhttp.getNodeValue(item, 'content:encoded');
  var image = xmlhttp.getNodeValue(item, 'xstep:image');
  //console.log([item, description, image]);
  var html = '<li><div class="clearfix"><div class="newsPh"><a href="'+link+'">';
  html += '<img src="'+image+'" /></a></div>';
  html += '<h3><a href="'+link+'">'+title+'</a></h3>';
  html += '<p><a href="'+link+'">'+description+'</a></p>';
  html += '<dl><dd>';
  //TODO: label
  html += '</dd>';
  html += '<dt>'+date_str+'</dt></dl></div>';
  html += '</li>';
  return html;
}

photolist = function(){
	this.init = function(tid, xml_url){
		this.tid = tid;
		this.xml_url = xml_url;
		this.count = (typeof(arguments[2]) != 'undefined' ? arguments[2] : 10);
		this.link_type = (typeof(arguments[3]) != 'undefined' ? arguments[3] : '_self');
		this.dateobj = new Date();
		this.now = this.dateobj.getTime();
		this.object = document.getElementById(this.tid);
		this.object.innerHTML = '<div style="font-size:12px;color:#bbb;">LOADING...</div>';
		this.load();
	}
	this.fail = function(){
		this.object.innerHTML = '<div style="font-size:12px;color:#900;">ERROR</div>';
	}
	this.load = function(){
		xmlhttp.res[this.xml_url] = new Object();
		xmlhttp.res[this.xml_url].pointer = this;
		xmlhttp.res[this.xml_url].exec = function(){
			var html = '';
			var root = xmlhttp.res[this.pointer.xml_url].XML.getElementsByTagName('rss');
			if(root.length > 0){
				var items = root.item(0).getElementsByTagName('item');
				if(items.length > 0){
					var count = (items.length < this.pointer.count ? items.length : this.pointer.count);
					for(var i = 0; i < count; i++){
						var item = items.item(i);
						var title = xmlhttp.getNodeValue(item, 'title');
						var link = xmlhttp.getNodeValue(item, 'link');
						var image = xmlhttp.getNodeValue(item, 'xstep:image');
						if(!image){
							image = '/resource/image/dummy/pickupphoto.jpg';
						}
						html += '<li' + (i % 3 == 2 ? ' class="lastchild"' : '') + '><div class="phBox"><a href="' + link + '" target="' + this.pointer.link_type + '"><img src="' + image + '" width="150" height="115" alt="' + title + '" /></a></div><p><a href="' + link + '" target="' + this.pointer.link_type + '">' + title + '</a></p></li>';
					}
				}
				if(html != ''){
					html = '<ul class="photoList">' + html + '</ul>';
				}
			}
			this.pointer.object.innerHTML = html;
		}
		xmlhttp.res[this.xml_url].fail = this.fail;
		xmlhttp.loadDoc(this.xml_url, 'get', null, true, true);
	}
}
