

var Calculator = new Class({

	Implements: [Events, Options],

	options: {
		defaultBackground: "none"
	},

	numCigarettes: null,
	costPerPackInput: null,
	calc: null,

	initialize: function(displayArea, options)
	{
		this.setOptions(options);
		this.displayArea = $(displayArea);
		// load the price ranges
		this.priceRanges = JSON.decode($("priceRanges").get("html"));
		this.showCalculator();
		this.preloadImages();
	},

	showCalculator: function()
	{
		this.displayArea.erase("html");
		if (!this.calc)
		{
			this.calc = new Element("div");

			var calcForm = new Element("div");
			calcForm.setStyle("background-color", "#FFFFFF");
			//calcForm.setStyle("height", "254px");

			var div = new Element("div");
			div.setStyle("width", "100%");
			div.setStyle("height", "100%");
			div.setStyle("background-image", "none");
			div.setStyle("background-repeat", "no-repeat");
			// insert the html
			div.set("html", '<p>How many cigarettes to you smoke per day?<br/><input type="text" id="numCigarettes" value="0"/></p><p>How much does a pack cost?<br/>&euro;<input type="text" id="costPerPack" value="0"/></p>');
			div.inject(calcForm);
			calcForm.inject(this.calc);
/*			var p = new Element("p");
			p.set("html", '<input id="btnCalculate" type="button" value="Stop Smoking Now" />');
			p.inject(this.calc);*/
		}
		this.calc.setStyle("opacity", "0");
		this.calc.inject(this.displayArea);
		this.calc.fade("in");
		this.numCigarettesInput = $("numCigarettes");
		this.costPerPackInput = $("costPerPack");
		// add keypress events to the inputs to make sure only numbers and . are entered
		this.numCigarettesInput.addEvent("blur", this.inputBlur.bindWithEvent(this));
		this.costPerPackInput.addEvent("blur", this.inputBlur.bindWithEvent(this));
		// tie the input button into showResults
		$("btnCalculate").addEvent("click", this.showResults.bindWithEvent(this));
	},

	showResults: function()
	{
		if (this.priceRanges.length == 0)
		{
			alert("The cost of smoking calculator is currently offline");
		}
		var numCigarettes = parseInt(this.numCigarettesInput.get("value"));
		var costPerPack = parseInt(this.costPerPackInput.get("value"));
		if (!(isNaN(numCigarettes) || isNaN(costPerPack)) && (numCigarettes * costPerPack != 0))
		{
			var cost = Math.floor((((numCigarettes * 365) / 20) * costPerPack));
			var priceRange = false;
			// loop over the price ranges until we find a match
			for (var i = 0; i < this.priceRanges.length; i++)
			{
				if (cost < this.priceRanges[i].price)
				{
					//alert(this.priceRanges[i].price);
					priceRange = this.priceRanges[i];
					break;
				}
			}
			// use the largest if none present
			if (!priceRange)
			{
				priceRange = this.priceRanges[this.priceRanges.length - 1];
			}
			// show the results to the user
			var heading = new Element("h2");
			heading.set("html", 'Savings Calculator');
			heading.inject(this.calc);

			var div = new Element("div");

			div.setStyle("background-image", "url(/uploads/tx_phsmokingcalc/" + priceRange.graphic + ")");
			div.setStyle("background-repeat", "no-repeat");
			div.setStyle("height", "100%");
			// set the text
			var elem = new Element("div");
			var extra = "";
			var minPriceRange = this.priceRanges[0];
			var maxPriceRange = this.priceRanges[this.priceRanges.length - 1];
			if (cost < minPriceRange.price)
			{
				extra = "nearly ";
				cost = minPriceRange.price;
			}
			else if (cost > maxPriceRange.price)
			{
				extra = "more than ";
				cost = maxPriceRange.price;
			}
			// number format the cost
			var tmp = cost.toString();
			tmp = tmp.split("");
			var counter = 0;
			cost = "";
			for (var i = (tmp.length - 1); i >= 0; i--)
			{
				if (counter % 3 == 0 && counter > 0)
				{
					cost = "," + cost;
				}
				cost = tmp[i] + cost;
				counter++;
			}
			elem.set("text", "Each year you spend " + extra + "...");
			elem.setStyle("color", priceRange.color);
			elem.setStyle("font-size",  "12pt");
			elem.inject(div);
			elem = new Element("div");
			elem.setStyle("color", "#FF0000");
			elem.setStyle("font-size",  "20pt");
			elem.setStyle("text-align", "center");
			var price = "&euro;" + cost;
			elem.set("html", price);
			elem.inject(div);
			elem = new Element("div");
			elem.set("text", "... on cigarettes. The equivalent of");
			elem.setStyle("color", priceRange.color);
			elem.setStyle("font-size",  "12pt");
			elem.inject(div);
			elem = new Element("div");
			elem.setStyle("color", priceRange.color);
			elem.setStyle("font-size",  "14pt");
			elem.set("text", priceRange.alt_purchases);
			elem.inject(div);
			this.displayArea.erase("html");
			div.setStyle("opacity", "0");
			div.inject(this.displayArea);
			div.fade("in");
		}
	},

	inputBlur: function(event)
	{
		var source = (event.target);
		if (isNaN(source.get("value")))
		{
			source.setStyle("background-color", "red");
		}
		else
		{
			source.setStyle("background-color", "#FFFFFF");
		}
	},

	preloadImages: function()
	{
		for (var i = 0; i < this.priceRanges.length; i++)
		{
			var graphic = new Element("img");
			graphic.setProperty("src", "/uploads/tx_phsmokingcalc/" + this.priceRanges[i].graphic);
		}
	}

});

window.addEvent("domready", function() {

});