/* htmltools_suggestbox functions */
/* $Id: suggestbox.js,v 1.4 2007/03/25 06:15:14 tt Exp $ */

var g_htmltools_active_suggestbox = -1;
var g_htmltools_suggestbox_known_patterns = {
	_keys : new Array(),
	_values : new Array(),
	length : 0,
	get : function(f) {
		for( var i=0; i<this.length; i++ ) {
			if( this._keys[i] == f ) {
				return this._values[i];
			}
		}
		return undefined;
	},
	set : function( f, v ) {
		for( var i=0; i<this.length; i++ ) {
			if( this._keys[i] == f ) {
				this._values[i] = v;
				return true;
			}
		}
		this._keys[this.length] = f;
		this._values[this.length] = v;
		this.length = this._keys.length;
		return true;
	}
	};

function htmltools_suggestbox_init()
{
	var listholder, list, ul;
	var els = document.getElementsByTagName( 'span' );
	for( i=0; i<els.length; i++ )
	{
		if( els[i].className != 'suggestbox' ) continue;
		inp = els[i].firstChild;
		anc = inp.nextSibling; /* the anchor with the AJAX URL to query */

		listholder = document.createElement('div');

		list = document.createElement('div');
		list.className = 'suggestbox_list';
		list.style.display = 'none';
		list.style.zIndex = 1000;
		list.style.position = 'absolute';

		ul = document.createElement('ul');
		ul.className = 'suggestbox_ul';
		list.insertBefore( ul, list.lastChild );
		
		listholder.appendChild( list );
		els[i].insertBefore( listholder, anc );
	}

	addEvent( document, "keyup", htmltools_suggestbox_keyup, false);
	addEvent( document, "mousedown", htmltools_suggestbox_mousedown, false);
	
}


function htmltools_suggestbox_show( box )
{
	var list, inp, anc;
	inp = box.firstChild;
	list = inp.nextSibling.firstChild;
	anc = inp.nextSibling.nextSibling;
	
	list.style.display = 'block';
	
	g_htmltools_active_suggestbox = box;
}
function htmltools_suggestbox_hide( box )
{
	var list, inp, anc;
	inp = box.firstChild;
	list = inp.nextSibling.firstChild;
	anc = inp.nextSibling.nextSibling;
	list.style.display = 'none';
	
	g_htmltools_active_suggestbox = -1;
}

function htmltools_suggestbox_choose( el )
{
	var li = el.parentNode;
	var ul = li.parentNode;
	var list = ul.parentNode;
	var box = list.parentNode.parentNode;
	var els = box.getElementsByTagName('input');
	var inp = els[0];
	
	inp.value = el.innerHTML;
	htmltools_suggestbox_hide( box );
	inp.focus();
}

function htmltools_suggestbox_over(a)
{
	var li = a.parentNode;
	var ul = li.parentNode;
	s = ul.getAttribute('f');
	
	if( typeof s == 'string' )
	{
		s = parseInt(s);
		if( s>=ul.childNodes.length || ul.childNodes[s].nodeName != 'LI' ) return;
		ul.childNodes[s].className = '';
	}
}

function htmltools_suggestbox_mousedown( e )
{
	var el, but;
	if( typeof g_htmltools_active_suggestbox != 'object' )
	{
		return;
	}
    el = e.target ? e.target : e.srcElement;
    while (el.nodeType != 1) el = el.parentNode;
	if( el.nodeName == 'scrollbar' )
	{
		return;
	}
	if(el.className.indexOf("suggestbox_")!=0)
    {
		htmltools_suggestbox_hide( g_htmltools_active_suggestbox,'' );
    }
}

function htmltools_suggestbox_keyup( e )
{
    var el = e.target ? e.target : e.srcElement;
	var code = (window.Event) ? e.which : event.keyCode;
	var shift = e.shiftKey; /* IE equiv? */
	var box, list, inp, anc;
	if( typeof el != 'object' || el.nodeName=='HTML' ||
		typeof el.parentNode != 'object' )
	{
		return;
	}
    if( hasClass(el.parentNode, "suggestbox") && el.nodeName == 'INPUT' )
	{
		/* a character has been typed in INPUT */
		box = el.parentNode;
		inp = box.firstChild;
		list = inp.nextSibling.firstChild;
		anc = inp.nextSibling.nextSibling;
		
		if( code == 40 ) /* down */
		{
			htmltools_suggestbox_scroll( list, 1 );
		}
		else if( code == 38 ) /* up */
		{
			htmltools_suggestbox_scroll( list, -1 );
		}
		else if( code == 9 ) /* tab */
		{
		}
		else if( code == 13 ) /* return */
		{
			htmltools_suggestbox_hide(box);
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}
		else
		{
			/** TODO: should check the INPUT text value for changes and
					act accordingly **/
			htmltools_suggestbox_getlist( inp.id );
			htmltools_suggestbox_show(box);
		}
	}
	else if( typeof el.parentNode.parentNode == 'object' && 
		hasClass(el.parentNode.parentNode, "suggestbox") )
	{
		/* key press in the suggestion list */
		var box = el.parentNode.parentNode;
		var inp = box.firstChild;
		var list = inp.nextSibling.firstChild;
		if( code == 40 ) /* down */
		{
			htmltools_suggestbox_scroll( list, 1 );
		}
		else if( code == 38 ) /* up */
		{
			htmltools_suggestbox_scroll( list, -1 );
		}
		else if( code == 9 ) /* tab */
		{
		}
		else if( code == 13 ) /* return */
		{
			htmltools_suggestbox_hide(box);
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}
		else
		{
		}
	}
}

