/** 
* @Class
*/
function Class(obj)
{
	var Constructor = function() 
	{
		if (this.initialize)
		{ 
			this.initialize.apply(this, arguments);
		}
	}
	
	Constructor.prototype.initializeWithXML = function(source)
	{
		var self = this;
		
		/*
		$.get(source, function(result){
			var xmlDoc = result;
			
			self.name = xmlDoc.documentElement.nodeName;		
			for (node = xmlDoc.documentElement.firstChild; node != null; node = node.nextSibling)
			{
				if (node.nodeType == 1)
				{
					if( node.getAttribute("type") == 'object' )
					{
						var obj= null;
					}
					else
					{
						
						self[node.nodeName] = "";
					}
				}
			}		
		});
		
		return;
		*/
		
		var xmlDoc = new DocumentFactory().createDocument("","",null);
		xmlDoc.async = false;
		xmlDoc.load(source); // + '?' + new Date().getTime() );

		this.name = xmlDoc.documentElement.nodeName;		
		for (node = xmlDoc.documentElement.firstChild; node != null; node = node.nextSibling)
		{
			if (node.nodeType == 1)
			{
				if( node.getAttribute("type") == 'object' )
				{
					var obj= null;
				}
				else
				{
					
					this[node.nodeName] = "";
				}
			}
		}		
	}
	
	Constructor.prototype.importXML = function(parent)
	{
		//console.log(this.name + ".importXML");
		var node = parent.cloneNode(true);
		
		//console.log(this.name + ".importXML");
		
		for (node = node.firstChild; node != null; node = node.nextSibling)
		{		
			for (var field in this)
			{
				if(typeof(this[field]) == 'object')
				{
					//ToDo
					if(this[field] != null && field != 'handle' && field != 'xmlDoc')
					{
						if (this[field].getAttribute("name") == node.nodeName)
						{
							if (node.nodeName == "MetaPaperOption")
							{
								//console.log(node.nodeName);
							}
							try
							{
								this[field].importXML(node.cloneNode(true));
							}
							catch(e)
							{
								console.log(e);
							}
							break;
						}
					}
				}
				else if (typeof(this[field]) == 'string')
				{
					if (field == node.nodeName)
					{
						if(node.firstChild)
						{
							this[field] = node.firstChild.nodeValue;
							//alert(field + " = " + node.firstChild.nodeValue);
						}
					}
				}
			}			
		}
		
		return true;	
	}
	
	Constructor.prototype.exportXML = function()
	{
			//alert(this.name + ".exportXML");
			var xmlDoc = new DocumentFactory().createDocument("","",null);
			var processingInstruction= xmlDoc.createProcessingInstruction( 'xml', 'version="1.0" encoding="ISO-8859-1"' );
			xmlDoc.appendChild(processingInstruction);
			var documentElement = xmlDoc.createElement(this.name);
			xmlDoc.appendChild(documentElement);
			
			for (field in this)
			{
				if(typeof(this[field]) == "string" && field != "name")
				{
					var node = xmlDoc.createElement(field);
					var value = xmlDoc.createTextNode(this[field]);
					node.appendChild(value);
					documentElement.appendChild(node);

				}
				else if (typeof(this[field]) == "object" && field != 'handle' && field != 'xmlDoc')
				{
					try
					{	 
						var node = this[field].exportXML().documentElement;
						documentElement.appendChild(node);
					}
					catch(e)
					{
						;
					}				
				}
			}
			return xmlDoc;			
	}
	
	Constructor.prototype.setAttribute = function(attr, val)
	{
		this[attr] = val;
	}
	
	Constructor.prototype.getAttribute = function(attr)
	{
		if (this[attr])
		{
			return this[attr];
		}
	}	
	
	Constructor.prototype.attributes = function()
	{
		var attrs = new Array();
		for(field in this)
		{
			if (typeof(this[field]) == "string")
			{
				attrs[field] = this[field];
			}
		}
		return attrs;
	}	
	
	for (field in obj)
	{
		Constructor.prototype[field] = obj[field];
	}
	
	return Constructor;
}

Function.prototype.mixin = function(obj) 
{
	for (field in obj) this.prototype[field] = obj[field];
	return this;
}

Function.prototype.extend = function(obj) 
{
	return Class(new this).mixin(obj);
} 


