/**
 * @author Developing
 */
Model = function(xmlRootName, ajaxPathLoad, ajaxPathSave, ajaxPathUpdate, ajaxPathRemove)
{
	this.xmlRootName = ( (xmlRootName != null) ? xmlRootName : "item" );
	this.ajaxLoadScriptPath = ajaxPathLoad;
	this.ajaxSaveScriptPath = ajaxPathSave;
	this.ajaxUpdateScriptPath = ajaxPathUpdate;
	this.ajaxRemoveScriptPath = ajaxPathRemove;
	this.properties = new Array();
}

Model.prototype.load = function(callbackFunction, callbackErrorFunction)
{
	if ( typeof(this.properties.id) != "undefined" )
	{
		var model = this;
		var dataArray = new Array();
		dataArray.push(parseInt(model.properties.id));
		$.ajax({
			metod: "POST",
			url: model.ajaxLoadScriptPath,
			cache: false,
			data: {data: JSON.stringify(dataArray) },
			timeout: 8000,
			success: function(xml){
				model.loadFromXML(xml);
				if ( typeof(callbackFunction) == "function" )
				{
					callbackFunction(xml);
				}
			},
			error: function(){
				if ( typeof(callbackErrorFunction) == "function" )
				{
					callbackErrorFunction();
				}
			}
		});
	}
}

Model.prototype.loadFromXML = function(xml)
{
	var model = this;
	$(this.xmlRootName, xml).contents().each( function() 
	{
		model.properties[$(this).get(0).tagName] = $(this).text();
	} );
}

Model.prototype.clone = function()
{
	var clone = new Model();
	clone.properties = this.properties;
	clone.xmlRootName = this.xmlRootName;
	clone.ajaxLoadScriptPath = this.ajaxLoadScriptPath;
	clone.ajaxSaveScriptPath = this.ajaxSaveScriptPath;
	clone.ajaxUpdateScriptPath = this.ajaxUpdateScriptPath;
	clone.ajaxRemoveScriptPath = this.ajaxRemoveScriptPath;
	return clone;
}

Model.prototype.save = function(callbackFunction, callbackErrorFunction)
{
	var model = this;
	var dataArray = new Array();
	dataArray[0] = new Array();
	for ( name in model.properties)
	{
		dataArray[0].push( {name: name, value: model.properties[name]} );
	}
	$.ajax({
		type: "POST",
		url: model.ajaxSaveScriptPath,
		cache: false,
		data: {data: JSON.stringify(dataArray) },
		timeout: 8000,
		success: function(msg){
			if ( typeof(callbackFunction) == "function" )
			{
				callbackFunction(msg);
			}
		},
		error: function(){
			if ( typeof(callbackErrorFunction) == "function" )
			{
				callbackErrorFunction();
			}
		}
	});
}

Model.prototype.remove = function(callbackFunction, callbackErrorFunction)
{
	var model = this;
	var dataArray = new Array();
	dataArray.push(this.properties.id);
	$.ajax({
		type: "GET",
		url: model.ajaxRemoveScriptPath,
		cache: false,
		data: {data: JSON.stringify(dataArray)},
		timeout: 8000,
		success: function(msg){
			if ( typeof(callbackFunction) == "function" )
			{
				callbackFunction(msg);
			}
		},
		error: function(){
			if ( typeof(callbackErrorFunction) == "function" )
			{
				callbackErrorFunction();
			}
		}
	});
}

/* Model List object
 * 
 * 
 */

ModelList = function(modelObject)
{
	this.modelObject = modelObject;
	this.models = new Array();
}

ModelList.prototype.loadListByIDs = function(IDs, callbackFunction, callbackErrorFunction)
{
 	var modelList = this;
	$.ajax({
		metod: "POST",
		url: modelList.modelObject.ajaxLoadScriptPath,
		cache: false,
		data: {data: JSON.stringify(IDs) },
		timeout: 8000,
		success: function(xml){
			modelList.loadModelsFromXML(xml);
			if ( typeof(callbackFunction) == "function" )
			{
				callbackFunction();
			}
		},
		error: function(){
			if ( typeof(callbackErrorFunction) == "function" )
			{
				callbackErrorFunction();
			}
		}
	});

}

ModelList.prototype.loadModelsFromXML = function(xml)
{
	var modelList = this;
	$(this.modelObject.xmlRootName, xml).each( function()
	{
		var array = new Array();
		$(this).contents().each( function()
		{
			array[$(this).get(0).tagName] = $(this).text();
		});
		modelList.models.push(array);
	});
}

ModelList.prototype.insertModels = function(models, callbackFunction, callbackErrorFunction)
{
	var modelList = this;
	var dataArray = new Array();
	for (var i = 0; i < models.length; ++i) {
		for (name in models[i]) 
		{
			dataArray[i].push({
				name: name,
				value: models[i][name]
			});
		}
	}
	$.ajax({
		type: "POST",
		url: modelList.modelObject.ajaxSaveScriptPath,
		cache: false,
		data: {data: JSON.stringify(dataArray) },
		timeout: 8000,
		success: function(msg){
			if ( typeof(callbackFunction) == "function" )
			{
				callbackFunction(msg);
			}
		},
		error: function(){
			if ( typeof(callbackErrorFunction) == "function" )
			{
				callbackErrorFunction();
			}
		}
	});
			
}

ModelList.prototype.updateModels = function(IDs, properties, callbackFunction, callbackErrorFunction)
{
	var modelList = this;
	var propertiesArray = new Array();
	for ( name in properties)
	{
		propertiesArray.push( {name: name, value: properties[name]} );
	}
	$.ajax({
		type: "POST",
		url: modelList.modelObject.ajaxUpdateScriptPath,
		cache: false,
		data: { ids: JSON.stringify(IDs), properties: JSON.stringify(propertiesArray) },
		timeout: 8000,
		success: function(msg){
			if ( typeof(callbackFunction) == "function" )
			{
				callbackFunction(msg);
			}
		},
		error: function(){
			if ( typeof(callbackErrorFunction) == "function" )
			{
				callbackErrorFunction();
			}
		}
	});
}

ModelList.prototype.deleteModels = function(IDs, callbackFunction, callbackErrorFunction)
{
	var modelList = this;
	
	$.ajax({
		type: "POST",
		url: modelList.modelObject.ajaxRemoveScriptPath,
		cache: false,
		data: { data: JSON.stringify(IDs)},
		timeout: 8000,
		success: function(msg){
			if ( typeof(callbackFunction) == "function" )
			{
				callbackFunction(msg);
			}
		},
		error: function(){
			if ( typeof(callbackErrorFunction) == "function" )
			{
				callbackErrorFunction();
			}
		}
	});
}
