/**
Manipulates folder structures
*/

/**
@param vNode :node: the node that contains our folder structure
*/
com.soulfresh.FolderStructure = function( vNode )
{

	// save properties
	this.folderNode = vNode;

}


/**
@param vStructure :array: (optional) an array of class names that define the url path to the current document.
                                       For example, if the document.location.pathname is "root/sub/subsub" then the array would
                                       be [ "root", "sub", "subsub" ]
@param vPathDepth :number: the depth at which we want to start parsing the path
@param vCurrFolder :node: (optional) the current branch in the folder structure which we are parsing
@param vNodeType :string: (optional) the type of node that defines this structure (ex: "div", "li", "a" ). The default is "div".
*/
com.soulfresh.FolderStructure.prototype.collapseStructure = function( vStructurePath, vPathDepth, vCurrFolder, vNodeType )
{

	// declare variables
	vNodeType = ( vNodeType == null || vNodeType == undefined ) ? "div" : vNodeType;
	vCurrFolder = ( vCurrFolder == null || vCurrFolder == undefined ) ? this.folderNode : vCurrFolder;
	var vStructureDepth = vStructurePath.length;
	var vRootNodes = this.folderNode.childNodes.length;
	
	// loop throught child nodes collapsing them
	for ( var i = 0; i < vRootNodes; i++ )
	{
		// declare variables
		var vCurr = vCurrFolder.childNodes[ i ];
		
		// if this node has child nodes
		if ( vCurr != undefined )
		{
			// if this node is one of the specified nodes to fold
			if ( vCurr.nodeName.toLowerCase() == vNodeType )
			{
				// if this is not the first item in the folder path
				if ( vCurr.className != vStructurePath[ vPathDepth ] )
					vCurr.className += " collapse";
				else 
				{
					// specify that this folder is open
					vCurr.className += " open";
					
					// recursively collapse folders
					this.collapseStructure( vStructurePath, ++vPathDepth, vCurr, vNodeType );
				}
			}
		}
	}

}

