var currentSuggestions = new Array();
var selectedSuggestionLine = '';
var suggestionLineId = new Array();

function domainregboxKeyPress(element, e)
{
	var disabledsuggestions=false;
	try
	{
		//Internet explorer and Chrome
		var KeyID = (window.event) ? event.keyCode : e.keyCode;
	}
	catch (e)
	{
		//Firefoxy
		KeyID = e.which;
	}

	if(element.value.indexOf('\.')>0)
	{
		//if there is a . no longer do the suggestions
		disabledsuggestions=true;
		resetSuggestionBox();
	}

	switch(KeyID)
	{
		case 16: //Shift
		case 17: //Ctrl
		case 18: //Alt
		case 19: //Pause
		case 37: //Left
		case 39: //Right
			break;
		case 38: //Up
			selectSuggestionUp();
			break;
		case 46:
			break;
		case 40: //Down
			selectSuggestionDown();
			break;
		case 27: //Esc
			resetSuggestionBox();
			break;
		default:
			if(!disabledsuggestions)
			{
				domainregboxGetSuggestions(element);
			}
			break;
	}
}

function domainregboxGetSuggestions(element)
{
	value = element.value;
	while (value.indexOf(' ') != -1)
	{
		value = value.replace(' ', '-');
	}
	if (value.length)
	{
		returnHandler = (function (suggestions){domainregboxShowSuggestions(suggestions);});
		googleSuggests.getSuggestions(value, returnHandler);
	}
	else
	{
		resetSuggestionBox();
	}
}

function domainregboxShowSuggestions(suggestions)
{
	if (suggestions.length)
	{
		resetSuggestionBox();
		for (i = 0; i < suggestions.length; i++)
		{
			while (suggestions[i].indexOf(' ') != -1)
			{
				suggestions[i] = suggestions[i].replace(' ', '-');
			}
			while (suggestions[i].indexOf('.') != -1)
			{
				suggestions[i] = suggestions[i].replace('.', '-');
			}
			createSuggestionLine(suggestions[i]);
		}
		currentSuggestions = suggestions;
		showSuggestionBox();
	}
	else
	{
		resetSuggestionBox();
		currentSuggestions = new Array();
	}
}

function resetSuggestionBox()
{
	box = document.getElementById('domainregbarsuggestionbox');
	box.style.display = 'none';
	box.innerHTML = '';
	selectedSuggestionLine = '';
}

function showSuggestionBox()
{
	box = document.getElementById('domainregbarsuggestionbox');
	registrationbarinput = document.getElementById('domainfinder-searchbox');
	boxTopLeft = GetTopLeft(registrationbarinput);
	box.style.top = (boxTopLeft.Top+30)+'px';
	box.style.left = (boxTopLeft.Left)+'px';
	box.style.display = 'block';
}

function GetTopLeft(elm)
{
	var x, y = 0;
	//set x to elm’s offsetLeft
	x = elm.offsetLeft;
	//set y to elm’s offsetTop
	y = elm.offsetTop;
	//set elm to its offsetParent
	elm = elm.offsetParent;
	//use while loop to check if elm is null
	// if not then add current elm’s offsetLeft to x
	//offsetTop to y and set elm to its offsetParent
	while(elm != null)
	{
		x = parseInt(x) + parseInt(elm.offsetLeft);
		y = parseInt(y) + parseInt(elm.offsetTop);
		elm = elm.offsetParent;
	}
	//here is interesting thing
	//it return Object with two properties
	//Top and Left
	return {Top:y, Left: x};
}

function createSuggestionLine(text)
{
	box = document.getElementById('domainregbarsuggestionbox');
	tr = document.createElement('div');
	tr.setAttribute('id','domainregbarsuggestionbox_line'+(box.childNodes.length))
	td = document.createElement('div');
	td.innerHTML = text;
	tr.appendChild(td);
	tr.className = 'domainregbarsuggestionboxline';
	box.appendChild(tr);
	suggestionLineId['domainregbarsuggestionbox_line'+(box.childNodes.length-1)] = (box.childNodes.length-1);
	tr.onmousemove = function() { selectSuggestionMouseOver(suggestionLineId[this.id]); };
	td.onclick = function() { selectSuggestionClick(suggestionLineId[this.parentNode.id]); };
}

function selectSuggestionDown()
{
	var box = document.getElementById('domainregbarsuggestionbox');
	var newSelectedSuggestionLine=0;
	if (selectedSuggestionLine === '')
	{
		selectedSuggestionLine = 0;
		newSelectedSuggestionLine = 0;
	}
	else
	{
		newSelectedSuggestionLine = selectedSuggestionLine+1;
	}
	if (newSelectedSuggestionLine > (box.childNodes.length - 1))
	{
		newSelectedSuggestionLine = 0;
	}
	selectSuggestion(newSelectedSuggestionLine);
}

function selectSuggestionUp()
{
	var box = document.getElementById('domainregbarsuggestionbox');
	var newSelectedSuggestionLine=0;
	if (selectedSuggestionLine === '')
	{
		selectedSuggestionLine = box.childNodes.length-1
		newSelectedSuggestionLine = box.childNodes.length-1
	}
	else
	{
		newSelectedSuggestionLine = selectedSuggestionLine-1;
	}
	if (newSelectedSuggestionLine < 0)
	{
		newSelectedSuggestionLine = box.childNodes.length-1;
	}
	selectSuggestion(newSelectedSuggestionLine);
}

function selectSuggestion(newSelectedSuggestionLine)
{
	if (selectedSuggestionLine !== '')
	{
		try
		{
			var oldSuggestionLine = document.getElementById('domainregbarsuggestionbox_line'+selectedSuggestionLine);
			oldSuggestionLine.style.backgroundColor = '#FFFFFF';
		}
		catch(e)
		{

		}
	}
	var newSuggestionLine = document.getElementById('domainregbarsuggestionbox_line'+newSelectedSuggestionLine);
	newSuggestionLine.style.backgroundColor = '#FEF0A5';
	registrationbarinput = document.getElementById('domainfinder-searchbox');
	registrationbarinput.value = currentSuggestions[newSelectedSuggestionLine];
	selectedSuggestionLine = newSelectedSuggestionLine;
}

function selectSuggestionClick(newSelectedSuggestionLine)
{
	var registrationbarinput = document.getElementById('domainfinder-searchbox');
	registrationbarinput.value = currentSuggestions[newSelectedSuggestionLine];
	window.location = 'http://www.mijndomein.nl/step1?rf=11323&domainname='+encodeURIComponent(currentSuggestions[newSelectedSuggestionLine]);
}

function selectSuggestionMouseOver(newSelectedSuggestionLine)
{
	if (selectedSuggestionLine !== '')
	{
		try
		{
			var oldSuggestionLine = document.getElementById('domainregbarsuggestionbox_line'+selectedSuggestionLine);
			oldSuggestionLine.style.backgroundColor = '#FFFFFF';
		}
		catch(e)
		{
		}
	}
	var newSuggestionLine = document.getElementById('domainregbarsuggestionbox_line'+newSelectedSuggestionLine);
	newSuggestionLine.style.backgroundColor = '#FEF0A5';
	selectedSuggestionLine = newSelectedSuggestionLine;
}
