var XML_PROJECTS 			= "projectsxml.asp";
var projectsXML;

function loadProjectXML(rebuild) {
	ajaxProjects = new sack(XML_PROJECTS);
	ajaxProjects.method = "POST";
	ajaxProjects.onCompletion = function() { 
		projectsXML = ajaxProjects.responseXML;
		if (rebuild) {
			refreshProjectDetails();
		}
	}
	ajaxProjects.runAJAX();
}

function saveAllLayersAjax() {
	ajaxUpdate = new sack("updatelayerprops.asp");
	ajaxUpdate.method = "POST";
	
	ajaxUpdate.onCompletion = function() { 
			//alert("oke");
		//	alert("[" + ajaxUpdate.response + "]");
			//buttonObj = document.getElementById(CSSID + "buttons");
			//buttonObj.style.visibility = "hidden";
	}
	
	ajaxUpdate.setVar("arr", layerArr);
	ajaxUpdate.runAJAX(null);
}

function setProjectCaption(sender) {
//	alert(sender);
	setProjectDetails(false,sender);
}

function clearProjectCaption() {
	var properties		= $("properties");

	properties.innerHTML = "";
}

function clearProjectDesciption() {
	var description		= $("description");

	description.innerHTML = "";
}

function clearProjectDetails() {
	clearProjectCaption();
	clearProjectDesciption();
}

var currentProjectDetailsLayername = null;

function refreshProjectDetails() {
	if (currentProjectDetailsLayername != null) {
		setProjectDetails(true, currentProjectDetailsLayername)
	}
}

function getMetadataProjectIndex(projectID) {
	for (var i = 0;i < projectData.length;i++) {
		if (projectData[i][0] == projectID) {
			return i;
		}
	}
	return null;
}

function setProjectDetails(allProps, layerName) {

	currentProjectDetailsLayername = layerName;
	//var layerName = dd.obj.name;
	var layerID = makeLayerID(layerName);
	var layerIndexInArray = getIndex(LAYER_CSSID,layerName);
	var currentProjectID = layerArr[layerIndexInArray][LAYER_PROJECTID];

	var properties	= $("properties");
	var description = $("description");
	properties.innerHTML = "";
	description.innerHTML = "";
	var index = getMetadataProjectIndex(currentProjectID);
	if (index != null) {
		var propertyArray = projectData[index][1];
		var longText = "";
		if (allProps) {
			if (!adminMode) {
				var eersteProperty = true;
	
				for (var i=0; i < propertyArray.length;i++) {
					if (propertyArray[i][0]) {
						if (propertyArray[i][1] == 11) {
							longText = propertyArray[i][4];
						}
					} else {
						displayProperty(propertyArray[i], properties, eersteProperty);
						eersteProperty = false;
					}
				}
	
				setLongText(description, longText,false);
			}

			
			
		} else {
			displayProperty(propertyArray[0], properties,true);
			/*var firstValue = getXMLNode("//wt/project[@id=" + currentProjectID + "]/property/value", projectsXML);		
			var firstDescr = getXMLNode("//wt/project[@id=" + currentProjectID + "]/property/descr", projectsXML);*/
			/*var propertyID = iterateNode.attributes[2].value;
			
			displayProperty(projectsXML, propertyID, properties,currentProjectID);
			*/
			
		}
	}
}




function getXMLNode(myXpathString, xmldoc) {
	return getXMLArr(myXpathString, xmldoc).iterateNext();
}


function getXMLArr(myXpathString, xmldoc) {
	return xmldoc.evaluate(myXpathString, xmldoc, null, XPathResult.ANY_TYPE,null);
}

function displayProperty(property, layerObj, hideDescr) {//xmldoc, propertyID, layerObj, projectID) {
	var descr 			= property[3];
	var value 			= property[4];
	
	if (value != "" && value != "") {
		var propertyID 		= property[2];
		var propertyTypeID 	= property[1];
		
		var propertyDiv = document.createElement("div");	
		propertyDiv.setAttribute("class", "property");
		propertyDiv.id = "property" + propertyID;
		
		if (!adminMode) {
			switch(propertyTypeID) {
				case 14:
					value = "<a href='" + value + "' target='_blank'>"+value+"</a>";
					hideDescr = true;
					break;
				case 15:
					value = "<a href='mailto:" + value + "'>"+value+"</a>";
					break;				
				case 16:
					hideDescr = true;
					break;									
				default:
					break;
	
			}
			if (!hideDescr) {
				propertyDiv.innerHTML = descr + ":<BR>";			
			}
			
			
			propertyDiv.innerHTML +=  value + "<BR><BR>";
		} 
		layerObj.appendChild(propertyDiv);
	}
}

function setLongText(layerObj, s) {
	layerObj.innerHTML = s;
}


function getIndex(compareIndex, compareValue) {
	for (var i=0;i < layerArr.length;i++) {
		if (
			layerArr[i][compareIndex].toString().toLowerCase()
			== 
			compareValue.toString().toLowerCase()
		) {
			return i;
		}
	}
	return null;
}


