var currentPrefItem = null;
var currentPrefItemDoc = null;
var docList = {};
var CurrentTreeSelectedItemInfo={"doc":null,"type":null,"path":null,"cell":null,"remote":false};
const img_src_folder_open = "chrome://transerver/skin/images/tree_folder_open.png";
const img_src_folder_close = "chrome://transerver/skin/images/tree_folder_close.png";
const img_src_file = "chrome://transerver/skin/images/tree_file.png";
const img_src_file_modify = "chrome://transerver/skin/images/tree_file_modify.png";

function EnumPrefFolderToTree(dirobj, children)
{
    var rootDir = dirobj;
    if(rootDir && rootDir.exists())
    {
       var subs = rootDir.directoryEntries;
	   var i = 0;
	   var files = [];
	   var folders = [];
       while(subs.hasMoreElements()) {
		var afile = subs.getNext();
		afile.QueryInterface(Components.interfaces.nsIFile);
		if(afile.isFile()) {
            files.push(afile);
        }
        else if(afile.isDirectory())
        {			
            folders.push(afile);
        }
       }	
	   for(i=0;i<files.length;++i)
	   {
			var item = HtmHelper.make("treeitem");
			item = children.appendChild(item);
			var row = item.appendChild( HtmHelper.make("treerow"));
			var title = getFileTitle(files[i].leafName);
			var cell = row.appendChild( HtmHelper.make("treecell", {"label":title,"value":files[i].path,"mtype":"file","src":img_src_file}));
	   }
	   for(i=0;i<folders.length;++i)
	   {		
			var item = HtmHelper.make("treeitem",{ container:"true", open:"false"});
			item = children.appendChild(item);
			var row = item.appendChild( HtmHelper.make("treerow"));
			var cell = row.appendChild( HtmHelper.make("treecell", {"label":folders[i].leafName,"value":folders[i].path,"mtype":"folder","src":img_src_folder_close}));
			var child = item.appendChild( HtmHelper.make("treechildren"));
			EnumPrefFolderToTree(folders[i],child);
	   }
    }
}
var fkid=0;
function EnumPrefFolderToTreeRemote(node, children)
{
	if(node)
	{
		var files = [];
		var folders = [];		
		var cnode = DomHelper.findChildNode(node, "file");
		while(cnode)
		{
			files.push(DomHelper.getNodeValue(cnode));
			cnode = DomHelper.findNode(DomHelper.getNextNode(cnode), "file");
		}
		cnode = DomHelper.findChildNode(node, "dir");
		while(cnode)
		{
			var value =  DomHelper.findChildNode(cnode, "value");			
			folders.push({"path":DomHelper.getNodeValue(value), "node":cnode});
			cnode = DomHelper.findNode(DomHelper.getNextNode(cnode), "dir");
		}
	   for(var i=0;i<files.length;++i)
	   {
			var item = HtmHelper.make("treeitem");
			item = children.appendChild(item);
			var row = item.appendChild( HtmHelper.make("treerow"));
			var title = getFileTitle(files[i]);
			//alert(files[i]);
			var cell = row.appendChild( HtmHelper.make("treecell", {"remote":true,"label":title,"value":files[i],"mtype":"file","src":img_src_file}));
	   }
	   for(var i=0;i<folders.length;++i)
	   {
		var dir = folders[i].path;
		var index =  dir.lastIndexOf('\\') == -1 ? dir.lastIndexOf('/') : dir.lastIndexOf('\\');
		dir = dir.substr(index+1, dir.length -1);
		//alert(dir);
		var item = HtmHelper.make("treeitem",{ container:"true", open:"false"});
		item = children.appendChild(item);
		var row = item.appendChild( HtmHelper.make("treerow"));
		var cell = row.appendChild( HtmHelper.make("treecell", {"remote":true,"label":dir,"value":folders[i].path,"mtype":"folder","src":img_src_folder_close}));
		var child = item.appendChild( HtmHelper.make("treechildren"));
		EnumPrefFolderToTreeRemote(folders[i].node,child);
	   }
	}
}

