/**
 * @author bbuffone
 * 
 * This is the base class in the swoosh engine.
 */

var Swoosh = function(){
	/*
	 * PRIVATE METHODS AND VARIABLES
	 * 
	 * Developers use the extensions of the swoosh client to access functionality.
	 * This class provides reusable functionality that is needed across modules
	 */

	/**
	 * This is the version of the Swoosh Client
	 */
	this._version='0.1.0';
	
	/**
	 * This is the list of modules that are included in the
	 * Client.  A module is a processor for an element type
	 * in a xml document.
	 * 
	 * <xal>
	 * 		<modifications/>
	 * 		<macros/>
	 * 		<ui/>
	 * 		<widgets/>
	 * 		<styles/>
	 * 		<animations/>
	 * 		<states/>
	 * </xal>
	 * 
	 */	
	this._modules={};
	
	/**
	 * comvert the node name into a lowercase string 
	 * without the prefix
	 * 
	 * @param {Object} name
	 */
	this._getNodeName = function(name){
    	var i = name.indexOf(":");
		if (i != -1) name = name.substring(i + 1);
		return name.toLowerCase();
	};
		
    /**
     * Convert an XML structure into a string 
     */
    this._convertToString = function(node){
	    var elements = "<" + node.nodeName;
	    if (node.attributes){
		    var arry = node.attributes, len = arry.length;
    	    for (var i = 0; i < len; i++)
    	          elements += " " + arry[i].name + "=\"" + 
				  			  arry[i].value + "\"";
    	}
		     	    
	    elements += ">";
		var arry = node.childNodes, len = arry.length;
	    for (var i = 0; i < len; i++)
	    	if (arry[i].nodeType == 1)
	        	elements += this._convertToString(arry[i]);
			else
	            elements += arry[i].nodeValue;
	          
	    elements += "</" + node.nodeName + ">";
	    return elements;
	};
   
	/**
     * Will return the modification element.
     *
     * Assumes the elements are under XAL.  Will need to look up in the 
     * module list to find the module and call the processor.
     * 
	 * <xal>
	 * 		<modifications/>
	 * 		<macros/>
	 * 		<ui/>
	 * 		<states/>
	 * </xal>
     *
	 * @param {Xml Document} doc
     */
	this._processDoc = function(doc){
		//if the supplied doc object is actual a document then use it otherwise
		//assume its the root element
        var arry = doc.documentElement ? doc.documentElement.childNodes : doc.childNodes, 
			len = arry.length;
	    for (var i = 0; i < len; i++)
			if (arry[i].nodeType == 1)
				this._moduleProcessor(this._getNodeName(arry[i].nodeName), arry[i]);           
    };
	
	/**
     * Will look for the module processor of the supplied element
     * and call the processing.
     * 
     * @param {String} name
     * @param {Element} node
     */
	this._moduleProcessor = function(name, node){
	    for (var i in this._modules) 
			if (i === name) 
				this._modules[i].apply(this, new Array(node));
	}
};



