function chagneCurDoc(key, value, nodename)
{
    if(currentPrefItemDoc)
    {
        var root = DomHelper.getChildNode(currentPrefItemDoc);
        DomHelper.changeNode(currentPrefItemDoc, root, key, value, nodename);
        setModifySymbol();
    }
}

function EnableSubtitleMenu(e)
{
    if(e)
    {
        $e("basic_subtitle_size").removeAttribute("disabled");
        $e("basic_subtitle_code").removeAttribute("disabled");
        $e("basic_subtitle_lang").removeAttribute("disabled");
        $e("basic_subtitle_pos").removeAttribute("disabled");
        $e("basic_subtitle_font").removeAttribute("disabled");
        $e("basic_subtitle_id").removeAttribute("disabled");
    }
    else
    {
        $e("basic_subtitle_size").setAttribute("disabled", true);
        $e("basic_subtitle_code").setAttribute("disabled", true);
        $e("basic_subtitle_lang").setAttribute("disabled", true);
        $e("basic_subtitle_pos").setAttribute("disabled", true);
        $e("basic_subtitle_font").setAttribute("disabled", true);
        $e("basic_subtitle_id").setAttribute("disabled", true);
    }
}

function OnSubtitleModeMenu(e)
{
    var val = parseInt(e.value);
    EnableSubtitleMenu(val != 0);
    chagneCurDoc("overall.subtitle.mode",val);  
}

function OnSubtitleFontMenu(e)
{
    var val = e.value;
    chagneCurDoc("overall.subtitle.font", val);
}

function OnSubtitleFontMenuInput(e)
{
    if(inputOn)
	{
	  inputOn = false;
	  OnSubtitleLangMenu(e);
	}
}

function OnSubtitleLangMenu(e)
{
    var val = e.value.split("(");
    chagneCurDoc("overall.subtitle.slang", val[0]);
}

function OnSubtitleLangMenuInput(e)
{
    if(inputOn)
	{
	  inputOn = false;
	  OnSubtitleLangMenu(e);
	}
}

function OnTextboxChange(e, key, byNum)
{
    var val = e.value;
    if(byNum)
    {
        val = parseInt(e.value)*byNum;
    }
    chagneCurDoc(key, val);
}
function OnMenuChange(e, key, nodename)
{
    chagneCurDoc(key, e.value, nodename);
}

function EnableThumbnailMenu(e)
{
    var rows = $e("basic_thumbnail_rows");
    var row = DomHelper.findChildNode(rows, "row");
    while(row)
    {
        var node = DomHelper.getChildNode(row);
        while(node)
        {
           // alert(node.nodeName);
            if(node.nodeName == "textbox" || node.nodeName == "menulist"){
                if(e)
                {
                    node.removeAttribute("disabled");
                }
                else
                {
                    node.setAttribute("disabled", true);
                }
            }
            node = DomHelper.getNextNode(node);    
        }         
       
        row = DomHelper.getNextNode(row);
        row = DomHelper.findNode(row, "row");
    }
    
    if(e)
    {
        $e("basic_thumbnail_prefix").removeAttribute("disabled");
        $e("basic_thumbnail_folder").removeAttribute("disabled");
    }
    else
    {
        $e("basic_thumbnail_prefix").setAttribute("disabled", true);
        $e("basic_thumbnail_folder").setAttribute("disabled", true);
    }
    $e("basic_thumbnail_enable").removeAttribute("disabled");
}

function OnThumbnailEnableMenu(e)
{
    EnableThumbnailMenu(e.value == "true");
    chagneCurDoc("videofilter.thumb.enabled", e.value);
}

function browserButtonFunction(e)
{
    var dirAtrr = Io.openDir('Select');
    if(dirAtrr == null)
    return;
    e.value = dirAtrr.path;
}

