/* MapFunction is used to access attributes of the functions.xml */
function MapFunction(layerName) {
	this.layerName = layerName;
	this.xmlDoc = null;
	this.init = init;
	this.getLayerName = getLayerName;
	this.getXmlDoc = getXmlDoc;
	this.setXmlDoc = setXmlDoc;
	this.findInDinoIdentify = findInDinoIdentify;
	this.findInDinoExtract = findInDinoExtract;
	this.findAttribute = findAttribute;
	
	/* init */
	this.init();
	
	function init() {
		setXmlDoc();
	}
    
	function getLayerName() {
		return this.layerName;
	}
	
	function getXmlDoc() {
		return this.xmlDoc;
	}
	
	function setXmlDoc() {
		this.xmlDoc = getXMLDOM();
		this.xmlDoc.async = false;
		this.xmlDoc.load("functions/functions.xml");
	}
	
	/*Finds an attribute in the dinoIdentify tag of the xml*/
	function findInDinoIdentify(attribute) {
		return findAttribute('dinoIdentify',attribute);
	}
	
	/*Finds an attribute in the dinoExtract tag of the xml*/
	function findInDinoExtract(attribute) {
		return findAttribute('dinoExtract',attribute);
	}

	/*Finds an attribute for a certain function in the xml*/
	function findAttribute(dinoFunction, attribute) {
		var value = null;
		var regexS = /M\d\dM\d\d\d\d/;
		var regxDino = new RegExp(regexS);		
		var results = regxDino.exec(getLayerName());
		
		var dinoNr = trim("" + results[0]);
		var dinoLayerName = trim("" + getLayerName().replace(dinoNr, ""));
			
		var layerFunctions = getXmlDoc().getElementsByTagName("layerfunction");
		for ( var i = 0; i < layerFunctions.length; i++) {
			if(layerFunctions[i]) {
				var xDinoNr = "" + layerFunctions[i].getAttribute("dinoNr");
				var xLayerName = "" + layerFunctions[i].getAttribute("layername");
				regxDino = new RegExp(xDinoNr);
				var regxLayer = new RegExp(xLayerName);
				if(dinoNr.match(regxDino) && dinoLayerName.match(regxLayer)) {
					var identifiers = layerFunctions[i].getElementsByTagName(dinoFunction);
					if(identifiers[0]) {
						value = identifiers[0].getAttribute(attribute);
					}
					break;
				}				
			}
		}
		return value;
	}
}

