
var image_cache = ["0b.gif", "1b.gif", "2b.gif", "3b.gif", "4b.gif", "5b.gif", "6b.gif", "7b.gif", "8b.gif", "9b.gif", "pb.gif", "cb.gif", "db.gif", "bb.gif"];

for (var i = 0; i < image_cache.length; i++) {
	var img = new Image();
	img.src = "/images/" + image_cache[i];
}


function CostOMeter (cost_per_second) {
	this.obj = "_Cost_O_Meter";
	eval(this.obj + "=this");	
	this.cost_per_second = cost_per_second;
}


CostOMeter.prototype.calculateTotalSeconds = function (date) {
	return Math.floor(date.getTime() / 1000);
}

CostOMeter.prototype.calculateSecondCount = function (today) {
	return (this.calculateTotalSeconds(today) - this.calculateTotalSeconds(new Date(today.getFullYear(), 0, 1)))
}

CostOMeter.prototype.calculateTotalCost = function (cost_per_second) {
	return (Math.ceil((this.calculateSecondCount(new Date()) * cost_per_second) * 100) / 100);
}

CostOMeter.prototype.getCents = function (cents_string) {
	if (cents_string) {
		return cents_string += (cents_string.length != 1) ? "" : "0";
	}
	return "00";
}

CostOMeter.prototype.getDollars = function (dollar_string) {
	var char_count = 0;
	var dollars = "";
	for (var i = dollar_string.length - 1; i > -1; i--) {
		var temp = dollar_string.charAt(i);
		dollars = temp + dollars;
		char_count++;
		if (char_count == 3 && i > 0) {
			dollars = "," + dollars; 
			char_count = 0;
		}
	}	
	return dollars;
}

CostOMeter.prototype.processCharacter = function (character) {
	switch (character) {
		case ".":
			return "p";
		case ",":
			return "c";
		case "$":
			return "d";
	}
	return character;
}

CostOMeter.prototype.calculateDollarString = function () {
	var temp = "" + this.calculateTotalCost(this.cost_per_second);
	var split_string = temp.split(".");
	return  ("$" + this.getDollars(split_string[0]) + "." + this.getCents(split_string[1]));
}

CostOMeter.prototype.changeImages = function () {
	var dollar_string = this.calculateDollarString();
	for (var i = dollar_string.length - 1; i > -1; i--) {
		document.images["cost_o_meter_img" + (dollar_string.length - i)].src = "/images/" + this.processCharacter(dollar_string.charAt(i)) + "b.gif";
	}
}

CostOMeter.prototype.startLoop = function (loop_delay) {
	this.changeImages();
	window.setInterval((this.obj + ".changeImages()"), (loop_delay * 1000)); 
}


