//define a variable to store Ztree Info.
var zTree;
setting = {
		editable: false,//参数设置为true便可使用拖拽功能
		edit_renameBtn:false,
		edit_removeBtn:false,
		dragCopy:true,//拖拽功能为复制然后在目的菜单添加
		dragMove:false,//拖拽功能为先删除然后在目的菜单添加
		data:	{
					key: {
							children: "nodes"
						 },
			simpleData : {
							enable : false,
							idKey : "id",
							pIdKey : "pId",
							rootPId : null
						 }

				},
		view: 	{
					showLine: true,
					showTitle:false,
					showIcon:false,
					selectedMulti: false
				},
		callback:{
					onClick: zTreeOnClick,
					//beforeClick: zTreeBeforeClick,
					  onRightClick: OnRightClick
				 }
	};
$(document).ready(function()
{
	reloadTree();
	UpdateSdtcinfo();
	getBreadcrumb();
});
var preSelectedNode1;
//单击事件
function zTreeOnClick(event, treeId, treeNode) 
{
	if (treeId=="treeDemo") 
	{
		if (preSelectedNode1 == treeNode) 
		{
			zTree.cancelSelectedNode();
			preSelectedNode1 = null;
		} 
		else 
		{
			preSelectedNode1 = treeNode;
		}
	}
}
//确定右键菜单显示的坐标
function showRMenu1(type, x, y) 
{
	$("#AddMenu1").show();
	$("#AddMenu1").css({"top":y+"px", "left":(x+10)+"px", "display":"block"});
}
function showRMenuSpecial(type, x, y) 
{
	$("#SpecialMenu").show();
	$("#SpecialMenu").css({"top":y+"px", "left":(x+10)+"px", "display":"block"});
}
function showRMenuModi(type, x, y) 
{
	$("#ModifyMenu").show();
	$("#ModifyMenu").css({"top":y+"px", "left":(x+10)+"px", "display":"block"});
}
//hidden rightclick menu 
function hideRMenu()
{
	$("#AddMenu1").hide();
	$("#ModifyMenu").hide();
	$("#SpecialMenu").hide();
}
function OnRightClick(event,treeId, treeNode)
{   
    var count=0;
	var a1,a2;
	var tree_area = document.getElementById("tree_area"); 
	if(count==0)
	{
	var a1 = tree_area.scrollTop;
	count++;
	}
	if (!treeNode) 
	{
		zTree.cancelSelectedNode();			
		hideRMenu();
	} 
	else if (treeNode && !treeNode.noR) 
	{
		if (event.target.tagName != "a" && $(event.target).parents("a").length == 0) 
		{			
			zTree.cancelSelectedNode();	
			hideRMenu();
		} 
		else if(treeId =="treeDemo") 
		{
			zTree.selectNode(treeNode);
			var rightclick_node = zTree.getSelectedNodes();
			$("#CurDescId").val(rightclick_node[0].ID);
			if(rightclick_node[0].level==2)//Add  descriptor
			{		
				$("#Desc_OperateType").val("3");
				showRMenu1("node", event.clientX, event.clientY-tree_area.scrollTop+a1);
				$("#ModifyMenu").hide();
				$("#SpecialMenu").hide();
			}
			else if((rightclick_node[0].level==3)&&(rightclick_node[0].SDTSD==1))//modify and delete descriptor
			{
				$("#SDTSD").val("1");
				showRMenuSpecial("node", event.clientX, event.clientY-tree_area.scrollTop+a1);
				$("#AddMenu1").hide();
				$("#ModifyMenu").hide();
			}
			else
			{
				showRMenuModi("node", event.clientX, event.clientY-tree_area.scrollTop+a1);
				$("#AddMenu1").hide();
				$("#SpecialMenu").hide();
			}
		}
	}
}
//Initialization of zTree
function reloadTree() 
{
	//json格式例子zNodes和zNodes8
	//简单数据类型treeNodes1
		zTree = $.fn.zTree.init($("#treeDemo"),setting, SDTNodes);
}
function change()
{
	var curid = $("#Curid").val();
	var customtype= document.getElementById("customtype");
	var tempvalue=parseInt($("#service_type").val(),10);
	if(tempvalue == 17)
	{			
			customtype.style.display="";
			if(service_type[curid]<17)
			{
				$("#custom").val("");
			}
	}
	else
	{
		customtype.style.display="none";
	}
}

function UpdateSdtcinfo()
{ 	
	$("#curpsiid").val(psiid);	
	var row;
	var col;
	var i;
	var Element1 = document.getElementById("table_info7");
	var Element2 = document.getElementById("table_info8");
	if(document.all)
	{
		for(i = 0; i < service_num; i++)
		{
			row = Element1.insertRow();
			col = row.insertCell();
				col.innerHTML = i+1;
			col = row.insertCell();				
				col.innerHTML = service_id[i];
			col = row.insertCell();
				col.innerHTML =service_name[i];
			col = row.insertCell();
				col.innerHTML =provider_name[i];
			col = row.insertCell();
				col.innerHTML =service_type[i];
			col = row.insertCell();
				col.innerHTML =running_status[i];
			col = row.insertCell();
				col.innerHTML =ca_mode[i];
			col = row.insertCell();
				col.innerHTML =EIT_schedule_flag[i];
			col = row.insertCell();
				col.innerHTML =EIT_pre_follow_flag[i];
			col = row.insertCell();
				col.innerHTML = operateEdit1;			
		}
	}
	else
	{
		for(i = 0; i <  service_num; i++)
		{
			row = document.createElement("tr");
			col = document.createElement("td");
				col.innerHTML = i+1;
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML = service_id[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML =service_name[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML =provider_name[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML =service_type[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML =running_status[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML =ca_mode[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML =EIT_schedule_flag[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML =EIT_pre_follow_flag[i];
			row.appendChild(col);
			col= document.createElement("td");
				col.innerHTML = operateEdit1;
			row.appendChild(col);
			Element1.appendChild(row);
		}
	}
	$("#tsid").val(ts_id);
	$("#netid").val(original_network_id);
	$("#version_num").val(version_num);
}
function editproginfo(index)
{
	var id = parseInt(index,10)-1;
	$("#Curid").val(id);
	$("#OperateType").val("1");
	$("#char_code").val(char_code[id]);
	$("#Prog_Pos").val(service_index[id]);
	$("#Service_Id").val(service_id[id]);
	$("#service_name").val(service_name[id]);
	$("#provider_name").val(provider_name[id]);
	if(service_type[id]>16)
	{	
		$("#service_type").val(17);
		var obj = document.getElementById("customtype");
		obj.style.display="";
		$("#custom").val(service_type[id]);
	}
	else
	{
		$("#service_type").val(service_type[id]);
	}
	$("#Run_Status").val(running_status[id]);
	$("#Ca_Mode").val(ca_mode[id]);
	$("#EIT_schedule_flag").val(EIT_schedule_flag[id]);
	$("#EIT_pre_follow_flag").val(EIT_pre_follow_flag[id]);
	
	$("#proginfoedit").modal();
}