/** Class that caches data for better performance. */
var Cache = {

	// Array for config cache
	cfg: [],
	// Array for template cache
	XSL: [],
	// Stores the last columnList string
	columnList: null,
	// Xml of the rows
	rowsXml: '',
	// Xml of the search fields in relational mode
	searchFieldsXml: '',

	/**
	 * Contain functions that refresh the cache.
	 */
	add: {
		/**
		 * Load given parts of the configuration file.
		 * 
		 * @param file    Configuration file.
		 * @param group   Array of first-level configuration tags to process.
		 */
		config: function(file, group) {
			var confXML = XML.loadDocument(file);
			// i: group (first level) element index, j: second level element index, k: attribute index
			for (var i = 0; i < group.length; i++) {
				var node = confXML.getElementsByTagName(group[i]).item(0);
				if (node.childNodes.length == 1 && node.childNodes[0].nodeType == 3) {
					var attributes = [];
					var k = 0;
					while (node.childNodes[0].attributes != null && node.childNodes[0].attributes[k] != null) {
						attributes[node.childNodes[0].attributes[k].name] = node.childNodes[0].attributes[k].value;
						k++; 
					}
					Cache.cfg[node.tagName] = {attrs: attributes, val: node.childNodes[0].nodeValue};
				} else {
					for (var j = 0; j < node.childNodes.length; j++) {
						if (node.childNodes[j].nodeType == 1) {
							if (Cache.cfg[node.tagName] == null) {
								Cache.cfg[node.tagName] = [];
							}
							var attributes = [];
							var k = 0;
							while (node.childNodes[j].attributes != null && node.childNodes[j].attributes[k] != null) {
								attributes[node.childNodes[j].attributes[k].name] = node.childNodes[j].attributes[k].value;
								k++; 
							}
							Cache.cfg[node.tagName][node.childNodes[j].tagName] = (node.childNodes[j].firstChild == null ?
								{attrs: attributes, val: ''} : {attrs: attributes, val: node.childNodes[j].firstChild.nodeValue});
						}
					}
				}
			}
		},

		/**
		 * Cache the given XSL template.
		 *
		 * @param xslFile   Name of the XSL.
		 */
		xsl: function(xslFile) {
	    	if (Cache.XSL[xslFile] == null) {
				Cache.XSL[xslFile] = XML.loadDocument(View.stylesheetDir+'/'+xslFile);
	    	}
		}
	}
};
