SZN.AjaxSelector = SZN.ClassMaker.makeClass({
	NAME: "AjaxSelector",
	VERSION: "1.0",
	CLASS: "class"
});

SZN.AjaxSelector.prototype.$constructor = function(selectElement, url, paramName, equipment) {
	this.elm = selectElement;
	this.url = url;
	this.paramName = paramName;
	this.equipment = equipment;
	this.eventsCache = [];
	this.childSelects = [];
	for (var i=4;i<arguments.length;i++) {
		var s = SZN.gEl(arguments[i]);
		this.childSelects.push(s);
	}
	this.AjaxSelector();
}

SZN.AjaxSelector.prototype.AjaxSelector = function() {
	this.eventsCache.push(SZN.Events.addListener(this.elm,"change",this,"_change",false,true));
	this.rq = new SZN.HTTPRequest();
	this.rq.setMethod("get");
	this.rq.setFormat("txt");
	this.rq.setMode("async");
}

SZN.AjaxSelector.prototype.$destructor = function() {
	for (var i=0;i<this.eventsCache.length;i++) {
		SZN.Events.removeListener(this.eventsCache[i]);
	}
	for (var p in this) { this[p] = null; }
}

SZN.AjaxSelector.prototype._change = function(e, elm) {
	var value = this.elm.value;
	var url = this.url+"?"+this.paramName+"="+value+"&_nocache="+Math.round(Math.random()*999);
	for (var i=0;i<this.childSelects.length;i++) {
		this.childSelects.disabled = true;
	}
	document.body.style.cursor = "wait";
	this.rq.send(url,this,"_response");
}

SZN.AjaxSelector.prototype._response = function(txt, status) {
	if (status == 200) {
		eval("var response = "+txt+";");
		document.body.style.cursor = "default";
		for (var i=0;i<this.childSelects.length;i++) {
			this.childSelects[i].removeAttribute("disabled");
		}
		for (var i=0;i<this.childSelects.length;i++) {
			var s = this.childSelects[i];
			SZN.Dom.clear(s);
			var values = response[i];
			for (var j=0;j<values.length;j++) {
				var pair = values[j];
				var opt = SZN.cEl("option");
				opt.value = pair[0];
				opt.innerHTML = pair[1];
				s.appendChild(opt);
			}
		}
	}
	
	if (this.equipment) {
		if (optionequipselect) { optionequipselect = null; }
		var values = response[response.length-1]; /* last part of response */
		var eq = SZN.gEl("equipList");
		SZN.Dom.clear(eq);

		var cn = Math.round(values.length/2);
		cn = (values.length % 2) != 0 ? cn+1 : cn;
		
		for(i=0;i<values.length;i++) {
			if(values[i][1] != "nerozhoduje"){
				if ((i % cn) == 0) {
					var item = SZN.cEl("div");
					item.className = "extCol";
					eq.appendChild(item);
				}
				var labelForId = 'eq_'+i;
				// nastavim atributy
				var label = SZN.cEl("label");
				label.htmlFor=labelForId;
				var labelText = document.createTextNode(values[i][1]);
				label.appendChild(labelText);
				// nastavim atrbuty
				var checkBox = SZN.cEl("input");
				checkBox.type = "checkbox";
				checkBox.name = "filter.equipmentIds[]";
				checkBox.value = values[i][0];
				checkBox.id = labelForId;
				
				item.appendChild(SZN.cEl("br"));
				item.appendChild(checkBox);
				item.appendChild(label);
				
				SZN.gEl("equip").innerHTML = "Výbava";
				SZN.Dom.removeClass(SZN.gEl("equip"),'noEquip');
			}else{
				SZN.gEl("equip").innerHTML = "";
				SZN.Dom.addClass(SZN.gEl("equip"),'noEquip');
			}
		}
		if (window.fix_checkboxes) { window.fix_checkboxes(); }
	}	
}
