var isCurrentPrefRemote = false;
var mccoreDoc = null;
var inputOn = false;
var isSymbol = true;

const PREFVIDEOCODEC = 1;
const PREFAUDIOCODEC = 2;
const PREFVIDEOFILTER = 3;
const PREFAUDIOFILTER = 4;
const PREFSUBTITLE = 5;
const PREFMUXER = 6;

var currentPrefType = PREFVIDEOCODEC;
var prefsDir = "../../prefs/";
var xslPrefsdoc = null;
var currentPrefPath = null;
var currentGobalData ={};

var file_pre_head = "";

function MakeTree()
{

}

function LoadPrefDocByPath(path)
{
	return DomHelper.LoadXMLFile(file_pre_head+path);
}

function quickUpdateSettingPanel(doc)
{
	currentPrefItemDoc = doc;
	//make pref tree
    if(!mccoreDoc){
		//alert(path.path);
        mccoreDoc = LoadPrefDocByPath("xslt/mccore.xml");
    }    
    if(currentPrefItemDoc)
    {
        //update the mccoreDoc
        UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
	UpdateSettingsAll(currentPrefItemDoc, mccoreDoc);
        //update setting boxes
        isSymbol = false;
        updateBasicSetting(currentPrefItemDoc);
        isSymbol = true;
       
    } 
}

function onListSelectRemote()
{
	if(!isCurrentPrefRemote)return;
	var e = $e("prefs_list_remote");
    var index = e.selectedIndex;
    currentPrefItem  = e.selectedItem;
	if(!currentPrefItem)return;
    currentPrefItemDoc = docList["remote:" + currentPrefItem.value]['doc']; 
	currentPrefPath = currentPrefItem.value;
	
	if(!currentPrefItemDoc)
    {
		//alert(currentPrefPath);
		currentPrefItemDoc = docList["remote:" + currentPrefPath]['doc'] = GetResponseFromServer2("file", "type=1&path=" + EscapeDiy(currentPrefPath), true);
        //alert(DomHelper.GetXmlContent(currentPrefItemDoc));
		if(!currentPrefItemDoc) {
            currentPrefItemDoc = docList["remote:" + currentPrefPath]['doc'] = createDefaultXml();
        }
	}
	
	 //make pref tree
    /*if(!mccoreDoc){
        mccoreDoc = LoadPrefDocByPath("xslt/mccore.xml");
    }    */
    if(currentPrefItemDoc)
    {
        //update the mccoreDoc
        UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
	UpdateSettingsAll(currentPrefItemDoc, mccoreDoc);
        //update setting boxes
        isSymbol = false;
        updateBasicSetting(currentPrefItemDoc);
        isSymbol = true;
        
    }   
}

function UpdateAdvancedSetting(doc, mcdoc)
{
    if(doc)
    {       
        if(mcdoc)
        {
            setDefValueMccore(mcdoc);
            if(!xslPrefsdoc)
                xslPrefsdoc = LoadPrefDocByPath("xslt/prefs.xsl");
            var root = DomHelper.getChildNode(doc);
            var node = DomHelper.getChildNode(root);
            while(node)
            {
                try
                {
                    UpdateMccoreValue(mcdoc, node.getAttribute("key"), DomHelper.getNodeValue(node));
                }
                catch(e)
                {
                   dumpErr(e);
                }
                node = DomHelper.getNextNode(node);
            }
            //add xml filter
	    var data = filterMcXml(mcdoc);
	    var subDoc = data["doc"];
	    $e("adv_coder_mode").setAttribute("hidden",true);
	    $e("adv_splitter").setAttribute("hidden",true);
	    $e("adv_splitter").setAttribute("state","open");
	    $e("adv_pref").setAttribute("hidden",true);
	    $e("adv_revert_btn").setAttribute("hidden",true);
	    transformXMLId(subDoc, xslPrefsdoc, "adv_pref");
	    if(currentPrefType == PREFVIDEOCODEC)
	    {				
		    var coder = data["coder"];
		    if(coder == "x264")
		    {
			    var xslX264doc = LoadPrefDocByPath("xslt/deck_video/deck_h264.xsl");
			    $e("adv_coder_mode").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("state","collapsed");
			    transformXMLId(subDoc, xslX264doc, "adv_coder_mode");
			    //return;
		    }
		    else if(coder == "xvid")
		    {
			    var xslXvidDoc = LoadPrefDocByPath("xslt/deck_video/deck_xvid.xsl");
			    $e("adv_coder_mode").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("state","collapsed");
			    transformXMLId(subDoc, xslXvidDoc, "adv_coder_mode");
		    }
		    else if(coder == "cuda264")
		    {
			    var xslDoc = LoadPrefDocByPath("xslt/deck_video/deck_cuda264.xsl");
			    $e("adv_coder_mode").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("state","collapsed");
			    transformXMLId(subDoc, xslDoc, "adv_coder_mode");
		    }
	    }
	    else if(currentPrefType == PREFAUDIOCODEC)
	    {
		    var coder = data["coder"];
		    if(coder == 'lame')
		    {
			    var xslDoc = LoadPrefDocByPath("xslt/deck_audio/deck_mp3lame.xsl");
			    $e("adv_coder_mode").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("state","collapsed");
			    transformXMLId(subDoc, xslDoc, "adv_coder_mode");
		    }
		    else if(coder == 'faac')
		    {
			    var xslDoc = LoadPrefDocByPath("xslt/deck_audio/deck_faac.xsl");
			    $e("adv_coder_mode").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("state","collapsed");
			    transformXMLId(subDoc, xslDoc, "adv_coder_mode");
		    }
		    else if(coder == 'nero')
		    {
			    var xslDoc = LoadPrefDocByPath("xslt/deck_audio/deck_nero.xsl");
			    $e("adv_coder_mode").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("hidden",false);
			    $e("adv_splitter").setAttribute("state","collapsed");
			    transformXMLId(subDoc, xslDoc, "adv_coder_mode");
		    }
	    }
	    $e("adv_pref").setAttribute("hidden",false);
	    $e("adv_revert_btn").setAttribute("hidden",false);
        }
    }
}

