//Defaults
var odometerReading = 1.00;
var odometerReadingCents = 0.00;
var odometerReadingCentsCurrent = 0.00;
var odometerFrequency = 1000;
var odometerChangeFrequency = 0.01;
var cents = false;
var animateDigits = true;

var odoArrayCurrent = new Array();
var odoArrayRun = new Array();

var imgWidth = 20;
var imgHeight = 20;
var imgPuncWidth = 8;
var imagebaseurl = "";

var dollarsign = false;

function run(parentDiv, opts) {
	if (!parentDiv) throw "ERROR: Missing HMTL element in Odometer init().";

	if (opts != null) {
		for (var key in opts) { this[key] = opts[key]; }
	}

	var odo = odometerReading.toFixed(2).toString();
	var cents = odo.substring(odo.length - 2, odo.length);

	odometerReadingCents = "0." + cents;

	odometerReading = odometerReading.toFixed(0).toString();
	var len = odometerReading.length;
	for (i = 0; i < len; i++) {
		odoArrayCurrent[i] = odometerReading.toString().charAt(i);
		odoArrayRun[i] = odometerReading.toString().charAt(i);
	}

	init(parentDiv);
}

function init(parentDiv) {
	var elementCount = odoArrayCurrent.length;

	//Parent Divs
	$('<div/>', { id: 'back', height: imgHeight + 'px' }).appendTo('#digits');
	$('<div/>', { id: 'backTop' }).appendTo('#back');
	$('<div/>', { id: 'backBottom' }).appendTo('#back');

	$('<div/>', { id: 'front', height: imgHeight + 'px' }).appendTo(parentDiv);
	$('<div/>', { id: 'frontTop' }).appendTo('#front');
	$('<div/>', { id: 'frontBottom' }).appendTo('#front');

	for (i = 0; i <= elementCount - 1; i++) {
		//1.00
		//10.00
		//100.00

		//1,000.00
		if (((elementCount - 3) == 1) && i == 1) {
			addBackComma();
			addFrontComma();
		}

		//10,000.00
		if (((elementCount - 3) == 2) && i == 2) {
			addBackComma();
			addFrontComma();
		}

		//100,000.00
		if (((elementCount - 3) == 3) && i == 3) {
			addBackComma();
			addFrontComma();
		}

		//1,000,000.00
		if (((elementCount - 3) == 4) && (i == 1 || i == 4)) {
			addBackComma();
			addFrontComma();
		}

		//10,000,000.00
		if (((elementCount - 3) == 5) && (i == 2 || i == 5)) {
			addBackComma();
			addFrontComma();
		}

		//100,000,000.00
		if (((elementCount - 3) == 6) && (i == 3 || i == 6)) {
			addBackComma();
			addFrontComma();
		}

		//1,000,000,000.00
		if (((elementCount - 3) == 7) && (i == 1 || i == 4 || i == 7)) {
			addBackComma();
			addFrontComma();
		}

		//Additional Digits Back
		$('#backTop').append('<img id="d' + ((elementCount - 1) - i) + 'BT" src="' + imagebaseurl + 'images/ticker/0u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		$('#backBottom').append('<img id="d' + ((elementCount - 1) - i) + 'BB" src="' + imagebaseurl + 'images/ticker/0d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');

		//Additional Digits Front
		$('#frontTop').append('<img id="d' + ((elementCount - 1) - i) + 'FT" src="' + imagebaseurl + 'images/ticker/0u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		$('#frontBottom').append('<img id="d' + ((elementCount - 1) - i) + 'FB" src="' + imagebaseurl + 'images/ticker/0d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
	}

	//Standard digits Back
	if (cents) {
		//period
		$('#backTop').append('<img src="' + imagebaseurl + 'images/ticker/pt.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
		$('#backBottom').append('<img src="' + imagebaseurl + 'images/ticker/pt.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
		//0.10's
		$('#backTop').append('<img id="d01BT" src="' + imagebaseurl + 'images/ticker/0u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		$('#backBottom').append('<img id="d01BB" src="' + imagebaseurl + 'images/ticker/0d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		//0.100's
		$('#backTop').append('<img id="d001BT" src="' + imagebaseurl + 'images/ticker/0u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		$('#backBottom').append('<img id="d001BB" src="' + imagebaseurl + 'images/ticker/0d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');

		//Standard digits Front
		//period
		$('#frontTop').append('<img src="' + imagebaseurl + 'images/ticker/pt.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
		$('#frontBottom').append('<img src="' + imagebaseurl + 'images/ticker/p.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
		//0.10's
		$('#frontTop').append('<img id="d01FT" src="' + imagebaseurl + 'images/ticker/0u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		$('#frontBottom').append('<img id="d01FB" src="' + imagebaseurl + 'images/ticker/0d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		//0.100's
		$('#frontTop').append('<img id="d001FT" src="' + imagebaseurl + 'images/ticker/0u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
		$('#frontBottom').append('<img id="d001FB" src="' + imagebaseurl + 'images/ticker/0d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />');
	}

	//$
	if (dollarsign) {
		addDollarSign();
	}

	//Set up passed in number.
	initDigits();

	count();
	setInterval('count()', odometerFrequency);
}

function count() {
	odometerReadingCents = parseFloat(odometerReadingCents) + parseFloat(odometerChangeFrequency);
	odometerReading = (parseFloat(odometerReading) - parseFloat(odometerReadingCentsCurrent)) + parseFloat(odometerReadingCents);
	odometerReadingCentsCurrent = odometerReadingCents;

	var tenths = ((odometerReadingCents * 1000) / 100).toFixed(1);
	var hundredths = (tenths * 10) % 10;

	//Cents always rotating
	if (tenths == '10.0') { tenths = '0.0'; }
	if (cents) {
		rotateCents('#d001FT', '#d001FB', '#d001BT', '#d001BB', hundredths, imagebaseurl + 'images/ticker/'); //Right hundredths
		rotateCents('#d01FT', '#d01FB', '#d01BT', '#d01BB', tenths, imagebaseurl + 'images/ticker/'); //Left tenths
	}

	//Conditional rotate
	if (cents) {
		if (odometerReadingCents.toFixed(2).toString().substring(2, 4) == '00') {
			odometerReadingCents = '00';
			odometerReadingCentsCurrent = '00';

			updateOdoRunArray(odometerReading.toFixed(0));
			odoArrayCurrent.reverse();
			odoArrayRun.reverse();

			if (odoArrayRun.length > odoArrayCurrent.length) {
				if ((odoArrayRun.length == 4) || (odoArrayRun.length == 7) || (odoArrayRun.length == 10)) {
					addDigit(odoArrayRun.length - 1, true);
				} else {
					addDigit(odoArrayRun.length - 1, false);
				}
			}

			rotateDigits2();
			odoArrayCurrent.reverse();
			odoArrayRun.reverse();
			syncArrays();
		}
	} else {
		//updateOdoRunArray(odometerReading.toFixed(0));
		updateOdoRunArray(parseInt(odometerReading));

		if (odoArrayRun.length > odoArrayCurrent.length) {
			if ((odoArrayRun.length == 4) || (odoArrayRun.length == 7) || (odoArrayRun.length == 10)) {
				addDigit(odoArrayRun.length - 1, true);
			} else {
				addDigit(odoArrayRun.length - 1, false);
			}
		}

		odoArrayCurrent.reverse();
		odoArrayRun.reverse();

		for (r = 0; r < odoArrayRun.length; r++) {
			var current = parseInt(odoArrayCurrent[r]);
			var run = parseInt(odoArrayRun[r]);

			if (current != run) {
				if (animateDigits) {
					rotateDigitsAnimate(r, run);
				} else {
					rotateDigits(r, run);
				}
			}
		}

		odoArrayCurrent.reverse();
		odoArrayRun.reverse();
		syncArrays();
	}
}

function rotateCents(ft, fb, bt, bb, num, path) {
	$(ft)
            .attr('src', $(bt).attr('src'))
            .height(imgHeight + "px")
            .css({ "visibility": "visible", 'display': 'inline-block' });

	$(bt).attr('src', path + parseInt(num) + "u.png");

	$(fb)
            .attr('src', path + parseInt(num) + "d.png")
            .height('0px')
            .css({ "visibility": "visible", 'display': 'inline-block' });

	$(ft).animate({ 'height': 0 }, { 'duration': 200, defaultEasing: 'easeinoutsine', 'complete': function() {
		$(fb).animate({ 'height': imgHeight }, { 'duration': 200, defaultEasing: 'easeinoutsine', 'complete': function() {
			$(bb).attr('src', $(fb).attr('src'));
			$(fb).add(ft).css({ "visibility": "hidden", "display": 'inline-block' }).height('0');
		}
		})
	}
	})
}

function rotateDigits(digit, num) {
	$('#d' + digit + 'FT').attr('src', imagebaseurl + 'images/ticker/' + num + 'u.png');
	$('#d' + digit + 'FB').attr('src', imagebaseurl + 'images/ticker/' + num + 'd.png');
	$('#d' + digit + 'BT').attr('src', imagebaseurl + 'images/ticker/' + num + 'u.png');
	$('#d' + digit + 'BB').attr('src', imagebaseurl + 'images/ticker/' + num + 'd.png');
}

function rotateDigitsAnimate(digit, num) {
	$('#d' + digit + 'FT')
            .attr('src', $('#d' + digit + 'BT').attr('src'))
            .height(imgHeight + "px")
            .css({ "visibility": "visible", 'display': 'inline-block' });

	$('#d' + digit + 'BT').attr('src', imagebaseurl + 'images/ticker/' + num + "u.png");

	$('#d' + digit + 'FB')
            .attr('src', imagebaseurl + 'images/ticker/' + num + "d.png")
            .height('0px')
            .css({ "visibility": "visible", 'display': 'inline-block' });

	$('#d' + digit + 'FT').animate({ 'height': 0 }, { 'duration': 200, defaultEasing: 'easeinoutsine', 'complete': function() {
		$('#d' + digit + 'FB').animate({ 'height': imgHeight }, { 'duration': 200, defaultEasing: 'easeinoutsine', 'complete': function() {
			$('#d' + digit + 'BB').attr('src', $('#d' + digit + 'FB').attr('src'));
			$('#d' + digit + 'FB').add('#d' + r + 'FT').css({ "visibility": "hidden", "display": 'inline-block' }).height('0');
		}
		})
	}
	})
}

function initDigits() {
	odoArrayCurrent.reverse();
	for (i = 0; i < odoArrayCurrent.length; i++) {
		$('#d' + i + 'FT').attr('src', imagebaseurl + 'images/ticker/' + parseInt(odoArrayCurrent[i]) + 'u.png');
		$('#d' + i + 'FB').attr('src', imagebaseurl + 'images/ticker/' + parseInt(odoArrayCurrent[i]) + 'd.png');
		$('#d' + i + 'BT').attr('src', imagebaseurl + 'images/ticker/' + parseInt(odoArrayCurrent[i]) + 'u.png');
		$('#d' + i + 'BB').attr('src', imagebaseurl + 'images/ticker/' + parseInt(odoArrayCurrent[i]) + 'd.png');
	}
	odoArrayCurrent.reverse();
}

function initDigitsCents() {
	//TODO: ??
}

function addBackComma() {
	$('#backTop').append('<img src="' + imagebaseurl + 'images/ticker/pt.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
	$('#backBottom').append('<img src="' + imagebaseurl + 'images/ticker/c.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
}

function addFrontComma() {
	$('#frontTop').append('<img src="' + imagebaseurl + 'images/ticker/pt.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
	$('#frontBottom').append('<img src="' + imagebaseurl + 'images/ticker/c.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;" />');
}

function addDollarSign() {
	$('<img id="dBT" src="' + imagebaseurl + 'images/ticker/du.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#backTop');
	$('<img id="dBB" src="' + imagebaseurl + 'images/ticker/dd.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#backBottom');
	$('<img id="dFT" src="' + imagebaseurl + 'images/ticker/du.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#frontTop');
	$('<img id="dFB" src="' + imagebaseurl + 'images/ticker/dd.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#frontBottom');
}

function addDigit(position, comma) {
	//Remove $
	if (dollarsign) {
		$('#backTop').find('#dBT').remove();
		$('#backBottom').find('#dBB').remove();
		$('#frontTop').find('#dFT').remove();
		$('#frontBottom').find('#dFB').remove();
	}
	//Additional Digits Back
	//comma
	if (comma) {
		$('<img src="' + imagebaseurl + 'images/ticker/ct.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;margin:2px" />').prependTo('#backTop'); ;
		$('<img src="' + imagebaseurl + 'images/ticker/c.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;margin:2px" />').prependTo('#backBottom');
	}
	$('<img id="d' + position + 'BT" src="' + imagebaseurl + 'images/ticker/1u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#backTop');
	$('<img id="d' + position + 'BB" src="' + imagebaseurl + 'images/ticker/1d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#backBottom');

	//Additional Digits Front
	//comma
	if (comma) {
		$('<img src="' + imagebaseurl + 'images/ticker/ct.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;margin:2px" />').prependTo('#frontTop');
		$('<img src="' + imagebaseurl + 'images/ticker/c.png" style="width:' + imgPuncWidth + ';height:10px;vertical-align:bottom;margin:2px" />').prependTo('#frontBottom');
	}
	$('<img id="d' + position + 'FT" src="' + imagebaseurl + 'images/ticker/1u.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#frontTop');
	$('<img id="d' + position + 'FB" src="' + imagebaseurl + 'images/ticker/1d.png" style="width:' + imgWidth + 'px;height:' + imgHeight + 'px" />').prependTo('#frontBottom');

	//Add $
	if (dollarsign) {
		addDollarSign();
	}
}

function updateOdoCurrentArray(odometer) {
	odoArrayCurrent.length = 0;

	var len = odometer.toString().length;
	for (i = 0; i < len; i++) {
		odoArrayCurrent[i] = odometer.toString().charAt(i);
	}
}

function updateOdoRunArray(odometer) {
	odoArrayRun.length = 0;

	var len = odometer.toString().length;
	for (i = 0; i < len; i++) {
		odoArrayRun[i] = odometer.toString().charAt(i);
	}
}

function syncArrays() {
	odoArrayCurrent.length = 0;

	var len = odoArrayRun.length;
	for (i = 0; i < len; i++) {
		odoArrayCurrent[i] = odoArrayRun[i];
	}
}
