
ReallyProductQuery = function(options) {
	this.formId = options.form;
	this.widgetId = options.target;
	this.resultsId = options.results;
	this.loadingId = options.loading;
	// this.loadingElement = ReallyHtml.byId(options.loading);

	this.updateFields = options.updateFields;

	/*
	if(options.updateFields) {
		this.updateFields = {};
		for(var k in options.updateFields) {
			var key = options.updateFields[k];
			this.updateFields[key] = true;
		}
	}
	*/
	
	/*
	if(options.elements) {
		for(var k in options.elements) {
			
			this.elements[k] = ReallyHtml.byId(options.elements[k]);
		}
	}
	else {
		for(var k in formElement.elements) {
			this.elements[k] = ReallyHtml.byId(options.elements[k]);
		}
	}
	*/

	this.init();
};

ReallyProductQuery.prototype = { };

ReallyProductQuery.prototype.init = function() {
	
	this.initElements();

	// this.loading = false;
};

ReallyProductQuery.prototype.reset = function() {
	console.log('reset');
	
	this.ignoreChanges = true;
	
	this._values = { 'do': 'reset' };
	this.refresh();

	delete this.ignoreChanges;
	
	return(false);
};

ReallyProductQuery.prototype.initElements = function() {
	var resetButton = ReallyHtml.byId('reset');	
	if(resetButton) {
		resetButton.onclick = ReallyEvent.callback(this, this.reset);
	}
	else {
		console.log('resetButton not found!');
	}
	
	this.formElement = ReallyHtml.byId(this.formId);
	var exclude = "file|submit|image|button|reset|";
	var elements = this.formElement.elements;
	for(var elementIdx = 0; elementIdx < elements.length; elementIdx++) {
		var e = elements.item(elementIdx);
		if(!e || !e.type || !e.name)
			continue;
		
		// console.log(e);
		
		var name = e.name;
		var type = (e.type||"").toLowerCase();
		if(exclude.indexOf(type) != -1)
			continue;
		
//		if(this.updateFields && !this.updateFields[name])
//			continue;
		
		if(type == "radio" || type == "checkbox") {
			ReallyEvent.addDomListener(e, 'click', ReallyEvent.callback(this, function(name) {
				this.onChange(name);
			}, name));
		}
		else {
			ReallyEvent.addDomListener(e, 'change', ReallyEvent.callback(this, function(name) {
				this.onChange(name);
			}, name));
		}
	}
};

ReallyProductQuery.prototype.onChange = function(name) {
	if(this.ignoreChanges)
		return;
	
	this._values = ReallyXhr.getFormValues(this.formElement);
	this.refresh();
};

ReallyProductQuery.prototype.refresh = function() {
	if(this.xhr) {
		this.xhr.cancel();
		delete this.xhr;
	}
	
	this.onLoadingBegin();
	
	this._values['do'] = 'search';
	this._values['method'] = 'update';
	this._values['target'] = this.widgetId;
	var attr = this.formElement.getAttributeNode("action");
	var url = (attr ? attr.value : null); 
	this.xhr = ReallyXhr.get({
		url: url,
		content: this._values,
		load: ReallyEvent.callback(this, '_handleResponse')
	});
};

ReallyProductQuery.prototype._handleResponse = function(response) {
	if(this.xhr) {
		delete this.xhr;
	}
	
	// this.loading = false;
	
	try {
		var resultsElement = ReallyHtml.byId(this.resultsId);
		if(resultsElement)
			ReallyHtml.replace(response, resultsElement);
	}
	catch(e) {
		// console.log(e);
	}
	
	this.initElements();
	
	this.onLoadingEnd();
};

ReallyProductQuery.prototype.onLoadingBegin = function() {
	if(this.loadingId) {
		var e = ReallyHtml.byId(this.loadingId);
		if(e)
			e.style.visibility = 'visible';
	}
};

ReallyProductQuery.prototype.onLoadingEnd = function() {
	if(this.loadingId) {
		var e = ReallyHtml.byId(this.loadingId);
		if(e)
			e.style.visibility = 'hidden';
	}
};

