//------------------------------------------------------------------------------------------
// BrainJar Menu Wrapper  (c) netAdapt Ltd.
//------------------------------------------------------------------------------------------
var g_tabMenuXmlDoc;
var g_tabMenuEvent;
var g_ajaxMenuItemId;

function WriteMenuBar( userId, bgColor, isLoggedIn, isSub, username, nMsgsWaiting, nGamesWaiting, cacheBuster )
{

	MenuBarOpen( bgColor );
	if ( isLoggedIn )
	{
		HomeIconAdd( );
		DropMenuAddStatic( 'My Home', 		'myHomeTabMenu' );
		DropMenuAddStatic( 'My Settings', 	'mySettingsTabMenu' );		
	}
	else
	{
		DropMenuAddStatic( 'Getting Started', 'starTabMenu' );
	}
	
	DropMenuAddStatic( 'New Game', 		'newGameTabMenu');
	DropMenuAddStatic( 'Forums', 'forumsTabMenu' );
	
	if ( isLoggedIn )
	{
		DropMenuAddAjax( 'My Clubs', 'clubsTabMenu',  '/xml/xtabmenu/clubsxml.php' );
		DropMenuAddAjax( 'My Clans', 'clansTabMenu',  '/xml/xtabmenu/clansxml.php' )	
	}
	DropMenuAddStatic( 'Community', 'communityTabMenu' );
	DropMenuAddStatic( 'Help', 'helpTabMenu' );



	if ( isLoggedIn )
	{
		LoggedInInfoAdd( username, nMsgsWaiting, nGamesWaiting );
	}
	else
	{
		LoginFormAdd( cacheBuster );
	}

	MenuBarClose( );

	//
	//Add Menu contents
	//

	if ( isLoggedIn )
	{
		AddMyHomeMenuContent( isSub );
		AddMySettingsContent( );
		AddNewGameMenuContent( );
	}
	else
	{
		AddGettingStarted( );
	}
	AddHelpMenuContent( isLoggedIn );

	AddForumMenuContent( isLoggedIn );
	AddCommunityContent( isLoggedIn );
	
	AddClansMenuContent( );
	AddClubsMenuContent( );
	
	//AddShopMenuContent( );	
}

function MenuBarOpen( bgColor )
{
	document.write('<div class="menuBar" style="background-color:' + bgColor +  '">');
	document.write('<table border="0" cellpadding="0" cellspacing="0" height="20px"><tr><td>&nbsp;&nbsp;</td>');
}

function MenuBarClose( )
{
	document.write('</tr></table> ');
	document.write('</div>');
}


function HomeIconAdd( )
{
	document.write('<td nowrap><a href="/myhome/myhome.php"><img src="/img/uix/myhome.gif" width="16px" height="16px" border="0" alt="my home"></a>&nbsp;&nbsp;&nbsp;</td>');
}

function LoginFormAdd( randId )
{	
	document.write('<td align="right" width="100%" valign="center" nowrap > ');
    document.write('<form name="loginform" action="/myhome/myhome.php?cachebustera=a-'  +  cacheBuster + '" method="post">' );
	document.write('<span class="navbarlabel">Email</span> <input type="text" class="inputtext" name="loginemail" size="18" class="navbarinputtext">');
    document.write('<span class="navbarlabel">Password</span> <input type="password" name="loginpassword" size="18" class="navbarinputpassword" > ');
	document.write('<input type="hidden" name="action" value="logout"> ');
	document.write('<input type="hidden" name="cachebusterb" value="b-' +  cacheBuster + '"> ');
	document.write('<input type="submit" value="Login" class="navbarbutton"> ');
	document.write('</form> ');
    document.write('</td> ');
	
}

