// LSB = LocationSuggestionBox
var LSB_Data = new Object();
var LSB_PREFIX = "";
var LSB_LOCATIONS = null;
var LSB_MAX_LOCATIONS_SHOW = 50;
var LSB_CONVERTED_NAMES = new Object();

function LSB_KeyDown(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	if (target.id == LSB_Data['input'])
	{
		if (e.keyCode == 9) // Tabulator
		{
			LSB_Hide();
		} else
		if (e.keyCode == 38 || e.keyCode == 40) // Pfeiltaste hoch/runter
		{
			if (e.keyCode == 40)
			{
				if (LSB_Data['selected'] == -1 || LSB_Data['selected'] == (LSB_Data['rows'] - 1))
				{
					LSB_SetCursor(0);
				} else {
					LSB_SetCursor(LSB_Data['selected'] + 1);
				}
			} else
			if (e.keyCode == 38) {
				if (LSB_Data['selected'] == -1 || LSB_Data['selected'] == 0)
				{
					LSB_SetCursor(LSB_Data['rows'] - 1);
				} else {
					LSB_SetCursor(LSB_Data['selected'] - 1);
				}
			}
			
			// LSB_Data['options'].max_rows
			// LSB_Data['options'].row_height
			
			if (LSB_Data['rows'] > LSB_Data['options'].max_rows)
			{
				var temp = Math.round(LSB_Data['options'].max_rows / 2);
				
				if (LSB_Data['selected'] >= temp)
				{
					var d_rows = (LSB_Data['selected'] - temp);
					
					document.getElementById(LSB_Data['frame']).scrollTop = (d_rows * LSB_Data['options'].row_height);
				} else {
					document.getElementById(LSB_Data['frame']).scrollTop = 0;
				}
			} else {
				document.getElementById(LSB_Data['frame']).scrollTop = 0;
			}
			
		} else
		if (e.keyCode == 13 && LSB_Data['selected'] > -1) {
			
			
			// LSB_SetRow(LSB_Data['ref_index'][LSB_Data['selected']]);
			LSB_SetRow(LSB_Data['selected']);
			
			// RETURN/ENTER == 13
			// alert(e.keyCode);
		}
	} else {
		
	}
}

function LSB_KeyUp(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	if (e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 37 || e.keyCode == 39) // Pfeiltaste hoch/runter
	{
		
	} else {
		LSB_Check();
	}
}

function LSB_MouseDown(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	// alert(target.id+"\n"+LSB_IsLocked());
	
	if (target.id != LSB_Data['input'] && !LSB_IsLocked())
	{
		LSB_Hide();
	}
}

function LSB_Focus(e)
{
	if (!e) var e = window.event;

	target = GetTarget(e);
	
	LSB_Check();
	
	if (LSB_Data['input'] == target.id)
	{
		document.getElementById(LSB_Data['input']).select();
	}
}

function LSB_Init(input, frame, options)
{
	if (!options.flag_style || options.flag_style == "undefined")
	{
		options.flag_style = "";
	}
	
	if (!options.loader)
	{
		if (WWW_SERVICE == "HOTELICA_DE")
		{
			options.loader = "/script/php/locations.php";
		} else {
			options.loader = "/script/php/suggestions.php";
		}
	}
	
	LSB_Data['locked'] = false;
	LSB_Data['input'] = input;
	LSB_Data['frame'] = frame;
	LSB_Data['options'] = options;
	LSB_Data['rows'] = LSB_Data['selected'] = -1;
	LSB_Data['ref_index'] = null;
	LSB_Data['ref_index_name'] = null;
	
	AttachEvent(document.getElementById(LSB_Data['input']), "keyup", LSB_KeyUp, false);
	AttachEvent(document.getElementById(LSB_Data['input']), "focus", LSB_Focus, false);
	
	AttachEvent(document, "keydown", LSB_KeyDown, false);
	AttachEvent(document, "mousedown", LSB_MouseDown, false);
	
}

function LSB_Check()
{
	var location = document.getElementById(LSB_Data['input']).value;
	
	if (location.length >= 3)
	{
		temp_prefix = location.toLowerCase().substr(0, 3);
		
		if (temp_prefix != LSB_PREFIX)
		{
			LSB_PREFIX = temp_prefix;
			
			/* Liste neu laden */
			// alert(LSB_Data['options'].loader);
			
			LSB_Hide();
			
			if (LSB_Data['options'].loader_image)
			{
				document.getElementById(LSB_Data['input']).style.backgroundImage = "url("+LSB_Data['options'].loader_image+")";
				document.getElementById(LSB_Data['input']).style.backgroundRepeat = "no-repeat";
				document.getElementById(LSB_Data['input']).style.backgroundPosition = "100% -1px";
			}
			
			if (LSB_Data['options'].loader)
			{
				dhtml_HttpPost(LSB_Data['options'].loader, "prefix=" + location + "&storage=LSB_LOCATIONS&callback=LSB_Build");
			}
		} else {
			LSB_Build(location);
		}
	} else {
		LSB_Hide();
	}
}

