﻿// vars for amination
var BarNumber = 0;
var maxBars = 7; // 0 index 8 bars
var myArray;
var intScoreValuesCombined = 0;
var holdScoreValues = [0, 0, 0, 0, 0, 0, 0, 0];
var maxPropertyValue = 1200000;
var maxScore = 80;

// GEOScoringAnimationLoop starts
function GEOScoringAnimationLoop(arg_BarNumber) {
    BarNumber = arg_BarNumber;
    //var myBar = $(".bar:eq(" + BarNumber + ")");
    BarSingleDraw(0);
}

// draw a bar
function BarSingleDraw(i) {
    // reset the bars
    if (i > maxBars)
        i = 0;

    if (i == 0) {
        myArray = ScoreArrayGet(8, 10);
    }
    // calculate new width, because the bars have a max value of 10 and a max width of 200 pixels
    var myWidth = myArray[i] * 20;

    $("#bar" + i).animate({ width: myWidth }, { duration: 2000, queue: true,
        step: function (now, fx) {
            // show a score the increments or decrements by 1
            var Score = Math.ceil(Math.round(fx.now) / 20);
            var tempScore = 0;
            holdScoreValues[i] = Score;
            for (var ii = 0; ii < holdScoreValues.length; ii++) {
                tempScore += holdScoreValues[ii];
                //alert(tempScore);
            }
            //intScoreValuesCombined = intScoreValuesCombined + Score;
            $("#ScoreValuesCombined").text(tempScore);
            var myValue = maxPropertyValue * (tempScore / maxScore);
            var myValueFormated = addCommas(myValue);
            $("#PropertyValue").text("$" + myValueFormated);
            $("#bar" + i).next().text(Score);
        },
        complete: function () {
            i++;
            BarSingleDraw(i);
        } 
    });
}

// format number with commas
function addCommas(str) {
    var amount = new String(str);
    amount = amount.split("").reverse();

    var output = "";
    for (var i = 0; i <= amount.length - 1; i++) {
        output = amount[i] + output;
        if ((i + 1) % 3 == 0 && (amount.length - 1) !== i) output = ',' + output;
    }
    return output;
}

// random ratings numbers 
function ScoreArrayGet(arraySize, arrayMax) {
    var arr = [];
    var i = 0;
    for (i = 0; i < arraySize; i++) {
        arr.push(Math.round(Math.random() * arrayMax))
    }
    return arr;
}