function InitPrefTreeList()
{
	docList = {};
	//OnPrefTreeBlur();	
	//return;
	var children = $e("lpf_tree_children");
	//if(!children.hasChildNodes())
	//{
	    //while(children.firstChild)children.removeChild(children.firstChild);	
	    //default ppref
	    /*var mcpath = getMcexPrefPath();
	    //alert(mcpath);
	    if(mcpath){
		    var item = HtmHelper.make("treeitem");
		    item = children.appendChild(item);
		    var row = item.appendChild( HtmHelper.make("treerow"));
		    var title = getFileTitle(mcpath);
		    var cell = row.appendChild( HtmHelper.make("treecell", {"label":title,"value":mcpath,"mtype":"file","src":img_src_file}));
	    }*/
	    var path = getAppdataDir();
	    if(!path)return;
	    var dirpath = Io.getFileFrom(path);
	    dirpath.append(LOCAL_PREF_DIR);
	    if(dirpath && dirpath.exists()) EnumPrefFolderToTree(dirpath,children);
	    $e("LocalPreserFileTree").view.selection.select(1);
	//}
	if(!IsAsSingle)
	{	   
	    InitRemoteFileList();
	}
	
	var dir = getAppdataDir();
	var iodir = Io.getFileFrom(dir);
	iodir.append(LOCAL_PREF_DIR);
	 $e("lpf_tree_cell").setAttribute("value", iodir.path);
	 iodir = iodir.parent;
	 iodir.append(REMOTE_PREF_DIR);
	 $e("rpf_tree_cell").setAttribute("value", iodir.path);
	//fix ME
	//alert(CurrentTreeSelectedItemInfo.cell);
}

function InitRemoteFileList()
{
	var doc = GetResponseFromServer2("file", "type=0", true);
	var children = $e("rpf_tree_children");
	while(children.firstChild)children.removeChild(children.firstChild);		
	EnumPrefFolderToTreeRemote(DomHelper.getChildNode(doc),children);
}

function OnPrefTreeSelect(tree)
{	
	var curidx = tree.currentIndex;
	if(curidx < 0)return;
	// alert(tree.view.selection.getRangeCount());
	var item = tree.view.getItemAtIndex(curidx);
	var cell = item.getElementsByTagName("treecell");
	var path = cell[0].getAttribute("value"); 
	var type = cell[0].getAttribute("mtype");
	var isremote = cell[0].getAttribute("remote");
	CurrentTreeSelectedItemInfo.type = type;
	CurrentTreeSelectedItemInfo.path = path;
	CurrentTreeSelectedItemInfo.cell = cell[0];
	//alert(path);
	CurrentTreeSelectedItemInfo.remote = isremote;
	currentPrefItemDoc = CurrentTreeSelectedItemInfo.doc = null;
	
	if(type != "file" && type != "mcfile")
	{
		DisableWholePrefPanel();
		return;
	}
	else if(type == "mcfile")
	{	   
	    if(!docList[path])
	    {
			var xmlhttp = new XMLHttpRequest();
			xmlhttp.open("GET", "http://127.0.0.1:"+mcport+"/prefs/prefs.xml?brief&modified", false);
			xmlhttp.send();
			docList[path] = {"doc":xmlhttp.responseXML};
	    }
	    currentPrefItemDoc = CurrentTreeSelectedItemInfo.doc = docList[path].doc;
	}
	
	if(isremote)
	{
		currentPrefItemDoc = CurrentTreeSelectedItemInfo.doc = GetResponseFromServer2("file", "type=1&path=" + EscapeDiy(path), true);
		//alert(DomHelper.GetXmlContent(currentPrefItemDoc));
		if(!docList[path])
		{
			docList[path] = {"doc":currentPrefItemDoc};
		}
	}
	else
	{
		if(!docList[path])
		{
			docList[path] = {"doc":DomHelper.LoadLocalXMLFile(path)};
		}
		currentPrefItemDoc = CurrentTreeSelectedItemInfo.doc = docList[path].doc;
	}	
    if(currentPrefItemDoc)
    {
        //update the mccoreDoc
        UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
	UpdateSettingsAll(currentPrefItemDoc, mccoreDoc);
        //update setting boxes
        isSymbol = false;
        updateBasicSetting(currentPrefItemDoc);
        isSymbol = true;
       
    }   
}