function EnableWaterMarkMenu(e)
{
    var rows = $e("basic_watermark_rows");
    var row = DomHelper.findChildNode(rows, "row");
    while(row)
    {
        var node = DomHelper.getChildNode(row);
        while(node)
        {
           // alert(node.nodeName);
            if(node.nodeName == "textbox" || node.nodeName == "menulist"){
                if(e)
                {
                    node.removeAttribute("disabled");
                }
                else
                {
                    node.setAttribute("disabled", true);
                }
            }
            node = DomHelper.getNextNode(node);    
        }         
       
        row = DomHelper.getNextNode(row);
        row = DomHelper.findNode(row, "row");
    }
    
     if(e)
    {
        $e("basic_watermark_path").removeAttribute("disabled");
	$e("basic_watermark_text").removeAttribute("disabled");
    }
    else
    {
        $e("basic_watermark_path").setAttribute("disabled", true);
	$e("basic_watermark_text").setAttribute("disabled", true);
    }
    
    $e("basic_watermark_enable").removeAttribute("disabled");
}

function OnWatermarEnableMenu(e)
{
    EnableWaterMarkMenu(e.value == "true");
    chagneCurDoc("videofilter.overlay.enabled", e.value);
}

function browserButtonImageFile()
{
    var e = $e("basic_watermark_path");
    var fileAtrr = Io.loadFile('Select', ['All Files | *.*', 'Image File(*.png,*.jpg,*.bmp)|*.png;*.jpg;*.bmp;'], false);
    if(!fileAtrr || !fileAtrr[0])
        return;
    e.value = fileAtrr[0].path;
    OnTextboxChange(e,"videofilter.overlay.image");
}

function browserButtonTextFile()
{
    var e = $e("basic_watermark_text");
    var fileAtrr = Io.loadFile('Select', ['All Files | *.*', 'Subtitle File(*.srt,*.sub)|*.tff;*.sub;'], false);
    if(!fileAtrr || !fileAtrr[0])
        return;
    e.value = fileAtrr[0].path;
    OnTextboxChange(e,"videofilter.overlay.text");
}


function GetMcexDefaultValue(curkey)
{
    if(!mccoreDoc)return "";
    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]);
            if(!node)
                break;
	    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;
		    }
		}	
	    }
	    if(type == "enum")
	    {
		var i = 0;
		var enumNode = DomHelper.findChildNode(node, "enum");
		while(enumNode)
		{
		    if(value == DomHelper.getNodeValue(enumNode))
		    {
			value = i;
			break;
		    }
		    else
			i++;
		    enumNode = DomHelper.getNextNode(enumNode);
		    enumNode = DomHelper.findNode(enumNode, "enum");
		}
	    }
	}
	return value;
    }
    return "";
}

function GetCurDocValue(root, key, nodename)
{
    var value="";
  
    var node = DomHelper.getNodeByAttribute(root.firstChild, nodename ? nodename : "node", "key", key);
    /*  if(nodename)
      {
        alert(node+"dd"+nodename);
        }*/
    if(node)
    {
        value = DomHelper.getNodeValue(node);
    }
    else
    {
        value = GetMcexDefaultValue(key);
    }
    return value;
}
function UpateBasicTabsHepler(root, type, id, key, byNum, nodename)
{
    var value=GetCurDocValue(root, key, nodename);
    if(byNum)
    {
        value = parseInt(value)*byNum;
    }
    switch(type)
    {
        case 0:
            $e(id).selectedIndex = parseInt(value);
            break;
        case 1:
            $e(id).value = parseInt(value);
            break;
        case 2:
            $e(id).value = value;
            break;
        case 3:
            $e(id).selectedIndex = (value == "true") ? 0 : 1;
            break;
    }
}