function UpdateSettingsAll(doc, mcdoc)
{
  /*  setDefValueMccore(mcdoc);
    var root = DomHelper.getChildNode(doc);
    var node = DomHelper.getChildNode(root);
    while(node)
    {
	try
	{
	    UpdateMccoreValue(mcdoc, node.getAttribute("key"), DomHelper.getNodeValue(node));
	}
	catch(e)
	{
	   dumpErr(e);
	}
	node = DomHelper.getNextNode(node);
    }*/
    transformXMLId(mcdoc, xslPrefsdoc, "adv_pref_all");
}

function ChangeSettingBox(index)
{
    if(index == 1)
    {
	$e('advpref_list_container1').appendChild($e('descview'));
	$e('advpref_list_container1').appendChild($e('adv_pref_modify_part'));
    }
    else if(index == 2)
    {
	$e('advpref_list_container2').appendChild($e('descview'));
	$e('advpref_list_container2').appendChild($e('adv_pref_modify_part'));
    }
}

function UpdateMccoreValue(doc, path, val)
{
    if(doc && path)
    {
        var keys = path.split(".");
        var node = null;		
        var parentNode = doc.firstChild; // root
        for(var i = 0; i < keys.length; ++i)
        {
            node = DomHelper.getNodeByAttribute(parentNode.firstChild, "node", "key", keys[i]);
            if(!node)
                break;
            parentNode = node;
        }

        if(node)
        {
            var value = val;
            if(node.getAttribute("type") == "enum")
            {
				if(isStringNumber(value))
				{
					var childNode = DomHelper.findChildNode(node, "enum");
					var index = 0;
					while(val > index && childNode)
					{
						++index;
						childNode = DomHelper.findNode(childNode.nextSibling, "enum");
					}
					if(childNode)
					{
						value = DomHelper.getNodeValue(childNode);
					}
				}
            }
            
            var valueNode = DomHelper.findChildNode(node, "value");
            var nodeDef = DomHelper.addElementNode(doc, node, "valuedef");
            if( valueNode )
            {
                var valueDef = DomHelper.getNodeValue(valueNode);                
                DomHelper.addTextNode(doc, nodeDef, valueDef);
                node.removeChild(valueNode);    
            }
            valueNode = DomHelper.addElementNode(doc, node, "value");
            DomHelper.addTextNode(doc, valueNode, value);           
        }
    }
}




function InitList()
{
	if(!xslPrefsdoc)
        xslPrefsdoc = LoadPrefDocByPath("xslt/prefs.xsl");
	
    if(!mccoreDoc)
	{
        mccoreDoc = LoadPrefDocByPath("xslt/mccore.xml");
		setDefValueMccore(mccoreDoc);
	}
	InitPrefTreeList();	
	
    if(!currentPrefItemDoc)
    {
      DisableWholePrefPanel();
    }	
}

function DisableWholePrefPanel()
{
	 $e("check_video_enable").checked = false;
       $e("check_audio_enable").checked = false;
       enableAudio(false);
       enableVideo(false);
       enableMuxer(false);    
       
       var e = $e("adv_pref");
       while(e.firstChild) e.removeChild(e.firstChild);
        e = $e("descview");
       while(e.firstChild) e.removeChild(e.firstChild);
        e = $e("valuebox");
       while(e.firstChild) e.removeChild(e.firstChild);
	   
	   EnableBasicExtentionPanel(false);
}

function setDefValueMccore(doc)
{
	var root = DomHelper.getChildNode(doc);
	var node1 = DomHelper.findChildNode(root, 'node');
	while(node1)
	{
		var node2 = DomHelper.findChildNode(node1, 'node');
		while(node2)
		{
			var node3 = DomHelper.findChildNode(node2, 'node');
			while(node3){
				var type = node3.getAttribute('type');
				if(!DomHelper.findChildNode(node3, 'value'))
				{
					var valueNode = DomHelper.addElementNode(doc, node3, 'value');
                    var valDefNode =  DomHelper.addElementNode(doc, node3, 'valuedef');
					switch(type)
					{
						case 'enum':
							var enumNode = DomHelper.findChildNode(node3, 'enum');
							var strValue = DomHelper.getNodeValue(enumNode);
							var selEnum = DomHelper.getNodeByAttribute(enumNode, 'enum', 'sel', '1');
							if(selEnum)
							{
								strValue = DomHelper.getNodeValue(selEnum);
							}
							DomHelper.addTextNode(doc, valueNode, strValue);
                                                        DomHelper.addTextNode(doc, valDefNode, strValue);
							break;
						case 'int':
							DomHelper.addTextNode(doc, valueNode, '0');
                                                        DomHelper.addTextNode(doc, valDefNode, '0');
							break;
						case 'bool':
							DomHelper.addTextNode(doc, valueNode, 'false');
                                                        DomHelper.addTextNode(doc, valDefNode, 'false');
							break;
						case 'float':
							DomHelper.addTextNode(doc, valueNode, '0');
                                                        DomHelper.addTextNode(doc, valDefNode, '0');
							break;
						/*case 'string':
							break;
						case 'file':
							break;*/
						default:
							DomHelper.addTextNode(doc, valueNode, '');
                                                        DomHelper.addTextNode(doc, valDefNode, '');
							break;
					}
				}
				else
				{
					var valNode = DomHelper.findChildNode(node3, 'value');
					var valDefNode = DomHelper.findChildNode(node3, 'valuedef');
					if(!valDefNode)
					{
						valDefNode =  DomHelper.addElementNode(doc, node3, 'valuedef');
						DomHelper.addTextNode(doc, valDefNode, DomHelper.getNodeValue(valNode));
					}
					else
					{
						while(valNode.firstChild)valNode.removeChild(valNode.firstChild);
						DomHelper.addTextNode(doc, valNode, DomHelper.getNodeValue(valDefNode));
					}
				}
			
				node3 = DomHelper.getNextNode(node3);
				node3 = DomHelper.findNode(node3, 'node');
			}
			
			node2 = DomHelper.getNextNode(node2);
			node2 = DomHelper.findNode(node2, 'node');
		}		
		
		node1 = DomHelper.getNextNode(node1);
		node1 = DomHelper.findNode(node1, 'node');
	}
}

