﻿//objStar 新实例名 objParentID 要显示的元素ID
function StarScore(objStar,objParentID)
{
    this.path = "../image/product/";
    this.img = [{id:1,src:this.path+'starGray.gif'},{id:2,src:this.path+'starGold.gif'},{id:3,src:this.path+'reScore.gif'}];
    this.scoreTitle = [{id:1,title:"很不喜欢"},{id:2,title:"不喜欢"},{id:3,title:"还行"},{id:4,title:"喜欢"},{id:5,title:"非常喜欢"},{id:6,title:"重新打分"},{id:7,title:"已保存"}];
    this.index = 0; //选择的图片索引
    this.score = 0;//分数
    this.objParent = document.getElementById(objParentID);
    this.over = function(obj)
    {
        var _this = this;
	    _this.getIndex(_this,obj);
	    for(var i=_this.index;i>=1;i--)
	    {
	        document.getElementById("star"+i).src = _this.img[1].src;
	    }
	    for(var a=_this.index;a<5;a++)
	    {
	        var i = parseInt(a,10)+1;
	        document.getElementById("star"+i).src = _this.img[0].src;
	    }
	    _this.showMsg(_this.scoreTitle[_this.index-1].title);
    }
    this.out = function()
    {
        var _this = this;
        if(_this.score>0)
        {
	        _this.hasScore();
	    }
	    else
	    {
	        _this.allGray(_this);
	        _this.showMsg(""); 
	    }
    }
    this.allGray = function(_this)
    {
        for(var i=1;i<=5;i++)
        {
            document.getElementById("star"+i).src = _this.img[0].src;
        }
    }
    this.hasScore = function()
    {
        var _this = this;
        for(var i=1;i<=5;i++)
        {
            if(i<_this.score+1)
            {
                document.getElementById("star"+i).src = _this.img[1].src;
            }
            else
            {
                document.getElementById("star"+i).src = _this.img[0].src;
            }
        }
        if(_this.score>0)
        {
            _this.showMsg("您给商品打的分数为"+_this.score+"分"); 
        }
        else
        {
            _this.showMsg("给商品打分");
        }
    }
    this.bulidHtml = function()
    {
        var _this = this;
        var html = "<img id='reStar' onclick='"+objStar+".reStar()' onmouseover='"+objStar+".reStarOver()' onmouseout='"+objStar+".reStarOut()' src="+_this.img[2].src+" />";
        for(var i =1;i<=5;i++)
        {
            html += "<img id='star"+i+"' onclick='"+objStar+".doClick("+i+")' src="+_this.img[0].src+" onmouseover='"+objStar+".over(this)' onmouseout='"+objStar+".out(this)' />";
        }
        html += "&nbsp;<font id='scoreTitle'></font>";
        //html += "<br /><input type='checkbox' onclick="+objStar+".cbHas(this) />已经有了";
        _this.objParent.innerHTML = html;
        _this.hasScore();
    }
    this.doClick = function(i)
    {
        var _this = this;
        if(_this.score != i)
        {
            _this.score = i;
            _this.showMsg("已保存");          
            callAjax("PrdScore",scoreComplete 
                ,{ name:"s", value:i}
                ,{ name:"pc", value: productCode}
                ,{ name:"m", value:member});
        }
    }
    this.reStar = function()
    {
        var _this = this;
        _this.score = 0;
        _this.allGray(_this);
        _this.showMsg("已保存");
        callAjax("PrdReScore",scoreComplete 
            ,{ name:"pc", value: productCode}
            ,{ name:"m", value:member});
    }
    this.reStarOver = function()
    {
        var _this = this;
        _this.allGray(_this);
        _this.showMsg(_this.scoreTitle[5].title);
    }
    this.reStarOut = function()
    {
        var _this = this;
        _this.out();
    }
    this.cbHas = function(obj)
    {
        //alert(obj.checked);
    }
    this.getIndex = function(_this,obj)
    {
        _this.index = obj.id.replace("star","");
    }
    this.showMsg = function(msg)
    {
        document.getElementById("scoreTitle").innerText = msg;
    }
}
function scoreComplete(http,date)
{
    star.showMsg("给商品打分");
}

//商品评分
//如果没有评分记录则为5星，否则取平均值
//重新打分，删除刚才打的分数
//在服务器端将打的分数写入缓存，
//在页面读取的时候将ID取出,显示在页面上
//没有则为0