// similar car finder javascript

// requires commonjs/ajax.js
var actTrue = false;
var similar_similar_callback_elem = null;
function similar_fetch(servername, clk, carMake, carModel, carDerv)
{
	var elem = document.getElementById('similar_left');
	similar_callback_elem = elem;
	// escape the arguments
	carMake = escape(carMake);
	carModel = escape(carModel);
	// manually stitch '+' which seems to get lost in transit
	carDerv = escape(carDerv.replace('+','%2B'));
	// build request
	var request = "http://"+servername+"/newcar/companycar/similar/components/similarcars.cfm?clk="+clk+"&make="+carMake+"&model="+carModel+"&derivative="+carDerv;
	// send the request
	// document.write(request);
	ajax_sendGet(request, "similar_callback_fetch");
}

function similar_callback_fetch(response)
{
	if (similar_callback_elem != null)
		similar_callback_elem.innerHTML = response;
}

var similar_expand_status = 0;
function similar_expand()
{
	var tx = document.getElementById("similar_exp_text");
	var tc = document.getElementById("similar_con_text");
	var bk = document.getElementById("similar_exp_block");
	if ((tx != null) && (bk != null))
	{
		if (similar_expand_status)
		{
			// contract
			tx.style.display = 'block';
			tc.style.display = 'none';
			bk.style.display = 'none';
			var elem = document.getElementById("similar_expand_showall");
			if (elem != null)
				elem.innerHTML = 'Show all';
		} else {
			// expand
			tx.style.display = 'none';
			tc.style.display = 'block';
			bk.style.display = 'block';
			var elem = document.getElementById("similar_expand_showall");
			if (elem != null)
				elem.innerHTML = 'Show fewer';
		}
	}

	// toggle var
	similar_expand_status = 1 - similar_expand_status;
}

function similar_move(e) {
//	var elem = document.getElementById("balloon_block_handle");
//	var posx = 0;
//	var posy = 0;
//	if (!e) var e = window.event;
//	if (e.pageX || e.pageY) 	{
//		posx = e.pageX;
//		posy = e.pageY;
//	}
//	else if (e.clientX || e.clientY) 	{
//		posx = e.clientX + document.body.scrollLeft
//			+ document.documentElement.scrollLeft;
//		posy = e.clientY + document.body.scrollTop
//			+ document.documentElement.scrollTop;
//	}
//	var rTop = $('#similar_left').offset();
//	var rHeight = $('#similar_left').height();
//	rTop = rTop.top; 	
//	elem.style.top = (-rHeight+(posy-rTop-300))+"px";
}

var similar_last_co2 = null;
var similar_last_mpg = null;
var similar_last_co2max = null;
var similar_last_mpgmax = null;
var similar_last_co = null;
var similar_last_hc = null;
var similar_last_noxpart = null;

function similar_hover(co2min, co2, co2max, mpgmin, mpg, mpgmax, fuelcost, ved, vedmax, price, pricemax, co, hc, noxpart, ispetrol,
						yAtaxatLow, yBtaxatLow, yCtaxatLow, yAtaxatHigh, yBtaxatHigh, yCtaxatHigh)
{
	// alert('co2min='+co2min+', co2='+co2+',  co2max='+co2max+',  mpgmin='+mpgmin+',  mpg='+mpg+',  mpgmax='+mpgmax+',  fuelcost='+fuelcost+',  ved='+ved+',  vedmax='+vedmax+',  price='+price+',  pricemax='+pricemax);
	// cache values for keydown changes
	similar_last_co2 = co2;
	similar_last_mpg = mpg;
	similar_last_fuelcost = fuelcost;
	similar_last_co2max = co2max;
	similar_last_mpgmax = mpgmax;
	similar_last_co = co;
	similar_last_hc = hc;
	similar_last_noxpart = noxpart;

	// filter co2 & mpg figures through slider
	co2 = similar_last_co2 * (1/similar_last_pctge);
	co2max = similar_last_co2max * (1/similar_last_pctge);
	mpg = similar_last_mpg * similar_last_pctge;
	mpgmax = similar_last_mpgmax * similar_last_pctge;
	// filter co, hc & nox/particle figures through slider
	co = similar_last_co * (1/similar_last_pctge);
	hc = similar_last_hc * (1/similar_last_pctge);
	noxpart = similar_last_noxpart * (1/similar_last_pctge);

	// calculate differences
	fueldiff = _similar_calc_fueldiff(mpgmax, mpg, fuelcost);
	co2diff = _similar_calc_co2diff(co2max, co2);

	// set differences to savings balloon
	_similar_update_clouds(co2, mpg, fuelcost, fueldiff, co2diff, vedmax - ved, pricemax - price, co, hc, noxpart, ispetrol,
							yAtaxatLow, yBtaxatLow, yCtaxatLow, yAtaxatHigh, yBtaxatHigh, yCtaxatHigh);
	
	_similar_update_bottomcar();
}

