/*
	Quick Search Tool
		
	by Anthony Dickens
*/

	function quickSearch (formElement, queryElement, resultsElement, searchUrl, resultsContainer) {
		this.formElement = document.getElementById(formElement);
		this.queryElement = document.getElementById(queryElement);
		this.resultsElement = document.getElementById(resultsElement);
		this.resultsContainer = resultsContainer ? document.getElementById(resultsContainer) : false;
		this.searchUrl = searchUrl;
		this.highlight = 0;
		this.total = 0;
		this.lastQuery = '';
		
		// Turn off autocomplete
		this.queryElement.setAttribute('autocomplete','off');
		
		// Events
		ccms.event.attach(this.queryElement,"keydown",this.keyWatch, this, true);
		ccms.event.attach(this.queryElement,"keyup",this.doSearch, this, true);
		ccms.event.attach(this.queryElement,"onchange",this.doSearch, this, true);
		ccms.event.attach(this.queryElement,"click",this.reset, this, true);
		ccms.event.attach(this.queryElement,"focus",this.reset, this, true);
	}
	
	quickSearch.prototype.keyWatch = function (event) {
		switch (event.keyCode) {
			case 13: // Enter
				if (this.highlight > 0 && document.getElementById('searchResultUri' + this.highlight)) {
					window.location = document.getElementById('searchResultUri' + this.highlight).getAttribute('href');
					return ccms.event.killEvent(event);
				}
			break;
		}
	}
	
	quickSearch.prototype.doSearch = function (event) {
		var query = this.queryElement.value;
		
		switch (event.keyCode) {
			case 32:
				return false;
			break;
			
			case 40:
			case 38:
				// Reset Old Class
				if (current = document.getElementById('searchResult' + this.highlight)) {
					current.className = 'item ' + (this.highlight % 2 == 1 ? 'row2' : 'row1');
				}
				
				// Update Counter
				switch (event.keyCode) {
					case 40:
						this.highlight = (this.highlight < this.total) ? this.highlight + 1 : 0;
					break;
					case 38:
						this.highlight = (this.highlight > 0) ? this.highlight - 1 : this.total;
					break;
				}
				
				// New Highlight
				if (highlight = document.getElementById('searchResult' + this.highlight)) {
					highlight.className = 'item highlight';
				}
				return ccms.event.killEvent(event);
			break;
			
			case 27: // Escape
				this.reset();
				return ccms.event.killEvent(event);
			break;
		}
		
		if (query.length > 2) {
			if (query != this.lastQuery) {
				this.lastQuery = query;
				ccms.async.http({method:'GET',url:this.searchUrl + '?query=' + encodeURIComponent(query),onSuccess:this.processResults,self:this});
			}
		}
		else {
			this.updateResults('');
		}
	}

	quickSearch.prototype.updateResults = function (theHTML) {
		if (theHTML == '') {
			if (this.resultsContainer) {
				this.resultsContainer.style.display = 'none';
			}
			else {
				this.resultsElement.style.display = 'none';
			}
		}
		else {
			if (this.resultsContainer) {
				this.resultsContainer.style.display = 'block';
			}
			else {
				this.resultsElement.style.display = 'block';
			}
		}
		this.resultsElement.innerHTML = '<div class="search-wrapper">' + theHTML + '</div>';
	}
	
	quickSearch.prototype.reset = function () {
		this.updateResults('');
		this.lastQuery = '';
		this.highlight = 0;
		this.queryElement.value = '';
	}

	quickSearch.prototype.processResults = function (req) {
		var items = req.responseXML.getElementsByTagName("item");
		var theHTML = '';
		this.self.total = items.length;
		if (items.length > 0) {
			for (var i = 0; i < items.length; i++) {
				var item = items[i];
				
				if (html = item.getElementsByTagName('html')[0]) {
					theHTML += html.firstChild.data;
				}
				else {
					url = item.getElementsByTagName('url')[0];
					rowClass = this.self.highlight == (i+1) ? 'highlight' : ((i+1) % 2 == 1 ? 'row2' : 'row1');
					theHTML += '<div id="searchResult' + (i+1) + '" class="item ' + rowClass + '">\n';
					if (icon = item.getElementsByTagName('icon')[0]) {
						theHTML += '<div class="left"><img class="framed" src="' + icon.firstChild.data + '" width="' + icon.getAttribute('width') + '" height="' + icon.getAttribute('height') + '" alt="" /></div>\n';
					}
					if (title = item.getElementsByTagName('title')[0]) {
						theHTML += '<h2 class="item-heading"><a id="searchResultUri' + (i+1) + '" href="'+ url.firstChild.data + '">' + title.firstChild.data + '</a></h2>\n';
					}
					if (desc = item.getElementsByTagName('description')[0]) {
						theHTML += '<p><strong>' + desc.firstChild.data + '</strong></p>\n';
					}
					theHTML += '<div class="clear"></div>\n';
					theHTML += '</div>\n';
				}
			}
			theHTML = '<div class="items">' + theHTML + '</div><p class="controls">[Esc] Close, [Up/Dn] Move, [Enter] Submit</p>';
		}
		this.self.updateResults(theHTML);
	}
