/* 文字コードはUTF-8 */
feedreader = function(override) {
  this.loading_html = '<div style="font-size:12px;color:#bbb;">読込中...</div>';
  this.error_html = '<div style="font-size:12px;color:#bbb;">データが読み込めませんでした。</div>';
  this.no_item_html = '<div style="font-size:12px;color:#bbb;">現在、準備中です。</div>';
  // 記事が最新か判断する範囲 単位ms デフォルト1日
  this.new_range = 86400000;
  this.skip_url = true;
  this.init = function(tid, xml_url){
    this.limit = (typeof(arguments[2]) != 'undefined' ? arguments[2] : 10);
    this.link_type = (typeof(arguments[3]) != 'undefined' ? arguments[3] : '_self');
    var dateobj = new Date();
    this.now = dateobj.getTime();
    var tobj = $('#' + tid).eq(0);
    tobj.html(this.loading_html);
    var self = this;
      $.get(xml_url, {}, function(xml, textStatus){
        var html = '';
        if(textStatus == 'success'){
          if ($(xml).find('item').length > 0){
            html += self.parse_xml(xml);
          }
          else{
            html += self.no_item_html;
          }
        }
        else{
          html += self.error_html;
        }
        tobj.html(html);

	//Added by kai @2011/2/10 for Rollover
	var image_cache = new Object();
  	$("#" + tid + " img.rollover").each(function(i) {
		var imgsrc = this.src;
		var dot = this.src.lastIndexOf('.');
		var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
		image_cache[this.src] = new Image();
		image_cache[this.src].src = imgsrc_on;
    		$(this).hover(
      			function() { this.src = imgsrc_on; },
      		function() { this.src = imgsrc; });
  	});

      });
  }
  this.parse_xml = function(xml) {
    var html = '';
    html += this.open_tag(xml);
    var items = $(xml).find('item');
    var count = (items.length < this.limit ? items.length : this.limit);
    var i = 0;
    var self = this;
    items.each(function(){
      var json = self.build_json(this);
      if (!(self.skip_url && document.URL.indexOf(json.link) >= 0)) {
        html += self.item_html(json, i, count);
        i++;
      }
      if(i >= count){
        return false;
      }
    });
    html += this.close_tag(xml);
    return html;
  }
  this.build_json = function(xml) {
    var title = $(xml).find('title').text();
    var link = $(xml).find('link').text();
    var pubDate = $(xml).find('pubDate').text();
    var description = $(xml).find('content\\:encoded').text() || $(xml).find('encoded').text() || $(xml).find('[nodename=conntent\\:encoded]').text() || $(xml).find('description').text()
    var image = $(xml).find('xstep\\:image').text() || $(xml).find('image').text() || $(xml).find('[nodename=xstep\\:image]').text();
    if (!image) {
      image = this.default_image(xml);
    }
    var pubtime = Date.parse(pubDate);
    var date_str = this.format_date(pubtime);
    var item = {
      title: title,
      link: link,
      pubDate: pubDate,
      pubtime: pubtime,
      date_str: date_str,
      description: description,
      image: image
    }
    return item;
  }
  this.default_image = function(xml) {
    return '';
  }
  this.format_date = function(pubtime) {
    var dateobj = new Date();
    dateobj.setTime(pubtime);
    var year = dateobj.getFullYear();
    var month = dateobj.getMonth() + 1;
    var day = dateobj.getDate();
    var date_str = year + '.' + (month < 10 ? '0' : '') + month + '.' + (day < 10 ? '0' : '') + day;
    return date_str;
  }
  this.open_tag = function(xml) {
    return '<dl>';
  }
  this.is_new = function(pubtime) {
    return ((this.now - pubtime) < this.new_range);
  }
  this.item_html = function(item, index, count) {
    var class_name = (index == 0 ? 'first' : ((count - index) == 1 ? 'last' : ''));
    if(this.is_new(item.pubtime)){
      class_name = (class_name != '' ? ' ' : '') + 'new';
    }
    var html = '<dt' + (class_name != '' ? ' class="' + class_name + '"' : '') + '>';
    html += item.date_str;
    html += '<small>';
    html += '<a href="' + item.link + '" target="' + this.link_type + '">';
    html += item.title;
    html += '</a>';
    html += '</small>';
    html += '</dt>';
    html += '<dd>';
    html += '<a href="' + item.link + '" target="' + this.link_type + '">';
    html += item.description;
    html += '</a>';
    html += '</dd>';
    return html;
  }
  this.close_tag = function(xml) {
    return '</dl>';
  }
  /**
   * コンストラクタに引数としてオブジェクトを渡すことで、
   * そのオブジェクトでfeedreaderをオーバーライドすることができる。
   * new feedreader({
   *   loading_html: function() {},
   *   error_html: function() {},
   *   no_item_html: function {},
   *   new_range: int,
   *   open_tag: function() {},
   *   item_html: function() {},
   *   cloase_tag: function() {}
   * });
   * 再定義する必要の無いメソッドは値を指定しないこと。
   */
  if (typeof(override) == 'object') {
    var superclass = new feedreader();
    var extended = jQuery.extend({}, superclass, override);
    return extended;
  }
}


