/*
 *
 * ROTATING BANNER v1.0
 * author: C2K internet solutions
 * website: http://www.c2k.nl/
 *
 */
 
/* ROTATOR CLASS */

function ArtistCompleter()
{
	/* PRIVATES */
	
	var lnCurrentImage  = 0;
	var div;
	var id;
	
	/* PUBLIC METHODS */
	
	this.init = init;
	this.getValues = getValues;
	
	/*  CONSTRUCTOR */
	
	function init(pstrID)
	{
		var input = document.getElementById(pstrID);
		
		input.setAttribute('autocomplete', 'off');
		
		id  = pstrID;
		div = document.createElement('div');

		div.setAttribute('id', 'c2k-autocompleter');
		
		div.style.width = input.clientWidth + 'px';
		div.style.position = 'absolute';
		
		var xy = getXYpos(input);
		
		div.style.top = (xy['y'] + input.clientHeight + 1) + 'px';
		div.style.left = xy['x'] + 'px';
		
		input.onkeyup = function () 
		{
			if(this.value != '') {
				getValues(this.value);
			}
			else {
				div.innerHTML = '';
			}
			
			returnValue = false;
			return false;
		}
		
		input.onblur = function () 
		{
			setTimeout(hideCompleter, 200);
		}
		
		input.onfocus = function () 
		{
			if(this.value != '') {
				getValues(this.value);
			}
			else {
				div.innerHTML = '';
			}
		}
		
		hideCompleter();
	
		document.body.appendChild(div);
	}
	
	function hideCompleter()
	{
		div.style.display = 'none';
	}
	
	function getValues(q)
	{
		var xmlhttp = null;
		var url 	= "/getArtists.php?q=" + q;

		if (window.XMLHttpRequest) // code for all new browsers
		{
			xmlhttp=new XMLHttpRequest();
		}
		else if (window.ActiveXObject) // code for IE5 and IE6
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		if (xmlhttp!=null)
		{
			xmlhttp.onreadystatechange = function ()
			{
				if (xmlhttp.readyState==4) // 4 = "loaded"
				{
					if (xmlhttp.status==200) // 200 = OK
					{
						var lstrResponse = xmlhttp.responseText;
						var laValues   = lstrResponse.split("|");
						
						div.innerHTML = '';
						
						for(var i = 0; i < laValues.length; i++)
						{
						/*	var a = document.createElement('a');
							
							a.onclick = function () {
								document.getElementById(id).value = this.innerHTML;
							}
							
							a.innerHTML = laValues[i];
							
							div.appendChild(a);*/
							
							var a = document.createElement('div');
							
							a.innerHTML = laValues[i];
							
							div.appendChild(a);
						}	

						div.style.display = 'block';
					}
				}
			};
			
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
		}
		else
		{
			alert("Your browser does not support XMLHTTP.");
		}
	}
	
	function getXYpos(poElement) 
	{
		if (!poElement) {
			return {"x":0, "y":0};
		}
   
		var xy={"x":poElement.offsetLeft,"y":poElement.offsetTop}
		var par=getXYpos(poElement.offsetParent);
   
		for (var key in par) {
			xy[key]+=par[key];
		}
		
		return xy;
	}

}