Formidable.Classes.ListerRow = Base.extend({
	uid: false,
	_parent: false,
	data: {},
	constructor: function(oConfig) { 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.Classes.RdtBaseClass.extend({
	constructor: function(oConfig) {
		this.base(oConfig);
	},
	init: function() {
		if(this.config.isajaxlister) {
			
			oFirst = $(this.config.id + "_pagelink_first");
			oPrev = $(this.config.id + "_pagelink_prev");
			oNext = $(this.config.id + "_pagelink_next");
			oLast = $(this.config.id + "_pagelink_last");
			
			if(oFirst) {
				oFirst.href="javascript:void(0);";
				Event.observe(oFirst, "click", this.repaintFirst.bindAsEventListener(this));
			}
			
			if(oPrev) {
				oPrev.href="javascript:void(0);";
				Event.observe(oPrev, "click", this.repaintPrev.bindAsEventListener(this));
			}
			
			if(oNext) {
				oNext.href="javascript:void(0);";
				Event.observe(oNext, "click", this.repaintNext.bindAsEventListener(this));
			}
			
			if(oLast) {
				oLast.href="javascript:void(0);";
				Event.observe(oLast, "click", this.repaintLast.bindAsEventListener(this));
			}

			for (iPage in this.config.repaintwindow) {
				oWindow = $(this.config.id + "_pagelink_window_" + iPage);
				if(oWindow) {
					oWindow.href="javascript:void(0);";
					Event.observe(oWindow, "click", this.repaintWindow.bindAsEventListener(this));
				}
			};
			
			Object.values(this.config.columns).each(function(sCol) {
				oSortLink = $(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";
					}
					
					Event.observe(oSortLink, "click", this.repaintSortBy.bindAsEventListener(this));
				}
			}.bind(this));
		}	
	},
	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);
	},
	repaintPrev: function() {
		Formidable.globalEval(this.config.repaintprev);
	},
	repaintNext: function() {
		Formidable.globalEval(this.config.repaintnext);
	},
	repaintLast: function() {
		Formidable.globalEval(this.config.repaintlast);
	},
	repaintSortBy: function(event) {
		//Formidable.globalEval(this.config.repaintsortby);
		eval(this.config.repaintsortby);	// not globalEval to pass local arguments (event) to further methods
	},
	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
	},
	getParamsForMajix: function(aValues, sEventName, aParams, aRowParams, aLocalArgs) {
		aValues = this.base(aValues, sEventName, aParams, aRowParams, aLocalArgs);
		
		aValues["sys_event"] = {};
		
		if((index = Object.values(aParams).indexOf("sys_event.sortcol")) != -1) {
			oElement = Event.element(aLocalArgs[0]);
			aValues["sys_event"].sortcol = oElement.sortcol;
		}
		
		if((index = Object.values(aParams).indexOf("sys_event.sortdir")) != -1) {
			oElement = Event.element(aLocalArgs[0]);
			if(oElement.sortdir == "asc") {
				aValues["sys_event"].sortdir = "desc";
			} else {
				// covers "no" and "desc"
				aValues["sys_event"].sortdir = "asc";
			}
		}

		return aValues;
	}
});

