var currentRow = -1;
var currentType = "fuel";
var loadMiles = 12000;
var actTrue = true;
var foreids = new Array();
var mousePrev = "";
// similar cars for compare mpg
$(document).ready(function() {
	if (window.balloons_fadeIn)
		balloons_fadeIn();
	var urlArr = createUrlParams();		
	if (getArrayEntry("foreids",urlArr) != "") {
		temp = getArrayEntry("foreids",urlArr);
		temp = unescape(temp);
		foreids = temp.split(",");
		foreids.splice(0,1);
	}		
});			  

function act_fetch(servername, args)
{
	var fullurl = "http://"+servername+"/newcar/companycar/similar/components/similaract.cfm?"+args;
	ajax_sendGet(fullurl,"act_callback_fetch");
}

function act_forecourt(servername,ids,clk,vanchooser)
{
	var fullurl = "http://"+servername+"/newcar/companycar/similar/components/act_forecourt.cfm?foreids="+ids+"&clk="+clk+"&vanchooser="+vanchooser;
	// alert(fullurl);
	ajax_sendGet(fullurl,"fore_callback_fetch");
}

function act_move(e) {
//	var elem = document.getElementById("balloon_block_handle");
//	var posx = 0;
//	var posy = 0;
//	if (!e) var e = window.event;
//	// get height of results div
//
//	// firefox case
//	if (e.pageX || e.pageY) {
//		posx = e.pageX;
//		posy = e.pageY;
//		var rHeight = $('#similar_left .results').height();			
//		var placeY = posy-rHeight;
//	// IE case
//	} 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 rHeight = $('#similar_left').height()+$('.slider_block').height();
//		//var placeY = 0-rHeight+posy;
//		var placeY = posy-rHeight-500;
//	}
//	elem.style.top = placeY+"px";	
}

function act_hover(co2min, co2, co2max, mpgmin, mpg, mpgmax, fuelcost, ved, vedmin, price, pricemin, co, hc, noxpart, ispetrol,
						yAtaxatLow, yBtaxatLow, yCtaxatLow, yAtaxatHigh, yBtaxatHigh, yCtaxatHigh, row,type)
{
	
	currentRow = row;
	currentType = type;
	// cache values for keydown changes
	similar_last_co2 = co2;
	similar_last_mpg = mpg;
	similar_last_fuelcost = fuelcost;
	similar_last_co2min = co2min;
	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);
	co2min = similar_last_co2min * (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 = _act_calc_fueldiff(row,type);
	co2diff = _act_calc_co2diff(co2min, co2);

	// set differences to savings 
	_similar_update_clouds(co2, mpg, fuelcost, fueldiff, co2diff, ved - vedmin, price - pricemin, co, hc, noxpart, ispetrol,
							yAtaxatLow, yBtaxatLow, yCtaxatLow, yAtaxatHigh, yBtaxatHigh, yCtaxatHigh);
	
	_similar_update_bottomcar();
}

// fuel difference for current row
function _act_calc_fueldiff(row,type)
{
	fuelc_min = $("#"+type+"c1").html()*(1-(similar_last_pctge-1));
	fuelc_current = $("#"+type+"c"+row).html()*(1-(similar_last_pctge-1));
	var diff = fuelc_current - fuelc_min;
	return(diff);
}

// co2 difference between 2 values passed in
function _act_calc_co2diff(co2min, co2)
{
	var mileselem = document.getElementById("mileage")
	if (mileselem == null) return;
	var miles = mileselem.value;
	
	co2_min = (miles * 1.61 * co2min);
	co2_current = (miles * 1.61 * co2);
	return(co2_current - co2_min);
}

// recalculates fuel cost cells when percentage is changed
function act_fuelCell(pctge) {
	var fuelcell;
	//update fuel cost in table cell	
	$('#tablemain tr.similarresult').each(function(i) {
		fuelcell = $('#fuelc'+(i+1)).html();
		fuelcell = fuelcell*(1-(pctge-1));
		$('#fuelCell'+(i+1)).html("&pound;"+parseInt(fuelcell));	
	});
	$('#tablefore tr.similarresult').each(function(i) {
		forecell = $('#forec'+(i+1)).html();
		forecell = forecell*(1-(pctge-1));
		$('#foreCell'+(i+1)).html("&pound;"+parseInt(forecell));	
	});	
}

