var CSeaFilter = Class.create();
CSeaFilter.prototype = {

	initialize: function(config){
		this.options = {
			'submitUrl': config['submitUrl'],
			'formId': config['formId'],
			'formContainer': config['formContainer'],
			'dataContainer': config['dataContainer'],
			'els': config['els']
		};
	},

	init: function()
	{
		Event.observe(window, 'load', function() {
			this.formId = $(this.options['formId']);
			this.formContainer = $(this.options['formContainer']);
			this.dataContainer = $(this.options['dataContainer']);

			this.formContainer.select('label input').each(function(selected){
				Event.observe(selected, 'click', this.onClk.bind(this, selected));
			}.bind(this));

			for (i = 0; i < this.options['els'].length; i++)
			{
				this.formContainer.select('a.sea' + this.options['els'][i] + 'All').each(function(selected){
					Event.observe(selected, 'click', this.onAll.bind(this, 'sea' + this.options['els'][i]));
				}.bind(this));
				this.formContainer.select('a.sea' + this.options['els'][i] + 'None').each(function(selected){
					Event.observe(selected, 'click', this.onNone.bind(this, 'sea' + this.options['els'][i]));
				}.bind(this));
			}

			this.formContainer.select('a.seaClearAll').each(function(selected){
				Event.observe(selected, 'click', function(){
					this.clearAll();
					this.clearPrice();
					this.onChange();
				}.bind(this));
			}.bind(this));

			this.formContainer.select('a.seaPriceClear').each(function(selected){
				Event.observe(selected, 'click', function(){
					this.clearPrice();
					this.onChange();
				}.bind(this));
			}.bind(this));

			this.formContainer.select('table.trackbar td.l').each(function(selected){
				Event.observe(selected, 'mouseup', this.onChange.bind(this));
			}.bind(this));
			this.formContainer.select('table.trackbar td.c').each(function(selected){
				Event.observe(selected, 'mouseup', this.onChange.bind(this));
			}.bind(this));
			this.formContainer.select('table.trackbar td.r').each(function(selected){
				Event.observe(selected, 'mouseup', this.onChange.bind(this));
			}.bind(this));

		}.bind(this));
	},

	onClk: function(sel)
	{
		if (false == sel.checked)
		{
			sel.up(0).removeClassName('checkboxOn');
			sel.up(0).addClassName('checkboxOff');
		}
		else
		{
			sel.up(0).removeClassName('checkboxOff');
			sel.up(0).addClassName('checkboxOn');
		}
		this.onChange();
	},

	clearAll: function()
	{
		this.formContainer.select('input.seaManufacturer').each(function(selected){
			selected.checked = false;
			selected.up(0).removeClassName('checkboxOn');
			selected.up(0).addClassName('checkboxOff');
		}.bind(this));
		this.formContainer.select('input.seaColor').each(function(selected){
			selected.checked = false;
			selected.up(0).removeClassName('checkboxOn');
			selected.up(0).addClassName('checkboxOff');
		}.bind(this));
		this.formContainer.select('input.seaSize').each(function(selected){
			selected.checked = false;
			selected.up(0).removeClassName('checkboxOn');
			selected.up(0).addClassName('checkboxOff');
		}.bind(this));
		this.formContainer.select('input.seaSlug').each(function(selected){
			selected.checked = false;
			selected.up(0).removeClassName('checkboxOn');
			selected.up(0).addClassName('checkboxOff');
		}.bind(this));
	},

	clearPrice: function()
	{
		$('seaMinPrice').value = 0;
		$('seaMaxPrice').value = 300;

		window.trackbar.getObject('one').updateLeftValue(0);
		window.trackbar.getObject('one').updateRightValue(300);
	},

	onAll: function(type)
	{
		this.formContainer.select('input.' + type).each(function(selected){
			selected.checked = true;
			selected.up(0).removeClassName('checkboxOff');
			selected.up(0).addClassName('checkboxOn');
		}.bind(this));
		this.onChange();
	},

	onNone: function(type)
	{
		this.formContainer.select('input.' + type).each(function(selected){
			selected.checked = false;
			selected.up(0).removeClassName('checkboxOn');
			selected.up(0).addClassName('checkboxOff');
		}.bind(this));
		this.onChange();
	},

	evalResponse: function(transport) {
		var response = (
			Object.isUndefined(transport.responseText) ? transport : transport.responseText
		);

		return response;
	},

	setPage: function(page)	{
		$('sea_filter_page').value = page ? page : 1;
	},

	setRange: function(range) {
		$('sea_filter_range').value = range ? range : '';
	},

	setSort: function(sort)	{
		$('sea_filter_sort').value = sort ? sort : '';
	},

	setShow: function(show)	{
		$('sea_filter_show').value = show ? show : '';
	},

	onChange: function()
	{
		var cntr = this.dataContainer, f = this.formId;
		cntr.startWaiting('bigWaiting');

		new Ajax.Request(this.options['submitUrl'], {
				method: 'POST',
				postBody: Form.serializeElements(f.getElements()),
				onFailure: function(cntr) {
					cntr.stopWaiting();
				}.bind(this, cntr),
				onSuccess: function(cntr, transport) {
					var data;
					if (!(data = this.evalResponse(transport)))
					{
						alert('Sorry! Internal error!');
					}
					else
					{
						cntr.update(data);
					}
					cntr.stopWaiting();
				}.bind(this, cntr)
			}
		);
	}
}