/* Stash Down Bar
	version 0.5
 	(c) 2007 Jon Pomeroy, modified by the less capable Rebecca Long
 	Licensed under the Creative Commons Attribution 3.0 License - http://creativecommons.org/licenses/by/3.0/
*/

function StashDownBar(unit, start, goal) {
	// script values
	this.currAmount = 0;
	this.currGoal = goal;
	this.currStart = start;
	this.currUnit = unit;

	this.showtext = false;

	// Dimensions
	this.size = 143;
	this.height = 20;

	// style values
	this.backgroundColor = "#ffffff";
	this.borderColor = "#66aa77";
	this.color = "#99bb55";

	// size limits
	this.minSize = 50;
	this.maxSize = 300;

	this.draw = function() {
		// status widget
		containerStyle = "width:" + this.size + "px; margin-bottom:2px; margin-top:-4px; background-color:" + this.backgroundColor + "; border:solid " + this.borderColor + " 1px;";
		leftStyle = "width:0%; height:" + this.height + "px; background-color:" + this.color + ";";

		document.write("<div style=\"z-index:1; text-align:left; float: left; margin-top:-2px\">&nbsp;" + parseInt(100 - this.getPercent()) + "% of goal met</div>");
		document.write("<div id=\"container\" style=\"" + containerStyle + "\">");
		document.write("<div id=\"amountLeft\" style=\"" + leftStyle + "\"></div>");
		document.write("</div>");
		
		this.containerObjId = document.getElementById("container");
		this.amountLeftObjId = document.getElementById("amountLeft");

		this.amountLeftObjId.style.width = this.getPercent() + "%";

		// Stats
		textDivStyle = "text-align:left; line-height:1em; font-size:75%;";

		stashSizeUnit = "";
		goalUnit = "";
		amtUnit = "";

		if (this.showtext) {
			document.write("<div style=\"" + textDivStyle + "\"><b><em>Stash: </em></b>" + this.currStart + " " + this.currUnit + ", <b><em>Goal: </em></b>" + this.currGoal + " " + this.currUnit + "</div>");
		}
	}

	this.getPercent = function() {
		return (100 - (this.currAmount/(this.currStart - this.currGoal)) * 100);
	}

	this.setAmountFinished = function(amount) {
		if (amount > (this.currStart - (this.currGoal + this.currAmount))) {
			alert("setAmountFinished must be less than " + (this.currStart - (this.currGoal + this.currAmount)));
			return;
		}
		
		this.currAmount += amount;	
	}

	this.setBackgroundColor = function(color) {
		this.backgroundColor = color;
	}

	this.setBorderColor = function(color) {
		this.borderColor = color;
	}
	
	this.setColor = function(color) {
		this.color = color;
	}

	this.setSize = function(size) {
		if ((size < this.minSize) || (size > this.maxSize)) {
			alert("The size must be between " + this.minSize + " and " + this.maxSize);
			return;
		}

		// an odd number size cause the neg and pos outer divs to 
		// draw on different lines. This forces it to be even.
		if (size%2) {
			this.size = size + 1;
		} else {
			this.size = size;
		}
	}

	this.showStartSize = function(show) {
		this.showtext = show;
	}
}

