/*
 *	Queries the Highlight database to find pages which match the criteria
 */

function kwasiHighlightPages() {
	var xmlHttp = null;
	var callBack = null;
	var lock = false;
	var lockTimer = null;
	var lockedQuery = null;
	var lockedCallback = null;
	var self = this;
	//var MIN_LENGTH = 2;
	
	/*
	 *	prepares the KEYWORD_A and queries the database and calls CALL_BACK on finisheds (public)
	 */
	this.query = function( KEYWORD_A, CALL_BACK ) {
		var keyWord_a = KEYWORD_A.slice( 0, KEYWORD_A.length ); //copy to not alter the keyword array
		callBack = CALL_BACK;
		
		if ( lock ) {
			lockedQuery = KEYWORD_A;
			lockedCallback = CALL_BACK;
		} else {
			lock = true;
			lockedQuery = null;
			lockedCallback = null;
			lockTimer = setTimeout( unLock, 500 );
			
			if ( typeof( keyWord_a ) == "string" ) {
				callBack( new Array(), true );
			}
			
			if ( keyWord_a.length ) {
				for ( var key = 0; key < keyWord_a.length; key++ ) {
					keyWord_a[key] = keyWord_a[key].join( "|" );
				}
				
				var formedQuery = keyWord_a.join( "&" );
				loadXML( "/cgi-bin/HighlightFinder.pl?query=" + encodeURIComponent( formedQuery ) );
			} else {
				lock = false;
				clearTimeout( lockTimer );
				callBack( new Array(), true );
			}
		}
	};
	
	/*
	 *	unlocks the lock which prevents too frequent querying (private)
	 */
	function unLock() {
		lock = false;
		
		if ( lockedQuery && lockedCallback ) {
			self.query( lockedQuery, lockedCallback );
		}
	}
	
	/*
	 *	loads XML file (private)
	 */
	function loadXML( URL ) {
		xmlHttp = new kwasiXMLHttpRequest();
		xmlHttp.onreadystatechange = processReqChange;
		xmlHttp.open( "GET", URL, true );
		xmlHttp.send( null );
	}
	
	/*
	 *	processes the XML result file (private)
	 */
	function processReqChange() {
		var nodes_a = new Array();
		
		if ( ( xmlHttp.readyState == 4 ) && ( xmlHttp.status == 200 ) ) {
			var xmlDOM = xmlHttp.responseXML;
			
			if ( xmlDOM ) {
				var documents_a = xmlDOM.getElementsByTagName( "Document" );

				for ( var doc = 0; doc < documents_a.length; doc++ ) {
					var url = documents_a[doc].getElementsByTagName( "URL" )[0].firstChild.nodeValue;
					var title = documents_a[doc].getElementsByTagName( "Title" )[0].firstChild.nodeValue;
					var hierarchy = documents_a[doc].getElementsByTagName( "Breadcrumb" )[0].firstChild.nodeValue;
					var inText = documents_a[doc].getAttribute( "InText" );
					var inImage = documents_a[doc].getAttribute( "InAttributes" );
					var styleClass = false ;
					
					if ( ( inText == "true" ) && ( inImage == "true" ) ) {
						styleClass = "imgtxt";
					} else if ( inText == "true" ) {
						styleClass = "txt";
					} else if ( inImage == "true" ) {
						styleClass = "img";
					}
					
					var newA = document.createElement( "a" );
					newA.setAttribute( "href", url );
					newA.setAttribute( "title", hierarchy );
					var newSpan = document.createElement( "span" );
					newSpan.appendChild( document.createTextNode( title ) );
					newA.appendChild( newSpan );
					
					if ( styleClass ) {
						newSpan.setAttribute( "class", styleClass );
					}
					
					nodes_a.push( newA );
				}
			}
			
			callBack( nodes_a, ( lockedQuery == null ) );
		} else if ( xmlHttp.readyState == 4 ) {
			callBack( nodes_a, ( lockedQuery == null ) );
		}
	}
}