function LoggedInInfoAdd( username, nMsgsWaiting, nGamesWaiting )
{
	document.write('<td align=right width=100% nowrap class="login">');
	document.write( username );
	if ( nMsgsWaiting > 0 )
	{
		document.write(' : <a class="loggedInInfo" href="/mymessages/msgbox.php">' + nMsgsWaiting + ' messages unread</a><a href="#" onclick="XPopMessagesUnread( event  );return false;"><img src="/img/uix/xmenupop.gif" hspace="5"  border="0" width="16" height="8"></a>: '); //
	}
	else
	{
		document.write(' : ');
	}
	document.write('<a class="loggedInInfo" href="/core/gameserve.php">' + nGamesWaiting + ' games waiting</a><a href="#" onclick="XPopGamesUrgent( event  );return false;"><img src="/img/uix/xmenupop.gif" hspace="5"  border="0" width="16" height="8"></a>: ');	//
	document.write('<a href="#" onclick="XPopGamesWatched( event  );return false;"><img src="/img/uix/xmenuwatch.gif" hspace="1"  border="0" width="16" height="11"></a>: ');	//
	document.write('<a class="loggedInInfo" href="/core/logout.php?">log out</a>' );
	document.write('&nbsp;&nbsp;</td> ');
}

function DropMenuAddStatic( menuItemText, tabMenuId )
{
	document.write('<td nowrap >');
	document.write('<a class="menuButton" href="#" onclick="return buttonClick(event, \'' + tabMenuId + '\');" onmouseover="buttonMouseover(event, \'' + tabMenuId + '\');" >&nbsp;' + menuItemText + '&nbsp;</a>');
	document.write('&nbsp;&nbsp;&nbsp;');
	document.write('</td>');
	document.write('<td width="20px">&nbsp;</td>');
}

function DropMenuAddAjax( menuItemText, tabMenuId, url )
{	
	document.write('<td nowrap >');
	document.write('<a class="menuButton" href="#" onclick="PopulateMenuFromXml(event, \'' + tabMenuId + '\', \'' + escape( url ) + '\' );return buttonClick(event, \'' + tabMenuId + '\');" onmouseover="if( buttonMouseover(event, \'' + tabMenuId + '\')){PopulateMenuFromXml(event, \'' + tabMenuId + '\', \'' + escape( url ) + '\' )};">&nbsp;' + menuItemText + '&nbsp;</a>');	
	document.write('&nbsp;&nbsp;&nbsp;');
	document.write('</td>');
	document.write('<td width="20px">&nbsp;</td>');
}

function DropMenuCreate( tabMenuId )
{	
	var dropMenu = document.createElement('div');			
	dropMenu.id = tabMenuId;
	dropMenu.className = 'menu';
	dropMenu.onmouseover = menuMouseover;
	document.body.appendChild( dropMenu );	
	//document.write(' <div id="' + tabMenuId + '" class="menu" onmouseover="menuMouseover(event)"> ');	
}

function DropMenuAddItem( tabMenuId,  text, url, onClickCodeP )
{
	var onClickCode = onClickCodeP || '';
	if ( onClickCode != '' )
	{
		onClickCode += 'return false;'
	}
	var tabMenu = document.getElementById( tabMenuId );
	if ( tabMenu )
	{
		tabMenu.isContent = true;
		tabMenu.innerHTML += '<a class="menuItem" href="' + url + '" onclick="' + onClickCode + '">' + text + '</a>';
	}	
}

function IsDropMenuContent( tabMenuId )
{
	var tabMenu = document.getElementById( tabMenuId );
	if ( tabMenu && tabMenu.isContent )
	{		
		return( true );
	}
	return false;
}

function DropMenuReset( tabMenuId )
{
	var tabMenu = document.getElementById( tabMenuId );
	if ( tabMenu )
	{
		tabMenu.innerHTML = '';
	}
	
}

function DropMenuAddSeperator( tabMenuId )
{	
	var seperator = document.createElement('div');				
	seperator.className = 'menuItemSep';	
	document.getElementById( tabMenuId ).appendChild( seperator );
}

