
$(function () {
    var startOffset = 10;
    var range = 379;
    var step = range / 9.0;

    var m_oDraggingThumb = null;

    var scoreToX = function (score, returnNullOnNull) {
        var round = true;

        if (returnNullOnNull == undefined) {
            returnNullOnNull = true;
        }

        if ((score == null) || (score == "")) {
            if (returnNullOnNull) {
                return null;
            } else {
                score = 5.5;
                round = false;
            }
        }

        if (typeof score == "string") {
            score = parseFloat(score);
        }

        score -= 1;
        return startOffset + ((round) ? Math.round(step * score) : (step * score));
    };

    var xToScore = function (x) {

        if ((x == null) || (x == "")) {
            return null;
        }

        x -= startOffset;
        var score = Math.round(x / step) + 1;
        if (score > 10) score = 10;
        if (score < 1) score = 1;
        return score;
    };

    var dragThumbToPageX = function (thumb, pageX, animate) {
        var tmp_iNewX = pageX - thumb.parent().offset().left;
        dragThumbToX(thumb, tmp_iNewX, animate);
    }

    var dragThumbToX = function (thumb, newX, animate) {

        if (animate == undefined) animate = false;

        //Round the X on Score
        var tmp_iScore = xToScore(newX);
        var tmp_iRoundedX = scoreToX(tmp_iScore, false);

        if (tmp_iScore != null) {
            $("input", thumb.parent()).val(tmp_iScore);
        }

        var tmp_iLastDragTargetX = thumb.data("lastX");

        if ((tmp_iLastDragTargetX == null) || (tmp_iLastDragTargetX != tmp_iRoundedX)) {

            thumb.data("lastX", tmp_iRoundedX);

            if (animate) {
                thumb.stop(true).animate({ left: tmp_iRoundedX }, 100);
            }
            else {
                thumb.stop(true).css({ left: tmp_iRoundedX });
            }
            setAverageScore(calculateAverage(), animate);
        }

    }

    var setAverageScore = function (score, animate) {

        //RVE 2011-10-19: We don't do this anymore.
        //        if (animate == undefined) animate = false;

        //        $(".Running.AverageScore").each(function () {
        //            if ((this.averageScore == undefined) || (score == null) || (score == "") || (!animate)) {
        //                this.averageScore = parseInt(Math.round(score * 10));
        //                $(this).html(formatFloat(score));
        //            } else {
        //                $(this).stop(true).animate(
        //                    { averageScore: parseInt(Math.round(score * 10)) },
        //                    { step:
        //                        function (now, fx) {
        //                            $(this).html(formatFloat(now / 10.0));
        //                        },
        //                        duration: 500

        //                    });
        //            }
        //        });
    };

    var calculateAverage = function () {
        var tmp_fTotal = 0;
        var tmp_iCount = 0;

        $("input.QSlider").each(function () {
            var value = $(this).val();
            if ((value != null) && (value != "")) {
                tmp_fTotal += parseInt(value);
                tmp_iCount++;
            }
        });

        if (tmp_iCount > 0) {
            return ((tmp_fTotal * 1.0) / (tmp_iCount * 1.0));
        } else {
            return null;
        }
    };

    var formatFloat = function (value, decimals) {

        if ((value == null) || (value == "") || (value == undefined)) {
            return null;
        }

        if (decimals == undefined) decimals = 1;

        returnValue = "";

        value *= Math.floor((10 * decimals));

        for (var i = 0; i < decimals; i++) {
            returnValue = Math.floor((value % 10)) + returnValue;
            value /= 10;
        }

        returnValue = Math.floor(value) + "," + returnValue;
        return returnValue;
    }

    $("input.QSlider").hide().wrap('<div class="QSlider"></div>');

    $("div.QSlider").each(function () {
        var tmp_fLeft = startOffset * 1.0;

        for (var i = 1; i <= 10; i++) {
            $(this).append('<div class="Digit" style="left:' + Math.round(tmp_fLeft) + 'px; ">' + i + '</div>');
            tmp_fLeft += step;
        }
    });

    $("div.QSlider").append("<div class=\"Thumb\"></div>");

    $("div.QSlider").
    mousedown(function (e) {
        dragThumbToPageX($(".Thumb", this), e.pageX, true);
    });

    $("div.QSlider")
    .each(function () {
        dragThumbToX($(".Thumb", this), scoreToX($("input", this).val()));
    });

    $("div.QSlider .Thumb")
    .data("lastX", null)
    .mousedown(function (e) {
        m_oDraggingThumb = $(this);
        $(document.body).css({ "cursor": "pointer" });
        return false;
    });

    $(document).mouseup(function (e) {
        if (m_oDraggingThumb != null) {
            $(document.body).css({ "cursor": "auto" });
            m_oDraggingThumb = null;
        }
    })
    .mousemove(function (e) {

        if (m_oDraggingThumb != null) {
            dragThumbToPageX(m_oDraggingThumb, e.pageX, true);
        }
    });

    setAverageScore(calculateAverage())
})

