// Licensed Materials - Property of IBM, 5724-B88, (C) Copyright IBM Corp.  2002 - All Rights reserved.

var NOSAMETIME = -1;
var OFFLINE = 0;
var ACTIVE = 32;
var AWAY = 64;
var AWAY2 = 96;
var NODISTURB = 128;
var ACTIVEMOBILE = 544;
var AWAYMOBILE = 608;
var linkId;
var STStatus;
var SametimeRunning = true;
var paErrorMsg;
var paProfileNotExistMsg;
var conferenceName;
var inviteText;
var paPeople = null;
var paPublicGroups = null;

function loadMenus(event) 
{
	// 1. define menus
	var peoplemenu_0 = new WPSMenu('peoplemenu_0');
	
	// isSametimeEnabled is defined in the PeopleInit tag, and reflects whether ST is enabled
	//      in the CSEnvironment.properties file.
	// SametimeRunning is defined in this file and defaults to true, assuming that ST
	//      is running (that STLinks is loaded).  If ST is not running, then person names will
	//      be written out using paServiceWriteSametimeLink below rather than writeSametimeLink
	//      from STLinks, and paServiceWriteSametimeLink sets SametimeRunning to false.
	
	if (isSametimeEnabled && SametimeRunning)
		peoplemenu_0.addMenuItem(personMenuItem_chat,"STLinksCreateIM(linkId);","PAisChattingOK();");
		
	//if (personMenuItems[linkId].emailaddress != "")  regardless this person has an email address or not, show this menu anyway so that people can type in the email address
		peoplemenu_0.addMenuItem(personMenuItem_email,"PAsendEmail();","true");
		
	if (isDiscoverEnabled && personMenuItems[linkId].profile != "")
		peoplemenu_0.addMenuItem(personMenuItem_showprofile,"PAshowProfile();","true");
		
	if (isDiscoverEnabled && personMenuItems[linkId].documents != "")
		peoplemenu_0.addMenuItem(personMenuItem_finddocs,"PAshowDocuments();","true");

	if (isSametimeEnabled && SametimeRunning)
		peoplemenu_0.addMenuItem(personMenuItem_addtostlist,"PAshowAddToContactsUI([linkId], null);","true");

	//see if there are more menu items that people added dynamically
	if (typeof peoplemenu_more != "undefined")
	{
		var items = peoplemenu_more.items;
		var actions = peoplemenu_more.actions;

		for (i = 0; i < items.length; i++)
		{
			peoplemenu_0.addMenuItem(items[i], actions[i], "true");
		}
	}

	// 2. hook up menus to a context
	window.peopleMenuContext = new WPSMenuContext();
	window.peopleMenuContext.addMenu(peoplemenu_0);
	window.peopleMenuContext.showMenu('peoplemenu_0', event)
} 

// Note that this is only to determine whether we should enable (gray it out or not) 
// the "Chat..." menu item for a person(s).
function PAisChattingOK() 
{
	var b = false;
	if (typeof STStatus != "undefined" && STStatus != null)
		b = (STStatus == ACTIVE || STStatus == AWAY || STStatus == AWAY2 || STStatus == ACTIVEMOBILE || STStatus == AWAYMOBILE);
	return b;
}

function PAaddtoSTList(group) 
{
	STLinksAddToContactList(escapeSlashComma(linkId), group);
}


function PAsendEmail()
{
	window.location=("mailto:"+personMenuItems[linkId].emailaddress);
}

function PAshowProfile()
{
	window.open(personMenuItems[linkId].profile);
}

function PAshowDocuments()
{
	// DS server only needs the CN from a Domino name to do the search correctly.
	var fSlash = linkId.indexOf("/");
	if (fSlash > -1)
	{
		var dsName = linkId.substr(0, fSlash);
		window.open(personMenuItems[linkId].documents+"?scope=2&search=" + dsName.replace(/\+/g, " ") + "&hidebns=1");
	}
	else
    {
	    window.open(personMenuItems[linkId].documents+"?scope=2&search=" + linkId.replace(/\+/g, " ") + "&hidebns=1");
	}
}


//
// HELPERS
//

var pa_stlinksPrivateGroups = new Array();

// String[] people
// String[] publicGroups
// You must provide either people or publicGroups, or both.
function PAshowAddToContactsUI(people, publicGroups)
{
	// call stlinks to get the group
	STLinksGetPrivateGroups();
}
function STLinksPrivateGroupsReceived(groups)
{
	openAddToSTListJSP(groups);
}
function STLinksPrivateGroupsFailed(reason)
{
	openAddToSTListJSP(null);
}
function openAddToSTListJSP(groups) 
{
	pa_stlinksPrivateGroups = new Array();
	if (groups != null && groups.length > 0)
	{
		pa_stlinksPrivateGroups = groups.split(";");
	}
	else
	{
		if (typeof pa_addtostlist_defaultGroup != "undefined")
			pa_stlinksPrivateGroups[0] = pa_addtostlist_defaultGroup;
		else
			pa_stlinksPrivateGroups[0] = "Work";
	}
	window.open(personMenuItem_addtostlisturl, "pa_addtostlist", "resizable=yes,width=400,height=500,toolbar=no,status=no,menubar=no", true);
}
function STLinksAddToContactListFailed(reason)
{
  alert(personMenuItem_addtostlistErr + ": " + reason);
}
function getStlinksPrivateGroups()
{
	return pa_stlinksPrivateGroups;
}
function getAddToSTListPerson()
{
	return linkId;
}
function STLinkClicked(person, personLinkText, status, event)
{
	linkId = person;
	
	if (typeof STLinksCreateIM == "function")
		STLinksCreateIM(escapeSlashComma(linkId));
}

// This function has the same name as the one in stlinks.js (provided by Sametime).
// It is executed when loading stlinks.js is not successful in the cases where Sametime
// is not enabled, Sametime server name is not found, or Sametime server is not running.
// Use this function to enable a person name link (hyperlink) so that the person's menu
// can be invoked by clicking on the person name link.
function paServiceWriteSametimeLink(userName, displayName, bResolve, options)
{
	if (typeof displayName == "undefined")
		displayName = userName;
	SametimeRunning = false;     // this variable tells later process that Sametime is not found or not running
	var act= "STLinkClicked('" + userName.replace(/'/g, "\\'").replace(/"/g, "&quot;") + "', '', NOSAMETIME, event)";
	var s =" href=\"javascript:" + act + "\" onclick=\"" + act + ";return false;\"";
	s = "<span> <A " + s + ">" + displayName + "</A> </span>";
	document.write(s);
}

function escapeQuote(str)
{
	return str.replace(/'/g, "\\'");
}