function DropMenuAddItemWithSub( tabMenuId,  text, url, subMenuId )
{		
	var tabMenu = document.getElementById( tabMenuId );
	if ( subMenuId == '' )
	{
		DropMenuAddItem( tabMenuId,  text, url );
		return true;
	}
	
	if ( tabMenu )
	{
		
		tabMenu.isContent = true;		
		tabMenu.innerHTML += '<a class="menuItem" href="' + url + '" onmouseover="menuItemMouseover(event, \'' + subMenuId + '\');"><span class="menuItemText">' + text + '</span><span class="menuItemArrow"><img src="/img/uix/tabmenuexpand.gif" class="xminicon"></span></a>';
	}
}

//---------------------------------------------------------------------------

function AddGettingStarted( )
{
	var tabMenuId  = 'startMenuTabMenu';
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Start a game', '/myhome/mydetails.php' );
	DropMenuAddItem( tabMenuId,  'Tour', 	'/tour/tour.php?tourpage=1' );
	DropMenuAddItem( tabMenuId,  'Spectate', '/core/viewpublicgames.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Register', '/myhome/mydetails.php' );
	
}

function AddMyHomeMenuContent( isSub )
{
	var tabMenuId  = 'myHomeTabMenu';
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'My Home', '/myhome/myhome.php' );
	DropMenuAddItem( tabMenuId,  'My Games', '/mygames/mygames.php' );
	DropMenuAddItem( tabMenuId,  'My Messages', '/mymessages/msgbox.php' );
	DropMenuAddItem( tabMenuId,  'My Bookmarks', '/bookmark/index.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'My Subscription', '/myhome/mysubscription.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'My Buddies', '/buddylist/index.php' );
	DropMenuAddItem( tabMenuId,  'My Ignore List', '/ignorelist/index.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'My Profile', '/profile/playerprofile.php' );
}

function AddMySettingsContent( )
{
	var tabMenuId  = 'mySettingsTabMenu';
	
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'My Personal Details', '/myhome/mydetails.php?action=viewdetails' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'My Email Settings', '/myhome/myemailsettings.php' );
	DropMenuAddItem( tabMenuId,  'My Site Settings', '/myhome/mysitesettings.php' );
	DropMenuAddItem( tabMenuId,  'My Board Settings', '/myhome/myboardsettings.php' );
	DropMenuAddItem( tabMenuId,  'My Forum Settings', '/myhome/myforumsettings.php' );
	DropMenuAddItem( tabMenuId,  'My Profile Settings', '/myhome/myprofilesettings.php' );
	DropMenuAddItem( tabMenuId,  'My Challenge Settings', '/myhome/mychallengesettings.php' );
	DropMenuAddItem( tabMenuId,  'My Clan Settings', '/myhome/myclansettings.php' );
	DropMenuAddItem( tabMenuId,  'My Images', '/myhome/myimages.php' );
	DropMenuAddItem( tabMenuId,  'My Time Zone', '/myhome/mytimezone.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'My Vacation Settings', '/myhome/myvacationsettings.php' );
	
}

function AddNewGameMenuContent( )
{
	var tabMenuId  = 'newGameTabMenu';
	
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'New Game', '/core/newgame.php' );
	DropMenuAddItem( tabMenuId,  'Open Invites', '/openinvites/index.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Player Directory', '/core/playerdirectory.php' );
	
}