function OnPrefTreeBlur()
{
	CurrentTreeSelectedItemInfo={"doc":null,"type":null,"path":null,"cell":null,"remote":null};
}

function setModifySymbol()
{	
  if(CurrentTreeSelectedItemInfo.cell && isSymbol) {
	CurrentTreeSelectedItemInfo.cell.setAttribute("src", img_src_file_modify);
  }
}

function removeModifySymbol()
{
  if(CurrentTreeSelectedItemInfo.cell && isSymbol) {
	CurrentTreeSelectedItemInfo.cell.setAttribute("src", img_src_file);
  }
}


function addNewPref()
{
    var docXml = createDefaultXml();
    var fileName;	
    if(CurrentTreeSelectedItemInfo.remote)
    {		
	var name = prompt(getLocalString('prefman.addnew.text'), 'Confirm', "default.xml");
	if(name)
	{
	    var _dir = CurrentTreeSelectedItemInfo.path;
	    //alert(_dir);
	    if(!_dir || _dir == null)
	    {
		    _dir = getAppdataDir();
		    _dir += "\\prefs";
	    }
	    else
	    {
		    if(CurrentTreeSelectedItemInfo.type == "file")
			    _dir = getFileDir(_dir);				
	    }
	    var xParam = new xmlrpcval({"file.name":new xmlrpcval( name, "string"),
							       "file.dir":new xmlrpcval( _dir, "string"),
							    "file.option":new xmlrpcval( "add", "string"),
							    "file.buffer":new xmlrpcval(DomHelper.GetXmlContent(docXml), "string")}, "struct");
	    var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
	    var payload = request.serialize("utf-8");
	    try{
		var resXml = SendRequstToServer(payload);
		var resData = parseRPC(resXml);
		var filename = resData["path"];
		if(filename)
		{
		    var treeChildren = null;
		    if(CurrentTreeSelectedItemInfo.type == "folder")
		    {
			treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode;
			treeChildren.setAttribute("open","true");
			treeChildren = treeChildren.getElementsByTagName("treechildren")[0];
		    }
		    else if(CurrentTreeSelectedItemInfo.type == "file")
		    {			
			treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
		    }
		    else if(CurrentTreeSelectedItemInfo.type == "root")
		    {
			treeChildren = $e("lpf_tree_children");
		    }
		     else if(CurrentTreeSelectedItemInfo.type == "remote_root")
		    {
			treeChildren = $e("rpf_tree_children");
		    }
		    //alert(filename);
		    if(treeChildren)
		    {
			var treeitem = HtmHelper.make("treeitem",{},HtmHelper.make("treerow",{}, HtmHelper.make("treecell",{"isremote":true,"label":getFileTitle(filename),"value":filename,"mtype":"file","src":img_src_file})));
			treeitem = treeChildren.appendChild(treeitem);
			var tree=$e("LocalPreserFileTree");
			var itemIndex = tree.view.getIndexOfItem(treeitem);
			tree.view.selection.select(itemIndex);
			docList[filename] = {'doc' : docXml};
		    }
		}
	    }
	    catch(e){}
	}
	return;
    }  
    var treeChildren = $e("lpf_tree_children");
    var dir = getAppdataDir();
    dir = Io.getFileFrom(dir);
    dir.append(LOCAL_PREF_DIR);
    dir = dir.path;
    if(CurrentTreeSelectedItemInfo.cell)
    {
	if(CurrentTreeSelectedItemInfo.type == "folder")
	{
		dir = CurrentTreeSelectedItemInfo.path;
		treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode;
		treeChildren.setAttribute("open","true");
		treeChildren = treeChildren.getElementsByTagName("treechildren")[0];
	}
	else if(CurrentTreeSelectedItemInfo.type == "file")
	{			
		dir = getFileDir(CurrentTreeSelectedItemInfo.path);
		treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
	}
	else if(CurrentTreeSelectedItemInfo.type == "root")
	{
	    dir = CurrentTreeSelectedItemInfo.path;
	    treeChildren = $e("lpf_tree_children");
	}
	 else if(CurrentTreeSelectedItemInfo.type == "remote_root")
	{
	    dir = CurrentTreeSelectedItemInfo.path;
	    treeChildren = $e("rpf_tree_children");
	}
    }
    else
	    return;
    fileName = dir + "\\default.xml";
    
    var mfile = Io.createUnique(fileName);
    if(!mfile)
    {
        Io.create(fileName);
    }
    else
        fileName = mfile.path;
	//alert(fileName);
    
    Io.saveUTF8File(DomHelper.GetXmlContent(docXml), mfile, true);
    var treeitem = HtmHelper.make("treeitem",{},HtmHelper.make("treerow",{}, HtmHelper.make("treecell",{"label":getFileTitle(fileName),"value":fileName,"mtype":"file","src":img_src_file})));
    treeitem = treeChildren.appendChild(treeitem);
    var tree=$e("LocalPreserFileTree");
    var itemIndex = tree.view.getIndexOfItem(treeitem);
    tree.view.selection.select(itemIndex);
    docList[fileName] = {'doc' : docXml};
}

