var curkey = null;
var curnode = null;
var url = document.location.href;
var xmlhttp = new XMLHttpRequest;

function loadMccoreXML(path)
{
	if(mccoreDoc)
	{
		var doc = new XMLDom();
		doc.async = false;
		doc.preserveWhiteSpace=false;
		var head = doc.createProcessingInstruction('xml','version="1.0" encoding="utf-8"');     	
		var root = doc.createElement("MediaCoderPrefs");
		doc.appendChild(head);
		root = doc.appendChild(root);
		
		//find node
		var keys = path.split(".");
		var node = null;
		var newNode = null;
		var newNodeParent = null;
		var parentNode = mccoreDoc.firstChild; // root
		
		for(var i = 0; i < keys.length; ++i)
		{
			node = DomHelper.getNodeByAttribute(parentNode.firstChild, "node", "key", keys[i]);
			parentNode = node;
			
			node = node.cloneNode(i == keys.length - 1);
			
			var childNode = DomHelper.findChildNode(node, "node");
			while(childNode)
			{
				node.removeChild(childNode);
				childNode = DomHelper.findChildNode(node, "node");
			}
			if(newNode)
			{
				newNodeParent = newNodeParent.appendChild(node);				
			}
			else
				newNodeParent = newNode = node;
			
		}
		root.appendChild(newNode);
		//alert(DomHelper.GetXmlContent(doc));
		return doc;
	}
	return null;
}

function UpdateTree(path)
{
	var xmlFile = loadMccoreXML(path);
	var xslTree = LoadPrefDocByPath("xslt/prefs.xsl");
	transformXML(xmlFile, xslTree, "treeview");
}

function ShowValue(path, update)
{
	var xmlFile = loadMccoreXML(path);
	var xslFile = LoadPrefDocByPath("xslt/value.xsl");	
	var keys = path.split(".");
	if(keys.length == 3)
	 transformXMLId(xmlFile, xslFile, "valuebox");
	else
	{
	   var e = $e("valuebox");
	   while(e.firstChild)
		e.removeChild(e.firstChild);
	}
	xslFile = LoadPrefDocByPath("xslt/desc.xsl");
	transformXMLId(xmlFile, xslFile, "descview");
	curkey = path;

	if(!update)
	{
		return false;
	}
	// update value in the tree
	var val = xmlFile.getElementsByTagName("value");
	if (val.length > 0 && val[0].firstChild) {
		UpdateListValue(val[0].firstChild.nodeValue);
	}
	return true;
}

function UpdateListValue(value)
{
	if (curnode) {
		var rownode = curnode.firstChild.firstChild;
		if ((rownode = rownode.nextSibling) && (rownode = rownode.nextSibling))
			rownode.setAttribute("label", value);		
	}
}

function RevertValue()
{
	if (curkey) {
		var keys = curkey.split(".");
		if(keys.length == 3)
		{
			var value;
			var node = null;		
			var parentNode = mccoreDoc.firstChild; // root		
			for(var i = 0; i < keys.length; ++i)
			{
				node = DomHelper.getNodeByAttribute(parentNode.firstChild, "node", "key", keys[i]);
				parentNode = node;
			}
			if(node)
			{
				value = "";
				var type = node.getAttribute("type");
				var valNode = DomHelper.findChildNode(node, "value");
				var valNodeDef = DomHelper.findChildNode(node, "valuedef");
				if(valNode && !valNodeDef)
				{
					value = DomHelper.getNodeValue(valNode);
				}
				else
				{
					if(valNodeDef && DomHelper.getNodeValue(valNodeDef))
					{
						value = DomHelper.getNodeValue(valNodeDef);
					}
					else{
						switch(type)
						{
							case "int":
							case "float":
								value= "0";
								break;
							case "bool":
								value = "false"
								break;
							case "file":
								{
									var enumNode = DomHelper.findChildNode(node, "enum");
									if(enumNode)
									{
										value = DomHelper.getNodeValue(enumNode);
									}
								}
								break;
							case "enum":
								{
									var enumNode = DomHelper.findChildNode(node, "enum");
									if(enumNode)
									{
										value = DomHelper.getNodeValue(enumNode);
										enumNode = DomHelper.getNodeByAttribute(enumNode, "enum", "sel", "1");
										if(enumNode)
										{
											value = DomHelper.getNodeValue(enumNode);
										}
									}							
								}
								break;
							default:
								break;
						}
					}
				}
				SaveValue(value);
				if(valNodeDef)
				{
					node.removeChild(valNodeDef);					
				}
				var doc = mccoreDoc;
				valNodeDef = DomHelper.addElementNode(doc, node, "valuedef");
				DomHelper.addTextNode(doc, valNodeDef, value);
				if(valNode)
				{
					node.removeChild(valNode);					
				}
				valNode = DomHelper.addElementNode(doc, node, "value");
				DomHelper.addTextNode(doc, valNode, value);
				ShowValue(curkey, true);
			}
		}
	}
}

function SaveValue(value)
{
	if (curkey && value) {
		SetPrefValue(curkey, value);
		UpdateListValue(value);
	}
	var hidden = $e("adv_coder_mode").getAttribute("hidden");
	if(hidden != false)
	{
		UpdateCurrentCoderSetting();
	}
}

function MccoreFormat(path, val)
{
	var keys = path.split(".");
	var node = null;		
	var parentNode = mccoreDoc.firstChild; // root		
	for(var i = 0; i < keys.length; ++i)
	{
		node = DomHelper.getNodeByAttribute(parentNode.firstChild, "node", "key", keys[i]);
		parentNode = node;
	}
	
	if(node)
	{
		if(node.getAttribute("type") == "enum")
		{
			var childNode = DomHelper.findChildNode(node, "enum");
			var index = 0;
			while(childNode)
			{
				if(DomHelper.getNodeValue(childNode) == val)
				{
					return index;
				}
				childNode = DomHelper.findNode(childNode.nextSibling, "enum");
				++index;
			}
		}
	}
	
	return val;
}

function SetPrefValue(param, val)
{
	if(currentPrefItemDoc)
    {
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, param, MccoreFormat(param, val));
		//updateBasicSetting(currentPrefItemDoc);
        setModifySymbol();
    }
}

function onTreeSelected(tree)
{
	//alert("fk");
	var curidx = tree.currentIndex;
	var items = tree.getElementsByTagName("treeitem");
	var i;
	for (i = 0, idx = 0; i < items.length && idx < curidx; i++, idx++) {
		if (items[i].getAttribute("container") == "true") {
			if (items[i].getAttribute("open") != "true") {
				// skip child nodes
				var next = items[i].nextSibling;
				if (!next) next = items[i].parentNode.parentNode.nextSibling;
				if (next) {
					// look for the next right node in items array
					for (i++; i < items.length && items[i] != next; i++);
					i--;
				}
			}
		}
	}
	if (idx == curidx) {
		var key = items[i].getAttribute("uri");
		var node = items[i];
		while ((node = node.parentNode) && (node = node.parentNode) && node.nodeName == "treeitem") {
			key = node.getAttribute("uri") + "." + key;
		}
		curnode = items[i];
		ShowValue(key, false);
	}
}