Formidable.Classes.ListerRow = Formidable.inherit({
	uid: false,
	_parent: false,
	data: {},
	__constructor: function(oConfig) {
		this.uid = false;
		this._parent = false;
		this.data = {};

		Object.extend(this, oConfig);
	},
	rdt: function(sRdt) {
		if((sRdtId = this._parent.config.rdtbyrow[this.uid][sRdt])) {
			return this._parent.oForm.o(sRdtId);
		} else {
			sPath = sRdt.replace(".", "/");
			aPath = sPath.split("/");
			if((oRdt = this.rdt(aPath[0]))) {
				aPath.shift();
				i = 0;
				aPath.each(function(sPathSegment) {
					if((oRdt = oRdt.rdt(sPathSegment))) {
						i++;
					} else {
						throw $break;
					}
				}.bind(this));
				if(i == aPath.size()) {
					return oRdt;
				}
			}
		}

		return false;
	}
});

Formidable.Classes.Lister = Formidable.inherit({
	allselected: false,
	init: function() {
		if(this.config.isajaxlister) {

			oFirst = Formidable.getElementById(this.config.id + "_pagelink_first");
			oPrev = Formidable.getElementById(this.config.id + "_pagelink_prev");
			oNext = Formidable.getElementById(this.config.id + "_pagelink_next");
			oLast = Formidable.getElementById(this.config.id + "_pagelink_last");

			if(oFirst) {
				oFirst.href="javascript:void(0);";
				Formidable.attachEvent(oFirst, "click", this.repaintFirst.bind(this));
			}

			if(oPrev) {
				oPrev.href="javascript:void(0);";
				Formidable.attachEvent(oPrev, "click", this.repaintPrev.bind(this));
			}

			if(oNext) {
				oNext.href="javascript:void(0);";
				Formidable.attachEvent(oNext, "click", this.repaintNext.bind(this));
			}

			if(oLast) {
				oLast.href="javascript:void(0);";
				Formidable.attachEvent(oLast, "click", this.repaintLast.bind(this));
			}

			for (iPage in this.config.repaintwindow) {
				oWindow = $(this.config.id + "_pagelink_window_" + iPage);
				if(oWindow) {
					oWindow.href="javascript:void(0);";
					Formidable.attachEvent(oWindow, "click", this.repaintWindow.bind(this));
				}
			};

			for(var iKey in this.config.columns) {
				sCol = this.config.columns[iKey];
				oSortLink = Formidable.getElementById(this.config.id + "_sortlink_" + sCol)
				if(oSortLink) {
					oSortLink.href="javascript:void(0);";
					oSortLink.sortcol = sCol;
					if(this.config.sort.column == sCol) {
						oSortLink.sortdir = this.config.sort.direction;
					} else {
						oSortLink.sortdir = "no";
					}

					Formidable.attachEvent(oSortLink, "click", this.repaintSortBy.bind(this));
				}
			}

			this.initSelectrow();
		}
	},
	initSelectrow: function() {
		if(this.config.iseditablelister) {
			if(this.config.selectedrow == null) {
				this.config.selectedrow = [];
			}

			oSelectAll = Formidable.getElementsBySelector('formidable-selectall');
			Formidable.attachEvent(oSelectAll, "click", this.selectAll.bind(this));
			for(var sKey in oSelectAll.get()) {
				oSelectAll[sKey].checked = false;
			}

			aOSelect = Formidable.getElementsBySelector('formidable-selectrow');
			Formidable.attachEvent(aOSelect, "click", this.selectRow.bind(this));

			this.checkSelectedRow();
		}
	},
	checkSelectedRow: function() {
		if(this.config.iseditablelister) {
			for(var iKey in this.config.selectedrow) {
				iUid = this.config.selectedrow[iKey];
				oSelectrow = Formidable.getElementById(this.config.id + '.' + iUid + '.selectrow');
				if(oSelectrow) {
					oSelectrow.checked = true;
				}
			}
		}
	},
	selectRow: function(event) {
		value = event.target.id.replace(this.config.id + '.', '');
		value = value.replace('.selectrow', '');
		value = value.replace('row', '');

		oCurCheckbox = Formidable.getElementById(event.target.id);

		if(oCurCheckbox.checked == true) {
			this.config.selectedrow[value] = value;
		} else {
			delete this.config.selectedrow[value];

			oSelectAll = Formidable.getElementsBySelector('formidable-selectall');
			for(var sKey in oSelectAll.get()) {
				if(oSelectAll[sKey].checked == true) {
					oSelectAll[sKey].checked = false;
				}
			}
		}

		Formidable.globalEval(this.config.selectrow);
	},
	selectAll: function(event) {
		bAllselected = false;
		oSelectAll = Formidable.getElementsBySelector('formidable-selectall');
		for(var sKey in oSelectAll.get()) {
			if(oSelectAll[sKey].checked != undefined) {
				bAllselected = oSelectAll[sKey].checked;
			}
		}

		aOSelect = Formidable.getElementsBySelector('formidable-selectrow');
		for(var iKey in aOSelect.get()) {
			oSelectrow = aOSelect[iKey];
			if(oSelectrow) {
				value = oSelectrow.id.replace(this.config.id + '.', '');
				value = value.replace('.selectrow', '');
				value = value.replace('row', '');

				if(bAllselected) {
					this.config.selectedrow[value] = value;
					oSelectrow.checked = true;
				} else {
					delete this.config.selectedrow[value];
					oSelectrow.checked = false;
				}
			}
		}

		Formidable.globalEval(this.config.selectrow);
	},
	getValue: function() {
		oResults = {};

		aRows = this.getRows();
		aRows.each(function(oRow) {
			oResults[oRow.uid] = {};
			$H(this.config.columns).each(function(column, key) {
				oRdt = oRow.rdt(this.config.columns[key]);
				oResults[oRow.uid][this.config.columns[key]] = oRdt.getValue();
			}.bind(this));
		}.bind(this));

		return oResults;
	},
	getRow: function(iUid) {
		return new Formidable.Classes.ListerRow({
			"uid": iUid,
			"_parent": this,
			"data": this.config.clientified[iUid]
		});
	},
	getRows: function() {
		var aRows = $A();

		$H(this.config.rdtbyrow).each(function(oData) {
			aRows.push(this.getRow(oData[0]));
		}.bind(this));

		return aRows;
	},
	getCurrentRow: function() {
		aContext = this.oForm.getContext();
		if(typeof(aContext.currentrow) != "undefined") {
			return this.getRow(aContext.currentrow);
		}
	},
	repaintFirst: function() {
		Formidable.globalEval(this.config.repaintfirst);
		this.checkSelectedRow();
	},
	repaintPrev: function() {
		Formidable.globalEval(this.config.repaintprev);
		this.checkSelectedRow();
	},
	repaintNext: function() {
		Formidable.globalEval(this.config.repaintnext);
		this.checkSelectedRow();
	},
	repaintLast: function() {
		Formidable.globalEval(this.config.repaintlast);
		this.checkSelectedRow();
	},
	repaintSortBy: function(event) {
		eval(this.config.repaintsortby);	// not globalEval to pass local arguments (event) to further methods
		this.checkSelectedRow();
	},
	repaintWindow: function(event) {
		iNameLength = event.target.name.length;
		iPage = event.target.id.substring(iNameLength+1, iNameLength+2);

		eval(this.config.repaintwindow[iPage]); // not globalEval to pass local arguments (event) to further methods
		this.checkSelectedRow();
	},
	getParamsForMajix: function(aValues, sEventName, aParams, aRowParams, aLocalArgs) {
		aValues = this.base(aValues, sEventName, aParams, aRowParams, aLocalArgs);
		aValues["sys_event"] = {};

		for(var iParamKey in aParams) {
			if(aParams[iParamKey] == "sys_event.sortcol") {
				//oElement = Event.element(aLocalArgs[0]);
				oElement = aLocalArgs[0].target;
				aValues["sys_event"].sortcol = oElement.sortcol;
			}

			if(aParams[iParamKey] == "sys_event.sortdir") {
				//oElement = Event.element(aLocalArgs[0]);
				oElement = aLocalArgs[0].target;
				if(oElement.sortdir == "asc") {
					aValues["sys_event"].sortdir = "desc";

				} else {
					// covers "no" and "desc"
					aValues["sys_event"].sortdir = "asc";
				}
			}

			if(aParams[iParamKey] == "sys_event.selectedrow") {
				sValue = '';
				for(var iSelectKey in this.config.selectedrow) {
					if(sValue != '') {
						sValue = sValue + ',' + this.config.selectedrow[iSelectKey];
					} else {
						sValue = this.config.selectedrow[iSelectKey];
					}
				}
				aValues["sys_event"].selectedrow = sValue;
			}
		}

		return aValues;
	}
}, Formidable.Classes.RdtBaseClass);

