//-----------------------------------------------------------------------------
// PID.js
// 2007.09.12 - written by rk7138 for AT&T Communications
// 2007.10.08 - added ancestor, children, parent, sibling methods
// 2007.10.12 - added environs [] from the domain extracted for URI
// 2007.10.17 - added remainder of PIDs to have them redirect to a valid 
//				LeftNavigation PID
//-----------------------------------------------------------------------------
// ASSUMES
// - writeHTML.js is loaded [ uses getQueryParameter(); }
// - PID.infoArray[] is in the order of desired presentation
//
// METHODS
//   getInfo (givenId, bIsPID) returns Hash or undefined
//   getLastSiblingId (givenId, bIsPID) returns Array index or -1
//   getNextSiblingId (givenId, bIsPID) returns Array index or -1
//   getParentId (givenId, bIsPID) returns Array index or -1
//
//   getChildrenIds (givenId, bIsPID) returns Array or -1
//	 getSiblingIds (givenId, bIsPID) returns Array or -1; will return self node
//   getAllParentIds (givenId, bIsPID) returns Array or -1
//
//   - givenId needs to be the array index for PID.infoArray[]
//   - if bIsPID is true, then givenId can be the Teamsite PID key such as 10481
//   - the PID key may contain a hash anchor such as 10505#basic
//
// USAGE  
//   pidObj = PID.getInfo(10473, true); //RETURNs level, label, token, target, tooltip via PID
//   pidObj = PID.getInfo(10473, false); //RETURNs level, label, token, target, tooltip; via Node ID
//
// INFORMATION
//   used by Breadcrumbs.js generate breadcrumb tokens based on current PID viewed
//   used by LeftNavigation.js to generate navigation tree and to select link of current PID viewed
//
//   the PID.init() must be called from LeftNavigation.js or Breadcrumbs.js
//
//	 if the token is '', then use the value of the label when calling PID.getInfo(..., false)
//   
//   if the target is '', then link is for existing pid
//   if the target is #something, then link is for existing pid#something
//   if the target is {test:'a', prod:'b', tooltip: ''}}, then link is a redirect to test or prod contextually
//
//   set duplicate = true for the second or third occurrence of any entry to force menu to remember it's
//   last nodeId when it redraws; effectively showin the first instance for page content by still
//   focussing on the last nodeId for drawing the navigation tree

