function HPW_ContextItem(sId, sName, vIsHead, sClickFunc)
{
	var oDiv = document.createElement("DIV");
	oDiv.id = sId;
	oDiv.isDynamic = true;
	oDiv.unselectable = "on";
	if (vIsHead)
	{
		oDiv.className = "title2";
		oDiv.innerHTML = "<b>"+sName+"</b>";
	}
	else
	{
		oDiv.className = "cells";
		oDiv.innerHTML = "<a href='#' style='font-family:verdana, arial; font-size:9px;'>"+sName+"</a>";
		oDiv.onmouseover = new Function("HPW_ContextItemOver(this);");
		oDiv.onmouseout = new Function("HPW_ContextItemOut(this);");
	}
	if (sClickFunc) {oDiv.onmousedown = new Function(sClickFunc);}
	oDiv.style.borderRight = "0px";
	oDiv.style.borderTop = "0px";
	oDiv.style.borderBottom = "0px";
	oDiv.style.borderLeft = "10px solid";
	oDiv.style.padding = "2px";
	oDiv.style.fontFamily = "Verdana, Arial";
	oDiv.style.fontSize = "9px";
	return oDiv;
}

function HPW_ToggleContext(sEditorID) //, oElement) // , sComboPath, sMode)
{
	oTd = oTr = oTable = oImg = oAhref = null;
	var oContext = document.getElementById(sEditorID+"_ContextMenu");
	oContext.innerHTML = "<div id='"+sEditorID+"_contextItemView' class='title2' style='border-bottom-width:0px; border-left-width:0px; border-right-width:0px;'></div>";
	var oView = document.getElementById(sEditorID+"_contextItemView");
//	if (!sMode && sMode != 'hide' && oContext.style.visibility == "hidden")
	if (oContext.style.visibility == "hidden")
	{
		// TODO!!!!!!
		// var aMyChildNodes = HPW_GetCurrentNodes();
		var sTagName
		
		HPW_GetParentNodes(sEditorID);
		for (var i = 0; i<aParentNodes.length; ++i)
		{
			sTagName = aParentNodes[i].tagName.toUpperCase();
			switch (sTagName)
			{
				case "TD":			if (!oTd) {oTd = aParentNodes[i];}  break;
				case "TABLE":		if (!oTable) {oTable = aParentNodes[i];}  break;
				case "TR":			if (!oTr) {oTr = aParentNodes[i];}  break;
				case "IMG":			if (!oImg) {oImg = aParentNodes[i];} break;
				case "A":				oAhref = aParentNodes[i]; break;
				default:				break;
			}
		}
		
		/*if (document.activeElement)
		{
			sTagName = document.activeElement.tagName.toUpperCase();
			alert(sTagName);
			switch (sTagName)
			{
				case "IMG":			oImg = oElement; break;
				case "A":				oAhref = oElement; break;
				default:				break;
			}
		}*/

		/*if (oElement)
		{
			sTagName = oElement.tagName.toUpperCase();
			switch (sTagName)
			{
				case "IMG":			oImg = oElement; break;
				case "A":				oAhref = oElement; break;
				default:				break;
			}
		}*/

		// TODO!!!!!!
		/*if (aMyChildNodes)
		{
			for (var i=0; i<aMyChildNodes.length; ++i)
			{
				var sTagName = aMyChildNodes[i].tagName.toUpperCase();
				switch (sTagName)
				{
					case "IMG":			oImg = aMyChildNodes[i]; break;
					case "A":				oAhref = aMyChildNodes[i]; break;
					default:				break;
				}
			}
		}*/

		var ne;
		var vFound = 0;
		
		//oContext.insertBefore(HPW_ContextItem(sEditorID+"_Allgemein", "Allgemein", 1), oView);
		
		if (oImg)
		{
			vFound = 1;
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_headIMG", "Bild", 1), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_openIMGWindow", "Eigenschaften", 0, "HPW_OpenImgWindow('"+sEditorID+"', oImg);"), oView);
			//oContext.insertBefore(HPW_ContextItem(sEditorID+"_editIMG", "", 0, "popupShowEditIMG();"), oView);
		}
		if (oAhref)
		{
			vFound = 1;
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_headHYPER", "Hyperlink", 1), oView);  
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_removeHYPER", "Verlinkung entfernen", 0, "HPW_RemoveHyperLink(oAhref); HPW_ToggleContext('"+sEditorID+"');"), oView);  
			//oContext.insertBefore(HPW_ContextItem(sEditorID+"_editHYPER", "", 0, "popupShowEditA();"), oView);  
		}
		if (oTd)
		{
			vFound = 1;
			if ((oTd.nextSibling) || (oTd.previousSibling) || (oTr.nextSibling) || (oTr.getElementsByTagName("TD").length > 1) || (oTd))
			{
				oContext.insertBefore(HPW_ContextItem(sEditorID+"_headCells", "Zelle", 1), oView);
				oContext.insertBefore(HPW_ContextItem(sEditorID+"_editTD", "Formatieren", 0, "HPW_FormatTd('"+sEditorID+"', oTd);"), oView);
				if (oTd.nextSibling)		{oContext.insertBefore(HPW_ContextItem(sEditorID+"_mergeCellRight", "mit rechter verbinden", 0, "HPW_MergeTableCellHorizontal('"+sEditorID+"', oTd,'right'); HPW_ToggleContext('"+sEditorID+"');"), oView);}
				if (oTd.previousSibling)	{oContext.insertBefore(HPW_ContextItem(sEditorID+"_mergeCellLeft", "mit linker verbinden", 0, "HPW_MergeTableCellHorizontal('"+sEditorID+"', oTd,'left'); HPW_ToggleContext('"+sEditorID+"');"), oView);}
				//if (oTr.previousSibling)	{oContext.insertBefore(HPW_ContextItem(sEditorID+"_mergeCellTop", "mit drüber verbinden", 0, "HPW_MergeTableCellVertical(oTd,'top'); HPW_ToggleContext('"+sEditorID+"');"), oView);}
				
				// TODO: oTr.nextSibling stimmt nicht zur Abfrage sobald ein Rowspan bereits existiert!
				if (oTr.nextSibling)			{oContext.insertBefore(HPW_ContextItem(sEditorID+"_mergeCellBottom", "mit drunter verbinden", 0, "HPW_MergeTableCellVertical('"+sEditorID+"', oTd,'bottom'); HPW_ToggleContext('"+sEditorID+"');"), oView);}
				if (oTr.getElementsByTagName("TD").length > 1) {oContext.insertBefore(HPW_ContextItem(sEditorID+"_delCell", "Zelle löschen", 0, "HPW_DelTableCell('"+sEditorID+"', oTd, oTr, oTable); HPW_ToggleContext('"+sEditorID+"');"), oView);}
			}
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_headTD", "Spalte", 1), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_newColBefore", "Spalte links einfügen", 0, "HPW_InsertTableCol('"+sEditorID+"', oTd, oTable, 'left'); HPW_ToggleContext('"+sEditorID+"');"), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_newColAfter", "Spalte rechts einfügen", 0,"HPW_InsertTableCol('"+sEditorID+"', oTd, oTable,  'Right'); HPW_ToggleContext('"+sEditorID+"');") , oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_delCol", "Spalte löschen", 0, "HPW_DeleteTableCol('"+sEditorID+"', oTd, oTable); HPW_ToggleContext('"+sEditorID+"');"), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_headTR", "Zeile", 1), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_newRowAfter", "Zeile unten einfügen", 0,"HPW_InsertTableRow('"+sEditorID+"', oTr, 'afterEnd'); HPW_ToggleContext('"+sEditorID+"');"), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_newRowBefore", "Zeile oben einfügen", 0, "HPW_InsertTableRow('"+sEditorID+"', oTr, 'beforeBegin'); HPW_ToggleContext('"+sEditorID+"');"), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_delRow", "Zeile löschen", 0, "HPW_DeleteTableRow('"+sEditorID+"', oTr, oTable); HPW_ToggleContext('"+sEditorID+"');"), oView);
			if (oTd.name == "database")
			{
				oContext.insertBefore(HPW_ContextItem(sEditorID+"_headDatabase", "Datensatz", 1), oView);
				oContext.insertBefore(HPW_ContextItem(sEditorID+"_openDatabaseWindow", "bearbeiten", 0, "HPW_OpenDatabaseWindow('"+sEditorID+"', oTd);"), oView);
			}
		}
		/*if (oTable)
		{
			vFound = 1;
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_headTABLE", "", 1), oView);
			oContext.insertBefore(HPW_ContextItem(sEditorID+"_editTABLE", "", 0,"popupShowEditTABLE()"), oView);
		}*/
		/*if (vFound)
		{
			ne = HPW_ContextItem(sEditorID+"_contextBreaker", "", 1);
			ne.style.borderBottom = "1px solid #9F9F9F";
			ne.style.paddingTop = "5px";
			oContext.insertBefore(ne, oView);
		}*/
		var ShowHTML = parseInt(document.getElementById(sEditorID+"_ShowHTML").value);
		
		oPosition = HPW_GetElementPosition(document.getElementById(sEditorID+"_dummy")); // hole Position des IFrames
		temp = oPosition.x + 10 + document.frames[sEditorID].event.clientX;
		oContext.style.left = temp+"px";
		temp = oPosition.y + 10 + document.frames[sEditorID].event.clientY;
		oContext.style.top = temp+"px";
		
		document.getElementById(sEditorID+"_contextItemView").innerText = (ShowHTML == 1)?"HTML mode":"Normal Mode";
		oContext.style.visibility = "visible";
		oContext.focus();
		document.frames[sEditorID].event.returnValue = false;
	}
	else
	{
		/*var oDivs = oContext.getElementsByTagName("DIV");
		for (var i=0; i<oDivs.length; ++i)
		{
			if (oDivs[i].isDynamic)
			{
				oDivs[i].removeNode(true);
				--i;
			}
		}*/
		oContext.style.visibility = "hidden";
		//oTd = oTr = oTable = oImg = oAhref = null;
	}
}

function HPW_ContextItemOver(oItem)
{
	oItem.className = "cells2";
	oItem.style.borderRight = "0px";
	oItem.style.borderTop = "0px";
	oItem.style.borderBottom = "0px";
	oItem.style.borderLeft = "10px solid";
	oItem.style.padding = "2px";
	oItem.style.fontFamily = "Verdana, Arial";
	oItem.style.fontSize = "9px";
	oItem.style.cursor = "hand";
}

function HPW_ContextItemOut(oItem)
{
	oItem.className = "cells";
	oItem.style.borderRight = "0px";
	oItem.style.borderTop = "0px";
	oItem.style.borderBottom = "0px";
	oItem.style.borderLeft = "10px solid";
	oItem.style.padding = "2px";
	oItem.style.fontFamily = "Verdana, Arial";
	oItem.style.fontSize = "9px";
	oItem.style.cursor = "hand";
}
