function GetInteger(n) {
    return Math.floor(Math.floor(n) / 10)
}
function GetNbrDigits(n) {
    var nLen = String(GetInteger(n)).length

    var nbrDigits
    nbrDigits = nLen
    nbrDigits = nbrDigits + 1 // 1/100 digit
    nbrDigits = nbrDigits + 1 // 1/10 digit
    nbrDigits = nbrDigits + 1 // decimal
    nbrDigits = nbrDigits + 1 // dollar sign
    //if(nLen > 3) {
    nbrDigits = nbrDigits + 1
    //}
    //if(nLen > 6) {
    nbrDigits = nbrDigits + 1
    //} 
    //alert ( GetInteger(n)+ ")" + nLen + ")" +nbrDigits)
    return 13
    return nbrDigits
}

function Odometer(parentDiv, opts) {

    if (!parentDiv) throw "ERROR: Odometer object must be past a document element.";

    this.digits = 6;
    this.tenths = 0;
    this.digitHeight = 20;
    this.digitPadding = 0.005;
    this.digitWidth = 15;
    this.bustedness = 0;
    this.fontStyle = "font-family: Courier New, Courier, monospace; font-weight: 900;";
    this.value = -1;
    this.tenths = true


    this.digitInfo = new Array();
    for (var key in opts) { this[key] = opts[key]; }

    //alert(GetNbrDigits(opts.value))
    this.digits = GetNbrDigits(opts.value)
    this.style = {
        digits: " position:absolute; height:" + this.digitHeight + "px; width:" + (this.digitWidth - (2 * this.digitPadding)) + "px; " +
                       "padding:" + this.digitPadding + "px; font-size:" + (this.digitHeight - (2 * this.digitPadding)) + "px; " +
                       "background:black; color:white; text-align:center; " + this.fontStyle,
        columns: "position:relative; float:left; overflow:hidden;" +
                        "background:black; color:white;" +
                       "height:" + this.digitHeight + "px; width:" + this.digitWidth + "px;" +
                       "border-top: solid 1px black;border-bottom: solid 1px black;border-right: solid 1px black",
        //highlight: " position:absolute; background:white; width:100%; left:0px;" +
        //            "opacity: .2; filter: progid:DXImageTransform.Microsoft.Alpha(opacity=20);filter:alpha(opacity=20);-moz-opacity:.2;",
        //lowlight: "position:absolute; background:gray; width:100%; left:0px;"
        //            +                    "zoom=1;opacity: .2; filter: progid:DXImageTransform.Microsoft.Alpha(opacity=20);filter:alpha(opacity=20);-moz-opacity:.2;",

        highlight: "", //"z-index:3;background-image:url('../../images/odometer.png');background-repeat:repeat;",
        lowlight: "",
        sidehighlight: "color:white",
        sidelowlight: ""
        //sidehighlight: " position:absolute; background:white; height:100%; top:0px;" + 
        //"opacity: .4; filter: progid:DXImageTransform.Microsoft.Alpha(opacity=40);filter:alpha(opacity=40);-moz-opacity:.4;",
        //sidelowlight: "position:absolute; background:black; height:100%; top:0px;" + 
        //            "opacity: .4; filter: progid:DXImageTransform.Microsoft.Alpha(opacity=40);filter:alpha(opacity=40);-moz-opacity:.4;"

    };


    this.highlights = [
        "top:20%;   height:32%;" + this.style.highlight,
        "top:27.5%; height:16%;" + this.style.highlight,
        "top:32.5%; height:6%;" + this.style.highlight,
        "right:0%;  width:6%;" + this.style.sidelowlight,
        "left:0%;   width:4%;" + this.style.sidehighlight,
        "top:0%;    height:14%;" + this.style.lowlight,
        "bottom:0%; height:25%;" + this.style.lowlight,
        "bottom:0%; height:8%;" + this.style.lowlight
    ];



    this.setDigitValue = function(digit, val, nextVal, frac) {
        var di = this.digitInfo[digit];
        var px = Math.floor(this.digitHeight * frac);
        px = px + di.offset;
        if (val != di.last_val) {
            var tmp = di.digitA;
            di.digitA = di.digitB;
            di.digitB = tmp;
            di.digitA.innerHTML = val;
            //            if (val == "." || val == "," || val == "$") {
            //                di.digitB.innerHTML = val;
            //            } else {
            di.digitB.innerHTML = nextVal;
            //            }
            di.last_val = val;
        }

        if (px != di.last_px) {
            di.digitA.style.top = (0 - px) + "px";
            di.digitB.style.top = (0 - px + this.digitHeight) + "px";
            di.last_px = px;
        }
    };


    this.set = function(inVal) {
        if (inVal < 0) throw "ERROR: Odometer value cannot be negative.";
        this.value = inVal;
        if (this.tenths) inVal = inVal * 10;
        var numb = Math.floor(inVal);
        var frac = inVal - numb;
        numb = String(numb);
        var intPart = String(GetInteger(inVal))
        var digitPos = 0;

        for (var i = 0; i < this.digits; i++) {
            var num; //= numb.substring(numb.length-i-1, numb.length-i) || 0;
            var nextNum;
            if (i == 2) {
                num = ".";
                nextNum = ".";
                //} else if(intPart.length > 4 && i == 6) {
            } else if (i == 6) {
                if (intPart.length > 4) {
                    num = ",";
                    nextNum = ",";
                } else {
                    num = "";
                    nextNum = ",";
                }
            } else if (i == 10) {
                if (intPart.length > 7) {
                    num = ",";
                    nextNum = ",";
                } else {
                    num = "";
                    nextNum = ",";
                }
                nextNum = ",";
            } else if (i == (this.digits - 1)) {
                num = "$";
                nextNum = "$";
            } else {
                num = numb.substring(numb.length - digitPos - 1, numb.length - digitPos); // || 0;
                if (numb.length == digitPos) {
                    //alert(1)
                    nextNum = 1;
                } else {
                    nextNum = (1 + Number(num)) % 10;
                }
                digitPos += 1
            }
            this.setDigitValue(this.digits - i - 1, num, nextNum, frac);

            //&& i != 6
            if (num != "." && num != "," && num != "$") {
                if ((num != 9 && num != '') || digitPos > numb.length) frac = 0;
            }
        }
    };

    this.get = function() {
        return (this.value);
    };

    /*
    if(document.getElementById("odometer") != null) 
    {            
    var prevODiv = document.getElementById("odometer")
    var m = prevODiv.children;
    for (i=0; i < m.length; i++) {
    m[i].style.display='none'; 
    prevODiv.removeChild(m[i]);
    }
    prevODiv.innerHTML = "";
    prevODiv.style.display='none';    
    parentDiv.removeChild(prevODiv);
    parentDiv.innerHTML = "";
     
    return;    
    }
    */
    this.odometerDiv = document.createElement("div")
    this.odometerDiv.setAttribute("id", "odometer");
    this.odometerDiv.style.cssText = "text-align: left";
    parentDiv.appendChild(this.odometerDiv);



    for (var i = 0; i < this.digits; i++) {
        AddDigit(this)
    };


    if (this.tenths) {

        this.digitInfo[this.digits - 1].digitA.style.background = "#cccccc";
        this.digitInfo[this.digits - 1].digitB.style.background = "#cccccc";
        this.digitInfo[this.digits - 1].digitA.style.color = "#000000";
        this.digitInfo[this.digits - 1].digitB.style.color = "#000000";
        this.digitInfo[this.digits - 2].digitA.style.background = "#cccccc";
        this.digitInfo[this.digits - 2].digitB.style.background = "#cccccc";
        this.digitInfo[this.digits - 2].digitA.style.color = "#000000";
        this.digitInfo[this.digits - 2].digitB.style.color = "#000000";

        this.digitInfo[this.digits - 1].digitCol.style.color = "#000000";
        this.digitInfo[this.digits - 1].digitCol.style.background = "#cccccc";
        //this.digitInfo[this.digits - 2].digitCol.style.borderRight = "darkgray";
        this.digitInfo[this.digits - 2].digitCol.style.color = "#000000";
        this.digitInfo[this.digits - 2].digitCol.style.background = "#cccccc";

    }


    if (this.value >= 0) this.set(this.value);
}


function AddDigit(odometer) {

    var i = odometer.digitInfo.length
    //alert("digit" + i)
    var digitDivA = document.createElement("div");
    digitDivA.setAttribute("id", "odometer_digit_" + i + "a");

    digitDivA.style.cssText = odometer.style.digits;

    var digitDivB = document.createElement("div");
    digitDivB.setAttribute("id", "odometer_digit_" + i + "b");
    digitDivB.style.cssText = odometer.style.digits;

    var digitColDiv = document.createElement("div");
    digitColDiv.style.cssText = odometer.style.columns;

    digitColDiv.appendChild(digitDivB);
    digitColDiv.appendChild(digitDivA);

    for (var j in odometer.highlights) {
        var hdiv = document.createElement("div");
        hdiv.innerHTML = "<p></p>"; // For Dumb IE
        hdiv.style.cssText = odometer.highlights[j];
        digitColDiv.appendChild(hdiv);
    }

    var offset = -4//Math.floor(Math.random() * odometer.bustedness);
    odometer.odometerDiv.appendChild(digitColDiv);
    odometer.digitInfo.push({ digitCol: digitColDiv, digitA: digitDivA, digitB: digitDivB, last_val: -1, last_px: -1, offset: offset });

}