LSB_Data = new Object();

LSB_Data['_data'] = new Object();
LSB_Data['_list'] = new Array();
LSB_Data['_curr'] = null;
LSB_Data['_lock'] = false;
LSB_Data['_load'] = false;

LSB_MAX_ROWS = 13;

/* *** BEGIN_Events *** */
function LSB_KeyDown(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	if (e.keyCode == 9) // Tabulator
	{
		LSB_Close();
	}
}

function LSB_KeyUp(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	if (target.id && !LSB_Data['_load'])
	{
		LSB_Data['_curr'] = target.id;
		
		LSB_Data['_data'][ LSB_Data['_curr'] ]['prefix'] = document.getElementById( LSB_Data['_curr'] ).value;
		
		LSB_Check();
	}
}

function LSB_MouseDown(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	if (target.id != LSB_Data['_curr'] && !LSB_IsLocked())
	{
		LSB_Close();
	}
}

function LSB_Focus(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	if (target.id)
	{
		LSB_Data['_curr'] = target.id;
		
		LSB_Data['_data'][ LSB_Data['_curr'] ]['prefix'] = document.getElementById( LSB_Data['_curr'] ).value;
		
		LSB_Check();
	}
}
/* *** END_Events *** */

function LSB_Init(input, div_main, div_info, div_results)
{
	LSB_Data['_list'].push(input);
	
	// alert(LSB_Data['_list'][0]);
	
	LSB_Data['_data'][input] = new Object();
	
	LSB_Data['_data'][input]['div_main'] = div_main;
	LSB_Data['_data'][input]['div_info'] = div_info;
	LSB_Data['_data'][input]['div_results'] = div_results;
	LSB_Data['_data'][input]['prefix'] = "";
	LSB_Data['_data'][input]['locations'] = new Array();
	
	AttachEvent(document.getElementById(input), "keyup", LSB_KeyUp, false);
	AttachEvent(document.getElementById(input), "focus", LSB_Focus, false);
	
	AttachEvent(document, "keydown", 	LSB_KeyDown, false);
	AttachEvent(document, "mousedown", 	LSB_MouseDown, false);
}

function LSB_Load(str_prefix)
{
	if (LSB_Data['_curr'])
	{
		dhtml_HttpPost("/script/suggestions.php", "prefix="+str_prefix+"&array="+LSB_Data['_curr']);
	}
}

function LSB_SetAirport(input, city, airport)
{
	if (LSB_Data['_curr'])
	{
		// alert(input+"\n"+city+"\n"+airport);
		var ap_name = LSB_Data['_data'][input]['locations'][city]['airports'][airport]['name'];
		var ap_city = LSB_Data['_data'][input]['locations'][city]['name'];
		var ap_iata = LSB_Data['_data'][input]['locations'][city]['airports'][airport]['iata'];
		
		// alert( ap_name );
		
		document.getElementById(LSB_Data['_curr']).value = ap_city + " [" + ap_iata + "]";
		
		LSB_Lock(0);
		LSB_Close();
	}
}

