var Calc = Class.create({
	initialize: function () {
		this.elm = {};
		
		this.estimate = .12;
		this.resale = .37;
	},

	init: function (wage, cpm, cpp, estimate, total_without, emp_cost, resale, total_with, percentage) {
		wage = (wage == '' || wage == null ? 'wage' : wage);
		cpm = (cpm == '' || cpm == null ? 'cpm' : cpm);
		cpp = (cpp == '' || cpp == null ? 'cpp' : cpp);
		estimate = (estimate == '' || estimate == null ? 'estimate' : estimate);
		total_without = (total_without == '' || total_without == null ? 'total_without' : total_without);
		emp_cost = (emp_cost == '' || emp_cost == null ? 'emp_cost' : emp_cost);
		resale = (resale == '' || resale == null ? 'resale' : resale);
		total_with = (total_with == '' || total_with == null ? 'total_with' : total_with);
		percentage = (percentage == '' || percentage == null ? 'percentage' : percentage);

		this.elm.wage = $(wage);
		this.elm.cpm = $(cpm);
		this.elm.cpp = $(cpp);
		this.elm.estimate = $(estimate);
		this.elm.total_without = $(total_without);
		this.elm.emp_cost = $(emp_cost);
		this.elm.resale = $(resale);
		this.elm.total_with = $(total_with);
		this.elm.percentage = $(percentage);
	},

	addCommas: function (nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	},

	roundNumber: function (num, length) {
		var newString;
		decimals = Number(length);
		if (decimals < 1) {
			newString = (Math.round(num)).toString();
		} else {
			var numString = num.toString();
			if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
				numString += ".";// give it one at the end
			}
			var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
			var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
			var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
			if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
				if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
					while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
						if (d1 != ".") {
							cutoff -= 1;
							d1 = Number(numString.substring(cutoff,cutoff+1));
						} else {
							cutoff -= 1;
						}
					}
				}
				d1 += 1;
			} 
			newString = numString.substring(0,cutoff) + d1.toString();
		}
		if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
			newString += ".";
		}
		var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
		for(var i=0;i<decimals-decs;i++) newString += "0";

		return Number(newString);
	},

	update: function() {
		var wage = this.elm.wage.value.strip();
		if(wage.length > 0 && !isNaN(wage) && 0 <= Number(wage))
		{
			this.calc();
		}
		else
		{
			for(var elm in this.elm)
			{
				elm.update('&nbsp;');
			}
		}
	},

	calc: function() {
		// Removing trailing & leading whitespace
		var wage = this.elm.wage.value.strip();

		var cpm = this.roundNumber(wage/60, 4);
		var cpp = this.roundNumber(cpm*6, 4);

		var total_without_display = this.roundNumber(cpp, 2)+this.estimate;
		var total_without = cpp+this.estimate;

		var emp_cost = this.roundNumber(cpm/4, 4);
		var total_with = emp_cost+this.resale;
		var percentage = (this.roundNumber((total_without-total_with)/total_without, 4))*100;

		this.elm.cpm.update('$'+this.addCommas(Number(cpm).toFixed(2)));
		this.elm.cpp.update('$'+this.addCommas(Number(cpp).toFixed(2)));
		this.elm.estimate.update('$'+this.addCommas(Number(this.estimate).toFixed(2)));
		this.elm.total_without.update('$'+this.addCommas(Number(total_without).toFixed(2)));
		this.elm.emp_cost.update('$'+this.addCommas(Number(emp_cost).toFixed(2)));
		this.elm.resale.update('$'+this.addCommas(Number(this.resale).toFixed(2)));
		this.elm.total_with.update('$'+this.addCommas(Number(total_with).toFixed(2)));
		this.elm.percentage.update(Number(percentage).toFixed(1)+'%');
	}
});

var panCalc = new Calc();
