/**
 * Prototypes for Search part
 */

//Prototype for search request
	var spellingRequest = function(query){

	//Constructor
		this.query			= query;
		this.engine			= 'YahooBoss';
		this.searchUrl;

	//Abstract methods
		this.onRequestReceived = new Function();

	//Public methods

		//Prepare query to searchWebService as xml string
		this.prepareQuery = function(){
			var sendQuery = this.query;

			if (sendQuery.length > 0) {
				sendQuery = sendQuery.replace(/&/g, '&amp;');
				sendQuery = sendQuery.replace(/</g, '&lt;');
				sendQuery = sendQuery.replace(/>/g, '&gt;');
				sendQuery = sendQuery.replace(/\\/g, '\\\\');
			};

			var xmlQuery 	= '<?xml version="1.0"?>'
							+ '<searchQuery>'
							+ '<query>'+sendQuery+'</query>'
							+ '<spellingSuggestionEngine>'+this.engine+'</spellingSuggestionEngine>'
							+ '</searchQuery>';
			return xmlQuery;
		};

		//Send query to searchWebService via HTTP/POST
		this.sendQuery = function() {
			var searchString = this.prepareQuery();
			$.post(this.searchUrl, {query: searchString}, this.onRequestReceived, 'text');
		};

		//Import from xml string
		this.importFromXml = function(xml) {
			this.query			= $('query', xml).text();
			this.engine			= $('spellingSuggestionEngine', xml).text();
		};
	};

//Prototype for response
	var spellingResponse = function(xml){

	//Constructor
		var totalHits		= $('totalHits', xml).text();
		this.time			= $('time', xml).text();

		this.spellingQuery	= new spellingRequest();
		this.spellingQuery.importFromXml( $('searchQuery', xml) );

		this.resultsList	= new Array();
		var resultsList 	= this.resultsList;

		var results			= $('responseResults', xml);
		var i=0;
		$('result', results).each(function() {
			resultsList[i] = new Object();
			resultsList[i].title		= $('title', this).text().replace(/&amp;/g, '&');
			i++;
		});
	};