function createDefaultXml()
{
    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);
    doc.appendChild(root);
	
//===================================basic video=====================================//
    var node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.enabled");
    DomHelper.addTextNode(doc, node, "true");
    //encoder type
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.encoder");
    DomHelper.addTextNode(doc, node, "XviD");
    //resolution
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "videofilter.scale.enabled");
    DomHelper.addTextNode(doc, node, "true");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "videofilter.scale.width");
    DomHelper.addTextNode(doc, node, "480");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "videofilter.scale.height");
    DomHelper.addTextNode(doc, node, "320");
    // fps output
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "videofilter.frame.enabled");
    DomHelper.addTextNode(doc, node, "true");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "videofilter.frame.fps");
    DomHelper.addTextNode(doc, node, "25");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "videofilter.frame.fpsScale");
    DomHelper.addTextNode(doc, node, "1");
    //ratio
	/*
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.ar");
    DomHelper.addTextNode(doc, node, "2");
	node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.arNum");
    DomHelper.addTextNode(doc, node, "4");
	node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.arDum");
    DomHelper.addTextNode(doc, node, "3");
	*/
    //bitrate
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.mode");
    DomHelper.addTextNode(doc, node, "0");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.bitrate");
    DomHelper.addTextNode(doc, node, "100");    
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.video.format");
    DomHelper.addTextNode(doc, node, VF_ENUM_STR[0]);
//===================================basic audio=====================================//
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.audio.enabled");
    DomHelper.addTextNode(doc, node, "true");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.audio.encoder");
    DomHelper.addTextNode(doc, node, AE_ENUM_STR[0]);
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.audio.channels");
    DomHelper.addTextNode(doc, node, "0");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "audiofilter.resample.samplerate");
    DomHelper.addTextNode(doc, node, "0");
    node = DomHelper.addElementNode(doc, root, "node");
    DomHelper.addAttribute(doc, node, "key", "overall.audio.format");
    DomHelper.addTextNode(doc, node, AF_ENUM_STR[0]);
    
    return doc;
}

function onFoucsLocal(e)
{
	$e("local_list_header").setAttribute("style","font-weight:bold");
	$e("remote_list_header").setAttribute("style","");
	isCurrentPrefRemote=false;
	$e("prefs_list_remote").selectedIndex = -1;	
}

function onFoucsRemote(e)
{
	$e("remote_list_header").setAttribute("style","font-weight:bold");
	$e("local_list_header").setAttribute("style","");
	isCurrentPrefRemote=true;
	$e("prefs_list").selectedIndex = -1;
	
}


function inputStart()
{
    inputOn = true;
}

function enableMuxer(isEnable)
{
    $e("menu_muxer_format").setAttribute("disabled", !isEnable);
}

function enableVideo(isEnable)
{
    $e("menu_video_format").setAttribute("disabled", !isEnable);
    $e("menu_video_mode").setAttribute("disabled", !isEnable);
    $e("menu_crop_mode").setAttribute("disabled", !isEnable);
    $e("cuda_enable").setAttribute("disabled", !isEnable);
	$e("avisynth_enable").setAttribute("disabled", !isEnable);
    //$e("video_bit_scale").setAttribute("disabled", !isEnable);
    $e("check_video_copy").setAttribute("disabled", !isEnable);
	
    if(!isEnable)
    {
		$e("video_maxbit_text").setAttribute("disabled", true);
		$e("video_minbit_text").setAttribute("disabled", true);
		$e("video_bufsize_text").setAttribute("disabled", true);
        $e("menu_video_resolution").setAttribute("disabled", true);
        $e("menu_video_ratio").setAttribute("disabled", true);
        $e("menu_video_fps").setAttribute("disabled", true);
        $e("video_bit_text").setAttribute("disabled", true);
		$e("video_quality_text").setAttribute("disabled", true);
    }
    else
    {
		$e("video_maxbit_text").removeAttribute("disabled");
		$e("video_minbit_text").removeAttribute("disabled");
		$e("video_bufsize_text").removeAttribute("disabled");
        $e("menu_video_resolution").removeAttribute("disabled");
        $e("menu_video_ratio").removeAttribute("disabled");
        $e("menu_video_fps").removeAttribute("disabled");
        $e("video_bit_text").removeAttribute("disabled");
		$e("video_quality_text").removeAttribute("disabled");
    }
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.enabled", isEnable ? "true" : "false");
       setModifySymbol();
    }
}

function enableCopyVideo(isEnable)
{
    $e("menu_video_format").setAttribute("disabled", isEnable);
    $e("menu_video_mode").setAttribute("disabled", isEnable);
    $e("menu_crop_mode").setAttribute("disabled", isEnable);
    $e("cuda_enable").setAttribute("disabled", isEnable);
	$e("avisynth_enable").setAttribute("disabled", isEnable);
	
    if(isEnable)
    {
		$e("video_maxbit_text").setAttribute("disabled", true);
		$e("video_minbit_text").setAttribute("disabled", true);
		$e("video_bufsize_text").setAttribute("disabled", true);
        $e("menu_video_resolution").setAttribute("disabled", true);
        $e("menu_video_ratio").setAttribute("disabled", true);
        $e("menu_video_fps").setAttribute("disabled", true);
        $e("video_bit_text").setAttribute("disabled", true);
		$e("video_quality_text").setAttribute("disabled", true);
    }
    else
    {
		$e("video_maxbit_text").removeAttribute("disabled");
		$e("video_minbit_text").removeAttribute("disabled");
		$e("video_bufsize_text").removeAttribute("disabled");
        $e("menu_video_resolution").removeAttribute("disabled");
        $e("menu_video_ratio").removeAttribute("disabled");
        $e("menu_video_fps").removeAttribute("disabled");
        $e("video_bit_text").removeAttribute("disabled");
		$e("video_quality_text").removeAttribute("disabled");
    }
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.copy", isEnable ? "true" : "false");
       setModifySymbol();
    }
}

