ProductivityCalculator = Class.create();
ProductivityCalculator.prototype = {
	benchmarks: {
		'G4 Dual 1.25GHz': {
			'Adobe InDesign Workflow': 54.91,
			'Adobe Photoshop': 78.76,
			'QuarkXPress Workflow': 57.17,
			'Application Basics': 41.59,
			'Adobe Photoshop Expert Options': 58.99
		},
		'G5 Dual 2.0GHz': {
			'Adobe InDesign Workflow': 38.20,
			'Adobe Photoshop': 54.88,
			'QuarkXPress Workflow': 33.82,
			'Application Basics': 35.73,
			'Adobe Photoshop Expert Options': 40.37
		},
		'G5 Quad': {
			'Adobe InDesign Workflow': 23.30,
			'Adobe Photoshop': 18.43,
			'QuarkXPress Workflow': 20.57,
			'Application Basics': 23.56,
			'Adobe Photoshop Expert Options': 16.64
		}
	},

	initialize: function() {
		// css noscript magic
		this.main = $('main');
		Element.addClassName(this.main, 'hasjs');

		// get the major DOM pieces
		this.workflow = {};
		this.workflow.select = $('workflow');
		this.workflow.options = $$('#productivitycalculator #workflow option');
		this.workflow.elements = $$('#main p.sosumi .workflow');
		this.workflow.select.onchange = this.onSelect.bindAsEventListener(this, this.workflow);
	
		this.labels = $$('#productivitycalculator label');
		this.form = $('productivitycalculator');
		this.form.onsubmit = this.checkFields.bindAsEventListener(this);
	
		this.after = $$('#main .after');
		for (var i=0; i<this.after.length; i++) Element.hide(this.after[i]);
		
		if (document.location.search) this.submitted();
	},

	onSelect: function(evt, item) {
		// change the elements when
		for (var i=0; i<item.options.length; i++) {
			var option = item.options[i];
			var element = item.elements[i];
			
			if (item.select.value == option.value) {
				Element.addClassName(element, 'active');
			} else {
				Element.removeClassName(element, 'active');
			}
		}
	},

	checkFields: function(evt) {
		Event.stop(evt);
		Element.addClassName(this.main, 'loading');

		// initial stuff
		var data = Form.serialize(this.form, true);
		var statusflag = 1;

		// check the form values we just got
		for (var value in data) {
			var status = 1;
			var input = $(value);
			for (var i=0; i<this.labels.length; i++) {
				if (this.labels[i].htmlFor == value) var label = this.labels[i];
			}

			// remove a dollar sign if they put it in the billable rate field
			if (value == 'billablerate' && data[value].match(/\$/)) data[value] = data[value].replace('$', '');

			// set our pass/fail status
			if (data[value] == '') {
				var status = 0;
			} else if (input.type == 'text' && data[value].match(/[^\d]/i)) {
				var status = 0;
			}

			// set the inputs and labels with the flagged/unflagged colors
			setBG(input, status);
			if (label) setLabel(label, status);

			// set the statusflag so we know we failed
			if (!status && statusflag) statusflag = 0;
		}

		// don't go any further if we have any issues
		if (!statusflag) return;
		
		// otherwise submit the form
		var string = '?';
		for (var value in data) {
			string += value+'='+data[value]+'&';
		}
		string = string.substring(0, string.length-1);
		document.location += string.replace(/ /g, '+');
	},

	populateFields: function(data) {
		for (var value in data) {
			var input = $(value);
			
			if (input.value != data[value]) input.value = data[value];
			if (input == this.workflow.select) this.onSelect(null, this.workflow);
		}
		
		$('submit').value = 'Refresh';
	},

	submitted: function() {
		var results = $$('#main .after .results');

		var parallelEffects = [];
		for (var i=0; i<this.after.length; i++) {
			var element = this.after[i];
			parallelEffects.push(new Effect.BlindDown(element, { sync:true }));
		}
		var effect = new Effect.Parallel(parallelEffects, { duration:.3 });

		var data = document.location.search.toQueryParams();
		for (var value in data) {
			data[value] = data[value].replace(/\+/g, ' ');
		}
		this.populateFields(data);

		// the formulas
		var hours = (this.benchmarks[data.mac][data.workflow]/100)*(data.waittime*5/60)*4.33;
		var dollarsPerMonth = hours*data.billablerate;
		var dollarsPerYear = dollarsPerMonth*12;
		var dollarsForDeployment = dollarsPerYear*data.deployment;

		// round appropriately
		var round = function(input) {
			input = Math.round(input*100)/100;
			input = input.toString();
			if (!input.match(/\./)) input += '.';
			while (!input.match(/\.\d\d/)) input += '0';
			return input
		}
		hours = round(hours);
		dollarsPerMonth = round(dollarsPerMonth);
		dollarsPerYear = round(dollarsPerYear);
		dollarsForDeployment = round(dollarsForDeployment);
		
		try {
			console.log('hours: ' +hours);
			console.log('dollars/month: ' +dollarsPerMonth);
			console.log('dollars/year: ' +dollarsPerYear);
			console.log('dollars/deployment: ' +dollarsForDeployment);
		} catch(e) {}


		for (var i=0; i<results.length; i++) {
			var result = results[i];

			if (Element.hasClassName(result, 'hours')) result.innerHTML = hours;
			if (Element.hasClassName(result, 'dollarsmonth')) result.innerHTML = '$'+dollarsPerMonth;
			if (Element.hasClassName(result, 'dollarsyear')) result.innerHTML = '$'+dollarsPerYear;
			if (Element.hasClassName(result, 'dollarsdeployment')) result.innerHTML = '$'+dollarsForDeployment;
		}
	}
}


// local
var initCalc = function() { new ProductivityCalculator(); }
Event.onDOMReady(initCalc);
