function applicationState()
{
	this.contextMenu = null;
	this.edit_node = null;
	this.edit_type = null;
}
var appState = new applicationState();

function appendMenu(menu_html)
{
	if(appState.contextMenu != null) removeMenu(); //appState.contextMenu.removeNode(true);
	
	document.body.insertAdjacentHTML("beforeEnd", menu_html);
	contextMenu = document.body.childNodes(document.body.childNodes.length-1);
	
	var l = event.clientX;
	if(document.body.scrollLeft > 0)
		l += parseInt(document.body.scrollLeft);
	
	var t = event.clientY;
	if(document.body.scrollTop > 0)
		t += parseInt(document.body.scrollTop);
	
	contextMenu.style.position = "absolute";
	contextMenu.style.left = l;
	contextMenu.style.top = t;
	
	appState.contextMenu = contextMenu;
  window.event.cancelBubble = true;
  window.event.returnValue = false;
}

function removeMenu() {
  var contextMenu

  if(appState.contextMenu != null) 
  {
    contextMenu = appState.contextMenu.removeNode(true)
    contextMenu = null
  }
}

function setMenu(node, node_type)
{
	menu_html = "<div id=\"simple_menu\">";	
	menu_html += "<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" width=\"60\" bgcolor=\"#000000\">";
	menu_html += "<tr bgcolor=#efefef height=20 onmouseover=\"setPointer(this, 'over', '#D0DCE0', '#efefef');\" onmouseout=\"setPointer(this, 'out', '#D0DCE0', '#efefef');\">";
	menu_html += "<td onclick=\"removeMenu(); onMenu('edit');\"><img src='imgs/b_edit.png'>&nbsp;编辑</td>";
	menu_html += "</tr>";
	menu_html += "<tr bgcolor=#efefef height=20 onmouseover=\"setPointer(this, 'over', '#D0DCE0', '#efefef');\" onmouseout=\"setPointer(this, 'out', '#D0DCE0', '#efefef');\">";
	menu_html += "<td onclick=\"removeMenu(); onMenu('del');\"><img src='imgs/b_drop.png'>&nbsp;删除</td>";
	menu_html += "</tr>";
	menu_html += "</table>";
	menu_html += "</div>";
	appendMenu(menu_html);
	appState.edit_node = node;
	appState.edit_type = node_type;
}

function onMenu(job)
{
	var obj = null;
        var dlg = null;
        var query_str = '';
	if(appState.edit_type == KisFlow.OBJECT_STATE)
        {
                dlg = 'dlg_state';
		obj = KisFlow.findState(appState.edit_node.id);
                query_str += "&state_id="+obj.getID();
                query_str += "&state_name="+obj.getText();
                query_str += "&state_title="+obj.getTitle();
                query_str += "&state_type="+obj.getStateType();
        }
	else if(appState.edit_type == KisFlow.OBJECT_ACTION)
        {
                dlg = 'dlg_action';
        	obj = KisFlow.findAction(appState.edit_node.id);
                query_str += "&action_id="+obj.getID();
                query_str += "&action_name="+obj.getText();
                query_str += "&action_title="+obj.getTitle();
                query_str += "&begin_state="+obj.begin_state.getText();
                query_str += "&end_state="+obj.end_state.getText();
        }
        
	if(job=='del')
	{
		if(confirm('确认删除: '+obj.getText()+" 节点？"))
			KisFlow.delNode(obj);
	}
	else if(job=='edit')
	{
		var json_str = OpenDialogRet("dialog/dialog.php?dlg="+dlg+query_str, 360, 240);
                var json_obj = eval("("+json_str+")");
                if(json_obj.obj_type == KisFlow.OBJECT_STATE)
                {
                        var obj = KisFlow.findState(json_obj.state_id);
                        if(!obj) return;
                        obj.setText(json_obj.state_name);
                        obj.setTitle(json_obj.state_title);
                        obj.setStateType(json_obj.state_type);
                }
                else if(json_obj.obj_type == KisFlow.OBJECT_ACTION)
                {
                        var obj = KisFlow.findAction(json_obj.action_id);
                        if(!obj) return;
                        
                        obj.setText(json_obj.action_name);
                        obj.setTitle(json_obj.action_title);
                }
	}
}