// recalculates fuel cost cells when mileage is changed
function act_mileage(miles) {
	var fuelcell;
	//update fuel cost in table cell	
	$('#tablemain tr.similarresult').each(function(i) {
		i++;
		fuelcell = (282.481/$('#fuelm'+i).html()) * (miles*1.609/100) * $('#fuelp'+i).html();
		$('#fuelc'+(i)).html(fuelcell);		
		fuelcell = fuelcell * (1-(similar_last_pctge-1));
		if (fuelcell < 0) fuelcell = 0;
		$('#fuelCell'+(i)).html("&pound;"+parseInt(fuelcell));
	});
	$('#tablefore tr.similarresult').each(function(i) {
		i++;												   
		forecell = (282.481/$('#forem'+i).html()) * (miles*1.609/100) * $('#forep'+i).html();
		$('#forec'+(i)).html(forecell);		
		forecell = forecell * (1-(similar_last_pctge-1));
		if (forecell < 0) forecell = 0;
		$('#foreCell'+(i)).html("&pound;"+parseInt(forecell));
	});		
	loadMiles = miles;
	_similar_reignInField('mileage')
	_similar_refresh();	
}

// method called from slider after it has been moved
function act_applySliderPercentage(pctge)
{
	act_fuelCell(pctge);
	similar_last_pctge = pctge;
	_act_refresh();
}

function act_ammend() {
	if ( $("#mileage").length > 0 ) {
		var miles = $('#mileage').val();
	} else {
		var miles = loadMiles;
	}
	var pctge = similar_last_pctge;
	if (miles != 12000) {
		act_mileage(miles);
	}
	if (similar_last_pctge != 1) {
		act_applySliderPercentage(pctge);
	}
}

function _act_refresh()
{
	if (similar_last_co2)
	{
		// filter co2 & mpg figures through slider
		co2 = similar_last_co2 * (1/similar_last_pctge);
		co2min = similar_last_co2min * (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 = _act_calc_fueldiff(currentRow,currentType);
		co2diff = _act_calc_co2diff(co2min, co2);
		
		_similar_update_clouds(co2, mpg, null, fueldiff, co2diff, null, null,
							   co, hc, noxpart, null, null, null);
	}
	_similar_update_bottomcar();
}

// adds a car to the forcourt
function act_addToFore(id,server,clk,vanchooser) {
	notListed = true;
	// check if id already in forecourt
	temp = foreids.join();
	if (temp.search(id) >= 0) {
		notListed = false;
	}
	if (foreids.length < 5 && notListed) {
		var temp = $('#foreids').val()+","+id;
		if (temp[1] == ",") {
			temp = temp.slice(1);
		}
		$('#foreids').val(temp);
		loadMiles = $('#mileage').val();		
		act_fetch(server, props_readSelectValues());
		act_forecourt(server, temp, clk, vanchooser);
		foreids.push(id);
	} else if (foreids.length < 5) {
		alert("You can not add the same car to forecourt again");
	} else {
		alert("You can only have 5 cars in the forecourt");
	}
	return false;
}

// removes a car from the forecout
function act_removeFromFore(id,server,clk,vanchooser) {
	var temp = $('#foreids').val();		
	temp = temp.replace(id,"");
	temp = unescape(temp);
	temp = temp.replace(/,[^0-9]+,/,",");
	temp = temp.replace(/^,/,"");
	$('#foreids').val(temp);	
	for (var x = 0;x < foreids.length;x++) {
		if (foreids[x] == id) {
			foreids.splice(x,1);
		}
		if (foreids[x] == "") {
			foreids.splice(x,1);
		}		
	}
	loadMiles = $('#mileage').val();
	act_fetch(server, props_readSelectValues());	
	if (foreids.length > 0) {
		act_forecourt(server,temp,clk,vanchooser);
	} else {
		$('#forecourt').html("");
	}
	return false;
}

function fore_callback_fetch(response) {
	document.getElementById('forecourt').innerHTML = response;
}

function act_callback_fetch(response) {
	$('#similar_left').html(response);
	$('#mileage').val(loadMiles);
	if ( $("p.selectcar").length != 0 ) {
		$('.slider_block').hide();
	}
	act_ammend();	
}

$(document).ready(function () {
	act_fetch(serverName, props_readSelectValues());							
});
