/**
 * @param {String} formID		the form that contains the search parameters
 * @param {String} targetID		the target DIV for the results to display
 * @param {String} type			the search method to call
 * @param {String} engineUrl	The Ajax backend CFC
 * @return {Search}
 * 
 * @projectDescription
 * <p>
 * <b>Usage:</b>  this searcher observes form input fields, so the DOM needs to be fully loaded <u>before</u> you instantiate it.
 * The proper way to do this is by listening for a window.onload event. 
 * </p>
 * 
 * <p>
 * <b>Example: </b> <br>
 * <code>
 * Event.observe(window, 'load', init); <br><br>
 * function init() { <br>
 *	var search = new Search('searchForm', 'searchResultsDiv', 'archiveSearch', '#searchCFC#'); <br>
 *	search.setLanguageFilter('NED'); <br>
 *	search.setCountryFilter('B'); <br>
 *	search.setNoResultsMsg("<h3>No results</h3><p>Probeer iets anders</p>"); <br>
 *	search.setErrorMsg("Vul minstens één zoekveld in!"); <br>
 * }
 * </code>
 * </p>
 * 
 * <p>
 * <b>Compatibility:</b> IE7/PC, IE6/PC, FireFox 2.0,  <br>
 * <b>Not tested:</b> IE5.5/PC and below, FireFox 1.5, IE5.2/Mac, Opera 8.0, Safari, Camino <br>
 * <b>Breaks</b> on 
 * </p>
 * 
 * @version built on Prototype 1.5
 * @author Maxime Cowez
 * @copyright <a href="http://www.edge.be">Edge.be</a>
 */
if (!Edge) var Edge = {};
Edge.MovieArchive = Class.create();
Edge.MovieArchive.prototype = {
		
	/**
	 * @constructor
	 * @param {String} form			the form that contains the search parameters
	 * @param {String} target		the target DIV for the results to display
	 * @param {String} type			the search method to call
	 * @param {String} engineUrl	The Ajax backend CFC
	 * @return {Search}
	 */
	initialize: function(form, target, type, engineUrl, options) {
		this.form = $(form) || Edge.Tools.Debug.error("You need to pass in a form elm as 1st parameter");
		this.target = $(target) || Edge.Tools.Debug.error("You need to pass in a div elm as 2nd parameter");
		this.type = type || Edge.Tools.Debug.error("You need to pass in an ajax search method as 3d parameter");		
		this.engineUrl = engineUrl || Edge.Tools.Debug.error("You need to pass in an ajax url as 4th parameter");
		
		this.languageFilter = options.languageFilter || '';
		this.countryFilter = options.countryFilter || '';
		this.noResultsMsg = options.errorMsg.noResults || "No results (default message)";
		this.badDateMsg = options.errorMsg.badDate || "Bad date (default message)";
		this.errorMsg = options.errorMsg.error || "Error! (default message)";
		
		$('resetButton').observe('click', this.resetFields.bind(this));
		this.form.getInputs('text').each(function(input) {
			Event.observe(input, 'keyup', this.checkKey.bind(this));	//1.5
		}.bind(this)); //1.5
	},
	
	/**
	 * checks whether the pressed key is <code>Enter</code> and submits
	 * @param {Event} evt	a <code>keyUp</code> event
	 */
	checkKey: function(evt) {
		if (evt.keyCode == Event.KEY_RETURN) this.submitSearch(evt);
	},
	
	/**
	 * submits the search parameter to the Ajax backend
	 * @param {Event} evt	either a <code>click</code> or a <code>keyUp</code> event
	 */
	submitSearch: function(evt) {
		if (evt) Event.stop(evt);
		
		var param = {
				method: this.type, 
				languageFilter: this.languageFilter, 
				countryFilter: this.countryFilter}
	
			this.form.getInputs('text').concat(this.form.getInputs('hidden')).each(function(input) {
				param[input.id] = input.value;
			});
				
			new Ajax.Request(this.engineUrl, 
				{method:'post', parameters:param, 
				onComplete: this.showResults.bind(this), 
				onFailure: Edge.Tools.Debug.error});
	},
	
	/**
	 * resets the text input field values
	 * @param {Event} evt	a <code>click</code> event
	 */
	resetFields: function(evt) {
		Event.stop(evt);
		this.form.getInputs().each(function(input) {input.value = '';})	
	},
	
	/**
	 * parses the backend response and displays it
	 * @param {XMLHttpRequest} resp
	 */
	showResults: function(resp) {
		resp = Edge.Tools.Ajax.get(resp, this.engineUrl);
		switch (resp) {
			case "noResults":	this.target.update(this.noResultsMsg); break;
			case "error":		this.target.update(this.errorMsg); break;
			case "badDate":		this.target.update(this.badDateMsg); break;
			default:			this.target.update(resp); break;
		}
	}
	
}