elib.EdithData = function ()
{
	/**
	 * Instanzname des Controllers im AJAX-Workspace, auf den bei remote-Verbindung zugegriffen wird.
	 */
	this.controllerInstance = "EDJC";

	/**
	 * Gibt an, ob die Daten remote per AJAX an den EdithDataJSController oder lokal �ber die SQLite-DB heruntergeladen werden.
	 */
	this.remote = true;

	/**
	 * Name der Tabelle aus der Daten bezogen werden sollen.
	 */
	this.table = "";

	/**
	 * Primary Key oder SQL-Statement, aufgrund dessen die Daten dieses Objekts bezogen werden sollen.
	 */
	this.selector = "";

	/**
	 * Wenn angegeben, wird diese Funktion nach Laden der Daten aufgerufen. Wird keine Callback-Funktion
	 * angegeben, so wird der AJAX-Aufruf synchron ausgef�hrt (kann das Script anhalten/verlangsamen).
	 */
	this.callback = null;

	/**
	 * Wenn angegeben, wird diese Funktion nach Speichern der Daten aufgerufen. Wird keine Callback-Funktion
	 * angegeben, so wird der AJAX-Aufruf synchron ausgef�hrt (kann das Script anhalten/verlangsamen).
	 */
	this.saveCallback = null;

	/**
	 * Wird nicht automatisch erkannt und muss daher bei Abweichung manuell gesetzt werden.
	 */
	this.primaryName = "id";

	/**
	 * Geladene Daten.
	 */
	this.data = {};

	

	if ( arguments.length >= 1 )
	{
		this.table = arguments[0];
	}
	
	if ( arguments.length >= 2 )
	{
		this.selector = arguments[1];
	}
	
	if ( arguments.length >= 3 )
	{
		this.callback = arguments[2];
	}
	
	if ( arguments.length >= 4 )
	{
		this.remote = arguments[3];
	}
	
	
	/**
	 *
	 */
	this.fetch = function () 
	{
		if (this.remote)
		{
			var parameter = {
					task: this.controllerInstance,
					mode: 'single',
					selector: this.selector,
					table: this.table
			};
		
			var url = elib.rootPath;
		
			$.ajax({
				ed_object: this,
				async: (this.callback != null),
				type: "POST",
				url: url,
				dataType: "json",
				data: parameter,
				success: function(json)
				{
					this.ed_object.remoteLoaded ( json );
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) 
				{
					alert ("Error: \n" + XMLHttpRequest + "\n\n" + textStatus + "\n" + errorThrown + ": " + url);
				}
			 });
		}
	}
	
	/**
	 *
	 */
	this.save = function () 
	{
		if ( arguments.length >= 1 )
		{
			this.saveCallback = arguments[0];
		}
		else
		{
			this.saveCallback = null;
		}

		var newRecord = ( (this.get (this.primaryName) <= 0) || (this.get (this.primaryName) == "") || (this.get (this.primaryName) == null) )

		if (this.remote)
		{
			var parameter = {
					task: this.controllerInstance,
					mode: (newRecord ? "insert" : "update"),
					table: this.table					
			};
			
			// Daten als values-Array hinzuf�gen
			for (fieldName in this.data)
			{
				parameter['values[' + fieldName + ']'] = this.data[fieldName];
			}
		
			var url = elib.rootPath;
		
			$.ajax({
				ed_object: this,
				async: (this.saveCallback != null),
				type: "POST",
				url: url,
				dataType: "json",
				data: parameter,
				success: function(json)
				{
					this.ed_object.remoteSaved ( json );
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) 
				{
					alert ("Error: \n" + XMLHttpRequest + "\n\n" + textStatus + "\n" + errorThrown + ": " + url);
				}
			 });
		}
	}
	
	/**
	 *
	 */
	this.del = function () 
	{
		if ( arguments.length >= 1 )
		{
			this.deleteCallback = arguments[0];
		}
		else
		{
			this.deleteCallback = null;
		}

		if (this.remote)
		{
			var parameter = {
					task: this.controllerInstance,
					mode: "delete",
					table: this.table,
					selector: this.get (this.primaryName)
			};
			
			var url = elib.rootPath;
		
			$.ajax({
				ed_object: this,
				async: (this.deleteCallback != null),
				type: "POST",
				url: url,
				dataType: "json",
				data: parameter,
				success: function(json)
				{
					this.ed_object.remoteDeleted ( json );
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) 
				{
					alert ("Error: \n" + XMLHttpRequest + "\n\n" + textStatus + "\n" + errorThrown + ": " + url);
				}
			 });
		}
	}
	
	/**
	 *
	 */
	this.remoteLoaded = function (json)
	{
		if ( json.errorMessage != null )
		{
			alert ("EdithData - Fehler beim ausfuehren des Statements: " + json.errorMessage);
			return;
		}

		this.data = json;
		
		if (this.callback != null)
		{
			this.callback (this);
		}
	}
	
	/**
	 *
	 */
	this.remoteSaved = function (json)
	{
		if ( !json.success )
		{
			alert ("EdithData - Fehler beim Speichern: " + json.errorMessage);
			return;
		}

		this.set (json.primaryName, json.primaryValue);

		if (this.saveCallback != null)
		{
			this.saveCallback (this);
		}
	}
	
	/**
	 *
	 */
	this.remoteDeleted = function (json)
	{
		if ( !json.success )
		{
			alert ("EdithData - Fehler beim Loeschen: " + json.errorMessage);
			return;
		}

		if (this.deleteCallback != null)
		{
			this.deleteCallback (this);
		}
	}
	
	/**
	 *
	 */
	this.get = function (field)
	{
		return this.data[field];
	}
	
	/**
	 *
	 */
	this.set = function (field, value)
	{
		this.data[field] = value;
	}
	
	/**
	 *
	 */
	this.getPrimary = function ()
	{
		return this.data[this.primaryName];
	}
	
	/**
	 *
	 */
	this.setPrimary = function (value)
	{
		this.data[this.primaryName] = value;
	}
	
	
	if ( (this.table != "") && (this.selector != "") )
	{
		this.fetch();
	}


};