var PID = {
	isReady: false,
		
	DEFAULT_ENVIRON: 'prod',
	DEFAULT_PID: 10229,
	MAX_LEVEL: 3,
		
	infoHash: {},		
	infoArray: [
		{ pid:'10229', detail:{level: 1, 		duplicate: false, 	label: 'Home', 	token: 'AT&amp;T Web Hosting', target: '', tooltip: 'Visit the AT&T Web Hosting Home Page'}},
		{ pid:'10481', detail:{level: 1, 		duplicate: false, 	label: 'Shared Hosting', token: '', target: '', tooltip: 'Learn about Shared Hosting Plans'}},
		{ pid:'10505', detail:	{level: 2, 		duplicate: false, 	label: 'Windows', token: '', target: '', tooltip: 'Learn about Windows Hosting plans'}},
		{ pid:'10507', detail:		{level: 3, 	duplicate: false, 	label: 'eCommerce', token: '', target: '', tooltip: 'Learn about Windows eCommerce'}},
		{ pid:'10509', detail:		{level: 3, 	duplicate: false, 	label: 'Options', token: '', target: '', tooltip: 'See Windows plan options'}},	
		{ pid:'10491', detail:	{level: 2, 		duplicate: false, 	label: 'UNIX', token: '', target: '', tooltip: 'Learn about UNIX Hosting plans'}},
		{ pid:'10497', detail:		{level: 3, 	duplicate: false, 	label: 'eCommerce', token: '', target: '', tooltip: 'Learn about UNIX eCommerce plans'}},
		{ pid:'10499', detail:		{level: 3, 	duplicate: false, 	label: 'Domain Parking', token: '', target: '', tooltip: 'Learn about Domain Parking'}},
		{ pid:'10493', detail:		{level: 3, 	duplicate: false, 	label: 'Business Email', token: '', target: '', tooltip: 'Learn about Business Email'}},
		{ pid:'10501', detail:		{level: 3, 	duplicate: false, 	label: 'Options', token: '', target: '', tooltip: 'See UNIX plan options'}},			
		{ pid:'10410', detail:{level: 1, 		duplicate: false, 	label: 'Virtual Dedicated Server (VDS)', token: 'VDS', target: '', tooltip: 'Learn about Virtual Dedicated Servers'}},
		{ pid:'10365', detail:	{level: 2, 		duplicate: false, 	label: 'Windows VDS', token: '', target: '', tooltip: 'Learn about Windows VDS'}},
		{ pid:'10369', detail:		{level: 3, 	duplicate: false, 	label: 'Windows VDS Basic', token: 'Basic', target: '', tooltip: 'Learn about the Windows VDS Basic plan'}},
		{ pid:'10371', detail:		{level: 3, 	duplicate: false, 	label: 'Windows VDS Enhanced', token: 'Enhanced', target: '', tooltip: 'Learn about the Windows VDS Enhanced plan'}},
		{ pid:'10373', detail:		{level: 3, 	duplicate: false, 	label: 'Windows VDS Premium', token: 'Premium', target: '', tooltip: 'Learn about the Windows VDS Premium plan'}},
		{ pid:'10367', detail:		{level: 3, 	duplicate: false, 	label: 'Compare Windows VDS Plans', token: 'Compare Plans', target: '', tooltip: 'Compare Windows VDS plans'}},			
		{ pid:'10361', detail:	{level: 2, 		duplicate: false, 	label: 'Linux VDS', token: '', target: '', tooltip: 'Learn about Linux VDS'}},
		{ pid:'10363', detail:		{level: 3, 	duplicate: false, 	label: 'VDS Technology', token: 'Technology', target: '', tooltip: 'Learn about Linux VDS'}},
		{ pid:'10540', detail:		{level: 3, 	duplicate: false, 	label: 'Linux VDS Basic', token: 'Basic', target: '', tooltip: 'Learn about Linux VDS Basic plan'}},
		{ pid:'10542', detail:		{level: 3, 	duplicate: false, 	label: 'Linux VDS Enhanced', token: 'Enhanced', target: '', tooltip: 'Learn about Linux VDS Enhanced plan'}},
		{ pid:'10357', detail:		{level: 3, 	duplicate: false, 	label: 'Linux VDS Premium', token: 'Premium', target: '', tooltip: 'Learn about Linux VDS Premium plan'}},
		{ pid:'10359', detail:		{level: 3, 	duplicate: false, 	label: 'Compare Linux VDS Plans', token: 'Compare Plans', target: '', tooltip: 'Compare Lunyx VDS plans'}},
		{ pid:'10321', detail:		{level: 3, 	duplicate: false, 	label: 'Support Documentation Library', token: 'Downloads', target: '', tooltip: 'Access Linux VDS Support Documents'}},		
		{ pid:'10375', detail:{level: 1, 		duplicate: false, 	label: 'Managed Dedicated Server (MDS)', token: 'MDS', target: '', tooltip: 'Learn about Managed Dedicated Servers'}},
		{ pid:'10377', detail:	{level: 2, 		duplicate: true, 	label: 'Windows MDS', token: '', target: '', tooltip: 'Learn about Windows MDS'}},
		{ pid:'10377', detail:		{level: 3, 	duplicate: true, 	label: 'MDS Advantage', token: '', target: '', tooltip: 'Learn the advantages of MDS plans'}},
		{ pid:'10391', detail:		{level: 3, 	duplicate: false, 	label: 'Windows MDS Basic', token: 'Basic', target: '', tooltip: 'Learn about Windows VDS Basic plan'}},
		{ pid:'10393', detail:		{level: 3, 	duplicate: false, 	label: 'Windows MDS Enhanced', token: 'Enhanced', target: '', tooltip: 'Learn about Windows VDS Enhanced plan'}},
		{ pid:'10395', detail:		{level: 3, 	duplicate: false, 	label: 'Windows MDS Premium', token: 'Premium', target: '', tooltip: 'Learn about Windows VDS Premium plan'}},
		{ pid:'10397', detail:		{level: 3, 	duplicate: false, 	label: 'Compare Windows MDS Plans', token: 'Compare Plans', target: '', tooltip: 'Compare Windows MDS Plans'}},
		{ pid:'10377', detail:	{level: 2, 		duplicate: true, 	label: 'Linux MDS', token: '', target: '', tooltip: 'Learn about Linux MDS'}},
		{ pid:'10377', detail:		{level: 3, 	duplicate: true, 	label: 'MDS Advantage', token: '', target: '', tooltip: 'Learn the advantages of MDS plans'}},
		{ pid:'10430', detail:		{level: 3, 	duplicate: false, 	label: 'Linux MDS Basic', token: 'Basic', target: '', tooltip: 'Learn about the Linux MDS Basic plan'}},
		{ pid:'10382', detail:		{level: 3, 	duplicate: false, 	label: 'Linux MDS Enhanced', token: 'Enhanced', target: '', tooltip: 'Learn about the Linux MDS Enhanced plan'}},
		{ pid:'10385', detail:		{level: 3, 	duplicate: false, 	label: 'Linux MDS Premium', token: 'Premium', target: '', tooltip: 'Learn about the Linux MDS Premium plan'}},
		{ pid:'10388', detail:		{level: 3, 	duplicate: false, 	label: 'Compare Linux Plans', token: 'Compare Plans', target: '', tooltip: 'Compare Linux MDS plans'}},
		{ pid:'10321', detail:		{level: 3, 	duplicate: false, 	label: 'Support Documentation Library', token: 'Downloads', target: '', tooltip: 'Access Linux MDS Support Documents'}},
		{ pid:'10479', detail:{level: 1, 		duplicate: false, 	label: 'Promotions', token: '', target: '', tooltip: 'Learn about promotions'}},
		{ pid:'10483', detail:	{level: 2, 		duplicate: false, 	label: 'Web Advantage', token: '', target: '', tooltip: 'Learn about the Web Advantage discount'}},
		{ pid:'10618', detail:{level: 1, 		duplicate: false, 	label: 'Support', token: '', target: '', tooltip: 'Get support for your plan'}},
		{ pid:'10664', detail:	{level: 2, 		duplicate: false, 	label: 'Shared Hosting', token: '', target: '', tooltip: 'Get support for Shared Hosting plans'}},
		{ pid:'10411', detail:	{level: 2, 		duplicate: false, 	label: 'Virtual Dedicated Server', token: '', target: {dev:'webhosting?pid=10664#vds', test:'webhosting?pid=10664#vds', prod:'webhosting?pid=10664#vds'}, tooltip: 'Get support for VDS plans'}},
		{ pid:'10435', detail:	{level: 2, 		duplicate: false, 	label: 'Managed Dedicated Server', token: '', target: {dev:'webhosting?pid=10664#mds', test:'webhosting?pid=10664#mds', prod:'webhosting?pid=10664#mds'}, tooltip: 'Get support for MDS plans'}},
		{ pid:'10405', detail:	{level: 2, 		duplicate: false, 	label: 'Contact Us', token: '', target: '', tooltip: 'Contact Us'}},
		{ pid:'10333', detail:	{level: 2, 		duplicate: false, 	label: 'Legal Policies', token: '', target: '', tooltip: 'Access our Legal Policies'}},		
		{ pid:'NNNN4', detail:{level: 1, 		duplicate: false, 	label: 'Sign Up Now', token: '', target: {dev:'http://csdev18.sbc.com:9086/SMWeb/signup.jsp', test:'http://whc-test.sbcis.sbc.com:5100/SMBWeb/sign-up.jsp', prod:'https://webhosting.att.com/SMBWeb/sign-up.jsp'}, tooltip: 'Sign up for a plan'}},
		{ pid:'10479', detail:{level: 1, 		duplicate: false, 	label: 'Other Offers & Services', token: '', target: '', tooltip: 'See Other Offers & Services'}},
		{ pid:'10657', detail:{level: 1, 		duplicate: false, 	label: 'Site Map', token: '', target: '', tooltip: 'See the site map'}}
	],
		
	REDIRECTS: {
		'10323': 	{ pid: 10618, token: 'Glossary' },
		'10328': 	{ pid: 10618, token: 'Shared' },
		'10336': 	{ pid: 10333, token: 'AUP' },
		'10339': 	{ pid: 10333, token: 'DNRA' },
		'10341': 	{ pid: 10333, token: 'T&amp;C' },
		'10408': 	{ pid: 10618, token: 'Control Panel Tutorial' },
		'10413': 	{ pid: 10664, token: 'Urchin' },
		'10417': 	{ pid: 10664, token: 'SSL' },
		'10421': 	{ pid: 10664, token: 'SPAM' },
		'10423': 	{ pid: 10664, token: 'POP3' },
		'10433': 	{ pid: 10664, token: 'Overflow' },
		'10437': 	{ pid: 10618, token: 'About' },
		'10439': 	{ pid: 10664, token: 'FTP' },
		'10441': 	{ pid: 10664, token: 'E-Mail' },
		'10443': 	{ pid: 10664, token: 'eCommerce' },
		'10445': 	{ pid: 10664, token: 'EasySite' },
		'10447': 	{ pid: 10664, token: 'Domain' },
		'10449': 	{ pid: 10664, token: 'Database' },
		'10451': 	{ pid: 10664, token: 'Control Panel' },
		'10453': 	{ pid: 10664, token: 'CGI Scripts' },
		'10455': 	{ pid: 10664, token: 'CGI Library' },
		'10459': 	{ pid: 10664, token: 'Billing' },
		'10461': 	{ pid: 10664, token: 'Account' },
		'10463': 	{ pid: 10664, token: 'Windows 2003' }
	},
		
	environs: {
		'webhosting.att.com': 'prod',
		'test-www.att.com': 'test',
		'stage-www.att.com': 'test',
		'att.com': 'prod',
		'www.att.com': 'prod'
	},
		
	getDestinationUrl: function (givenDetailsObj){
		var uriObj = window.location.toString().split('?');
		var URI = uriObj[0];
		var parms = uriObj[1];
		var domain = URI.match( /:\/\/(www\.)?([^\/:]+)/ );
    	domain = domain[2] ? domain[2]:'';
		
		var entryPid = givenDetailsObj['pid'];
		var environ = PID.environs[domain];

		if (!environ){ environ = PID.DEFAULT_ENVIRON; }

		var label = givenDetailsObj['label'];		
		var targetObj = givenDetailsObj['target'];
		var targetStr;
		var finalUrl = URI + '?' + 'pid=' + entryPid;
		
		if (typeof targetObj == 'object'){ return targetObj[environ]; }
		else { targetStr = targetObj; }	

		if (targetStr){
			if (targetStr.indexOf('http') != -1){ return targetStr; }
			else { finalUrl  += '#' + targetStr; }
		}
		
		return finalUrl;		
	},
		
	getInfo: function (givenId, bIsPID){
		var detailObj = {};
		var nodeId = givenId;	

		if (!givenId){ nodeId = 0; }		
		if (bIsPID){ nodeId = PID.infoHash[givenId]; }
		if (!PID.infoArray[nodeId]){ nodeId = 0; }
		
		var detailObj = clone( PID.infoArray[nodeId]['detail'] );
		var entryPid = PID.infoArray[nodeId]['pid'];	

		detailObj['nodeId'] = nodeId;
		detailObj['pid'] = entryPid;
				
		return detailObj;
	},
		
	getNodeId: function (givenId, bIsPID, bCheckNext, bGetParent, bGetChildren){
		var resultArray = [];
		var found = false;
		var nodeId = parseInt(givenId);
		
		if (bIsPID){ nodeId = PID.infoHash[givenId]; }
		if (nodeId < 0){ return -1; }
		if (nodeId >= (PID.NUM_NODES - 1)){ return -1; }	
				
		var myLevel = PID.infoArray[nodeId]['detail']['level'];		
		var checkId;
		
		if (bCheckNext){ checkId = nodeId + 1; }
		else { checkId = nodeId - 1; }
			
		while(!found){
			if (checkId < 0){ break; }
			if (checkId >= (PID.NUM_NODES - 1)){ break; }
			
			var checkDetail = PID.infoArray[checkId]['detail'];
			var checkLevel = checkDetail['level'];
			
			if (bGetChildren){
				if (myLevel == checkLevel){ break; }			
				if (myLevel > checkLevel){ break; }
				if (myLevel < checkLevel){
					if (myLevel == (checkLevel -1)){ resultArray.push(checkId); }
				}
			}
			else {
				if (myLevel == checkLevel){ if (!bGetParent){ found = true; break; }}			
				if (myLevel > checkLevel){ if (bGetParent){ found = true; break; } else { break; }}
			}

			if (bCheckNext){ checkId++; }
			else { checkId--; }
		}
		
		if (bGetChildren){
			return resultArray;
		}
		else {
			if (found){ return checkId; }
			else { return -1; }			
		}
	},
		
	getNextSiblingId: function (givenId, bIsPID){
		return PID.getNodeId(givenId, bIsPID, true, false, false);
	},
		
	getLastSiblingId: function (givenId, bIsPID){
		return PID.getNodeId(givenId, bIsPID, false, false, false);
	},
		
	getParentId: function (givenId, bIsPID){
		return PID.getNodeId(givenId, bIsPID, false, true, false);
	},
		
	getChildrenIds: function(givenId, bIsPID){	
		return PID.getNodeId(givenId, bIsPID, true, false, true);		
	},
		
	getAllParentIds: function (givenId, bIsPID){
		var parentList = [];
		var result = PID.getParentId(givenId, bIsPID);
		
		if (result > 0){ parentList.push(result); }
		while (true){
			result = PID.getParentId(result, false);
			
			if (result > -1){ parentList.push(result); }
			else { break; }	
		}
		
		return parentList;			
	},
		
	getSiblingIds: function (givenId, bIsPID){
		var parentId = PID.getParentId(givenId, bIsPID);
		
		if (parentId > -1){ return PID.getChildrenIds(parentId, false); }
		else { return -1; }
	},
		
	buildInfoHash: function (){
		var deepestLevel = 0;
		var lastLevel = 0;
		var lastId = -1;
		var parentId = 0;	
		var numNodes = PID.infoArray.length;
		
		for (var i = 0; i < numNodes; i++){
			var pid = PID.infoArray[i]['pid'];
			var level = PID.infoArray[i]['detail']['level'];
			var target = PID.infoArray[i]['detail']['target'];
			var key = pid;
			
			if (level > deepestLevel){ deepestLevel = level; }
			if (typeof target == 'string' && target != ''){ key = pid + "#" + target; }
			
			PID.infoHash[key] = i;
		}
		
		PID.MAX_LEVEL = deepestLevel;
		PID.NUM_NODES = numNodes;
		

	},
			
	init: function (){
		if (!PID.isReady){ PID.buildInfoHash();	PID.isReady = true; }	
	}
}
