var doSearch = false;
var dateChosen = false;
var countryChosen = false;
var loadingMessage = 0;

// from http://www.quirksmode.org
function takeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}


function formatDate(date)
{
	var day = date.getDate();
	var month = date.getMonth() + 1;
	var year = takeYear(date);
	return year + '-' + month + '-' + day;
}


function selectCell(date)
{	
	var cellId = 'date-' + formatDate(date);
	var cell = $($(cellId));
	
	if (cell)
	{
		cell.addClassName('selectedday');
	}
}


function late_choose_date(year, month, day)
{
	dateChosen = true;
	currentDate = new Date(year, (month -1), day, 12, 0, 0);
	if (countryChosen)
	{
		$('late-static').hide();
		late_update_new();
	}
	else
	{
		alert('Please select a country before selecting a date');
	}
	return false;
}

function late_page(pageNum)
{
	currentPage = pageNum;
	late_update();
	return false;
}

var timer = false;

function late_loading()
{
	timer = setTimeout('big_loading()', 600);
	//$($('late_avail_content')).hide();
	var small = $($('search_indicator'));
	
	if (small)
	{
		small.show();
	}
	else
	{
		$($('late-avail-loading')).show();
	}
}

function big_loading()
{
	$($('late_avail_content')).hide();
	$($('late-avail-loading')).show();
	showNextLoadingMessage();
	
	
	
}

var loadtimeout = false

function showNextLoadingMessage()
{
	var messages = $$('#late-avail-loading p[id^=lm]'); 
	var next = (loadingMessage + 1) % (messages.size() +1) ;
	
	
	var id = "#lm_" + next;
	$(messages).each(function(e){
	
		if($(e).match(id))
		{
			$(e).show();
			loadtimeout = setTimeout(function() {showTravelTeamLoadingMessage(e);}, 3000);
			
		}
		else {
			if((e).visible())
			{
				$(e).hide();
			}
		}
	});
	loadingMessage = next;

}

function showTravelTeamLoadingMessage(e) 
{
	$(e).hide();
	$('lm_tt').show();
}

function late_loaded()
{
	clearTimeout(timer);
	var indicators = $$('div.loading');
	indicators.each(function(indicator) {
		indicator.hide();
	});
	
	$($('late_avail_content')).show();
}

var myAjax = false;

function late_update_new()
{
	$($('late-static')).hide()
	currentPage = 1;
	late_update();
}

function late_update()
{
	late_loading();
	
	var currentSelected = $$('.calendar .selectedday');
	currentSelected.each(function(td) {
		$(td).removeClassName('selectedday');
	});
	
	var nights = $('nights').value;
	
	for (var i = 0; i < nights; i++)
	{
		var tempDate = new Date(takeYear(currentDate), currentDate.getMonth(), (currentDate.getDate() + i), 12, 0, 0);
		selectCell(tempDate);
		//alert(i + ' - ' + tempDate.toUTCString());
	}
	
	//TODO : update hotel display
	
	var pars = 'ajax=true&nights=' + nights + '&adults=' + $('adults').value + '&date=' + formatDate(currentDate) + '&page=' + currentPage;

	myAjax = new Ajax.Updater(
		'late_avail_content', 
		url, 
		{
			method: 'get', 
			parameters: pars,
			onComplete: late_loaded
		});
}



window.onload = function() 
{
	if (doSearch)
	{
		$($('late-static')).hide();
		big_loading();
		late_update();
	}
	populate_quick_links();
	
	if('' != $('country').value)
	{
		countryChosen = true;
		url = $('country').value;
	}
}


