var targetClicked;
var mainMenuOpen = false;

function clickMenu(evt)
{
	//when the custom menu is visible (and capturing events),
	//this handler runs after a click event.  if one of the
	//menu items is clicked, it takes appropriate action.
	//otherwise, it just hides the menu.
	var menuDiv = window.ContentFrame.document.getElementById("menu")
	if(menuDiv != null)
	{
		menuDiv.style.display = "none";
		var el;
		if(getSpecificBrowserName() == "Internet Explorer")
			el = evt.srcElement;
		else		      
			el = evt.target;
		if(el.id == "back")
			window.ContentFrame.history.back();
                else if(el.id == "forward")
			window.ContentFrame.history.forward();
		else if(el.id == "print")
			window.print();
		else if(el.id == "vsrc")
			vSrc();	
		else if(el.id == "debug")
			showDebugInfo();
		else if(el.id == "prop")
			showImageInfo(targetClicked);
		if(getSpecificBrowserName() == "Internet Explorer")
			menuDiv.releaseCapture();
		else
			window.ContentFrame.document.removeEventListener("mousedown",catchMozMouseClick, true);
		mainMenuOpen = false
	}
}

function toggleMenu(evt)
{
	var el;
	if(getSpecificBrowserName() == "Internet Explorer")
		el = evt.srcElement;
	else
		el = evt.target;
	if (el.className == "NSmenuItem")
		el.className = "NShighlightItem";
	else if (el.className == "NShighlightItem")
	    	el.className = "NSmenuItem";
	else if (el.className == "IEmenuItem")
	   	el.className = "IEhighlightItem";
	else if (el.className == "IEhighlightItem")
	    	el.className = "IEmenuItem";
}

function showMainMenu(evt)
{
	var menuDiv = window.ContentFrame.document.getElementById("menu")
	if(menuDiv != null && !window.parent.inputMenuOpen)
	{
		if(getSpecificBrowserName() == "Internet Explorer")
			targetClicked = evt.srcElement;
		else
			targetClicked = evt.target;
		var child = null;
		child = menuDiv.firstChild
		removeChildNodes(menuDiv);
		createMenu();
		menuDiv.style.left = evt.clientX+document.documentElement.scrollLeft;
		menuDiv.style.top = evt.clientY+document.documentElement.scrollTop;
		menuDiv.style.display = "";
		if(getSpecificBrowserName() == "Internet Explorer")
			menuDiv.setCapture();
		else
			window.ContentFrame.document.addEventListener("mousedown",catchMozMouseClick,false);
		mainMenuOpen = true;	
	}
}

function createMenu()
{
	var menuItemName = ["Back", "Forward", "---------------", "Print", "---------------", "View Source", "Content Info", "Properties"]

	var menuItemclass = ["menuItem", "menuItem", "separatorItem", "menuItem", "separatorItem", "menuItem", "menuItem", "menuItem"]
	var menuItemId = ["back", "forward", "separator", "print", "separator", "vsrc", "debug", "prop"]
	for(x=0; x< menuItemName.length; x++)

	{
		ele = window.ContentFrame.document.createElement("DIV");
		if(getSpecificBrowserName() == "Internet Explorer")

			ele.setAttribute("className", "IE" + menuItemclass[x]);
		else
			ele.setAttribute("class", "NS" + menuItemclass[x]);
		ele.setAttribute("id", menuItemId[x])
		if(menuItemclass[x] == "separatorItem" && getSpecificBrowserName() == "Internet Explorer")
			txt = window.ContentFrame.document.createElement("HR");
		else
			txt = window.ContentFrame.document.createTextNode(menuItemName[x]);
		ele.appendChild(txt);
		window.ContentFrame.document.getElementById("menu").appendChild(ele);
	}
}

function contextTwice(evt) 
{
  var clickedDiv;
  if(getSpecificBrowserName() == "Internet Explorer")
	clickedDiv = evt.srcElement;
  else
	clickedDiv = evt.target;
  if (clickedDiv == targetClicked)
  	showMainMenu(evt);
}

function catchMozMouseClick(evt)
{
	clickMenu(evt);
	if(getSpecificBrowserName() != "Internet Explorer")	
		toggleMenu(evt)	
	return;
}

function showDebugInfo()
{
	try

	{
		window.ContentFrame.setValues();
		var alertmsg = "Subject: " + window.ContentFrame.subname + "\nActivity: " + window.ContentFrame.actname + "\nPage: " + window.ContentFrame.page +
				       "\nAssess Subject: " + window.ContentFrame.assesssub + "\nAssess Activity: " + window.ContentFrame.assessact + "\nResolution: " + screen.width + "x" + screen.height +
				       "\nBrowser: " + getBrowserNameVersion();
		alert(alertmsg);
	}catch(e)
	{}
}

function showImageInfo(obj)
{
	try

	{
		var alertmsg = "";

		var type = "";
		var name = "";
		if(targetClicked.src != undefined)
		{
			var src = obj.src;
			var nameIndex = src.lastIndexOf("/");

			var typeIndex = src.lastIndexOf(".");
			if(nameIndex != -1)
			{
				name = src.substring(nameIndex+1, src.length)
			}
			if(typeIndex != -1)
			{
				type = src.substring(typeIndex+1 , src.length)
				type = type.toUpperCase();
			}
			alertmsg = name + "\n\n";

			alertmsg += "Type: " + type + " Image\n\n";
			alertmsg += "Address (URL): " + obj.src + "\n\n";
			alertmsg += "Dimensions: " + obj.width + " X " + obj.height + " pixels"
			alert(alertmsg);
		}else
		{
			alertmsg = "Protocol: HyperText Transfer Protocol\n\n";
			alertmsg += "Type: HTML Document\n\n";
			alertmsg += "Address (URL): " + window.ContentFrame.document.location + "\n\n";
			alert(alertmsg);
		}
	}catch(e)
	{}	
}

function vSrc()
{
	var password = "";
	if(window.parent.frames["macapiframe"] != null && parent.frames["macapiframe"].spw != null)
		password = parent.frames["macapiframe"].spw;
	
	var userReply = prompt("Please enter password.", "")
	if(userReply != null && userReply != num_to_str(password))
		alert("Incorrect Password.");		
	else if(userReply == num_to_str(password))
	{
		if(getSpecificBrowserName() == "Internet Explorer")
		{
			var nWin = window.open("");
			var src = window.ContentFrame.document.getElementById("html");
			if(src != null)
				nWin.document.write("<XMP>" + src.outerHTML + "</XMP>");
		}else
			var nWin = window.open("view-source:"+window.location.href);

	}
		
}

function removeChildNodes(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 str_to_num(val) 
{
	var str_in;

	var num_out = "";
	if(val == "") 
		return "null";
	else 
	{
		var str_in = escape(val);
		for(i = 0; i < str_in.length; i++) 
			num_out += str_in.charCodeAt(i) - 23;
		
		return num_out;
   	}
}

function num_to_str(val) 
{
	

	var num_in;

	var str_out = "";
	if(val == "") 
		return "null";
	else 
	{
		var num_out = val;  
		for(i = 0; i < num_out.length; i += 2) 
		{
			num_in = parseInt(num_out.substr(i,[2])) + 23;
			num_in = unescape('%' + num_in.toString(16));
			str_out += num_in;
		}
		return unescape(str_out);
   	}
}

