var msg;
var flightCalendar;
var carCalendar;
var fromSuggest;
var toSuggest;

Event.observe(window, 'load', initializeMsgBox, false);

function initializeMsgBox() {
	// ### GLASS + MESSAGE BOX
	var glassOptions = {
		color: '#003366',
		opacity: 0.5
	}
	var glass = new Gibbon.Glass(glassOptions);

	msg = new Gibbon.MessageBox('MsgBox', glass);	
}

function searching() {
	progress = 0;
	progressbar();
	msg.show();
}

//Event.observe(window, 'beforeunload', progressbar, false);
var progress = 0;
var progressAnimId = 0;
function progressbar() {
	progressAnimId = window.setTimeout('progressbar()', 200);
	progress += 8;
	if (progress > 400) progress = 0;
	Element.setStyle('progress', { width: (progress+'px') });
}

function cancelSearch() {
	if (document.all) {
		document.execCommand("Stop");
	} else {
		window.stop();
	}
	msg.hide();	
	window.clearTimeout(progressAnimId);
}

function initializeFlightForm() {
	// ### FIELD FUNCTIONS
	Gibbon.Form.autoSelectText('fromName');
	Gibbon.Form.autoSelectText('toName');
	Gibbon.Form.blockEnter('fromName');
	Gibbon.Form.blockEnter('toName');

	// ### CALENDAR
	var now = new Gibbon.Date();
	flightCalendar = new Gibbon.Calendar(now.getYear(), now.getMonth(), {
		id: 'flightCalendar',
		range: {
			start: new Gibbon.Date(2010, 1, 1), 
			stop: new Gibbon.Date(2012, 1, 1)
		}
	});	
	flightCalendar.dateChangedEvent = function(date) {
		var prefix = date.index == 0 ? 'departure' : 'return';
		$(prefix+'Month').value = date.date.getYear() + '-' + date.date.getMonth(true);
		$(prefix+'Day').value = date.date.getDay();
	}

	// ### AUTOCOMPLETE
	fromSuggest = new TextSuggest('fromName', '/cityAjaxSearch.do', { 
		matchAnywhere      : true,
		ignoreCase         : true,
		selectionColor     : '#336699',
		createHiddenInput  : false,
		hiddenInput        : 'fromIATA'        
	});
	toSuggest = new TextSuggest('toName', '/cityAjaxSearch.do', { 
		matchAnywhere      : true,
		ignoreCase         : true,
		selectionColor     : '#336699',
		createHiddenInput  : false,
		hiddenInput        : 'toIATA'    
	});
	
	// ### TOGGLE CHILDREN AGES
	toggleChildAges();	
}

function updateFlightCalendar(index) {	
	var prefix = index == 0 ? 'departure' : 'return';
	var yearMonth = $(prefix+'Month').value.split('-');
	var year = parseInt(yearMonth[0], 10);
	var month = parseInt(yearMonth[1], 10);
	var day = parseInt($(prefix+'Day').value, 10);
	day=1;
	if (!isNaN(year) && !isNaN(month) && !isNaN(day)) {
		flightCalendar.setDate(year, month, day, true, false, false);
	}
}

function initializeCarForm() {
	// ### FIELD FUNCTIONS
	Gibbon.Form.autoSelectText('pickupCity');
	Gibbon.Form.autoSelectText('dropoffCity');
	Gibbon.Form.blockEnter('pickupCity');
	Gibbon.Form.blockEnter('dropoffCity');

	// ### CALENDAR
	var now = new Gibbon.Date();	
	carCalendar = new Gibbon.Calendar(now.getYear(), now.getMonth(), {
		id: 'carCalendar',
		range: {
			start: new Gibbon.Date(2006, 1, 1), 
			stop: new Gibbon.Date(2010, 1, 1)
		}
	});		
	carCalendar.dateChangedEvent = function(date) {
		var prefix = date.index == 0 ? 'pickup' : 'dropoff';
		$(prefix+'Month').value = date.date.getYear() + "-" + date.date.getMonth(true);
		$(prefix+'Day').value = date.date.getDay();
	}	

	// ### AUTOCOMPLETE
	pickupSuggest = new TextSuggest('pickupCity', '/cityAjaxSearch.do', { 
		matchAnywhere      : true,
		ignoreCase         : true,
		selectionColor     : '#336699',
		createHiddenInput  : false,
		hiddenInput        : 'puIA'        
	});
	dropoffSuggest = new TextSuggest('dropoffCity', '/cityAjaxSearch.do', { 
		matchAnywhere      : true,
		ignoreCase         : true,
		selectionColor     : '#336699',
		createHiddenInput  : false,
		hiddenInput        : 'doIA'    
	});
}

function updateCarCalendar(index) {
	var prefix = index == 0 ? 'pickup' : 'dropoff';	
	var yearMonth = $(prefix+'Month').value.split('-');
	var year = parseInt(yearMonth[0], 10);
	var month = parseInt(yearMonth[1], 10);
	var day = parseInt($(prefix+'Day').value, 10);
	if (!isNaN(year) && !isNaN(month) && !isNaN(day)) {
		carCalendar.setDate(year, month, day, true, false, false);
	}
}

function toggleDropoff() {
	var dropoffField = $('dropoffField');
	dropoffField.toggle();
	if (dropoffField.visible()) {
		$('dropoffCity').focus();
	}
}

function toggleTransTypes() {
	$('transTypeFields').toggle();
}

function toggleCarTypes() {
	$('carTypeFields').toggle();
}

function toggleChildAges() {
	var children = $('children');
	if (children != null && children.options != null) {
		var count = children.options[children.selectedIndex].value;		
		for (var i = 1; i <= 8; i++) {
			if (count >= i) {
				$('child' + (i-1)).show();
			} else {
				$('child' + (i-1)).hide();
			}
		}
	}
}

$(function() {
$("#flightSearchForm").submit(function(){
	/*
	var prefix = index == 0 ? 'departure' : 'return';
	var yearMonth = $(prefix+'Month').value.split('-');
	var year = parseInt(yearMonth[0], 10);
	var month = parseInt(yearMonth[1], 10);
	var day = parseInt($(prefix+'Day').value, 10);
	$("#departureMonthYear").val($.datepicker.formatDate('yy-mm', selDate));
	$("#departureDay").val($.datepicker.formatDate('dd', selDate));
	$("#returnMonthYear").val($.datepicker.formatDate('yy-mm', $("#returnDatePicker").datepicker("getDate")));
	$("#returnDay").val($.datepicker.formatDate('dd', $("#returnDatePicker").datepicker("getDate")));*/
});
});