function LSB_ClearChars(str)
{
	str = str.replace(/Á/g, "A");
	str = str.replace(/á/g, "a");
	str = str.replace(/À/g, "A");
	str = str.replace(/à/g, "a");
	str = str.replace(/Â/g, "A");
	str = str.replace(/â/g, "a");
	str = str.replace(/Å/g, "A");
	str = str.replace(/å/g, "a");
	str = str.replace(/ã/g, "a");
	
	str = str.replace(/Ç/g, "C");
	str = str.replace(/ç/g, "c");
	
	str = str.replace(/Ð/g, "D");
	str = str.replace(/ð/g, "d");
	
	str = str.replace(/É/g, "E");
	str = str.replace(/é/g, "e");
	str = str.replace(/È/g, "E");
	str = str.replace(/è/g, "e");
	str = str.replace(/Ê/g, "E");
	str = str.replace(/ê/g, "e");
	str = str.replace(/ë/g, "e");
	
	str = str.replace(/Í/g, "I");
	str = str.replace(/í/g, "i");
	str = str.replace(/Ì/g, "I");
	str = str.replace(/ì/g, "i");
	str = str.replace(/Î/g, "I");
	str = str.replace(/î/g, "i");
	
	str = str.replace(/ñ/g, "n");
	
	str = str.replace(/Ó/g, "O");
	str = str.replace(/ó/g, "o");
	str = str.replace(/Ò/g, "O");
	str = str.replace(/ò/g, "o");
	str = str.replace(/Ô/g, "O");
	str = str.replace(/ô/g, "o");
	str = str.replace(/õ/g, "o");
	
	str = str.replace(/Š/g, "S");
	str = str.replace(/š/g, "s");
	
	str = str.replace(/Ú/g, "U");
	str = str.replace(/ú/g, "u");
	str = str.replace(/Ù/g, "U");
	str = str.replace(/ù/g, "u");
	str = str.replace(/Û/g, "U");
	str = str.replace(/û/g, "u");
	
	str = str.replace(/Ý/g, "Y");
	str = str.replace(/ý/g, "y");
	
	str = str.replace(/-/g, " ");
	str = str.replace(/'/g, " ");
	str = str.replace(/´/g, " ");
	str = str.replace(/`/g, " ");
	
	return str;
}

function LSB_Build()
{
	if (!LSB_LOCATIONS)
		return false;
	
	if (LSB_Data['options'].loader_image)
	{
		document.getElementById(LSB_Data['input']).style.backgroundImage = "";
		document.getElementById(LSB_Data['input']).style.backgroundRepeat = "no-repeat";
		document.getElementById(LSB_Data['input']).style.backgroundPosition = "100% -1px";
	}
	
	var LOCATION_NAME_SET = new Object();
	var LOCATION_CITY_SET = new Object();
	
	if (LSB_LOCATIONS.length > 0)
	{
		prefix = document.getElementById(LSB_Data['input']).value;
		
		var inner_html = "";
		
		var c = 0;
		var p = 0;
		
		LSB_Data['ref_index'] = new Object();
		LSB_Data['ref_index_name'] = new Object();
		
		LSB_Visible_Parents = new Object();
		
		prefix_converted = LSB_ClearChars(prefix).toLowerCase();
		
		for (i = 0; i < LSB_LOCATIONS.length; i++)
		{
			location_name = LSB_LOCATIONS[i].name;
			
			if (!LSB_CONVERTED_NAMES[location_name])
			{
				LSB_CONVERTED_NAMES[location_name] = LSB_ClearChars(location_name).toLowerCase();
			}
			
			if (!LOCATION_NAME_SET[LSB_LOCATIONS[i].full_row] && (LSB_LOCATIONS[i].type != "CITY" || (LSB_LOCATIONS[i].type == "CITY" && !LOCATION_CITY_SET[LSB_LOCATIONS[i].id])))
			{
				p_start = LSB_CONVERTED_NAMES[location_name].indexOf( prefix_converted );
				
				LOCATION_NAME_SET[LSB_LOCATIONS[i].full_row] = true;
	
				// if (LSB_ClearChars(location_name.substr(0, prefix.length).toLowerCase()) == LSB_ClearChars(prefix).toLowerCase() || p_start != -1) //  || LSB_Visible_Parents[LSB_LOCATIONS[i].parent]
				if (LSB_CONVERTED_NAMES[location_name].substr(0, prefix.length) == prefix_converted || p_start != -1)
				{
					if (LSB_LOCATIONS[i].type == "CITY")
					{
						LOCATION_CITY_SET[LSB_LOCATIONS[i].id] = true;
					}
					
					location_name_html = "";
	
					if (p_start == -1)
					{
					} else {
					}
					
					if (LSB_LOCATIONS[i].type != "CITY" && LSB_LOCATIONS[i].type != "REGION")
					{
						location_name_html = LSB_LOCATIONS[i].full_row;
					} else {
						location_name_html = LSB_LOCATIONS[i].full_row.substr(0, p_start) + "<span style=\"font-weight: bold; text-decoration: underline;\">" + LSB_LOCATIONS[i].full_row.substr(p_start, prefix.length) + "</span>" + LSB_LOCATIONS[i].full_row.substr(p_start + prefix.length, LSB_LOCATIONS[i].full_row.length - (p_start + prefix.length - 0)); // + "|" + LSB_LOCATIONS[i].type + "|" + LSB_LOCATIONS[i].id;
					}
					
					var inner_style = null;
					
					if (LSB_LOCATIONS[i].selectable)
						inner_html += '<a href="" class="' + LSB_Data['options'].style_row.normal + '" onMouseOver="LSB_SetCursor(' + c + ');" onMouseOut="LSB_ClearCursor(' + c + ');" id="LSB_ROW_' + c + '" onClick="LSB_SetRow(' + c + '); return false;">';
					else
						inner_html += '<a href="" class="' + LSB_Data['options'].style_row.normal + '" onMouseOver="LSB_SetCursor(' + c + ');" onMouseOut="LSB_ClearCursor(' + c + ');" id="LSB_ROW_' + c + '" onClick="LSB_SetRow(' + c + '); return false;">';
					
					inner_html += '<div style="width: 1000px; cursor: pointer;">';
					
					if (LSB_Data.options.show_flags && LSB_LOCATIONS[i].type != "REGION") //  && LSB_LOCATIONS[i].type != "REGION"
					{
						inner_html += '<div class=\"sg-lnk_flag\" style=\"background-image: url(/images/fahnen/PXH14/' + LSB_LOCATIONS[i].iso + '.gif); background-repeat: no-repeat; ' + LSB_Data['options'].flag_style + ';\"> </div>';
					}
					
					inner_html += '<div class=\"sg-lnk_content\" style=\"\">' + location_name_html + '</div>';
					
					inner_html += '</div>';
					
					inner_html += '</a>';
					
					LSB_Data['ref_index'][c] = i;
					
					if (LSB_LOCATIONS[i].selectable)
						LSB_Data['ref_index_name'][c] = LSB_LOCATIONS[i].full_row;
					else
						LSB_Data['ref_index_name'][c] = "UNSELECTABLE";
					
					c++;
					p++;
					
					if (LSB_LOCATIONS[i].childs.length > 0)
					{
						for (chd = 0; chd < LSB_LOCATIONS[i].childs.length; chd++)
						{
							location_name_html = "";
							location_name_html = LSB_LOCATIONS[i].childs[chd].full_row;
							
							LOCATION_NAME_SET[location_name_html] = true;
							
							// alert(LSB_LOCATIONS[i].childs.length);
							inner_html += '<a href="" class="' + LSB_Data['options'].style_row.normal + '" onMouseOver="LSB_SetCursor(' + c + ');" onMouseOut="LSB_ClearCursor(' + c + ');" id="LSB_ROW_' + c + '" onClick="LSB_SetRow(' + c + '); return false;">';
							inner_html += '<div style="width: 1000px; cursor: pointer;">';
							
							if (LSB_LOCATIONS[i].type == "CITY")
							{
								if (LSB_Data.options.show_flags)
								{
									inner_html += '<div class=\"sg-lnk_special\" style=\"background-image: url(/images/sg-SUB.png); background-repeat: no-repeat;\"> </div>';
								}
								
								if (LSB_Data.options.show_icons)
								{
									inner_html += '<div class=\"sg-lnk_special\" style=\"background-image: url(/images/sg-' + LSB_LOCATIONS[i].childs[chd].type + '.png); background-repeat: no-repeat; ' + LSB_Data['options'].flag_style + '\"> </div>';
								}
							} else
							if (LSB_LOCATIONS[i].type == "REGION")
							{
								if (LSB_Data.options.show_flags)
								{
									inner_html += '<div class=\"sg-lnk_special\" style=\"background-image: url(/images/sg-SUB.png); background-repeat: no-repeat; ' + LSB_Data['options'].flag_style + '\"> </div>';
								}
							}
							
							inner_html += '<div class=\"sg-lnk_content\" style=\"\">' + location_name_html + '</div>';
							inner_html += '</div>';
							inner_html += '</a>';
							
							LSB_Data['ref_index'][c] = i;
							LSB_Data['ref_index_name'][c] = LSB_LOCATIONS[i].childs[chd].full_row;
							
							c++;
						}
					}
					
					if (p > LSB_MAX_LOCATIONS_SHOW)
					{
						i = LSB_LOCATIONS.length;
					}
				}
			}
		}
		
		LSB_Data['rows'] = c;
		
		LSB_Data['selected'] = -1;
		
		if (c > 0)
		{
			document.getElementById(LSB_Data['frame']).innerHTML = inner_html;
			
			document.getElementById(LSB_Data['frame']).style.height = ((c < LSB_Data['options'].max_rows ? c : LSB_Data['options'].max_rows) * LSB_Data['options'].row_height) + 'px';
			
			LSB_Show();
			
			//LSB_SetCursor(2); 
		} else {
			document.getElementById(LSB_Data['frame']).innerHTML = "";
			
			LSB_Hide();
		}
		// alert(LSB_LOCATIONS[0].name);
	} else {
		LSB_Hide();
	}
}

function LSB_ClearCursor(row)
{
	//alert("Clear");
	LSB_Data['selected'] = -1;
	
	document.getElementById("LSB_ROW_"+row).className = LSB_Data['options'].style_row.normal;
}

function LSB_SetCursor(row)
{
	i = 0;
	
	while (document.getElementById("LSB_ROW_"+i))
	{
		if (i == row)
		{
			if (LSB_Data['ref_index_name'][row] == "UNSELECTABLE")
			{
				document.getElementById("LSB_ROW_"+i).className = LSB_Data['options'].style_row.unselectable;
			} else {
				document.getElementById("LSB_ROW_"+i).className = LSB_Data['options'].style_row.hover;
			}
		} else
		if (document.getElementById("LSB_ROW_"+i).className != LSB_Data['options'].style_row.normal) {
			document.getElementById("LSB_ROW_"+i).className = LSB_Data['options'].style_row.normal;
		}
		i++;
	}
	
	LSB_Data['selected'] = row;
}

function LSB_SetRow(index)
{
	if (LSB_Data['ref_index_name'][index] == "UNSELECTABLE")
	{
		alert("Eine Suche in dieser Region ist zu umfangreich. Bitte wählen Sie daher eine Unterregion aus.");
		return false;
	}
	/*
	document.getElementById(LSB_Data['input']).value = LSB_LOCATIONS[index].full_row;
	*/
	document.getElementById(LSB_Data['input']).value = LSB_Data['ref_index_name'][index];
	
	LSB_Hide();
}

function LSB_SetLocation(location)
{
	document.getElementById(LSB_Data['input']).value = location;
	
	LSB_Hide();
}

function LSB_Show()
{
	document.getElementById(LSB_Data['frame']).style.display = 'block';
	document.getElementById(LSB_Data['frame']).style.visibility = 'visible';
	
	if (LSB_Data['options'].loader_image)
	{
		document.getElementById(LSB_Data['input']).style.backgroundImage = "";
		document.getElementById(LSB_Data['input']).style.backgroundRepeat = "no-repeat";
		document.getElementById(LSB_Data['input']).style.backgroundPosition = "100% -1px";
	}
}

function LSB_Hide()
{
	document.getElementById(LSB_Data['frame']).style.display = 'none';
	document.getElementById(LSB_Data['frame']).style.visibility = 'hidden';
	
	LSB_Data['selected'] = -1
}

function LSB_IsVisible()
{
	return (document.getElementById(LSB_Data['frame']).style.visibility == 'visible');
}

function LSB_IsLocked()
{
	return LSB_Data['locked'];
}

function LSB_Lock()
{
	LSB_Data['locked'] = true;
}

function LSB_Unlock(frame)
{
	LSB_Data['locked'] = false;
}