var similar_last_pctge = 1;
function similar_applySliderPercentage(pctge)
{
	similar_last_pctge = pctge;
	_similar_refresh();
}

function similar_keydown()
{
	_similar_reignInField('mileage')
	_similar_refresh();
}

function _similar_refresh()
{
	if (similar_last_co2)
	{
		// filter co2 & mpg figures through slider
		co2 = similar_last_co2 * (1/similar_last_pctge);
		co2max = similar_last_co2max * (1/similar_last_pctge);
		mpg = similar_last_mpg * similar_last_pctge;
		mpgmax = similar_last_mpgmax * similar_last_pctge;
		// filter co, hc & nox/particle figures through slider
		co = similar_last_co * (1/similar_last_pctge);
		hc = similar_last_hc * (1/similar_last_pctge);
		noxpart = similar_last_noxpart * (1/similar_last_pctge);
	
		fueldiff = _similar_calc_fueldiff(mpgmax, mpg, similar_last_fuelcost);
		co2diff = _similar_calc_co2diff(co2max, co2);
		_similar_update_clouds(co2, mpg, null, fueldiff, co2diff, null, null,
							   co, hc, noxpart, null, null, null);
	}
	_similar_update_bottomcar();
}

function _similar_reignInField(fieldid)
{
	if ( $("#"+fieldid).length > 0 ) {
		var elem = $("#"+fieldid);
		if (typeof(elem) != "undefined") {
			var x = parseFloat(elem.value);
			if (x > 999000)
			{
				elem.value = "999000";
				alert("Are you going for a new Guiness World record?");
			}
		}
	}
}

function _similar_calc_fueldiff(mpgmax, mpg, fuelcost)
{
	var mileselem = document.getElementById("mileage")
	if (mileselem == null) return;
	var miles = mileselem.value;
	fuelc_max = ((miles/mpgmax) * 4.54609 * fuelcost/100);
	fuelc_current = ((miles/mpg) * 4.54609 * fuelcost/100);
	return(fuelc_max - fuelc_current);
}

function _similar_calc_co2diff(co2max, co2)
{
	var mileselem = document.getElementById("mileage")
	if (mileselem == null) return;
	var miles = mileselem.value;
	
	co2_max = (miles * 1.61 * co2max);
	co2_current = (miles * 1.61 * co2);
	return(co2_max - co2_current);
}

function _similar_update_bottomcar()
{
	var mileselem = document.getElementById("mileage")
	if (mileselem == null) return;
	var fuelelem = document.getElementById("fuelcost")
	if (fuelelem == null) return;
	var mpgelem = document.getElementById("basempg")
	if (mpgelem == null) return;
	var fueltot = document.getElementById("fueltotal")
	if (fueltot == null) return;

	var miles = mileselem.value;
	var fuelcost = fuelelem.value;
	var mpg = mpgelem.value * similar_last_pctge;
	fueltot.value = Math.round(fuelcost/100 * miles/mpg * 4.54609);
}