function enableAudio(isEnable)
{
    $e("menu_audio_format").setAttribute("disabled", !isEnable);
    $e("menu_audio_channel").setAttribute("disabled", !isEnable);
    $e("check_audio_copy").setAttribute("disabled", !isEnable);
    if(!isEnable)
    {
        $e("menu_audio_resample").setAttribute("disabled", true);
        $e("audio_bit_text").setAttribute("disabled", true);
    }
    else
    {
        $e("menu_audio_resample").removeAttribute("disabled");
        $e("audio_bit_text").removeAttribute("disabled");
    }
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.audio.enabled", isEnable ? "true" : "false");
       setModifySymbol();
    }
}

function enableCopyAudio(isEnable)
{
    $e("menu_audio_format").setAttribute("disabled", isEnable);
    $e("menu_audio_channel").setAttribute("disabled", isEnable);
    if(isEnable)
    {
        $e("menu_audio_resample").setAttribute("disabled", true);
        $e("audio_bit_text").setAttribute("disabled", true);
    }
    else
    {
        $e("menu_audio_resample").removeAttribute("disabled");
        $e("audio_bit_text").removeAttribute("disabled");
    }
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.audio.copy", isEnable ? "true" : "false");
       setModifySymbol();
    }
}

function enableX264Options()
{
    var root = DomHelper.getChildNode(currentPrefItemDoc);
    var bitrate = 0;
    var mbitrate = 0;
    var bufsize = 0;
    var node;
    node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(root), "node", "key", "overall.video.bitrate");
    if(node)
    {
		bitrate = parseInt(DomHelper.getNodeValue(node));
    }
	
	var key = "videoenc.x264.vbv_maxrate";
	if($e("cuda_enable").checked)
	{
		key = "videoenc.cuda264.peak";
		$e("video_bufsize_text").setAttribute("disabled", true);
	}
	else
	{
		$e("video_bufsize_text").removeAttribute("disabled");
	}
    node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(root), "node", "key", key);
    if(node)
    {
		mbitrate = parseInt(DomHelper.getNodeValue(node));
		$e("video_maxbit_text").value = mbitrate.toString();
    }
    else
    {
		mbitrate = 1.2 * bitrate;
		mbitrate = parseInt(mbitrate);
		$e("video_maxbit_text").value = mbitrate.toString();
		DomHelper.changeNode(currentPrefItemDoc, root, key, mbitrate.toString());
		
    }
    
    node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(root), "node", "key", "videoenc.x264.vbv_bufsize");
    if(node)
    {
		bufsize = parseInt(DomHelper.getNodeValue(node));
		//alert(bufsize);
		$e("video_bufsize_text").value = bufsize.toString();
    }
    else
    {
		bufsize = bitrate;
		$e("video_bufsize_text").value = bufsize.toString();
		DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.x264.vbv_bufsize", bufsize.toString());
		
    }
    //
	$e("video_minbit_text").setAttribute("disabled", true);
    $e("video_maxbit_text").removeAttribute("disabled");
}

function enableFFmpegOptions()
{
	var root = DomHelper.getChildNode(currentPrefItemDoc);
    var bitrate = 0;
    var mbitrate = 0;
    var bufsize = 0;
    var node;
	node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(root), "node", "key", "overall.video.bitrate");
	if(node)
	{
		bitrate = parseInt(DomHelper.getNodeValue(node));
	}
	
	node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(root), "node", "key", "videoenc.ffmpeg.vrcMaxRate");
	if(node)
	{
		mbitrate = parseInt(DomHelper.getNodeValue(node));
		$e("video_maxbit_text").value = mbitrate.toString();
	}
	else
	{
		mbitrate = 1.2 * bitrate;
		mbitrate = parseInt(mbitrate);
		$e("video_maxbit_text").value = mbitrate.toString();
		DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.ffmpeg.vrcMaxRate", mbitrate.toString());
	}
	
	node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(root), "node", "key", "videoenc.ffmpeg.vrcMinRate");
	if(node)
	{
		mbitrate = parseInt(DomHelper.getNodeValue(node));
		$e("video_minbit_text").value = mbitrate.toString();
	}
	else
	{
		mbitrate = 0;
		$e("video_minbit_text").value = mbitrate.toString();
		DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.ffmpeg.vrcMinRate", mbitrate.toString());
	}
	
	node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(root), "node", "key", "videoenc.ffmpeg.vrcBufSize");
	if(node)
	{
		bufsize = parseInt(DomHelper.getNodeValue(node));
		//alert(bufsize);
		$e("video_bufsize_text").value = bufsize.toString();
	}
	else
	{
		bufsize = bitrate;
		$e("video_bufsize_text").value = bufsize.toString();
		DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.ffmpeg.vrcBufSize", bufsize.toString());
	}
	
	$e("video_minbit_text").removeAttribute("disabled");
    $e("video_maxbit_text").removeAttribute("disabled");
	$e("video_bufsize_text").removeAttribute("disabled");
}

function GetValueOrWithDef(root,key)
{
    var node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", key);
    if(node)
	return DomHelper.getNodeValue(node);
    else
	return GetMcexDefaultValue(key);
}