function AddForumMenuContent( isLoggedIn )
{
	var tabMenuId  = 'forumsTabMenu';
	
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'All Public Forums', '/board/index.php' );
	if ( isLoggedIn )
	{
		DropMenuAddItem( tabMenuId,  'My Private Forums', '/board/indexprivate.php' );
	}
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Site Announcements', '/board/threadlist.php?forumid=15' );
	DropMenuAddItem( tabMenuId,  'Chess', '/board/threadlist.php?forumid=9' );
	DropMenuAddItem( tabMenuId,  'Help', '/board/threadlist.php?forumid=2' );
	DropMenuAddItem( tabMenuId,  'General', '/board/threadlist.php?forumid=1' );
	DropMenuAddItem( tabMenuId,  'Posers & Puzzles', '/board/threadlist.php?forumid=13' );
	DropMenuAddItem( tabMenuId,  'Tournaments', '/board/threadlist.php?forumid=11' );
	DropMenuAddItem( tabMenuId,  'Clans', '/board/threadlist.php?forumid=16' );
	DropMenuAddItem( tabMenuId,  'Clubs', '/board/threadlist.php?forumid=605' );	
	DropMenuAddItem( tabMenuId,  'Sports', '/board/threadlist.php?forumid=259' );
	DropMenuAddItem( tabMenuId,  'Debates', '/board/threadlist.php?forumid=18' );
	DropMenuAddItem( tabMenuId,  'Science', '/board/threadlist.php?forumid=393' );
	DropMenuAddItem( tabMenuId,  'Culture', '/board/threadlist.php?forumid=394' );
	DropMenuAddItem( tabMenuId,  'Spirituality', '/board/threadlist.php?forumid=23' );
	DropMenuAddItem( tabMenuId,  'Site Ideas', '/board/threadlist.php?forumid=20' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Recommended Posts', '/board/recommended.php' );
	
}

function AddCommunityContent( isLoggedIn )
{
	var tabMenuId  = 'communityTabMenu';
	
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Public Games', '/core/viewpublicgames.php' );
	DropMenuAddItem( tabMenuId,  'Tournaments', '/tournament/index.php' );
	DropMenuAddItem( tabMenuId,  'Ladders', '/ladder/index.php' );
	DropMenuAddItem( tabMenuId,  'Sieges', '/siege/index.php' );
	DropMenuAddItem( tabMenuId,  'Clubs', '/club/index.php' );
	DropMenuAddItem( tabMenuId,  'Clans', '/clan/index.php' );
	DropMenuAddItem( tabMenuId,  'Clan Leagues', '/clanleague/index.php' );
	DropMenuAddItem( tabMenuId,  'Player Tables', '/core/playertable.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Forums', '/board/index.php' );
	DropMenuAddItem( tabMenuId,  'Votes', '/vote/index.php' );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Meet ups', '/comhub/meetups/index.php' );

	if ( isLoggedIn )
	{
		DropMenuAddSeperator( tabMenuId );
		DropMenuAddItem( tabMenuId,  'Send Message', '/mymessages/msgsend.php' );
		DropMenuAddItem( tabMenuId,  'Buddy List', '/buddylist/index.php' );
		DropMenuAddItem( tabMenuId,  'Ignore List', '/ignorelist/index.php' );
		DropMenuAddSeperator( tabMenuId );
		DropMenuAddItem( tabMenuId,  'Site Links', '/extlinkpoint/index.php' );
	}
	
}

function AddHelpMenuContent( isLoggedIn )
{
	var tabMenuId = 'helpTabMenu';
	
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Introduction', '/help/index.php?help=intro');
	DropMenuAddItem( tabMenuId, 'FAQ', '/help/index.php?help=faq');
	DropMenuAddItem( tabMenuId, 'Tournaments', '/help/index.php?help=tourn');
	DropMenuAddItem( tabMenuId, 'Clans', '/help/index.php?help=clans');
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId, 'Subscriptions', '/help/index.php?help=subs');
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Help Forum', '/board/threadlist.php?forumid=2');
	DropMenuAddItem( tabMenuId,  'Send Feedback', '/core/feedback.php' );

	if ( ! isLoggedIn )
	{
		DropMenuAddItem( tabMenuId,  'Password Reminder', '/core/passwordreminder.php' );
	}
	

}
function AddClansMenuContent( )
{	
	var tabMenuId = 'clansTabMenu';
	DropMenuReset( tabMenuId );
	DropMenuCreate( tabMenuId );		
	
}

function AddClubsMenuContent( )
{	
	var tabMenuId = 'clubsTabMenu';
	DropMenuReset( tabMenuId );
	DropMenuCreate( tabMenuId );	
}