function htmltools_suggestbox_getlist(id)
{
	var inp = document.getElementById( id );
	var box, list, anc;
	var url, rel;

	box = inp.parentNode;
	list = inp.nextSibling.firstChild;
	anc = inp.nextSibling.nextSibling;

	for( var i=0; i<g_htmltools_suggestbox_known_patterns.length; i++ )
	{
		regex = g_htmltools_suggestbox_known_patterns._keys[i];
		var reg = new RegExp( regex, '' );
		if( reg.exec( inp.value ) )
		{
			htmltools_suggestbox_populate( box, g_htmltools_suggestbox_known_patterns._values[i] );
// 			htmltools_suggestbox_show( box );
			return;
		}
	}
	
	if( hasClass( box, 'loading' ) )
	{
		if( !hasClass( box, 'timeout' ) )
		{
			addClass( box, 'timeout' );
		}
		return;
	}
	addClass( box, 'loading' );
	
	url = anc.getAttribute('href');
	rel = anc.getAttribute('rel');
	url = appendQuery( url, rel + "=" + inp.value );
	req = new AJAX();
	req.open("GET", url, true );
	req.onreadystatechange = function() { 
		if( req.readyState==4 ) {
			delClass( box, 'loading' );
			if( hasClass( box, 'timeout' ) )
			{
				delClass( box, 'timeout' );
				setTimeout( "htmltools_suggestbox_getlist('" + id + "')", 300 );
			}
			var c= req.responseXML.getElementsByTagName( 'l' );
			if( c.length < 1 )
				return;
			c = c[0];
			g_htmltools_suggestbox_known_patterns.set(c.getAttribute('p'),req.responseXML);
			htmltools_suggestbox_populate( box, req.responseXML );
// 			htmltools_suggestbox_show( box );
		}
	}
	req.send(null);
}

function htmltools_suggestbox_populate( box, xml )
{
	var i;
	var els;
	var inp = box.firstChild;
	var list = inp.nextSibling.firstChild.firstChild;
	
	els = list.childNodes;
	while( els.length > 0 )
	{
		els[els.length-1].parentNode.removeChild( els[els.length-1] );
	}
	els = xml.getElementsByTagName( 'i' );
	for( i=0; i<els.length; i++ )
	{
		li = document.createElement('li');
		a = document.createElement('a');
		a.setAttribute('href', '#');
		a.className = 'suggestbox_a';
		a.innerHTML = els[i].firstChild.nodeValue;
		a.onclick = function() { htmltools_suggestbox_choose(this); return false; };
		a.onmouseover = function() { htmltools_suggestbox_over(this); return false; };
		li.appendChild( a );
		list.appendChild( li );
	}
}


function htmltools_suggestbox_scroll( list, dir )
{
	var box = list.parentNode.parentNode;
	var inp = box.firstChild;
	var ul = list.firstChild;
	var li, cur_li;
	var a;
	var s = ul.getAttribute('f');
//	alert( typeof s );
//	alert( s );
	if( typeof s == 'string' )
	{
		s = parseInt(s);
	}
	else if( typeof s == 'object' )
	{
		s=0-dir;
	}
	if( s>=0 && s<ul.childNodes.length )
	{
		cur_li = ul.childNodes[s];
	}
	s += dir;
	if( s>=0 && s<ul.childNodes.length )
	{
		li = ul.childNodes[s];
		a = li.firstChild;
		li.className = 'active';
		if( typeof cur_li == 'object' )
		{
			cur_li.className = '';
		}
		inp.value = a.innerHTML;
		ul.setAttribute('f',s);
// 		if( a.nodeName == 'A' ) a.focus();
// 		if( inp.nodeName == 'INPUT' ) inp.focus();
	}
	return;
	
	for( var i=s; (dir>0 && i<ul.childNodes.length) || (dir<0 && i>=0); (dir > 0 ? i++ : i--) )
	{
		if( ul.childNodes[i].nodeName != 'LI' ) continue;
		li = ul.childNodes[i];
		if( li.firstChild.nodeName == 'A' )
		{
//			 li.firstChild.focus();
			a = li.firstChild;
			li.className = 'active';
			cur_li.className = '';
			inp.value = a.innerHTML;
			ul.setAttribute('f',i);
			s = ul.getAttribute('f');
			a.focus();
			inp.focus();
//			alert( typeof s);
			break;
		}
	}
}

addLoadEvent(htmltools_init_forms);
addLoadEvent(htmltools_suggestbox_init);

/* end htmltools_suggestbox_functions */