function updateBasicSetting(doc)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
        //video
        var node;
        var enable;
        var value;
        enableMuxer(true);
        
	if(1)
        {
            //format
	    value = PREF_GetVideoFormat(GetValueOrWithDef(root, "overall.video.format"));
	    
	    $e("cuda_enable").setAttribute("disabled", true);
            if(value == "XviD" ) {               
                $e("cuda_enable").checked = false;
		$e("video_maxbit_text").setAttribute("disabled", true);
		$e("video_minbit_text").setAttribute("disabled", true);
		$e("video_bufsize_text").setAttribute("disabled", true);
            }			
            else
            {
		$e("video_maxbit_text").removeAttribute("disabled");
		$e("video_minbit_text").removeAttribute("disabled");
		$e("video_bufsize_text").removeAttribute("disabled");
				
                var iencoder = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.encoder");
                if(iencoder) {
                    var ivalue =PREF_GetVideoEncoder(DomHelper.getNodeValue(iencoder));
                    if(ivalue == "CUDA Encoder")
                    {
                        $e("cuda_enable").checked = true;
						$e("video_minbit_text").setAttribute("disabled", true);
						$e("video_bufsize_text").setAttribute("disabled", true);
                    }
                    else{
                        $e("cuda_enable").checked = false;
                    }
                }
		else
		{
		   	
		}
		
		if(value == "H.264")
		{
		    $e("cuda_enable").removeAttribute("disabled");										
		    enableX264Options();
		}
		else{
		    enableFFmpegOptions();
		}
            }
            if(value=="WMV 9")
	    {
		    DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.profile","Main");
		    DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.outType","RAW");
	    }
	    else if(value=="VC-1")
	    {
		    DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.outType","ES");
		    DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.profile","Advanced");
	    }
			
            var item = $e("menu_video_format").getItemAtIndex(PREF_GetVideoFormat(value,true));
            if(item) {
                $e("menu_video_format").selectedItem = item;
            }
            //mode
	    value = GetValueOrWithDef(root,  "overall.video.mode");	  
	    
            var item = $e("menu_video_mode").getItemAtIndex(parseInt(value, 10));
            if(item) {
                $e("menu_video_mode").selectedItem = item;
            }
            menuVideoModeChange($e("menu_video_mode"));
            //crop
            value = GetValueOrWithDef(root, "videofilter.crop.mode");
            var mindex = parseInt(value);
            if(mindex > 0)
            {
                mindex -= 1;
            }
            var item = $e("menu_crop_mode").getItemAtIndex(mindex);
            if(item) {
                $e("menu_crop_mode").selectedItem = item;
            }
            
            //resolution
            node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "videofilter.scale.enabled");
            value = getLocalString("pref.meun.original");
            if(node)
            {
                if("true" == DomHelper.getNodeValue(node)) {
                    var w = "unset";
                    var h = "unset";
                    node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "videofilter.scale.width");
                    if(node) {
                        w = DomHelper.getNodeValue(node);
                    }
                    node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "videofilter.scale.height");
                    if(node) {
                        h = DomHelper.getNodeValue(node);
                    }
                    value = w + "x" + h;
                }
            }
            $e("menu_video_resolution").value = value;
            //ratio
            node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.ar");
            value = getLocalString("pref.meun.original");
            if(node)
            {
                if("2" == DomHelper.getNodeValue(node) || "Set DAR" == DomHelper.getNodeValue(node)) {
                    var w = "0";
                    var h = "0";
                    node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.arNum");
                    if(node) {
                        w = DomHelper.getNodeValue(node);
                    }
                    node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.arDen");
                    if(node)  {
                        h = DomHelper.getNodeValue(node);
                    }
		    if(w != "0" || h!= "0")
			value = w + ":" + h;
                }
            }
            $e("menu_video_ratio").value = value;
            //fps
            node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "videofilter.frame.enabled");
            value = getLocalString("pref.meun.original");
            if(node)
            {
                if("true" == DomHelper.getNodeValue(node)) {
                    var w = "unset";
                    var h = "unset";
                    node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "videofilter.frame.fps");
                    if(node) {
                        w = DomHelper.getNodeValue(node);
                    }
                    node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "videofilter.frame.fpsScale");
                    if(node) {
                        h = DomHelper.getNodeValue(node);
                    }
                    if(h != "1") {
                        value = w + "/" + h;
                    }  else {
                        value = w;   
                    }                    
                }
            }
            $e("menu_video_fps").value = value;
            //bitrate
            value = GetValueOrWithDef(root, "overall.video.bitrate");
            $e("video_bit_text").value = value;
	    //quality
	    value = GetValueOrWithDef(root, "overall.video.quality");
            $e("video_quality_text").value = value;
            //saveflag = true;
            //$e("video_bit_scale").value = value;
        }		
			
	//node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.enabled");
        var enable1 = GetValueOrWithDef(root, "overall.video.enabled") == "true";
	      
        $e("check_video_enable").checked = enable1;
		enableVideo(enable1);
		
	node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.copy");
	enable = false;
        if(node) {
            enable = (DomHelper.getNodeValue(node) == "true");
        }
	$e('check_video_copy').checked = enable;
	
	if(!enable && !enable1){}
	else enableCopyVideo(enable);
			   
	//audio
	enable1 = GetValueOrWithDef(root, "overall.audio.enabled") == "true";
      	
        $e("check_audio_enable").checked = enable1;
        enableAudio(enable1);
	
	node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.audio.copy");
	enable = false;
        if(node) {
            enable = (DomHelper.getNodeValue(node) == "true");
        }
	$e('check_audio_copy').checked = enable;	        
	if(!enable && !enable1){}
	else enableCopyAudio(enable);
		
	//format
	value = GetValueOrWithDef(root, "overall.audio.format");
        var audioformat = PREF_GetAudioFormat(value);
        
	//alert(audioformat+":"+value);
		
        var item = $e("menu_audio_format").getItemAtIndex(PREF_GetAudioFormat(value,true));
        if(item) {
            $e("menu_audio_format").selectedItem = item;
        }
        //channel
	value = "0";
        node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.audio.channels");
        if(node) {
            value = DomHelper.getNodeValue(node);
        }
        var ichannel =  parseInt(value, 10);
        item = $e("menu_audio_channel").getItemAtIndex(ichannel);
        if(item){
            $e("menu_audio_channel").selectedItem = item;
        }
        //resample
        node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "audiofilter.resample.samplerate");
        value = getLocalString("pref.meun.original");
        if(node) {
            value = DomHelper.getNodeValue(node) + "Hz";
            if(value == "0Hz") {
                value = getLocalString("pref.meun.original");
            }
        }
        $e("menu_audio_resample").value = value;
        
        //audio bitrate
        //audioenc.ffmpeg.bitrate
        var mcroot = DomHelper.getChildNode(mccoreDoc);
        value = "128";
        {
            node = DomHelper.getNodeByAttribute(mcroot.firstChild, "node", "key", "audioenc");
            var codeckey = "ffmpeg";
            if(audioformat)
            {
               codeckey = PREF_GetAudioEncNameByFormat(audioformat);
            }
           // alert(audioformat+":" + codeckey);
            node = DomHelper.getNodeByAttribute(node.firstChild, "node", "key", codeckey);
            currentGobalData['audio.bit.key'] = codeckey;
            node = DomHelper.getNodeByAttribute(node.firstChild, "node", "key", "bitrate");
            if(node) {
                node = DomHelper.findChildNode(node, 'value');
                if(node) {
                  value = DomHelper.getNodeValue(node);
                }
            }
        }
        $e("audio_bit_text").value = value;
                
        //container format
        node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.container.format");
        value = 0;
        if(node) {
            value = PREF_GetContainerFormat(DomHelper.getNodeValue(node),true);
            //alert(value);
        }
        item = $e("menu_muxer_format").getItemAtIndex(value);
        if(item) {
            $e("menu_muxer_format").selectedItem = item;
        }
		
		//avisynth
		node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.source");
		if(node && DomHelper.getNodeValue(node) == '4')
		{
			$e('avisynth_enable').checked = true;
		}
		else
		{
			$e('avisynth_enable').checked = false;
		}
    }
	UpateBasicTabs();
}