function AddClanSubMenu( clanId )
{
	var tabMenuId  = 'clansubmenuid_' + clanId;
	DropMenuCreate( tabMenuId );		
	DropMenuAddItem( tabMenuId,  'Forum', '/clan/forum.php?cid=' + clanId );	
	return tabMenuId;
}

function AddClubSubMenu( clubId )
{
	var tabMenuId  = 'clubsubmenuid_' + clubId;	
	DropMenuCreate( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Members', '/club/members.php?clubid=' + clubId );
	DropMenuAddItem( tabMenuId,  'Games', '/club/games.php?clubid=' + clubId );
	DropMenuAddItem( tabMenuId,  'Open Invites', '/club/clubopeninvites.php?clubid=' + clubId );
	DropMenuAddItem( tabMenuId,  'Table', '/club/table.php?clubid=' + clubId );
	DropMenuAddItem( tabMenuId,  'Tournaments', '/club/tournaments.php?clubid=' + clubId );
	DropMenuAddSeperator( tabMenuId );
	DropMenuAddItem( tabMenuId,  'Forum', '/club/forum.php?clubid=' + clubId );
		
	return tabMenuId;
}

//-------------------------------------------------------------------------------------------
// Ajax menu options
//-------------------------------------------------------------------------------------------

function PopulateMenuFromXml( event, tabMenuId, url )
{		
	if ( IsDropMenuContent( tabMenuId ))
	{
		return false;
	}
	
	g_tabMenuEvent = event;
	g_ajaxMenuItemId = tabMenuId;	
	url = unescape( url );


	var isQueryString = (url.indexOf('?') > 0);

	url += (isQueryString)?'&':'?';
	url += '&cachebuster=' + (Math.floor(Math.random( )*999999));
	
	g_tabMenuXmlDoc = GetXMLHttpRequest( );
	g_tabMenuXmlDoc.onreadystatechange = TabMenuProcessReqChange;	
	g_tabMenuXmlDoc.open('GET', url, true);	
	g_tabMenuXmlDoc.send( null );
	
}

function TabMenuProcessReqChange( )
{
	if (g_tabMenuXmlDoc.readyState == 4)  //state complete
	{
		if (g_tabMenuXmlDoc.status == 200)  // only if status ok
		{

			var xmlDocRes = g_tabMenuXmlDoc.responseXML;
			var xmlRootEl = xmlDocRes.documentElement;
			var menuItems = xmlRootEl.childNodes;
			var numMenuItems = menuItems.length;

			//( g_tabMenuEvent.type + ' ' + g_ajaxMenuItemId );
			
			for (var i=0; i<numMenuItems; i++)
			{

				var itemName = menuItems[i].firstChild.nodeValue;
				var typeName = menuItems[i].getAttribute('typename');
				var itemId = menuItems[i].getAttribute('itemid');
				
				var subMenuId = CreateSubMenuId( typeName, itemId )
				
				DropMenuAddItemWithSub( g_ajaxMenuItemId,  itemName, GetUrlFromTabMenuTypeName( typeName, itemId), subMenuId );
			}													
		}
		else
		{
			alert('There was a problem retrieving the XML data:\n' + xmlDoc.statusText);
		}
	}
	else
	{
		//
	}
}

function CreateSubMenuId( typeName, itemId )
{	
	switch (typeName)
	{
		case 'clan':return AddClanSubMenu( itemId );break;	
		case 'club':return AddClubSubMenu( itemId );break;					
	}
	return '';
}

function GetUrlFromTabMenuTypeName( typeName, itemId )
{
	switch (typeName)
	{
		case 'clan':		return '/clan/home.php?cid=' +  itemId;break;
		case 'clanjoin':	return '/clan/index.php?isjoinreq=true';break;
		case 'club':		return '/club/home.php?clubid=' +  itemId;break;
		case 'clubjoin':	return '/club/index.php?isjoinreq=true';break;		
	}
	return '';
}