feedreader.top_topic_list = {
  open_tag: function(xml) {
    return '<ul class="top_topic">';
  },
  close_tag: function(xml) {
    return '</ul>';
  },

  item_html: function(item, index, count) {
    var html = '<li>';

	html += '<div class="topic_image">';
    if (item.image) {
		html += '<a href="'+item.link+'">';
		html += '<img src="'+item.image+'" />';
		html += '</a>';
    }
	html += '</div>';

	html += '<div class="topic_content">';
	html += '<div class="topic_date">' + item.date_str + '</div>';
    html += '<div class="topic_title">';
    html += '<a href="'+item.link+'">';
    html += item.title;
    html += '</a>';
    html += '</div>';

    html += '<div class="topic_description">' + item.description + '</div>';

    html += '</li>';
    return html;
  }
}
feedreader.backnumber_html = {
  item_html: function(item, index, count) {
    if (index == 0) {
      return '';
    }
    var class_name = (index == 0 ? 'first' : ((count - index) == 1 ? 'last' : ''));
    if(this.is_new(item.pubtime)){
      class_name = (class_name != '' ? ' ' : '') + 'new';
    }
    if (!item.description) {
      item.description = '本文へ';
    }
    var html =
      '<dt' + (class_name != '' ? ' class="' + class_name + '"' : '') + '>' + item.date_str + ' <small>' + item.title + '</small></dt><dd><a href="' + item.link + '" target="' + this.link_type + '">' + item.description + '</a></dd>';
    return html;
  }
}


feedreader.top_html = {
  open_tag: function(xml) {
    return '';
  },
  close_tag: function(xml) {
    return '';
  },
  item_html: function(item, index, count) {
    var html = '<div class="headline">';
    html += item.title;
    html += '</div>';
    if (item.image) {
      html += '<img src="'+item.image+'" />';
    }
    html += '<small>'+item.date_str+'</small>';
    html += item.description;
    html += '<br/>';
    html += '<div class="btn_detail2" style="margin:0 0 10px 120px;"><a href="'+item.link+'">'+'詳細を読む'+'</a></div>';
    return html;
  }
}



feedreader.event_new = {
  item_html: function(item, index, count) {

    var html = '<a href="' + item.link + '">';
    if (item.image) {
      html += '<img src="'+item.image+'" class="banner" />';
    }
	html += '</a>';

    return html;
  }
}

feedreader.event_old = {
  item_html: function(item, index, count) {
    if (index == 0) {
      return '';
    }

    var html = '<a href="' + item.link + '">';
    if (item.image) {
      html += '<img src="'+item.image+'" class="banner" />';
    }
	html += '</a>';

    return html;
  }
}