function addPrefFolder()
{
    if(CurrentTreeSelectedItemInfo.cell)
    {
	var treeChildren;
	if(CurrentTreeSelectedItemInfo.type == "folder")
	{
	    dir = CurrentTreeSelectedItemInfo.path;
	    treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode;
	    treeChildren.setAttribute("open","true");
	    treeChildren = treeChildren.getElementsByTagName("treechildren")[0];
	}
	else if(CurrentTreeSelectedItemInfo.type == "file")
	{			
	    dir = getFileDir(CurrentTreeSelectedItemInfo.path);
	    treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
	}
	else if(CurrentTreeSelectedItemInfo.type == "root")
	{
	    dir = CurrentTreeSelectedItemInfo.path;
	    treeChildren = $e("lpf_tree_children");
	}
	 else if(CurrentTreeSelectedItemInfo.type == "remote_root")
	{
	    dir = CurrentTreeSelectedItemInfo.path;
	    treeChildren = $e("rpf_tree_children");
	}
	
	var name = prompt(getLocalString('prefman.addnew.dir'), 'Confirm', "New Folder");
	if(!name)return;
	var type = CurrentTreeSelectedItemInfo.type;
	var iodir = Io.getFileFrom(dir);
	iodir.append(name);
	dir = iodir;	
	if(CurrentTreeSelectedItemInfo.remote)
	{
	    var xParam = new xmlrpcval({"file.path":new xmlrpcval( dir.path, "string"),
							"file.option":new xmlrpcval( "makeDir", "string")}, "struct");
	    var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
	    var payload = request.serialize("utf-8");
	    try{
		var resXml = SendRequstToServer(payload);
	    }
	    catch(e){}
	}
	else
	{
	    Io.createDir(dir.path);
	}
	var item = HtmHelper.make("treeitem",{ container:"true", open:"false"});
	item = treeChildren.appendChild(item);
	var row = item.appendChild( HtmHelper.make("treerow"));
	var cell = row.appendChild( HtmHelper.make("treecell", {"label":name,"value":dir.path,"mtype":"folder","src":img_src_folder_close}));
	var child = item.appendChild( HtmHelper.make("treechildren"));
	
	var tree=$e("LocalPreserFileTree");
	var itemIndex = tree.view.getIndexOfItem(item);
	tree.view.selection.select(itemIndex);
    }
}