function _similar_update_clouds(co2, mpg, fuel, fueldiff, co2diff, ved, price, co, nox, hcpart, ispetrol,
								yAtaxatLow, yBtaxatLow, yCtaxatLow, yAtaxatHigh, yBtaxatHigh, yCtaxatHigh)
{
	/*
	 * Blue and Black clouds
	 */
	if ((mpg != null) && (mpg != 0)) {
		var top = document.getElementById("balloon_top");
		if (top != null)
			top.innerHTML = Math.round(mpg*10)/10 + "mpg";
	} else {
		var top = document.getElementById("balloon_top");
		top.innerHTML = "n/a";
	}
	if ((co2 != null) && (co2 != 0)){
		var bottom = document.getElementById("balloon_bottom");
		if (bottom != null)
			bottom.innerHTML = Math.round(co2) + "g/km";
	} else {
		bottom.innerHTML = "n/a";
	}
	
	/*
	 *  Green cloud
	 */
	if (fuel != null)
	{
		var fcost = document.getElementById("fuelcost");
		if (fcost != null)
			fcost.value = fuel;
	}
	var asfuel = document.getElementById("balloon_as_fuel");
	if (asfuel != null){//alert(fueldiff);
		if (fueldiff == "-Infinity") {
			asfuel.innerHTML = "n/a"
		} else {
			asfuel.innerHTML = "&pound;" + Math.round(fueldiff);
		}
	}
	var asco2 = document.getElementById("balloon_as_co2");
	if (asco2 != null)
		asco2.innerHTML = Math.round((co2diff) / 1000)+"&nbsp;kg";
	if (ved != null)
	{
		var asved = document.getElementById("balloon_as_ved");
		if (asved != null)
			asved.innerHTML = "&pound;" + Math.round(ved);
	}
	if (price != null)
	{
		var asprice = document.getElementById("balloon_as_price");
		if (asprice != null)
		{
			if (price < -10000)
			{
				asprice.innerHTML = Math.round(price);
			} else {
				asprice.innerHTML = "&pound;" + Math.round(price);
			}
		}
	}

	/*
	 *  Brown cloud - Emissions (update always if field available)
	 */
	var aspetro = document.getElementById("ballon_as_hcpart_title");
	if (aspetro != null)
	{
		if (ispetrol == null)
		{
			// leave as is, don't change
		} else if (ispetrol == true) {
			aspetro.innerHTML = "HC";
		} else {
			aspetro.innerHTML = "Partics";
		}
	}
	var asco = document.getElementById("balloon_as_co");
	if (co == 0) co = null;
	if (asco != null)
	{
		if (co != null)
			asco.innerHTML = Math.round(co * 1000)/1000;
		else
			asco.innerHTML = "n/a";
	}
	var asnox = document.getElementById("balloon_as_nox");
	if (nox == 0) nox = null;
	if (asnox != null)
	{
		if (nox != null)
			asnox.innerHTML = Math.round(nox * 1000)/1000;
		else
			asnox.innerHTML = "n/a";
	}
	var ashcpart = document.getElementById("balloon_as_hcpart");
	
	if (ashcpart != null){
		// fix to display "n/a" if the partics are less than 0
		if (hcpart > 0) {
			
				ashcpart.innerHTML = hcpart.toFixed(3);
				
			} else {
				
				ashcpart.innerHTML = "n/a";
		}
	}
	
	/*
	 *  Brown cloud - tax data
	 */
	_similar_update_clouds_taxSinglet("balloon_as_yAtaxatLow", yAtaxatLow);
	_similar_update_clouds_taxSinglet("balloon_as_yBtaxatLow", yBtaxatLow);	
	_similar_update_clouds_taxSinglet("balloon_as_yCtaxatLow", yCtaxatLow);	
	_similar_update_clouds_taxSinglet("balloon_as_yAtaxatHigh", yAtaxatHigh);
	_similar_update_clouds_taxSinglet("balloon_as_yBtaxatHigh", yBtaxatHigh);	
	_similar_update_clouds_taxSinglet("balloon_as_yCtaxatHigh", yCtaxatHigh);	

}

function _similar_update_clouds_taxSinglet(fieldname, value)
{
	if (value != null)
	{
		var elem = document.getElementById(fieldname);
		if (elem != null)
			elem.innerHTML = "&pound;" + Math.round(value);
	}
}

var similar_lastelem = null;
function similar_rolloverColour(elemname)
{
	var listelem = document.getElementById(elemname);
	if (similar_lastelem != null)
		similar_lastelem.className = "similarresult";		
	if (listelem != null)
		listelem.className = "similarresult rollover";
	similar_lastelem = listelem;
}

