/*
 マイリスト数／マイリスト率の表示スクリプト
 (c)[nl424.txt]までの作者
 (c)2010 CJ？ごろう

 (2012-05-02)
*/


/*
 グラフとマイリスト数／マイリスト率を挿入。
*/
var nl_mylistCountBar_jarty = function (mode, count_off, caption_off, denominator_mode, graph_mode)
{
/*
 変数の宣言。
*/
 var _doc = document;

 var mcb_mode = false, mcb_count_off = false, mcb_caption_off = false, mcb_denominator_mode = false, mcb_graph_mode = false;

 var block_start = '', block_end = '';
 var pbar_class = 'nl_mylistCountBar_Container';
 var graph_class = '{mcb_class';
 var mcb_tmp = '', mcb_video = '';
 var denominator_video = '';
 var re = /([\s\S]*?)(<p[^>]+?class="[^"]*?(font10|normal_thumb|font12)[^>]+?[\s\S]*?<\/p>)([\s\S]*)/i;


/*
 動作設定の設定。
*/
 this.mode = (typeof(mode) == 'number') ? (mode === 1) : this.mode;
 this.count_off = (typeof(count_off) == 'number') ? (count_off === 1) : this.count_off;
 this.caption_off = (typeof(caption_off) == 'number') ? (caption_off === 1) : this.caption_off;
 this.denominator_mode = (typeof(denominator_mode) == 'number') ? (denominator_mode === 1) : this.denominator_mode;
 this.graph_mode = (typeof(graph_mode) == 'number') ? (graph_mode === 1) : this.graph_mode;

 mcb_mode = this.mode;
 mcb_count_off = this.count_off;
 mcb_caption_off = this.caption_off;
 mcb_denominator_mode = this.denominator_mode;
 mcb_graph_mode = this.graph_mode;

 if (mcb_denominator_mode)
 {
  denominator_video = 'num_res';
 }
 else
 {
  denominator_video = 'view_counter';
 }

 if (mcb_graph_mode)
 {
  graph_class = '{mcb_class2';
 }

/*
 キャプションの準備。
*/
 if (!mcb_caption_off)
 {
  block_end = '</div>&nbsp;';
  pbar_class = 'nl_mylistCountBar_ContainerBox';
  if (mcb_count_off)
  {
   block_start = '<p class="nl_mylistCountBar_Caption">マイリスト率：</p><div class="nl_mylistCountBar_InContainer">'
  }
  else
  {
   block_start = '<p class="nl_mylistCountBar_Caption">マイリスト数：</p><div class="nl_mylistCountBar_InContainer">'
  }
 }


/*
 挿入する HTML を準備。
*/
 mcb_tmp = "<div class='";
 mcb_tmp += pbar_class;
 mcb_tmp += "'>";
 mcb_tmp += block_start;
 mcb_video = mcb_tmp;

 mcb_video += graph_class;
 mcb_video += ' mylist=$video.mylist_counter denominator=$video.';
 mcb_video += denominator_video;

 if (mcb_count_off)
 {
  mcb_video += '}{mcb_percent mylist=$video.mylist_counter denominator=$video.';
 }
 else
 {
  mcb_video += '}{$video.mylist_counter|number_format}';
  mcb_video += ' ({mcb_percent mylist=$video.mylist_counter denominator=$video.';
 }

 mcb_video += denominator_video;

 if (mcb_count_off)
 {
  mcb_tmp = '}</p> ';
 }
 else
 {
  mcb_tmp = '})</p> ';
 }
 mcb_tmp += block_end;
 mcb_tmp += '</div>';
 mcb_video += mcb_tmp;


