/*!
 * BAC Calculator Front-end
 *
 * @fileoverview BAC Calc
 * @depends 
 * @author pjkix <pj@pjkix.com
 * @copyright (cc) 2010 pjkix
 * @license http://creativecommons.org/license/by-nc-nd/3.0
 * @version $Id: bac-calc.js 812 2010-04-05 05:20:38Z pjkix $
 * @todo make it work!
 */

/* -*- mode: c; tab-width: 0; c-basic-offset: 2; comment-column: 0 -*- */

/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxerr: 50, indent: 4 */

/*globals console */

/*members counter, browser, checked, className, computeBAC, ddd, foo, 
    getElementById, getNumDrinks, init, length, log, percentToProof, 
    proofToPercent, round, sex, subtotalAlc, value, version
*/

"use strict";

/**
 * BAC Object
 */
var BAC = {
	version: '2.0.2a',
	counter: 0,
	browser: {},
	foo: [],

	init: function () {
		if (!document.getElementById) {
			return;
		}
		var bw = document.getElementById('bac-wrapper');
		bw.className = 'bac-form';
	},
	
	// get the amount of pure alcohol
	subtotalAlc: function (numDrinks, ozDrinks, percentAlc) {
		var pureAlc = ((numDrinks * ozDrinks) * (percentAlc / 100));
		return pureAlc;
	},

	// multiply percent by two to get proof
	percentToProof: function (percent) {
		var proof = (percent * 2);
		return proof;
	},

	// Divide proof by two to get percent alcohol. For instance, 80 proof is 40%
	proofToPercent: function (proof) {
		var percent = (proof / 2);
		return percent;
	},

	// figure out the number of drinks
	getNumDrinks: function (pureAlc, percentAlc, ozDrinks) {
		var numDrinks = ((pureAlc / (percentAlc / 100)) / ozDrinks);
		return Math.round(numDrinks);
	},

	computeBAC: function (sex, weight, time, pureAlc) {
		// standard input
		var bodyWeight, hours, widmarkR, widmarkB, bloodWeight, pureAlcWeight, burnOff, reducedBodyMass, bac, peakBAC, totalPureAlcOz;

		bodyWeight = document.getElementById('weight'); // body weight of person
		hours = document.getElementById('hours').value; // amount of time since first drink
		// check gender of person to determinen widmarkR
		// sex = -1;
		// for (i = 0; i < form.sex.length; i + 1) {
		//   if (form.sex[i].checked) {
		//     sex = i;
		//   }
		// }
		// if (sex == -1) {
		//  lert("You must choose a sex");
		//  return false;
		// }

		//  Reduced body mass. Widmark r: Men: 0.50 to 0.90, average 0.68. Women: 0.45 to 0.63, average 0.55.
		if (sex === "0") { // male
			widmarkR = 0.68;
		} else { // female
			widmarkR = 0.55;
		}

		// constants
		widmarkB = 0.017; // burn off rate of alcohol absorbtion. Widmark ß: 0.010%/hr to 0.024%/hr, average 0.17%/hr.
		bloodWeight = 1.055; // gravity of blood 1.055 g/ml
		pureAlcWeight = 0.0514; // weight of pure alcohol 0.0514 lbs/oz


		// define vars
		//var pureAlcOz = ((numDrinks * ozDrink) * (percentAlc / 100)); // total amount of EtOH or pure alcohol drank
		burnOff = (hours * widmarkB); // burn off rate of alcohol absorbtion
		reducedBodyMass = (bodyWeight * widmarkR);
		peakBAC = ((totalPureAlcOz * pureAlcWeight * 100 * bloodWeight) / reducedBodyMass); // calculate peak BAC level without burn off


		// Widmark Formula for BAC
		// Used to determine BAC taking into account metabolism time, sex, and blood weight
		// [(pureAlcOz * .0514 * 100% * 1.055) / (bodyWeight * widmarkR)] - (hours * widmarkB) = BAC%
		//var bac = ((pureAlcOz * pureAlcWeight * 100 * bloodWeight) / (bodyWeight * widmarkR)) - (hours * widmarkB) ;
		bac = peakBAC - burnOff; // simplified version

		// Widmark 8/10 Method (simplified Widmark formula - less accurate)
		// Used to determine BAC taking into account metabolism time, sex, and blood weight
		// Male = 8 Female = 10
		// ([pureAlcOz * ( 8 or 10 )] / bodyWeight) - (hours * widmarkB) = BAC%
		//if (sex == "male") { var sexNum = 8 } if (sex == "female") { var sexNum = 10 } ;
		//var bac = ((pureAlcOz * sexNum / bodyWeight) - (bours * widmarkB)) ;
	}

};//();


// set up event listeners and stuff here ...


// window.onload=BAC.init;


// save settings in cookie ... save drinks in local db?

// save last drink timestamp ... + this drink timestamp + add total drinking time