function menuVideoFormatChange(e)
{
    $e("cuda_enable").setAttribute("disabled", true);
    if(currentPrefItemDoc)
    {
        var root = DomHelper.getChildNode(currentPrefItemDoc);
		var format =  PREF_GetVideoFormat(e.value);
        DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.format", format);        
		var codec = PREF_GetVideoEncoderByFormat(format);
        if(codec == "x264" && $e("cuda_enable").setAttribute("disabled", false) )
			codec = "CUDA Encoder";  
        DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.encoder", codec);
        
        setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
		//alert(format);
		if(format == "H.264")
		{
			enableX264Options();
		}
		else if(format == "XviD")
		{
			$e("video_maxbit_text").setAttribute("disabled", true);
			$e("video_minbit_text").setAttribute("disabled", true);
			$e("video_bufsize_text").setAttribute("disabled", true);
		}
		else
		{
			enableFFmpegOptions();
		}
		
		if(format=="WMV 9")
		{
			DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.profile","Main");
			DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.outType","RAW");
		}
		else if(format=="VC-1")
		{
			DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.outType","ES");
			DomHelper.changeNode(currentPrefItemDoc, root,"videoenc.vc1.profile","Advanced");
		}
    }    
}

function menuVideoModeChange(e)
{
    if(currentPrefItemDoc)
    {
		if(parseInt(e.value) == 2)
		{
			$e("video_bufsize_text").setAttribute("disabled", true);
			$e("video_maxbit_text").setAttribute("disabled", true);
			$e("video_minbit_text").setAttribute("disabled", true);
		}
		else
		{
			$e("video_bufsize_text").removeAttribute("disabled");
			$e("video_maxbit_text").removeAttribute("disabled");
			$e("video_minbit_text").removeAttribute("disabled");
		}
		
		if(parseInt(e.value) == 1)
		{
			$e("vbandvq_1").setAttribute("hidden", true);
			$e("vbandvq_2").setAttribute("hidden", true);
			$e("vbandvq_3").setAttribute("hidden", false);
			$e("vbandvq_4").setAttribute("hidden", false);
		}
		else
		{
			$e("vbandvq_3").setAttribute("hidden", true);
			$e("vbandvq_4").setAttribute("hidden", true);
			$e("vbandvq_1").setAttribute("hidden", false);
			$e("vbandvq_2").setAttribute("hidden", false);
		}
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.mode", e.value);
        setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }    
}

function menuCropModeChange(e)
{
    if(currentPrefItemDoc)
    {
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, "videofilter.crop.mode", e.value);
        setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }    
}

function menuVideoResolutionChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoResolutionChange(e);
    }
}

function menuVideoResolutionChange(e)
{
    if(currentPrefItemDoc)
    {
        var enabled;
        var w, h;
        var vals = e.value.toString().split("x");
        if(e.value == getLocalString("pref.meun.original") || vals.length != 2)
        {
            enabled = "false";
            w = "0";
            h = "0";
        }
        else
        {
            w = parseInt(vals[0], 10).toString();
            h = parseInt(vals[1], 10).toString();
            if(w == "0" || h == "0")
            {
                enabled = "false"
            }
            else
            {
                enabled = "true";
            }
        }
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, "videofilter.scale.enabled", enabled);
        DomHelper.changeNode(currentPrefItemDoc, root, "videofilter.scale.width", w);
        DomHelper.changeNode(currentPrefItemDoc, root, "videofilter.scale.height", h);
        setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function menuVideoRatioChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoRatioChange(e);
    }
}

