var inputTargetClicked;
var menuItemName = [];
var tarID;
var inputMenuOpen = false;

function clickInputMenu(evt)
{
	var menuDiv = window.ContentFrame.document.getElementById("inputmenu")
	if(menuDiv != null)
	{
		menuDiv.style.display = "none";
		var el;
		if(navigator.appName.indexOf( "Microsoft" ) != -1)
			el = evt.srcElement;
		else
			el = evt.target;
		for(var x=0; x<menuItemName.length; x++)
		{
			if(el.id == x)
				placeCharacter(menuItemName[x]);
		}
		if(navigator.appName.indexOf( "Microsoft" ) != -1)
			menuDiv.releaseCapture();
		else
			window.ContentFrame.document.removeEventListener("mousedown",catchInputMozMouseClick, true);
		inputMenuOpen = false;
	}
}

function showInputMenu(evt)
{
	var target = evt.target != null ? evt.target : evt.srcElement;
	if(target != null)
		tarID = target.id		
	if(menuItemName.length > 0)
	{
		var menuDiv = window.ContentFrame.document.getElementById("inputmenu")
		if(menuDiv != null && !window.parent.mainMenuOpen)
		{
			if(navigator.appName.indexOf( "Microsoft" ) != -1)
				inputTargetClicked = evt.srcElement;
			else
				inputTargetClicked = evt.target;
			var child = null;
			child = menuDiv.firstChild
			removeInputChildNodes(menuDiv);
			createInputMenu();
			var isSafari = navigator.userAgent.indexOf('Safari')>0;
			if(isSafari)
			{
				var ele = window.ContentFrame.document.getElementById(tarID);
				if(ele != null)
				{
					var pos = findPos(ele)
					menuDiv.style.left = pos[0];
					menuDiv.style.top = pos[1];
				}
			}else
			{
				menuDiv.style.left = evt.clientX+document.documentElement.scrollLeft;
				menuDiv.style.top = evt.clientY+document.documentElement.scrollTop;
			}
			menuDiv.style.display = "";
			if(navigator.appName.indexOf("Microsoft") != -1)
				menuDiv.setCapture();
			else
				window.ContentFrame.document.addEventListener("mousedown",catchInputMozMouseClick,false);
			inputMenuOpen = true;	
		}
	}
}

function createInputMenu()
{
	if(menuItemName.length > 0)
	{
		var longest = 0
		for(x=0; x< menuItemName.length; x++)
		{	
			var len = menuItemName[x].length;
			if(len > longest)
				longest = len;
			ele = window.ContentFrame.document.createElement("DIV");
			if(navigator.appName.indexOf( "Microsoft" ) != -1)

				ele.setAttribute("className", "inputMenuItem");
			else
				ele.setAttribute("class", "inputMenuItem");
			if(menuItemName[x].indexOf("\t", 0) == -1)
				ele.setAttribute("id", x)
			else
				ele.setAttribute("id", -1)
			txt = window.ContentFrame.document.createTextNode(menuItemName[x]);
			ele.appendChild(txt);
			window.ContentFrame.document.getElementById("inputmenu").appendChild(ele);
		}
		var menuDiv = window.ContentFrame.document.getElementById("inputmenu")
		if((10 * longest) > 30)
		{
			menuDiv.style.width = 10 * longest + "px";
			var theRules = new Array();	
			if(window.ContentFrame.document.styleSheets[0].cssRules) 
				theRules = window.ContentFrame.document.styleSheets[0].cssRules;	
			else if(window.ContentFrame.document.styleSheets[0].rules) 
				theRules = window.ContentFrame.document.styleSheets[0].rules;	
			for(var x=0; x<theRules.length; x++)
			{
				if(theRules[x].selectorText == ".inputMenuItem" || theRules[x].selectorText == ".inputmenuitem")
					theRules[x].style.width = 10 * longest + "px";	
				else if(theRules[x].selectorText == ".inputHighlightItem" || theRules[x].selectorText == ".inputhighlightitem")
					theRules[x].style.width = 10 * longest + "px";
			}

		}
		
	}
}

function removeInputChildNodes(obj)
{
	var nodelist = obj.childNodes;
	var c = nodelist.length;
	for(var i = 0; i < c; i++)
	{
		this_node = nodelist[0];
		obj.removeChild(this_node);
	}
}

function toggleInputMenu(evt)
{
	var el;
	if(navigator.appName.indexOf("Microsoft") != -1)
		el = evt.srcElement;
	else
		el = evt.target;
	if (el.className == "inputMenuItem")
		el.className = "inputHighlightItem";
	else if (el.className == "inputHighlightItem")
			el.className = "inputMenuItem";
}

function contextTwiceInput(evt)
{
  var clickedDiv;
  if(navigator.appName.indexOf("Microsoft") != -1)
	clickedDiv = evt.srcElement;
  else
	clickedDiv = evt.target;
  if (clickedDiv == inputTargetClicked)
	showInputMenu(evt);
}

function catchInputMozMouseClick(evt)
{
	clickInputMenu(evt);
	if(navigator.appName.indexOf("Microsoft") != -1)
		toggleInputMenu(evt)
	return;
}

function storeCaret(texta)
{
	if(texta != null)
		texta.caretPos = GetCaretPosition(texta);
}

function placeCharacter(character)
{
	var texta = window.ContentFrame.document.getElementById(tarID)
	if (texta != null)
	{
		var isSafari = navigator.userAgent.indexOf('Safari')>0;
		if(isSafari || texta.caretPos == 0)
			texta.value += character;
		else
		{
			var val = texta.value;
			var begin = val.substring(0,texta.caretPos)
			var end = val.substring(texta.caretPos,val.length)
			texta.value = begin + character + end;
		}
	}
}

function GetCaretPosition(ctrl)
{
	var CaretPos = 0;
	if(document.selection)
	{
		ctrl.focus();
		var Sel = document.selection.createRange();
		Sel.moveStart('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}else if(ctrl.selectionStart != null || ctrl.selectionStart == '0')
	{
		CaretPos = ctrl.selectionStart;
	}
	return CaretPos;
}

function createInputDiv()
{
	setMenuItems();
	var newdiv  = window.ContentFrame.document.createElement("div");
	newdiv.id = "inputmenu";
	window.ContentFrame.document.body.appendChild(newdiv);
	newdiv.style.position = "absolute";
	newdiv.style.display = "none";
	newdiv.style.width = "30px";
	newdiv.style.height = 20 * menuItemName.length + "px";
	newdiv.style.border = "solid #999999 1px";
	newdiv.style.zIndex = 1000;
	newdiv.style.backgroundColor = "#FFFFFF"
	if(newdiv.addEventListener)
	{
		newdiv.addEventListener('click',function (event) {
		  clickInputMenu(event);
		},true);
		newdiv.addEventListener('mouseover',function (event) {
		  toggleInputMenu(event);
		},true);
		newdiv.addEventListener('mouseout',function (event) {
		  toggleInputMenu(event);
		},true);
	}else if(newdiv.attachEvent)
	{
		newdiv.attachEvent('onclick',clickInputMenu);
		newdiv.attachEvent('onmouseover',toggleInputMenu);
		newdiv.attachEvent('onmouseout',toggleInputMenu);
	}
}

function setMenuItems()
{
	if(window.ContentFrame.rightClickMenu)
		menuItemName = window.ContentFrame.rightClickMenu;	
}

function inputcontextmenuisloaded()
{
	return true;
}