function removePref()
{
	if(!ask(getLocalString("prefman.remove.ask"),"Confirm"))return;
    if(CurrentTreeSelectedItemInfo.cell)
    {
		var type = CurrentTreeSelectedItemInfo.type;
		if(CurrentTreeSelectedItemInfo.remote)
		{
			if(type == "folder")
			{
				var xParam = new xmlrpcval({"file.path":new xmlrpcval( CurrentTreeSelectedItemInfo.path, "string"),
							"file.option":new xmlrpcval( "deleteDir", "string")}, "struct");
				var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
				var payload = request.serialize("utf-8");
				try{
					var resXml = SendRequstToServer(payload);
					var treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
					treeChildren.removeChild(CurrentTreeSelectedItemInfo.cell.parentNode.parentNode);
				}
				catch(e){}
			}
			else if(type == "file")
			{
				var xParam = new xmlrpcval({"file.path":new xmlrpcval( CurrentTreeSelectedItemInfo.path, "string"),
							"file.option":new xmlrpcval( "delete", "string")}, "struct");
				var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
				var payload = request.serialize("utf-8");
				try{
					var resXml = SendRequstToServer(payload);
					docList[CurrentTreeSelectedItemInfo.path] = null;
					DomHelper.removeChildren($e("adv_pref"));
					var treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
					treeChildren.removeChild(CurrentTreeSelectedItemInfo.cell.parentNode.parentNode);
					OnPrefTreeBlur();
				}
				catch(e){}
			}
			return;
		}
		
		if(type == "folder")
		{			
			var treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
			var treeitem = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode;
			var treecells = treeitem.getElementsByTagName("treecell");
			for(var i=0;i<treecells.length;++i)
			{
				var mtype = treecells[i].getAttribute("mtype");
				var path =  treecells[i].getAttribute("value");
				//alert(mtype + ":" + path );
				if(mtype == "file")
				{
					Io.deleteFile(path);
					docList[path] = null;
				}
			}
			Io.deleteFile(CurrentTreeSelectedItemInfo.path);
			treeChildren.removeChild(treeitem);
			OnPrefTreeBlur();
		}
		else if(type == "file")
		{
			Io.deleteFile(CurrentTreeSelectedItemInfo.path);
			docList[CurrentTreeSelectedItemInfo.path] = null;
			DomHelper.removeChildren($e("adv_pref"));
			var treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
			treeChildren.removeChild(CurrentTreeSelectedItemInfo.cell.parentNode.parentNode);
			OnPrefTreeBlur();
		}
		   
    }
}

function resetListDocObject(oldname, newname)
{
	var obj = docList[oldname];
	if(obj)
	{
		 docList[newname] = obj;
		 docList[oldname] = null;
	}
}

