$(function() {
    var enterClass = "";

    $(".star").hover(function() {
        var val = getcookie(votingkey + "_" + $(this).attr("id"));
        if ($(this).attr("class") != "star redstar" && val == "")
            $(this).attr("class", "star bluestar");
    }, function() {
        var val = getcookie(votingkey + "_" + $(this).attr("id"));
        if ($(this).attr("class") != "star redstar" && val == "") {
            $(this).find("span").attr("class", enterClass);
            $(this).attr("class", "star okstar");
        }
        enterClass = "";
    });

    $(".star").mousemove(function(e) {
        if (enterClass == "")
            enterClass = $(this).find("span").attr("class");

        var val = getcookie(votingkey + "_" + $(this).attr("id"));
        if ($(this).attr("class") != "star redstar" && val == "") {
            var positionX = e.originalEvent.x - $(this).offset().left || e.originalEvent.layerX - $(this).offset().left || 0;
            var g = Math.floor(positionX / 63 * 100 / 20) + 1;
            if (g > 5) g = 5;
            $(this).find("span").attr("class", "g" + g);
        }
    });

    $(".star").mousedown(function(e) {
        var val = getcookie(votingkey + "_" + $(this).attr("id"));

        if ($(this).attr("class") != "star redstar" && val == "") {
        
            var positionX = e.originalEvent.x - $(this).offset().left || e.originalEvent.layerX - $(this).offset().left || 0;
            var g = Math.floor(positionX / 63 * 100 / 20) + 1;
            if (g > 5) g = 5;
            $(this).find("span").attr("class", "g" + g);
            $(this).css("cursor", "default");
            $(this).attr("class", "star redstar");

            setcookie(votingkey + "_" + $(this).attr("id"), g);
            //alert($(this).attr("id"));

            // 发送 ajax
            $.ajax({
                url: "/Service/BusinessGradeService.ashx",
                data: { itemkey: $(this).attr("id"), buid: votingkey, grade: g },
                cache: false,
                success: function(html) {
                    //alert("评分成功");
                }
            });
        }
    });

    $(".voting .star").each(function() {
        for (var i = 0; i < gradeJSON.length; i++) {
            if (gradeJSON[i].Name == $(this).attr("id")) {
                $(this).find("span").attr("class", "g" + Math.round(gradeJSON[i].Value));
                //                        $(this).find("span").css("width", gradeJSON[i].Value / 5 * 63);
                break;
            }
        }

        var val = getcookie(votingkey + "_" + $(this).attr("id"));

        if (val != "")
            $(this).css("cursor", "default");

        //$(this).attr("class", "star redstar");
    });

    $(".bigstar").find("span").css("width", totalgrade / 5 * 174);
});