/******************* B R O W S E R ************/
function browserObj (formObj)
{
	this.searchResultsHide = null
	this.formObj = $(formObj);
	
	var thisObj = this;
	this.formObj.submit(function() {
		return thisObj.filtering();
	});
	
	// HIDE SUBMIT BUTTON
	$("input").each(function () {
		var $this = $(this),
			type = $this.attr("type");
		if (type === 'submit') {
			$this.css("display", "none");
		} else if (type === 'text' && $this.attr("name") === 'search') {
			thisObj.searchObj = this;
			$this.keyup(function (e) {
				if (thisObj.searchResultsWait) {
					clearTimeout(thisObj.searchResultsWait);
					thisObj.searchResultsWait = null;
				}
				if (thisObj.searchObj.value.length > 2) {
					thisObj.searchResultsWait = setTimeout(function () {
						thisObj.filtering.call(thisObj);
					}, 700);
				}
			});
		}
	});
	
	$("select").change(function () {
		thisObj.filtering();
	});
	
	this.formObj.find("ul").each(function () {
		var $this = $(this);
		$this.find("input[type=checkbox]").css("display", "none");
		var parentLabel,
			childsLabel = [],
			childFn = function() {
				thisObj.switching(this);
				thisObj.uncheckParent(parentLabel, childsLabel);
				return thisObj.filtering();
			},
			parentFn = function() {
				thisObj.check(this);
				thisObj.uncheckChilds(childsLabel);
				return thisObj.filtering();
			};
		$this.find("label").each(function (i) {
			if (i) {
				childsLabel.push(this);
			} else {
				parentLabel = this;
			}
			$(this).click(i ? childFn : parentFn);
		});
	});
	
	this.dynamicContent = $("#dynamic-content");
	this.initDynamic();
}

browserObj.prototype = {
	initDynamic: function() {	
		var thisObj = this;
		this.dynamicContent.find("input[type=checkbox][rel=comparison]").click(function () {
			thisObj.parseCompare(this);
		});
	},
	switching: function(label) {
		label.className === 'paramNotSelected' ? this.check(label) : this.uncheck(label);
	},
	getInput: function (label) {
		return document.getElementById(label.getAttribute("for"));
	},
	check: function (label) {
		this.getInput(label).checked = true;
		label.className = 'paramSelected';
	},
	uncheck: function (label) {
		this.getInput(label).checked = false;
		label.className = 'paramNotSelected';
	},
	uncheckChilds: function (childsLabel) {
		for (var i = 0; i < childsLabel.length; i++) {
			this.uncheck(childsLabel[i]);
		}
	},
	uncheckParent: function (parent, childsLabel) {
		this.uncheck(parent);
		var oneIsSelected = false;
		for (var i = 0; i < childsLabel.length; i++) {
			if (this.getInput(childsLabel[i]).checked) {
				oneIsSelected = true;
				break;
			}
		}
		!oneIsSelected && this.check(parent);
	},
	filtering: function () {
		var params = this.formObj.serialize(),
			getString = this.formObj.attr("action");
		if (params) {
			getString += "&" + params;
		}
		if (getString.charAt(getString.length - 1) != '?') {
			getString += '&';
		}
		getString += (getString.charAt(getString.length - 1) !== '?' ? "&" : "") + "onthebody=true";
		startLoading();
		var thisObj = this;
		$.post(getString, function (response) {
			thisObj.dynamicContent[0].innerHTML = response;
			thisObj.initDynamic();
			stopLoading();
		});
		return false;
	},
	// C O M P A R I S O N //
	parseCompare: function (obj) {
		var pid = obj.value,
			cid = obj.getAttribute('categories_id'),
			getString = "index.php?load=comparison&task=module&categories_id="+cid+"&products_id="+pid+"&onthefly=true&action=" + (obj.checked ? "add" : "remove");
		$.post(getString, function (response) {
			document.getElementById('comparison').innerHTML = response;
		});
	}
}