/*
 マイリスト数／マイリスト率の挿入。
*/
 var replace_template = function (template_id, mcb_html)
 {
  var dom_template = _doc.getElementById(template_id);
  var template_in = dom_template.innerHTML;
  var template_out, p0, p1, p2, p3;
  template_in.replace(re, function ()
  {
   p0 = arguments[0];
   p1 = arguments[1];
   p2 = arguments[2];
   p3 = arguments[4];
  });

  if (mcb_mode)
  {
   template_out = p1 + p2 + mcb_html + p3;
  }
  else
  {
   template_out = p1 + mcb_html + p2 + p3;
  }

  dom_template.id = template_id + '_bak_mylistCountBar';
  _doc.write('<script type="application/x-jarty" id="');
  _doc.write(template_id);
  _doc.write('">');
  _doc.write(template_out);
  _doc.write('</script>');

  dom_template = null;
  template_in = null;
  template_out = null;
  p0 = null;
  p1 = null;
  p2 = null;
 };

 replace_template('recommend_tpl', mcb_video);


/*
 処理が最後まで完了したので変数の後始末をしてから return 0 。
*/
 _doc = null;

 mcb_mode = null;
 mcb_count_off = null;
 mcb_caption_off = null;
 mcb_denominator_mode = null;

 block_start = null;
 block_end = null;
 pbar_class = null;
 mcb_tmp = null;
 mcb_video = null;
 denominator_video = null;
 re = null;

 denominator = null;

 replace_template = null;

 return 0;
};


(function ($) {
/* Jarty 拡張 */
 $.extend(Jarty.Function, {
  mcb_class : function (r, params)
  {
   var mylist = params.mylist;
   var tmp_class = '8';
   var tmp_html = '';

   if (mylist < 10)
   {
    tmp_class = '0';
   }
   else if (mylist < 50)
   {
    tmp_class = '1';
   }
   else if (mylist < 100)
   {
    tmp_class = '2';
   }
   else if (mylist < 500)
   {
    tmp_class = '3';
   }
   else if (mylist < 1000)
   {
    tmp_class = '4';
   }
   else if (mylist < 5000)
   {
    tmp_class = '5';
   }
   else if (mylist < 10000)
   {
    tmp_class = '6';
    }
   else if (mylist < 20000)
   {
    tmp_class = '7';
   }

   tmp_html = '<div class="nl_mylistCountBar_Percent nl_mylistCountBar_Graph';
   tmp_html += tmp_class;
   tmp_html += '"></div><p class="nl_mylistCountBar_Count nl_mylistCountBar_Number';
   tmp_html += tmp_class;
   tmp_html += '">';

   r.write(tmp_html);
  },
  mcb_class2 : function (r, params)
  {
   var mylist = params.mylist, denominator = params.denominator;
   var percent = (denominator > 0) ? mylist / denominator : 0;
   var tmp_class = '8';
   var tmp_html = '';

   if (percent < 0.0125)
   {
    tmp_class = '0';
   }
   else if (percent < 0.025)
   {
    tmp_class = '1';
   }
   else if (percent < 0.0375)
   {
    tmp_class = '2';
   }
   else if (percent < 0.05)
   {
    tmp_class = '3';
   }
   else if (percent < 0.0625)
   {
    tmp_class = '4';
   }
   else if (percent < 0.075)
   {
    tmp_class = '5';
   }
   else if (percent < 0.0875)
   {
    tmp_class = '6';
    }
   else if (percent < 0.1)
   {
    tmp_class = '7';
   }

   tmp_html = '<div class="nl_mylistCountBar_Percent nl_mylistCountBar_Graph';
   tmp_html += tmp_class;
   tmp_html += '"></div><p class="nl_mylistCountBar_Count nl_mylistCountBar_Number';
   tmp_html += tmp_class;
   tmp_html += '">';

   r.write(tmp_html);
  },
  mcb_percent : function (r, params)
  {
   var mylist = params.mylist, denominator = params.denominator;
   var percent = ((denominator > 0) ? Math.round(mylist * 1000 / denominator) / 10 : '---.-') + '%';
   r.write(percent);
  }
 });
})(jQuery);
