/* Place Class and functions*/
function Place (n, r, c){
	this.name = n;
	this.region = r;
	this.country = c;
}

function getLocationName(place) {
	var name = '';

	// Insert location name
	if(place['name']) 
		name += place.name + ',';

	// Insert region name
	if(place['region']) 
		name += place.region + ',';

	// Insert Country name
	if(place['country']) 
		name += place.country;
		
	return name;
}

// Function takes a string and replaces spaces + dots with dashes
function urlize(str) {
	var ret = str;
	ret = ret.replace(/ /g,"-");
	ret = ret.replace(/'/g,"");
	ret = ret.replace(/\./g,"-");
	ret = ret.toLowerCase();
	return ret;
}

function getLocationUrl(arr) {
	if ( arr.length < 3 )
	{
		alert ( 'getLocationUrl(): Array too short!' );
		return;
	}
	
	var country = arr['country'];
	var region = arr['region'];
	var place = arr['name'];
	var result = 'pages/';

	if(region.length > 0 && place.length > 0)
	{
		result += urlize( country ) + '/' + urlize( region ) + '/' + urlize( place );
		return result + '.htm';;
	}

	// region thumbnail pages are within their region's subfolder
	else if(region.length > 0 && place.length == 0)
	{	
		result += urlize( country ) + '/' + urlize( region ) + '/' + urlize( region );
		return result + '.htm';;
	}
	
	else if(region.length == 0 && place.length > 0)
	{	
		result += urlize( country ) + '/' + urlize( place );
		return result + '.htm';;
	}

	// country thumbnail pages are within their country's subfolder
	else
	{
		result += urlize( country ) + '/' + urlize( country );
	}

	return result + '.htm';;
}

/* AUTOCOMPLETE STUFF 
   ****************** */

// current selection
var selection = 0;
var matchset;
var search = "";

function keyUpEvent(e)
{	
	var selector = document.getElementById('selector');
	var newSearch = document.getElementById('where').value;
	var nameArr;
	var html = '';
	var rowCount = 0;
	var code = getKeyCode(e);

	if(!newSearch.beginsWith(search) || !search.beginsWith(newSearch))
		hideSelector();
	search = newSearch;

	if ( search.length == 0 )
	{
		hideSelector();
		return;
	}
	

	if ( (code < 65 || (code > 90 && code < 97) || code > 122) && code != 8 && code != 32) // not alpha character or backspace or space
		return;

	matchset = new Array();	

	// look for matches
	for(i in places)
	{		
		if( search.beginsWith(places[i].name) )		
			matchset.push( i );
	}	
	for(i in places)
	{
		if( places[i].name.length == 0 && places[i].region.length == 0 && search.beginsWith(places[i].country) )		
			matchset.push( i );
	}	
	for(i in places)
	{
		//if( places[i].name.length == 0 && search.beginsWith(places[i].region) )
		if( search.beginsWith(places[i].region) )
			matchset.push( i );
	}	

	// fill match list
	for(i in matchset)
	{
		html += '<p class="selector" id="row' + rowCount++ + 
				'" index="' +  matchset[i] + 
				'" onclick="clickSelection(' + i + ')" onmouseover="makeSelection(' + i + ')">';
		nameArr = fixName(getLocationName(places [matchset[i] ])).split(", ");
		for (j in nameArr)
		{
			if(j == 0)
			{
				html += '<strong>' + nameArr[0] + '</strong>';
				if(nameArr.length > 1) html += ', ';
				continue;
			}
			else if (j < nameArr.length - 1)
				html += nameArr[j] + ', ';
			else
				html += nameArr[j];
		}
		
		html += '</p>';
	}

	if(html.length == 0)
	{
		html = "<p class='red'>Not in database: '<strong>" + search + "</strong>'&nbsp;&nbsp;</p>";
	}

	if ( selector.style.display != 'block' )
		selector.style.display = 'block'

	if ( selector.innerHTML != html )
	{		
		selector.innerHTML = html;
	}

	if(matchset.length > 0)
		highlightCurrentSelection();
}

function keyDownEvent(e)
{
	if ( document.getElementById("selector").style.display != "block" )
		return;

	var code = getKeyCode(e);
	
	switch (code)
	{
		case 9:
		case 13:
			confirmSelection(); 
			break;
		case 27:
			hideSelector();
			break;
		case 38:
			moveSelectionUp();
			 break;
		case 40:
			moveSelectionDown(); 
			break;
	}
}

function moveSelectionDown()
{	
	if ( selection < matchset.length-1 )
	{
		eraseCurrentSelection();
		selection++;
		highlightCurrentSelection();
	}	
}

function moveSelectionUp()
{
	if ( selection > 0  )
	{
		eraseCurrentSelection();
		selection--;
		highlightCurrentSelection();
	}
}

function eraseCurrentSelection()
{
	row = document.getElementById("row" + selection ).style.backgroundColor = "#FFFFFF";	
}

function highlightCurrentSelection()
{
	row = document.getElementById("row" + selection ).style.backgroundColor = "#99CCFF";	
}

function clickSelection(which)
{
	selection = which;
	confirmSelection();
}

function makeSelection(which)
{
	if ( which != selection )
	{
		eraseCurrentSelection();
		selection = which;
		highlightCurrentSelection();
	}
}

function confirmSelection()
{
	document.getElementById("where").value = fixName( getLocationName(places[ matchset[selection] ]) );
	var index = document.getElementById("row" + selection ).getAttribute('index');
	hideSelector();
	var place = places[index];
	
	window.location = getLocationUrl(place);	
}

function hideSelector()
{
	var selector = document.getElementById("selector");
	selector.style.display = "none";
	selector.innerHTML = "";
	selection = 0;

}

function selectAll()
{
	var w = document.getElementById('where');
	w.focus();
	w.select();
}

function getKeyCode(e)
{
	if (document.all)
		return window.event.keyCode;
	else return e.which;
}

// add String object functionality
String.prototype.beginsWith = MatchBeginsWith;
function MatchBeginsWith(str) {
	var compStr = this.toLowerCase();
	str = str.toLowerCase();
	str = str.substr(0, compStr.length);
	
	return compStr == str;
}

function fixName(locationName) {
	var arr = locationName.split(",");
	var result = "";
	for(s in arr)
	{
		if( arr[s].length > 0 )
		{
			result += arr[s];
			if ( s < arr.length - 1 )
				result += ', ';
		}	
			
	}	
	return result;
}

function debug(str)
{
	document.getElementById('output').innerHTML += str + "<br />\n";
}