function menuVideoRatioChange(e)
{
    if(currentPrefItemDoc)
    {
        var enabled;
        var w, h;
        var vals = e.value.toString().split(":");
        if(e.value == getLocalString("pref.meun.original") || vals.length != 2)
        {
            enabled = "false";
            w = "0";
            h = "0";
        }
        else
        {
            w = parseInt(vals[0], 10).toString();
            h = parseInt(vals[1], 10).toString();
            if(w == "0" || h == "0")
            {
                enabled = "false"
            }
            else
            {
                enabled = "true";
            }
        }
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.ar", 2);
        DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.arNum", w);
        DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.arDen", h);
        setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function menuVideoFpsChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoFpsChange(e);
    }
}

function menuVideoFpsChange(e)
{
    if(currentPrefItemDoc)
    {
        var enabled;
        var num, den;
        var vals = e.value.toString().split("/");
	//alert(e.value);
	//alert(getLocalString("pref.meun.original"));
        if(e.value == getLocalString("pref.meun.original") || vals.length > 2)
        {
           enabled = "false";
           num = "0";
           den = "0";
        }
        else if(vals.length == 2)
        {
            num = parseInt(vals[0], 10).toString();
            den = parseInt(vals[1], 10).toString();
            if(num == "0" || den == "0")
            {
                enabled = "false"
            }
            else
            {
                enabled = "true";
            }
        }
        else if(vals.length == 1)
        {
            num = parseInt(vals[0], 10).toString();
            den = "1";
            if(num == "0")
            {
                enabled = "false"
            }
            else
            {
                enabled = "true";
            }
        }
	//alert(num);
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, "videofilter.frame.enabled", enabled);
        DomHelper.changeNode(currentPrefItemDoc, root, "videofilter.frame.fps", num);
        DomHelper.changeNode(currentPrefItemDoc, root, "videofilter.frame.fpsScale", den);
        setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

var saveflag = false;

function menuVideoBitRateChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoBitRateChange(e);
    }
}

function menuVideoBitRateChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.bitrate", e.value);
       setModifySymbol();
       //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function menuVideoQualityChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoQualityChange(e);
    }
}

function menuVideoQualityChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.quality", e.value);
       setModifySymbol();
    }
}

function menuVideoMaxBitRateChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoMaxBitRateChange(e);
    }
}

function menuVideoMaxBitRateChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
	   var format = "Xvid";
		var node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.format");
		if(node) {
			format = PREF_GetVideoFormat(DomHelper.getNodeValue(node));
		}
		if(format == "H.264")
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.x264.vbv_maxrate", e.value);
			if($e("cuda_enable").checked)
			{
				DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.cuda264.peak", e.value);
			}
		}
		else if(format != "Xvid")
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.ffmpeg.vrcMaxRate", e.value);
		}
       setModifySymbol();
       //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function menuVideoMinBitRateChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoMinBitRateChange(e);
    }
}

function menuVideoMinBitRateChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
	   var format = "Xvid";
		var node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.format");
		if(node) {
			format = PREF_GetVideoFormat(DomHelper.getNodeValue(node));
		}
		if(format != "Xvid" && format != "H.264")
		{			
			DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.ffmpeg.vrcMinRate", e.value);
		}
	   setModifySymbol();
       //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function menuVideoBufSizeRateChangeInput(e)
{
    if(inputOn)
    {
        inputOn = false;
        menuVideoBufSizeRateChange(e);
    }
}

function menuVideoBufSizeRateChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
	   var format = "Xvid";
		var node = DomHelper.getNodeByAttribute(root.firstChild, "node", "key", "overall.video.format");
		if(node) {
			format = PREF_GetVideoFormat(DomHelper.getNodeValue(node));
		}
		if(format == "H.264")
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.x264.vbv_bufsize", e.value);
		}
		else if(format != "Xvid")
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "videoenc.ffmpeg.vrcBufSize", e.value);
		}
       setModifySymbol();
       //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}


function menuAudioFormatChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       var format = PREF_GetAudioFormat(e.value);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.audio.format", format);
       var codec = PREF_GetAudioEncoderByFormat(format);     
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.audio.encoder", codec);
       setModifySymbol();
       //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function menuAudioChannelChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.audio.channels", e.value == "Original" ? "0" : e.value);
       setModifySymbol();
       //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function menuAudioResampleChangeInput(e)
{
	if(inputOn)
	{
	  inputOn = false;
	  menuAudioResampleChange(e);
	}
}

