// DRAGON FILL
// dragfill.js
// by KuNgFo0
// revised 7/31/2008

var dragFillURL = 'ajaxGetFill.php';
//var mainColor = '';
//var highlightColor = '#FFFFC7';

function DragFillClass(options) {
	var anchorElement	= options['anchorElement'];
	var className		= options['className'];
	var timer			= options['timer'];
	var resultsNum		= options['resultsNum'];
	
	if (timer == null) timer = 3;
	if (resultsNum == null) resultsNum = 10;
	
	var divFillElement, tableElement;
	var timerID = null;
	
	divFillElement = document.createElement('div');
	divFillElement.className = className;
	divFillElement.style.cursor = 'pointer';
	
	anchorElement.onkeypress = startFill;
	anchorElement.onchange = stopFill;
	anchorElement.onblur = stopFill;
	anchorElement.parentNode.insertBefore(divFillElement, anchorElement.nextSibling);
	
	var spacerElement = document.createElement('div');
	spacerElement.style.clear = 'both';
	anchorElement.parentNode.insertBefore(spacerElement, divFillElement);

	function startFill() {
		if (timerID != null) clearTimeout(timerID);
		timerID = setTimeout(function() { fill(); }, timer * 1000);
	}
	
	function stopFill() {
		if (timerID != null) clearTimeout(timerID);
		timerID = null;
		hideFill();
	}
	
	function hideFill() {
		divFillElement.style.display = 'none';
	}
	
	function fill() {
		var searchString = anchorElement.value;
		if (searchString == '') stopFill();
		else {
			var url = dragFillURL + '?s=' + encodeURIComponent(searchString);
			var http_request = false;

			if (window.XMLHttpRequest) { // Mozilla, Opera, Safari, ...
				http_request = new XMLHttpRequest();
				if (http_request.overrideMimeType) http_request.overrideMimeType('text/xml');
			} else if (window.ActiveXObject) {
				try { // IE 6+
					http_request = new ActiveXObject('Msxml2.XMLHTTP');
				} catch (e) {
					try { // IE 5.5
						http_request = new ActiveXObject('Microsoft.XMLHTTP');
					} catch (e) {}
				}
			}

			if (!http_request) {
				alert('Unfortunatelly your browser doesn\'t support this feature.');
				return false;
			}
		   
			http_request.onreadystatechange = function() {
				if (http_request.readyState == 4) {
					if (http_request.status == 200) fillResults(http_request.responseXML);
					else alert('There was a problem with the request. (Code: ' + http_request.status + ')');
				}
			}
			
			http_request.open('GET', url, true);
			http_request.send(null);
		}
	}
	
	function fillResults(xmldoc) {
		var rowElement, cellElement;
		
		var lines = xmldoc.getElementsByTagName('line');
		if (!lines.length) {
			stopFill();
			return;
		}
		
		if (tableElement != null) divFillElement.removeChild(tableElement);
		try {
			tableElement = document.createElement('<table cellspacing="0" cellpadding="0" border="0">');
		} catch(ex) {
			tableElement = document.createElement('table');
			tableElement.setAttribute('cellspacing', '0');
			tableElement.setAttribute('cellpadding', '0');
			tableElement.setAttribute('border', '0');
		}
		
		tableElement.className = 'dragtable';
		tableElement.onmouseout = doHighlight;
		
		for (var i = 0; i < lines.length && i < resultsNum; i++) {
			rowElement = tableElement.insertRow(i);
			cellElement = rowElement.insertCell(0);
			cellElement.className = 'content';
			cellElement.onmousedown = fillClick;
			cellElement.onmouseover = doHighlight;
			cellElement.innerHTML = lines[i].firstChild.data.toString();
		}
		divFillElement.appendChild(tableElement);
		
		divFillElement.style.display = 'block';
	}
	
	function fillClick(e) {
		var targetElement = window.event ? window.event.srcElement : e.target;
		anchorElement.value = targetElement.innerHTML;
	}
	
	function doHighlight(e) {
		var targetElement = window.event ? window.event.srcElement : e.target;
		var elements = tableElement.getElementsByTagName('td');
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].className == 'content') unHighlight(elements[i]);
		}
		if ((targetElement != null) && (targetElement.className == 'content')) highlight(targetElement);
	}

	function highlight(element) {
		element.style.background = highlightColor;
	}

	function unHighlight(element) {
		element.style.background = mainColor;
	}
}
