var scalefactor = 1;

var parkingtime = '';
var checkintime = '';

function parseHumanDelay(delaystr)
{
	var tokens = delaystr.match(/^([0-9]*?) min/);
	if (tokens != null && tokens.length)
	{
		return parseInt(tokens[1]);
	}
	else
	{
		tokens = delaystr.match(/^([0-9]*?) hrs? ([0-9]*?) min/);
		if (tokens != null)
		{
			return (parseInt(tokens[1]) * 60) + parseInt(tokens[2]);
		}
		return 0;
	}
}
function humanDelay(minutes)
{
	if (minutes == null || minutes == 0) return "0 min";
	if (minutes >= 60)
	{
		var hrs = Math.floor(minutes / 60);
		if (hrs == 1)
			return hrs + " hr " + (minutes % 60) + " min";
		else
			return hrs + " hrs " + (minutes % 60) + " min";
	}
	else
	{
		return minutes + " min";
	}
}

function shortHumanDelay(minutes)
{
	hours = Math.floor(minutes/60);
	mins  = minutes%60;

	return ('0' + hours).substr(-2) + ":" + ('0' + mins).substr(-2);
}

var original_parking_time = null;

function showHideParking()
{
	$('#parking .delays').toggle();
	$('#graphparking').toggle();
	
	totalspan = $('h2#totaltime span.time');
	timetotal = parseHumanDelay(totalspan.text());

	parkingspan = $('#parking h3 span.time');
	original_parking_time = parkingtime = original_parking_time || parseHumanDelay(parkingspan.text());

	var newtimetotal;
	if (this.checked)
	{
		newtimetotal = timetotal - parkingtime;
		parkingspan.text(humanDelay(0));
	}
	else
	{
		newtimetotal = timetotal + parkingtime;
		parkingspan.text(humanDelay(parkingtime));
	}

	totalspan.text(humanDelay(newtimetotal));
	$('#graphfirsttotal span.graphTime').text(shortHumanDelay(newtimetotal));
	$('#graphfirsttotal .graph img').attr('width', Math.min(newtimetotal, 180) * 1.6);
}

var original_checkin_time = null;
function showHideCheckin()
{
	$('#checkin .delays').toggle();
	$('#graphcheckin').toggle();
	
	totalspan = $('h2#totaltime span.time');
	timetotal = parseHumanDelay(totalspan.text());

	checkinspan = $('#checkin h3 span.time');
	original_checkin_time = checkintime = original_checkin_time || parseHumanDelay(checkinspan.text());

	var newtimetotal;
	if (this.checked)
	{
		newtimetotal = timetotal - checkintime;
		checkinspan.text(humanDelay(0));
	}
	else
	{
		newtimetotal = timetotal + checkintime;
		checkinspan.text(humanDelay(checkintime));
	}

	totalspan.text(humanDelay(newtimetotal));
	$('#graphfirsttotal span.graphTime').text(shortHumanDelay(newtimetotal));
	$('#graphfirsttotal .graph img').attr('width', Math.min(newtimetotal, 180) * 1.6);
}

var ajaxy = 0;
var doneAirportOnce = false; 

function airportInfoLoadCallback(responseData)
{
	$('#airport_loading').hide();
	$('#airport_info').html(responseData).animate({height: 'show'}, 'slow');
}

function selectedAirportCode()
{
	/* if(selectedAirportTimer)
	{
		clearInterval(selectedAirportTimer);
	}
	var selectedAirport = $("input[name='selectairportcode']").val();
	if(ajaxy && selectedAirport.indexOf('Enter ')==-1)
	{
		var selectedAirport = $("input[name='selectairportcode']").val();
	
		if (!doneAirportOnce) $('#airport_loading').show();
		$.post(
			"/flight/airportinfo", 
			{
				selectairportcode: selectedAirport
			},
			airportInfoLoadCallback
		); */
		doneAirportOnce = true;
	//}
}

var blanker = function(event){if (this.value == event.data.defaultValue) this.value = '';};
var unblanker = function(event)
{
	if ($.trim(this.value) == '' || this.value == event.data.defaultValue) this.value = event.data.defaultValue;
	else if (this.name == 'selectairportcode') selectedAirportCode();
};
	
/*  This code will be executed after the page has loaded, but before the images have loaded */
jQuery.extend({log:function(){console.log(this);}});
$(function(){
	// first, set this properly
	ajaxy = $('#formResults').length==0 ? 1 : 0;
	
	$("input[name='selectairportcode']")
		.autocomplete('/flight/chooseairport',{
			cacheLength: 15,
			onItemSelect: selectedAirportCode
		});/*.bind('focus', 
			{defaultValue: 'Enter airport name or code'}, blanker
		).bind('blur',
			{defaultValue: 'Enter airport name or code'}, unblanker
		);*/
	
	
	$("input[name='airline']")
		.autocomplete('/flight/chooseairline', {
			cacheLength: 10
		});/*.bind('focus', 
			{defaultValue: 'Enter airline name or code'}, blanker
		).bind('blur',
			{defaultValue: 'Enter airline name or code'}, unblanker
		);*/
	
	
	$("input[name='flight_number']")
		.bind('keydown', function(event) {
			if (event.keyCode==13) this.form.submit(); // on return
		});/*.bind('focus', 
			{defaultValue: 'Enter flight number'}, blanker
		).bind('blur',
			{defaultValue: 'Enter flight number'}, unblanker
		);
	*/
	
	$("input[name='direction']")
		.change(function(){
			var newval = this.value;
			if (!doneAirportOnce && $("input[name='origairportcode']").length)
			{
				$("input[name='selectairportcode']").val(
					newval=='departing' ? 
						$("input[name='origairportcode']").val() : 
						$("input[name='destairportcode']").val()
				);
			}
		})
	;
	
	// checkbox stuff for details
	if ($('#parking_toggle"'))
	{
		$("#parking_toggle").click(showHideParking);
	}

	if ($('#checkin_toggle"'))
	{
		$("#checkin_toggle").click(showHideCheckin);
	}
});


