var Data = (function(initConfig)
{
	if(!initConfig) initConfig = {};

    var self = {};
	self.name = initConfig['name'] || '';
	self.count = 0;
	self.records = [];
	self.fields = initConfig['fields'] || [];
	
	self.Record = function()
	{
		this.internalId = self.count + 1;
		this.data = new Object();
		this.phantom = false;
		this.isPhantom = function(bool) {
			if(typeof bool == "undefined") {
				return this.phantom;
			} else {
				this.phantom = bool;
				return this.phantom;
			}
		}
	}
	
	self.copy = function(obj)
	{
		var copy = {};
		
		for(var key in obj)
		{
			copy[key] = obj[key];
		}
		
		return copy;
	}
	
	self.merge = function()
	{
		var copy = {};
		
		for(var obj in arguments)
		{
			for(var key in arguments[obj])
			{
				copy[key] = arguments[obj][key];
			}
		}
		
		return copy;
	}
	
	self.add = function(obj, options)
	{
		if(!options) { options = {}; }
		if(!obj.length) { obj = [obj]; }
	
		for(var i in obj)
		{
			var record = new self.Record();
			
			record = self.merge(record, options);
			record.data = obj[i];
			
			self.count++;
			self.records.push(record);
			self.fireEvent('added', record);
		}
			
		return self;
	}
	
	self.each = function(func)
	{
		for(var i=0; i < self.count; i++)
		{
			func(self.records[i], i, self.count);
		}
	}
	
	self.fireEvent = function()
	{
		if(arguments && arguments.length)
		{
			var args = Array.prototype.slice.call(arguments);
			var eventName = args.shift();
			var functionName = 'on' + self.name.toProperCase() + eventName.toProperCase();
			
			var fn = window[functionName];
			if(typeof fn === 'function') {
				fn.apply(this, args);
			}
		}
	}
	
	return self;
});