function OnBasicPageSplitRadioChange(e)
{
    //alert(e.selectedIndex);
    var index = parseInt(e.selectedIndex);
    if(index==0)
    {
        $e("basic_split_time_text").setAttribute("disabled",true);
        $e("basic_split_size_text").setAttribute("disabled",true);
	$e("basic_split_prefix").setAttribute("disabled",true);
        chagneCurDoc("extention.split.type","none");
    }
    else if(index==1)
    {
	 $e("basic_split_prefix").removeAttribute("disabled");
        $e("basic_split_time_text").removeAttribute("disabled");
        $e("basic_split_size_text").setAttribute("disabled",true);
        chagneCurDoc("extention.split.type","average");
        chagneCurDoc("extention.split.subfix","index");
        chagneCurDoc("extention.split.unitType","time");
        chagneCurDoc("extention.split.clips",$e("basic_split_time_text").value);
    }
    else if(index==2)
    {
	$e("basic_split_prefix").removeAttribute("disabled");
        $e("basic_split_time_text").setAttribute("disabled",true);
        $e("basic_split_size_text").removeAttribute("disabled");
        chagneCurDoc("extention.split.type","average");
        chagneCurDoc("extention.split.subfix","index");
        chagneCurDoc("extention.split.unitType","size");
        chagneCurDoc("extention.split.clips",parseInt($e("basic_split_size_text").value)*1024);
    }
}

function UpateBasicTabs()
{
	var fontpopup = $e("basic_popup_font");
	if(!fontpopup.hasChildNodes() && typeof(GetResponseFromServer2) != "undefined")
	{
		var fonts = GetResponseFromServer2("query.xml","type=60",false);
		if(fonts)
		{
			fonts = fonts.split(";");
			for(var i = 0; i < fonts.length; i++)
			{
				fontpopup.appendChild(HtmHelper.make('menuitem', {'label':fonts[i]}));
			}
		}
	}
	
    if(currentPrefItemDoc)
    {
        var root = DomHelper.getChildNode(currentPrefItemDoc);
		$e("basic_thumbnail_enable").removeAttribute("disabled");
		$e("basic_subtitle_mode").removeAttribute("disabled");
		$e("basic_watermark_enable").removeAttribute("disabled");
		$e("basic_split_type_group").removeAttribute("disabled");
                $e("basic_mapchannel_enable").removeAttribute("disabled");
        //subtitle
        UpateBasicTabsHepler(root, 0, "basic_subtitle_mode", "overall.subtitle.mode");
        EnableSubtitleMenu(parseInt($e("basic_subtitle_mode").value) != 0);
        UpateBasicTabsHepler(root, 1, "basic_subtitle_id", "overall.subtitle.sid");
        UpateBasicTabsHepler(root, 2, "basic_subtitle_font", "overall.subtitle.font");
        UpateBasicTabsHepler(root, 1, "basic_subtitle_pos", "overall.subtitle.pos");
        UpateBasicTabsHepler(root, 2, "basic_subtitle_lang", "overall.subtitle.slang");
        UpateBasicTabsHepler(root, 1, "basic_subtitle_size", "overall.subtitle.scale");
        UpateBasicTabsHepler(root, 1, "basic_subtitle_code", "overall.subtitle.encoding");
        //thumbnail
        UpateBasicTabsHepler(root, 3, "basic_thumbnail_enable", "videofilter.thumb.enabled");
        EnableThumbnailMenu($e("basic_thumbnail_enable").value == "true");
        UpateBasicTabsHepler(root, 1, "basic_thumbnail_width", "videofilter.thumb.width");
        UpateBasicTabsHepler(root, 1, "basic_thumbnail_height", "videofilter.thumb.height");
        UpateBasicTabsHepler(root, 0, "basic_thumbnail_format", "videofilter.thumb.format");
        UpateBasicTabsHepler(root, 1, "basic_thumbnail_start", "videofilter.thumb.start");
        UpateBasicTabsHepler(root, 1, "basic_thumbnail_end", "videofilter.thumb.end");
        UpateBasicTabsHepler(root, 1, "basic_thumbnail_num", "videofilter.thumb.count");
        UpateBasicTabsHepler(root, 3, "basic_thumbnail_stitch", "videofilter.thumb.stitching");
        UpateBasicTabsHepler(root, 0, "basic_thumbnail_align", "videofilter.thumb.align");
        UpateBasicTabsHepler(root, 2, "basic_thumbnail_prefix", "videofilter.thumb.name");
        UpateBasicTabsHepler(root, 2, "basic_thumbnail_folder", "videofilter.thumb.folder");
        //water mark
        UpateBasicTabsHepler(root, 3, "basic_watermark_enable", "videofilter.overlay.enabled");
        EnableWaterMarkMenu($e("basic_watermark_enable").value == "true");
        UpateBasicTabsHepler(root, 0, "basic_watermark_pos", "videofilter.overlay.pos");
        UpateBasicTabsHepler(root, 3, "basic_watermark_colored", "videofilter.overlay.colored");
        UpateBasicTabsHepler(root, 1, "basic_watermark_offsetX", "videofilter.overlay.offsetX");
        UpateBasicTabsHepler(root, 1, "basic_watermark_offsetY", "videofilter.overlay.offsetY");
        UpateBasicTabsHepler(root, 2, "basic_watermark_opaque", "videofilter.overlay.opaque");
        UpateBasicTabsHepler(root, 2, "basic_watermark_path" , "videofilter.overlay.image");
	UpateBasicTabsHepler(root, 2, "basic_watermark_text" , "videofilter.overlay.text");
        //split

        var group = $e("basic_split_type_group");
        var type = GetCurDocValue(root,"extention.split.type");
        if(type==null || type == "")type="none";
        var unit = GetCurDocValue(root,"extention.split.unitType");
        if(unit==null || unit == "")unit="time";
        if(type=="none")group.selectedIndex=0;
        else if(type=="average"&&unit == "time")
        {
            UpateBasicTabsHepler(root, 1, "basic_split_time_text", "extention.split.clips");
            group.selectedIndex=1;
        }
        else if(type=="average"&&unit == "size")
        {
            UpateBasicTabsHepler(root, 1, "basic_split_size_text", "extention.split.clips", 1/1024);
            group.selectedIndex=2;
        }
	UpateBasicTabsHepler(root, 2, "basic_split_prefix", "extention.split.prefix");
        OnBasicPageSplitRadioChange(group);
        //map channel
        UpateBasicTabsHepler(root, 3, "basic_mapchannel_enable", "channel.map.enable",null,"config");
        EnableMapChannelMenu($e("basic_mapchannel_enable").value == "true");
        UpateBasicTabsHepler(root, 0, "basic_mapchannel_left", "channel.map.left",null,"config");
        UpateBasicTabsHepler(root, 0, "basic_mapchannel_right", "channel.map.right",null,"config");
    }
}