function renamePref()
{
	if(CurrentTreeSelectedItemInfo.cell)
    {
		var type = CurrentTreeSelectedItemInfo.type;
		
		if(CurrentTreeSelectedItemInfo.remote)
		{
			if(type == "folder")
			{
				var newTitle = prompt(getLocalString("prefMan.renameBox.title"), 'Confirm', CurrentTreeSelectedItemInfo.cell.getAttribute('label'));
				if(!newTitle)return;
				else
				{
					var oFile = Io.getFileFrom(CurrentTreeSelectedItemInfo.path);
					var newPath = oFile.append(newTitle).path;					 
					var xParam = new xmlrpcval({"file.path":new xmlrpcval( CurrentTreeSelectedItemInfo.path, "string"),
											   "file.newpath":new xmlrpcval(newPath, "string"),
												"file.option":new xmlrpcval( "move", "string")}, "struct");
					var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
					var payload = request.serialize("utf-8");
					try{
						var resXml = SendRequstToServer(payload);
						InitRemoteFileList();
					}
					catch(e){}
				}
			}
			else if(type == "file")
			{
				var newTitle = prompt(getLocalString("prefMan.renameBox.title"), 'Confirm', getFileTitle(CurrentTreeSelectedItemInfo.path));
				if(!newTitle)return;
				resetListDocObject(CurrentTreeSelectedItemInfo.path, name);
				if(newTitle)
				{
					var newPath =getFileDir(CurrentTreeSelectedItemInfo.path) + "\\" + newTitle+".xml";			
					var xParam = new xmlrpcval({"file.path":new xmlrpcval( CurrentTreeSelectedItemInfo.path, "string"),
											   "file.newpath":new xmlrpcval(newPath, "string"),
												"file.option":new xmlrpcval( "move", "string")}, "struct");
					var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
					var payload = request.serialize("utf-8");
					try{
						var resXml = SendRequstToServer(payload);
						CurrentTreeSelectedItemInfo.cell.setAttribute("value", newPath);
						CurrentTreeSelectedItemInfo.cell.setAttribute("label", newTitle);
						CurrentTreeSelectedItemInfo.path = newPath;
					}
					catch(e){}
				}
			}	
			return;
		}
		
		if(type == "folder")
		{
			var newTitle = prompt(getLocalString("prefMan.renameBox.title"), 'Confirm', CurrentTreeSelectedItemInfo.cell.getAttribute('label'));
			if(!newTitle)return;
			var oFile = Io.getFileFrom(CurrentTreeSelectedItemInfo.path);
			oFile.moveTo(oFile.parent, newTitle);
			CurrentTreeSelectedItemInfo.path = oFile.path;
			CurrentTreeSelectedItemInfo.cell.setAttribute("label", newTitle);
			CurrentTreeSelectedItemInfo.cell.setAttribute("value", oFile.path);
			CurrentTreeSelectedItemInfo.path = oFile.path;
			var _children = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode;
			_children = _children.getElementsByTagName("treechildren")[0];
			while(_children.firstChild)_children.removeChild(_children.firstChild);
			EnumPrefFolderToTree(oFile,_children);
		}
		else if(type == "file")
		{
			var title = getFileTitle(CurrentTreeSelectedItemInfo.path);
			var newTitle = prompt(getLocalString("prefMan.renameBox.title"), 'Confirm', title);
			if(!newTitle)return;
			var oldPath = CurrentTreeSelectedItemInfo.path;
			if(newTitle.length)
			{
				var newPath = getFileDir(oldPath) + "\\" + newTitle + ".xml";
				CurrentTreeSelectedItemInfo.cell.setAttribute("value", newPath);
				CurrentTreeSelectedItemInfo.cell.setAttribute("label", newTitle);
				CurrentTreeSelectedItemInfo.path = newPath;
				var oFile = Io.getFileFrom(oldPath);
				oFile.moveTo(oFile.parent, newTitle+ ".xml");				
				resetListDocObject(oldPath, newPath);
			}			
		}
	}
}

function saveCurPref()
{
  if(CurrentTreeSelectedItemInfo.cell && CurrentTreeSelectedItemInfo.type == "file")
  {
	if(CurrentTreeSelectedItemInfo.remote)
	{
		var docXml = DomHelper.GetXmlContent(currentPrefItemDoc);
		var xParam = new xmlrpcval({"file.path":new xmlrpcval( CurrentTreeSelectedItemInfo.path, "string"),
									"file.option":new xmlrpcval( "save", "string"),
									"file.buffer":new xmlrpcval(docXml, "string")}, "struct");
		var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
		var payload = request.serialize("utf-8");
		try{
			var resXml = SendRequstToServer(payload);		
		}
		catch(e){}
	}
	else
		Io.saveUTF8File(DomHelper.GetXmlContent(CurrentTreeSelectedItemInfo.doc), CurrentTreeSelectedItemInfo.path, true);

	removeModifySymbol();
  }
}

