// Initialize the LoanCalculator object
GasCalc = function() {
	this.init();
};

GasCalc.prototype.init = function() { 
	this.write_div = document.getElementById( 'gas_calc' );
	if (this.write_div) {
		this.buildInterface();
	}
};

GasCalc.prototype.buildInterface = function() {
	// Build the containing element from the CalcLib library and return the elements
	var els = buildTable( 'gas_calc', 'Gas Consumption Calculator' );
	var inner = els[0];
	var table = els[1];
	var tbody = els[2];
	this.buildTable( tbody );
};

GasCalc.prototype.buildTable = function( tbody ) {
	this.el_array = new Array();
	this.el_array['mpg'] = new Array( 'Average MPG', 'input', 'Your combined city/highway miles per gallon.' );
	this.el_array['miles'] = new Array( 'Miles Per Week', 'input', 'The average amount of miles you drive each week.' );
	this.el_array['gas'] = new Array( 'Gost of Gas', 'input', 'The cost of gasoline per gallon in your area.' );
	this.el_array['mpy'] = new Array( 'Miles Per Year', 'span', '' );
	this.el_array['gpy'] = new Array( 'Gallons Per Year', 'span', '' );
	this.el_array['yearly'] = new Array( 'Yearly Gas Cost', 'span', '' );
	this.el_array['weekly'] = new Array( 'Weekly Gas Cost', 'span', '' );
	this.el_array['per_mile'] = new Array( 'Cost Per Mile', 'span', '' );

	
	for( var el in this.el_array) {
		var tr = document.createElement( 'tr' );
		var td1 = document.createElement( 'td' );
		td1.className = 'td-label';
		td1.innerHTML = this.el_array[el][0];
		
		if (this.el_array[el][2] != '') {
			buildTooltip( td1, this.el_array[el][2] );
		}
		
		tr.appendChild( td1 );
		var td2 = document.createElement( 'td' );
		td2.className = 'td-input';
		var elem = document.createElement( this.el_array[el][1] );
		elem.id = el;
		if (this.el_array[el][1] == 'input') {
			elem.type = 'text';
			elem.size = 8;
			//elem.onclick = function() { this.select(); };
		}

		td2.appendChild( elem );
		tr.appendChild( td2 );
		
		if (this.el_array[el][1] == 'span') {
			tr.className = 'tr-value';
		} 
		
		if (el == 'mpg') {
			var td4 = document.createElement( 'td' );
			td4.rowSpan = 8;
			td4.className = 'td-ad';
			this.buildAd( td4 );
			tr.appendChild( td4 );
		}
		
		tbody.appendChild( tr );	
		
	
	}
	this.createButtons( tbody );
	this.preFill();
};

GasCalc.prototype.buildAd = function( td4 ) {
	var a = document.createElement( 'a' );
	a.tabIndex = -1;
	a.href = 'http://www.tkqlhce.com/click-3080792-10408874';
	a.target = '_blank';
	
	var img = document.createElement( 'img' );
	img.src = 'http://www.awltovhc.com/image-3080792-10408874"';
	img.alt = 'Locate the Car You Want at the Price You Want';
	img.width = 125;
	img.height = 125;
	a.appendChild( img );
	td4.appendChild( a );
};

GasCalc.prototype.createButtons = function( tbody ) {
	var obj = this;
	var tr = document.createElement( 'tr' );
	var td = document.createElement( 'td' );
	td.colSpan = "4";
	td.className = 'td-buttons';
	td.align = 'right';
	var but_calc = document.createElement( 'button' );
	but_calc.innerHTML = 'Calculate';
	
	but_calc.onclick = function() { obj.calculate(); };
	
	td.appendChild( but_calc );
	tr.appendChild( td );
	tbody.appendChild( tr );
};

GasCalc.prototype.preFill = function() {
	document.getElementById( 'mpg' ).value = '20';
	document.getElementById( 'miles' ).value = '200';
	document.getElementById( 'gas' ).value = '4.00';
	document.getElementById( 'mpg' ).focus();
	document.getElementById( 'mpg' ).select();
};

GasCalc.prototype.calculate = function( ) {
	var mpg = document.getElementById( 'mpg' ).value;
	var miles = document.getElementById( 'miles' ).value * 52;
	var cost = document.getElementById( 'gas' ).value;
	var cpy = ( miles / mpg ) * cost;
	document.getElementById( 'mpy' ).innerHTML = formatNumber( miles, false );
	document.getElementById( 'gpy' ).innerHTML = formatNumber( ( miles / mpg ).toFixed(2), false );
	document.getElementById( 'yearly' ).innerHTML = formatNumber( cpy, true );
	document.getElementById( 'weekly' ).innerHTML = formatNumber( cpy / 52, true  );
	document.getElementById( 'per_mile' ).innerHTML = formatNumber( cpy / miles, true );
};

gasCalc = new GasCalc();