function EnableBasicExtentionPanel(e)
{
	EnableSubtitleMenu(e);
	EnableThumbnailMenu(e);
	EnableWaterMarkMenu(e);
        EnableMapChannelMenu(e);
	if(!e)
	{
		$e("basic_thumbnail_enable").setAttribute("disabled",true);
		$e("basic_subtitle_mode").setAttribute("disabled",true);
		$e("basic_watermark_enable").setAttribute("disabled",true);
		$e("basic_split_type_group").setAttribute("disabled",true);
                $e("basic_mapchannel_enable").setAttribute("disabled",true);
	}
	else
	{
		$e("basic_thumbnail_enable").removeAttribute("disabled");
		$e("basic_subtitle_mode").removeAttribute("disabled");
		$e("basic_watermark_enable").removeAttribute("disabled");
		$e("basic_split_type_group").removeAttribute("disabled");
                $e("basic_mapchannel_enable").removeAttribute("disabled");
	}	
	
}

function EnableMapChannelMenu(e)
{
    var rows = $e("basic_mapchannel_rows");
    var row = DomHelper.findChildNode(rows, "row");
    while(row)
    {
        var node = DomHelper.getChildNode(row);
        while(node)
        {
           // alert(node.nodeName);
            if(node.nodeName == "textbox" || node.nodeName == "menulist"){
                if(e)
                {
                    node.removeAttribute("disabled");
                }
                else
                {
                    node.setAttribute("disabled", true);
                }
            }
            node = DomHelper.getNextNode(node);    
        }         
       
        row = DomHelper.getNextNode(row);
        row = DomHelper.findNode(row, "row");
    }
    
    $e("basic_mapchannel_enable").removeAttribute("disabled");
}

function OnMapChannelEnableMenu(e)
{
    EnableMapChannelMenu(e.value == "true");
    chagneCurDoc("channel.map.enable", e.value, "config");
}