function importPref()
{
	var fileAtrr = Io.loadFile("Import Pref", ["Pref File(*.xml)|*.xml"], true);
	if(!fileAtrr || !fileAtrr[0])
        return;
	
	var treeChildren = $e("lpf_tree_children");
	var dir = getAppdataDir();
	dir = Io.getFileFrom(dir);
	dir.append(LOCAL_PREF_DIR);
	dir = dir.path;
	if(CurrentTreeSelectedItemInfo.cell)
	{
	    if(CurrentTreeSelectedItemInfo.type == "folder")
	    {
		    dir = CurrentTreeSelectedItemInfo.path;
		    treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode;
		    treeChildren.setAttribute("open","true");
		    treeChildren = treeChildren.getElementsByTagName("treechildren")[0];
	    }
	    else if(CurrentTreeSelectedItemInfo.type == "file")
	    {			
		    //dir = getFileDir(CurrentTreeSelectedItemInfo.path);
		    //In case mcex default file	 		    
		    treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
		    
		    var cell = treeChildren.parentNode.getElementsByTagName("treecell");
		    var path = cell[0].getAttribute("value");
		    dir = path;
	    }
	    else if(CurrentTreeSelectedItemInfo.type == "root")
	    {
		dir = CurrentTreeSelectedItemInfo.path;
		treeChildren = $e("lpf_tree_children");
	    }
	     else if(CurrentTreeSelectedItemInfo.type == "remote_root")
	    {
		dir = CurrentTreeSelectedItemInfo.path;
		treeChildren = $e("rpf_tree_children");
	    }
	}
	
	for(var i=0; fileAtrr[i]; i++)
	{
		var fileName;
		var docXml;
		if(CurrentTreeSelectedItemInfo.remote)
		{
			docXml = DomHelper.LoadLocalXMLFile(fileAtrr[i].path);
			var xParam = new xmlrpcval({"file.name":new xmlrpcval( fileAtrr[i].leafName, "string"),
									"file.option":new xmlrpcval( "add", "string"),
									"file.dir":new xmlrpcval( dir, "string"),
									"file.buffer":new xmlrpcval(DomHelper.GetXmlContent(docXml), "string")}, "struct");
			var request = new xmlrpcmsg("ts.file.remoteopt", [xParam]);
			var payload = request.serialize("utf-8");
			try{
				var resXml = SendRequstToServer(payload);
				var resData = parseRPC(resXml);
				fileName = resData["path"];
				//alert(fileName);
				if(fileName)
				{					
					docList[fileName] = {'doc' : docXml};
				}
			}
			catch(e){
				continue
			}			
		}
		else {
			if(dir) {
				fileName = dir + "\\"+ fileAtrr[i].leafName;
			}	   
			var mfile = Io.createUnique(fileName);
			if(!mfile)
			{
				Io.create(fileName);
			}
			else
				fileName = mfile.path;
				
			docXml = DomHelper.LoadLocalXMLFile(fileAtrr[i].path);
			Io.saveUTF8File(DomHelper.GetXmlContent(docXml), mfile, true);
			docList[fileName] = {'doc': docXml};
		}
	
		var treeitem = HtmHelper.make("treeitem",{},HtmHelper.make("treerow",{},
					HtmHelper.make("treecell",{"label":getFileTitle(fileName),"value":fileName,"mtype":"file","src":img_src_file})));
		treeitem = treeChildren.appendChild(treeitem);		
	}
}

function exportCurPref()
{
	if(!CurrentTreeSelectedItemInfo.cell || !CurrentTreeSelectedItemInfo.doc)
	{
		alert(getLocalString("overall.alert.nofile"));
		return;
	}
	
	var ofile = Io.saveAsFile("Save Pref", ["Pref File|*.xml"], getFileTitle(CurrentTreeSelectedItemInfo.path) + ".xml");

	if(Io.saveUTF8File(DomHelper.GetXmlContent(CurrentTreeSelectedItemInfo.doc), ofile, true))
	{
		alert(getLocalString("pref.export.ok"));
	}
	else
	{
		alert(getLocalString("pref.export.error"));
	}
}

var autoHttp = new XMLHttpRequest();