function LSB_Build()
{
	var result_rows = 0;
	var result_height = 0;
	var result_html = "";
	
	var div_res = document.getElementById( LSB_Data['_data'][ LSB_Data['_curr'] ]['div_results'] );
	
	if (LSB_Data['_curr'])
	{
		locations = LSB_Data['_data'][ LSB_Data['_curr'] ]['locations'];
		
		if (locations.length > 0)
		{
			/*
			<span class="suggestion_location">Berlin, Deutschland</span>
			
			*/
			// alert(locations.length);
			// -> Städte
			for (i = 0; i < locations.length; i++)
			{
				result_temp = "";
				// result_out  = false;
				// 
				
				// if (true)
				parse_location = false;
				
				cur_location = locations[i]['name'];
				inp_location = LSB_Data['_data'][ LSB_Data['_curr'] ]['prefix'];
				
				if (inp_location.indexOf("[") > 0)
				{
					inp_location = inp_location.substr(0, inp_location.indexOf("[") - 1);
					
					// alert("|"+inp_location+"|");
				}
				
				if (cur_location.substr(0, inp_location.length).toLowerCase() == inp_location.toLowerCase() || cur_location.toLowerCase().indexOf(inp_location.toLowerCase()) > 0)
				{
					// Prüfe Städtenamen
					parse_location = true;
				} else {
					// Prüfe IATA
					for (j = 0; j < locations[i]['airports'].length; j++)
					{
						if (locations[i]['airports'][j]['iata'].toUpperCase() == inp_location.toUpperCase())
						{
							parse_location = true;
						}
					}
				}
				
				if (parse_location)
				{
					result_temp += '<span class="suggestion_location">' + locations[i]['name'] + (locations[i]['state'] ? ", " + locations[i]['state'] : "") + ', ' + locations[i]['country'] + '&nbsp;<img src="/images/fahnen/PXH14/' + locations[i]['iso_3166'] + '.gif" align="absmiddle"></span>';
					
					if (result_rows < LSB_MAX_ROWS)
					{
						result_height += 18;
						result_rows++;
					}
					
					// -> Flughäfen
					for (j = 0; j < locations[i]['airports'].length; j++)
					{
						result_temp += '<a href="" class="suggestion_airport" onClick="LSB_SetAirport(\'' + LSB_Data['_curr'] + '\', ' + i + ', ' + j + '); return false;"><div style="width: 1000px;">&raquo; ' + locations[i]['airports'][j]['name'] + ' [' + locations[i]['airports'][j]['iata'] + ']</div></a>';
						
						if (result_rows < LSB_MAX_ROWS)
						{
							result_height += 16;
							result_rows++;
						}
					}
				}
				
				// if (result_out)
				{
					result_html += result_temp;
				}
			}
		}
		
		if (result_rows == 0)
		{
			result_height = 24;
			result_html = '<em style="display: block; padding: 5px; color: RED; font-size: 12px;">Leider konnte kein entsprechender Eintrag gefunden werden!</em>'
		}
		
	}
	/*
	alert(LSB_Data['_curr']);
	alert(LSB_Data['_data'][ LSB_Data['_curr'] ]['div_results'])
	*/
	div_res.style.height 	= result_height+"px";
	div_res.innerHTML  		= result_html;
}

function LSB_Check()
{
	if (LSB_Data['_curr'])
	{
		// alert(LSB_Data['_data'][ LSB_Data['_curr'] ]['div_info']);
		// 
		
		// document.getElementById( LSB_Data['_data'][ LSB_Data['_curr'] ]['div_info'] ).innerHTML = document.getElementById( LSB_Data['_curr'] ).value;
		
		var prefix_changed = false;
		
		if (document.getElementById( LSB_Data['_data'][ LSB_Data['_curr'] ]['div_info'] ).innerHTML.substr(0, 3) != LSB_Data['_data'][ LSB_Data['_curr'] ]['prefix'].substr(0, 3))
		{
			prefix_changed = true;
		}
		
		if (LSB_Data['_data'][ LSB_Data['_curr'] ]['prefix'].length >= 3)
		{
			document.getElementById( LSB_Data['_data'][ LSB_Data['_curr'] ]['div_info'] ).innerHTML = LSB_Data['_data'][ LSB_Data['_curr'] ]['prefix'];
		
			// Daten neu laden oder anzeigen...
			if (prefix_changed)
			{
				// Zeige zuerst Ladeinfo...
				var div_res = document.getElementById( LSB_Data['_data'][ LSB_Data['_curr'] ]['div_results'] );
				
				div_res.style.height = "42px";
				div_res.innerHTML  = '<div style="clear: left; float: left; margin-top: 7px;"><img src="/images/sanduhr.gif" height="34"></div>';
				div_res.innerHTML += '<div style="float: left; margin-top: 14px;">Bitte warten, Datenbank wird durchsucht...</div>';
				
				// Jetzt das Array laden
				LSB_Data['_load'] = true;
				
				LSB_Load(LSB_Data['_data'][ LSB_Data['_curr'] ]['prefix']);
				
			} else {
				LSB_Build();
			}
			
			LSB_Open();
		} else {
			LSB_Close();
		}
	}
}

function LSB_Open()
{
	if (LSB_Data['_curr'])
	{
		document.getElementById( LSB_Data['_data'][ LSB_Data['_curr'] ]['div_main'] ).style.display = 'block';
		document.getElementById( LSB_Data['_data'][ LSB_Data['_curr'] ]['div_main'] ).style.visibility = 'visible';
	}
}

function LSB_Close()
{
	for (i = 0; i < LSB_Data['_list'].length; i++)
	{
		DIV = document.getElementById( LSB_Data['_data'][ LSB_Data['_list'][i] ]['div_main'] );
		
		if (DIV)
		{
			DIV.style.display = 'none';
			DIV.style.visibility = 'hidden';
		}
	}
	
	LSB_Data['_curr'] = null;
}


function LSB_IsLocked()
{
	return LSB_Data['_lock'];
}

function LSB_Lock(status)
{
	LSB_Data['_lock'] = status;
}
