var locations = new Array();
var locationsUrl = new Array();
var locationsName = new Array();
var locationMessages = new Array();

function locationSearch(id)
{
	var searchField = $('#'+id+'_txt');
	if(searchField && searchField.val() != '' && locationsUrl[id])
	{
		$.ajax({
			type: 'GET',
			url: locationsUrl[id],
			data: ({locationSearch: searchField.val(), allowCountry: locations[id]}),
			dataType: 'json',
			beforeSend: function() {locationClear(id); searchField.addClass('small_loading');},
			success: function(results, textStatus) {
				searchField.removeClass('small_loading');
				if(results['results'].length>0) buildResults(id, results);
				else searchField.addClass('small_error').attr('title', locationMessages['no_results']);
			},
			error: function() {
				searchField.removeClass('small_loading');
				searchField.addClass('small_error').attr('title', locationMessages['search_error']);
			}
		});
	} else {
		if(typeof updateTraficType == 'function')
		{
			updateTraficType();
		}
	}
}

function buildResults(id, results)
{
	// clear fields :
	// result area
	result = $('#'+id+'_results');
	result.html('');
	result.hide();
	
	// search area
	searchField = $('#'+id+'_txt');
	searchField.hide('fast');
	
	// detect result type
	
	if(results['resultType'] == 'city') {
		// City result
		// perform city parsing by default for now
		if(results['results'].length == 1)
		{
			// we only have one result so no radios. is this a good ideea really ?
			result.append('<input type=hidden name='+locationsName[id]+' value='+results['results'][0]['id']+' />');
			result.append(results['results'][0]['name']);
			result.append(' <a href="#" class="helpLink" onclick="locationReset(\''+id+'\'); return false;" >'+locationMessages['new_search']+'</a>');
		} else {
			// multiple results require radio buttons
			for(i in results['results'])
			{
				result.append('<input type=radio name='+locationsName[id]+' id='+id+'_'+i+' value='+results['results'][i]['id']+' /><label for='+id+'_'+i+'>'+results['results'][i]['name']+'</label><br />');
			}
			i++;
			result.append(' <a href="#" class="helpLink" onclick="locationReset(\''+id+'\'); return false;" >'+locationMessages['new_search']+'</a>');
			//result.append('<input type=radio name='+locationsName[id]+' id='+id+'_'+i+' value=0 /><label for='+id+'_'+i+'>'+locationMessages['new_search']+'</label><br />');
			//result.children('input:last').change(function(){locationReset(id);});
			// for stupid IE 6
			//result.children('input:last').click(function(){locationReset(id);});
			//result.children('label:last').click(function(){locationReset(id);});
			
			result.children('input:first').click();
		}
	} else {
		// Country result
		result.append('<input type=hidden name='+locationsName[id]+' value='+results['results'][0]['ISO']+' />');
		result.append(results['results'][0]['name'] + ', ' + results['results'][0]['IRU']);
		result.append(' <a href="#" class="helpLink" onclick="locationReset(\''+id+'\'); return false;" >'+locationMessages['new_search']+'</a>');
	}
	result.show();
	
	if(typeof updateTraficType == 'function')
	{
		updateTraficType();
	}
}

function locationClear(id, event)
{
	var searchField = $('#'+id+'_txt');
	if(searchField)
	{
		searchField.removeClass('small_loading');
		searchField.removeClass('small_error');
	}
}

function locationReset(id)
{
	// clear and hide results
	result = $('#'+id+'_results');
	result.hide('fast');
	result.html('');
	
	// search area
	$('#'+id+'_txt').show('fast', function(){this.focus();this.select();});
}