function menuAudioResampleChange(e)
{
  if(currentPrefItemDoc)
  {
	var enabled;
	var rate;

	if(e.value != getLocalString("pref.meun.original")) {
	  var vals = e.value.toString().split("H");
	  var root = DomHelper.getChildNode(currentPrefItemDoc);
	  DomHelper.changeNode(currentPrefItemDoc, root, "audiofilter.resample.samplerate", parseInt(vals[0]).toString());
	  setModifySymbol();
	  //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
    else
    {
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, "audiofilter.resample.samplerate", 0);
        setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
  }
}

function menuMuxerFormatChange(e)
{
    if(currentPrefItemDoc)
    {
       var root = DomHelper.getChildNode(currentPrefItemDoc);
       var codec = PREF_GetContainerFormat(e.value);
       DomHelper.changeNode(currentPrefItemDoc, root, "overall.container.format", codec);
        
      codec= PREF_GetMuxerByFormat(codec);
      DomHelper.changeNode(currentPrefItemDoc, root, "overall.container.muxer", codec);
      setModifySymbol();
      //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function editRomove(e)
{
    e.removeAttribute("editable");
}

function editAdd(e)
{
    e.setAttribute("editable",true);
}

function filterMcXml(mcdoc)
{
    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);
	var encoderStr;
    if(mcdoc)
    {
        var mcroot = DomHelper.getChildNode(mcdoc);
        switch(currentPrefType)
        {
         case PREFVIDEOCODEC:
            {
                var node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(mcroot), "node", "key", "videoenc");
                var resnode = root.appendChild(node.cloneNode(false));
                if(currentPrefItemDoc)
                {
                    var codecNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(DomHelper.getChildNode(currentPrefItemDoc)), "node", "key", "overall.video.encoder");
                    var childNode = null;
                    encoderStr = "";
                    if(codecNode)
                    {
                         encoderStr = PREF_GetVideoEncNameByEncoder( DomHelper.getNodeValue(codecNode) );       
                    }
                    else
                    {
			
                       /* codecNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(DomHelper.getChildNode(currentPrefItemDoc)), "node", "key", "overall.video.format");
                        if(codecNode)
                        {*/
                            encoderStr = PREF_GetVideoEncNameByFormat(GetValueOrWithDef(DomHelper.getChildNode(currentPrefItemDoc),"overall.video.format"));
			   // alert(encoderStr);
			    if( encoderStr == "x264" && $e("cuda_enable").checked)
			    {                                        
				    encoderStr == "cuda264";
			    }                                  
                         
                       /* }*/
                    }					
                    childNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(node), "node","key", encoderStr);                            
                    if(childNode)
                        resnode.appendChild(childNode.cloneNode(true));
                }                
             break;
            }
         case PREFAUDIOCODEC:
            {
                var node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(mcroot), "node", "key", "audioenc");
                var resnode = root.appendChild(node.cloneNode(false));
                if(currentPrefItemDoc)
                {
                    var codecNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(DomHelper.getChildNode(currentPrefItemDoc)), "node", "key", "overall.audio.encoder");
                    var childNode = null;
                    encoderStr = "";
                    if(codecNode)
                    {
                        encoderStr = PREF_GetAudioEncNameByEncoder( DomHelper.getNodeValue(codecNode) );                       
                    }
                    else
                    {
                       /* codecNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(DomHelper.getChildNode(currentPrefItemDoc)), "node", "key", "overall.audio.format");
                        if(codecNode) {}*/
                            encoderStr = PREF_GetAudioEncNameByFormat(GetValueOrWithDef(DomHelper.getChildNode(currentPrefItemDoc),"overall.audio.format"));                           
                        
                    }
                    
                    childNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(node), "node","key", encoderStr);                
                    if(childNode)
                        resnode.appendChild(childNode.cloneNode(true));
                }                
                break;
            }
         case PREFVIDEOFILTER:
            {
                var node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(mcroot), "node", "key", "videofilter");
                var resnode = root.appendChild(node.cloneNode(true));
             break;
            }
         case PREFAUDIOFILTER:
            {
                var node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(mcroot), "node", "key", "audiofilter");
                var resnode = root.appendChild(node.cloneNode(true));
                break;
            }
        case PREFSUBTITLE:
            {
                var node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(mcroot), "node", "key", "overall");
                var resnode = root.appendChild(node.cloneNode(false));
                node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(node), "node", "key", "subtitle");
                resnode.appendChild(node.cloneNode(true));
                break;
            }
        case PREFMUXER:
            {
                var node = DomHelper.getNodeByAttribute(DomHelper.getChildNode(mcroot), "node", "key", "muxer");
                var resnode = root.appendChild(node.cloneNode(false));
                if(currentPrefItemDoc)
                {
                    var codecNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(DomHelper.getChildNode(currentPrefItemDoc)), "node", "key", "overall.container.muxer");
                    var childNode;
                    var childNode = null;
                    var index = 0;
                    encoderStr = "ffmpeg";
                    if(!codecNode)
                    {
                        codecNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(DomHelper.getChildNode(currentPrefItemDoc)), "node", "key", "overall.container.format");
						if(codecNode)
                        {
                            encoderStr = PREF_GetMuxNameByFormat(DomHelper.getNodeValue(codecNode));  
                        }
                    }
		    else
		    {
			    encoderStr = PREF_GetMuxNameByMuxer(DomHelper.getNodeValue(codecNode));
		    }
					
                    childNode = DomHelper.getNodeByAttribute(DomHelper.getChildNode(node), "node","key", encoderStr);                                  
                    if(childNode)
                        resnode.appendChild(childNode.cloneNode(true));
                }
                break;
            }
        }
    }
    return {"doc":doc,"coder":encoderStr};
}

function selectCodec(index, elem)
{
	// Adjust backgroud (change css class)
	const selectedClass = "select_checked";
	const unselectedClass = "select_list";
    if(elem) {
		elem.setAttribute("class", selectedClass);
		var parentNode = elem.parentNode;
		if(parentNode) {
			var nextDeckBox = DomHelper.getChildNode(parentNode);
			while(nextDeckBox) {
				if(nextDeckBox != elem && nextDeckBox.getAttribute("class") != unselectedClass) {
					nextDeckBox.setAttribute("class", unselectedClass);
				}
				nextDeckBox = DomHelper.getNextNode(nextDeckBox);
			}
		}
	}
	
   currentPrefType = index;
   UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
}

function enableCuda(e)
{
    if(currentPrefType == PREFVIDEOCODEC)
    {
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        if(e)
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.encoder", "CUDA Encoder");
			$e("video_bufsize_text").setAttribute("disabled", true);
		}
        else
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.encoder", "x264");
			$e("video_bufsize_text").removeAttribute("disabled");
		}
		setModifySymbol();
        //UpdateAdvancedSetting(currentPrefItemDoc, mccoreDoc);
    }
}

function enableAviSynth(e)
{
	if(currentPrefItemDoc)
	{
		var root = DomHelper.getChildNode(currentPrefItemDoc);
		if(e)
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.source", 4);
		}
		else
		{
			DomHelper.changeNode(currentPrefItemDoc, root, "overall.video.source", 2);
		}
		 setModifySymbol();
	}
}

function menuAudioBitRateChange(e)
{
    var key = "audioenc." +  currentGobalData['audio.bit.key'] + ".bitrate";
	if(currentPrefItemDoc)
	{
		var root = DomHelper.getChildNode(currentPrefItemDoc);
		DomHelper.changeNode(currentPrefItemDoc, root, key, e.value.toString());
		setModifySymbol();
	}    
}