function GuideAutoParse()
{
    window.openDialog("autoparse/win.xul","Auto Parse Guider",
    'chrome, titlebar, centerscreen,modal=no,resizable=no,depended=yes,alwaysRaised=yes',
    {"plugin":$e('extention_plugin')});
}

function TestAutoParse()
{   
   var file = Io.loadFile("choose File",['*.*|*.*'],false);
   
    var res =  $e('extention_plugin').getFileMediaInfoXml(file[0].path);
   
  // var res = GetResponseFromServer("type=51&path="+ EscapeDiy(file[0].path));
   if(!res)
   {
     alert('file parse error');
     return null;
   }
   var payload = res;
   payload = payload.replace(/\\/g, "/");
   //alert(payload);
   try {
	var fileListOnServer = "http://www.broadintel.com/intelliparse/analyseinfo.php";			
	var sth = $e('extention_plugin').post(fileListOnServer, payload);
	//alert("rrrr"+sth);
	if(!sth)return 0;
	else
	{
	     var mdoc = new XMLDom();
	     mdoc.loadXML(sth);
	     var docXml = mdoc;
	     //alert(sth);
	     var treeChildren = $e("lpf_tree_children");
	    var dir = getAppdataDir();
	    dir = Io.getFileFrom(dir);
	    dir.append(LOCAL_PREF_DIR);
	    dir = dir.path;
	    if(CurrentTreeSelectedItemInfo.cell)
	    {
		if(CurrentTreeSelectedItemInfo.type == "folder")
		{
			dir = CurrentTreeSelectedItemInfo.path;
			treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode;
			treeChildren.setAttribute("open","true");
			treeChildren = treeChildren.getElementsByTagName("treechildren")[0];
		}
		else if(CurrentTreeSelectedItemInfo.type == "file")
		{			
			dir = getFileDir(CurrentTreeSelectedItemInfo.path);
			treeChildren = CurrentTreeSelectedItemInfo.cell.parentNode.parentNode.parentNode;
		}
		else if(CurrentTreeSelectedItemInfo.type == "root")
		{
		    dir = CurrentTreeSelectedItemInfo.path;
		    treeChildren = $e("lpf_tree_children");
		}
		 else if(CurrentTreeSelectedItemInfo.type == "remote_root")
		{
		    dir = CurrentTreeSelectedItemInfo.path;
		    treeChildren = $e("rpf_tree_children");
		}
	    }
	    else
		treeChildren = $e("lpf_tree_children");
	   // alert("dd");
	    
	    fileName = dir + "\\auto_parse.xml";
	    
	    var mfile = Io.createUnique(fileName);
	    if(!mfile)
	    {
		Io.create(fileName);
	    }
	    else
		fileName = mfile.path;
		//alert(fileName);
	    
	   // alert(DomHelper.GetXmlContent(docXml));
	    Io.saveUTF8File(DomHelper.GetXmlContent(docXml), mfile, true);
	    var treeitem = HtmHelper.make("treeitem",{},HtmHelper.make("treerow",{}, HtmHelper.make("treecell",{"label":getFileTitle(fileName),"value":fileName,"mtype":"file","src":img_src_file})));
	    //alert(treeChildren);
	    treeitem = treeChildren.appendChild(treeitem);
	    var tree=$e("LocalPreserFileTree");
	    var itemIndex = tree.view.getIndexOfItem(treeitem);
	    tree.view.selection.select(itemIndex);
	    docList[fileName] = {'doc' : docXml};
	}
	return null;		
    } catch (e) {

	return null;
    }
}

function ApplyPrefSet()
{
    if(!currentPrefItemDoc)return;
    /*alert(currentPrefItemDoc);
    return;*/
    var payload=DomHelper.GetXmlContent(currentPrefItemDoc);
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", "http://127.0.0.1:"+mcport+"/prefs/import", false);
	xmlhttp.setRequestHeader("Content-Type", "text/xml")
	xmlhttp.send(payload);
}