var CSearch = Class.create();
CSearch.prototype = {
	
	initialize: function() {
		this.form = $('searchForm2');
		this.resDisp = $('advsearch');
		this.waitDisp = $('advsearch');
		this.formContent = '';
		this.queryParams = window.location.href.toQueryParams();
		//alert($H(this.queryParams).inspect());
		this.advFields = new Array('m', 'match', 'sort', 'genre_id', 'cat_id', 'actor', 'director');
		this.hideWait();
		
		var showAdvanced = false;
		for (i=0; i<this.advFields.length; i++) {
			if (this.queryParams[this.advFields[i]]) {
				showAdvanced = true;
			}
		}
		if (showAdvanced) {
			this.showAdvanced();
		}
		
	},
	
	toggleAdvanced: function() {
		if (!this.resDisp.innerHTML.length) {
			this.showAdvanced();
		} else {
			this.resDisp.innerHTML = '';
			this.resDisp.hide();
			$('a_more').update('дополнительно');
		}
	},
	
	showAdvanced: function() {
		this.getAdvanced();
		this.resDisp.show();
		$('a_more').update('простой поиск');
	},
	
	toggleGenres: function(ex) {
		if (this.form.cat_id.value == ex){
			$('span_genre').show();
		} else {
			$('span_genre').hide();
			this.selectAllGenres(false);
			this.form.elements['genre_id[]'].value='';
		}
	},

	getAdvanced: function() {

		if (this.formContent) {
			this.resDisp.update(this.formContent);
			this.updateFields();
			return false;
		}
		var url = "/search/";
		var pars = "a=getadvanced";
		this.showWait();
		var myAjax = new Ajax.Request(
			url,
			{method: 'get', parameters: pars, onFailure: this.reportError.bind(this), onComplete: this.afterGet.bind(this)}
		);
		return false;
	},
	
	afterGet: function(request) {
		this.formContent = request.responseText;
		this.resDisp.update(this.formContent);
		this.updateFields();
	},
	reportError: function (request) {
		this.resDisp.innerHTML = 'ошибка';
	},
	
	updateFields: function () {
		if (this.queryParams['cat_id']) this.form.cat_id.value = this.queryParams['cat_id'];
		if(this.queryParams['sort']) this.form.sort.value = this.queryParams['sort'];
		if(this.queryParams['match']) this.form.match.value = this.queryParams['match'];
		if (this.queryParams['exact']) this.form.exact.checked = true;
		
		var pGenres = this.queryParams['genre_id[]'];
		if (!pGenres) {
			//this.selectAllGenres(true);
		} else {
			if (typeof(pGenres) != 'object') pGenres = new Array(pGenres);
			var formGenres = $A(this.form.elements['genre_id[]'].getElementsByTagName('option'));
			for (var i=0; i<formGenres.length; i++) {
				formGenres[i].selected = false;
				for (var g=0; g<pGenres.length; g++) {
					if (pGenres[g] == formGenres[i].value) {
						formGenres[i].selected = true;
					}
				}
			}
		}
		
		var modes = Form.getInputs(this.form,'radio','m');
		for (var i=0; i<modes.length; i++) {
			if (modes[i].value == this.queryParams['m']) {
				modes[i].checked = true;
			}
		}
	},
	
	selectAllGenres: function (method) {
		var formGenres = $A(this.form.elements['genre_id[]'].getElementsByTagName('option'));
		for (var i=0; i<formGenres.length; i++) {
			formGenres[i].selected = method;
		}
	},
	
	showWait: function() {
		this.waitDisp.innerHTML = "<span style='color: blue;'>загрузка.. </span><img src='/pics/waiting.gif' />";
		Element.show(this.waitDisp);
	},
	hideWait: function() {
		this.waitDisp.innerHTML = "";
		Element.hide(this.waitDisp);
	}
	
}
