﻿Showbo.Score = {
  version: 1.0,
  author: 'showbo',
  cfgStars: null,
  starRed: '/images/star_red.gif',
  starGray: '/images/star_gray.gif',
  starHalf: '/images/star_half.gif',
  total: 0,
  totalScore: 0,
  stars: null,
  fid: null,
  //==============评分代码
  reset: function (e, o) {
    var s = parseInt(o.getAttribute('star'), 10);
    if (e) {
      var toObj = e.toElement || e.relatedTarget;
      if (toObj == o.nextSibling) o.nextSibling.src = Showbo.Score.starRed;
      else if (toObj == o.previousSibling) o.src = Showbo.Score.starRed;
      else for (var i = 0; i < 5; i++) Showbo.Score.stars[i].src = Showbo.Score.starGray;
    }
    else {
      for (var i = 0; i < s; i++) Showbo.Score.stars[i].src = Showbo.Score.starRed;
      for (var i = s; i < 5; i++) Showbo.Score.stars[i].src = Showbo.Score.starGray;
    }
  },
  click: function (o) {
    var p = o.parentNode, score = parseInt(o.getAttribute('star'), 10), me = this;
    p.style.display = 'none';
    p.nextSibling.style.display = 'block';
    function ajaxHandle(xhr) {
      var json = xhr.status == 200 ? Showbo.GetJson(xhr.responseText) : { success: false, err: '评分出错，请联系管理员！' };
      if (json.success) {
        me.reset(null, o);
        p.style.display = 'block';
        p.nextSibling.style.display = 'none';
        p.innerHTML = '您的评分为' + p.innerHTML.substring(5);
        for (var i = 0; i < me.stars.length; i++) me.stars[i].onclick = me.stars[i].onmouseover = me.stars[i].onmouseout = Showbo.emptyFN;
        me.update(score);
        Showbo.SetCookie(me.fn, score);
      } else alert(json.err);
    }
    Showbo.Ajax.send({ url: '/codedown.aspx', method: 'post', params: 'id=' + this.fid + '&star=' + score, success: ajaxHandle, failure: ajaxHandle });
  },
  //=============详细信息
  starDTout: function (e) {
    e = e || event;
    var o = e.toElement || e.relatedTarget, obj = Showbo.$('starDetails');
    if (!o || !obj.contains(o)) obj.style.display = 'none';
  },
  init: function (star) {
    if ("undefined" == typeof (star)) star = '';
    this.cfgStars = star.split('|');
    for (var i = 0; i < this.cfgStars.length; i++)
      if (!Showbo.IsNumeric(this.cfgStars[i])) this.cfgStars[i] = 0;
      else {
        this.cfgStars[i] = parseInt(this.cfgStars[i]);
        this.totalScore += this.cfgStars[i] * (5 - i);
      }
    while (this.cfgStars.length < 5) this.cfgStars.push(0);
    this.total = eval(this.cfgStars.join('+'));
  },
  initStar: function (container) {
    var total = this.total, p = Showbo.getAbsPos('imgShowbo.Star', { x: -265, y: 12 }), arrPT = new Array();
    for (var i = 0; i < 5; i++) arrPT.push(Math.round((this.cfgStars[i] / total) * 100) + '%');
    var html = '<div class="top">评分详情(<span id="spStarTotal">' + total + '</span>人)<img src="/images/close.gif" alt="关闭" onclick="this.parentNode.parentNode.style.display=\'none\'"/></div>' +
       '<div class="dt"><ul>';
    for (var i = 0; i < 5; i++) {
      html += '<li><span class="star">';
      for (var j = i; j < 5; j++) html += '<img src="' + this.starRed + '" alt=""/>';
      for (var j = 5 - i; j < 5; j++) html += '<img src="' + this.starGray + '" alt=""/>';
      html += '</span><span class="picnum"><img src="/images/startiao.gif" id="imgStarPN' + i + '" width="' + arrPT[i] + '"/></span><span class="num" id="spStarN' + i + '">' + this.cfgStars[i] + '人</span></li>';
    }
    html += "</ul>";
    var ckstar = 0; //获取文件名
    if (this.fn) { ckstar = Showbo.GetCookie(this.fn); if (Showbo.IsNumeric(ckstar)) ckstar = parseInt(ckstar); else ckstar = 0; }
    html += "<div id='giveScore'><span>" + (ckstar == 0 ? '请给出' : '您的') + "评分" + (ckstar == 0 ? '' : '为') + "："
    if (ckstar == 0) {
      for (var i = 0; i < 5; i++)
        html += '<img src="' + this.starGray + '" title="' + (i + 1) + '星" onmouseout="Showbo.Score.reset(event,this)" star="'
          + (i + 1) + '" onclick="Showbo.Score.click(this)" onmouseover="Showbo.Score.reset(null,this)"/>';
      html += "</span><span style='display:none;'>正在更新，请等待...</span></div>";
    } else {
      for (var i = 0; i < ckstar; i++) html += '<img src="' + this.starRed + '"/>';
      for (var i = ckstar; i < 5; i++) html += '<img src="' + this.starGray + '"/>';
    }
    html += "</div>";
    var div = document.createElement("div");
    div.style.left = p.x + "px";
    div.style.top = p.y + "px";
    div.id = "starDetails";
    div.onmouseout = function (e) { Showbo.Score.starDTout(e); }
    div.innerHTML = html;
    document.body.appendChild(div);
    this.stars = Showbo.$s(Showbo.$('giveScore'), 'img')
  },
  update: function (star) {
    var Index = 5 - star;
    this.cfgStars[Index] += 1;
    this.total += 1;
    this.totalScore += star;
    Showbo.$('spStarTotal').innerHTML = this.total;
    Showbo.$('spStarN' + Index).innerHTML = this.cfgStars[Index] + '人';
    //重新计算百分比并设置图片长度百分比
    for (var i = 0; i < 5; i++) Showbo.$('imgStarPN' + i).style.width = Math.round((this.cfgStars[i] / this.total) * 100) + '%';
    //更新分数
    Showbo.$('spAVG').innerHTML = Showbo.round(this.totalScore / this.total, 1);
  },
  render: function (cfg) {
    if (!cfg.id) throw ('未指定容器');
    var o = Showbo.$(cfg.id);
    if ("object" != typeof (o)) throw ('未指定容器');
    o.innerHTML = '';
    this.fid = cfg.dId;
    this.fn = 'codedown' + this.fid;
    this.init(cfg.star);
    var avg = this.total == 0 ? 0 : Showbo.round(this.totalScore / this.total, 1), it = Math.floor(avg);
    var html = '<font color="#cc3300"><span style="font-size:30px;font-weight:bold;" id="spAVG">' + avg + '</span>星</font>';
    for (var i = 1; i <= it; i++) html += '<img src="' + this.starRed + '" title="' + avg + '星级" class="h"/>';
    if (avg > eval(it + '.4')) html += '<img src="' + this.starHalf + '" title="' + avg + '星级" class="h" style="height:12px;width:12px;"/>';
    html += "&nbsp;<img src='/images/btndown.gif' id='imgShowbo.Star' width='12px' height='12px' title='顾客游客' "
       + "border='0' style='cursor:pointer' onmouseover=\"this.src=this.src.replace('down','up');Showbo.$('starDetails').style.display='block';\" "
       + "onmouseout=\"this.src=this.src.replace('up','down')\"/>";
    var div = document.createElement("div");
    div.id = "dvInfoStar";
    div.innerHTML = html;
    o.appendChild(div);
    this.initStar(o);
  }
};
