SudokuLobby.SetDailyPuzzleData  = function( puzzleLinksDiv, linksTable, puzzleSignUpDiv, notifyFreqs, currFreq )
{
	var sd = Game.mSingleton;
	if ( !sd ) { return; }
	sd.mPuzzleLinksElement 	= GetElement( puzzleLinksDiv );
	sd.mLinksTableElement	= GetElement( linksTable );
	sd.mPuzzleSignUpElement = GetElement( puzzleSignUpDiv );
	sd.mNotifyFrequencies 	= notifyFreqs;
	sd.mCurrFrequency		= currFreq;
};

SudokuLobby.prototype = new GameLobby();
SudokuLobby.prototype.constructor = SudokuLobby;
function SudokuLobby()
{
	GameLobby.call( this );
//	this.mChoiceLevelElements;
}

SudokuLobby.prototype.CreateBody = function ( parent )
{
	if ( !parent ) { return; }
	
	var html = '';
	html +=		'<TABLE cellpadding=0 cellspacing=0 border=0>';

	html +=			'<TR><TD style="vertical-align:top;">';
	html +=	this.GetHTML_Tabs();
	html +=			'</TD></TR>';

	html +=			'<TR><TD><FORM onsubmit="return false;"><TABLE cellpadding=0 cellspacing=0 border=0 id="sd_games" class="wg_tab_body">';
	html +=			(gbMultiplayer && this.mGIPDivName) ? '<TR><TD id="sdmpgip_dest"></TD></TR>' : this.GetHTML_Variations();
/*
	html +=				(IsEmbededMini() || gbMultiplayer) ? '' : ('<TR><TD colspan=4 style="border-bottom:1px solid #000000; padding-top:12px;"><DIV style="width:1px; height:1px;"></DIV></TR>');
	if ( !gbMultiplayer )
	{
		var from = Search_GetValue( 'from' );
		var level = from ? SudokuVariation.GetLevel( from ) : 0;
		html +=				'<TR><TD colspan=4><TABLE>';
		if ( !IsEmbededMini() )
		{		
			html +=				'<TR><TD class="wg_text" style="font-size:90%; font-weight:bold; border-bottom:1px solid #000000; text-align:center;">OPTIONS</TD></TR>';
			html +=				'<TR><TD>';
		}
		else
		{
			html +=				'<TR class="wg_text" style="font-size:80%;"><TD style="font-weight:bold; text-align:center;">OPTIONS:</TD>';
			html +=				'<TD>';	
		}
		var descs = [ 'Easy', 'Medium', 'Hard', 'Ouch' ];
		for ( var k = 0; k < 4; ++k )
		{
			var id = 'sd_choice_level' + k;
			html +=					'<INPUT type=radio id="'+id+'" name="sd_choice_level" style="vertical-align:middle;" onclick="return SudokuLobby.LevelOptionOnClick(\''+id+'\');" '+ (level!==undefined && level==k?'checked':'') + '><LABEL for="'+id+'" class="wg_text" style="font-size:90%; vertical-align:middle;">'+descs[k]+'</LABEL>';
			html +=						(k < 3 ? '&nbsp;&nbsp':'');
		}
		html +=					'</TD></TR>';
		html +=				'</TABLE></TD></TR>';
	}

	html +=				(IsEmbededMini() || gbMultiplayer) ? '' : ('<TR><TD colspan=4 style="border-top:1px solid #000000; padding-bottom:12px;"><DIV style="width:1px; height:1px;"></DIV></TR>');
*/
	html +=			'</TABLE></FORM></TD></TR>';
	html +=		'</TABLE>';


	parent.innerHTML = html;

//	this.mChoiceLevelElements = GetElements( 'sd_choice_level', 0 );

	this.SetupTabs();
	this.SetupVariations();		
	this.SetupLinks();
};

SudokuLobby.prototype.SetupLinks = function()
{
	GameLobby.prototype.SetupLinks.call( this );
	
	var sd = this;
	if ( this.mPuzzleLinksElement && this.mLinksTableElement )
	{
		var link = this.mPuzzleLinksElement;
		var t = this.mLinksTableElement;
		link.onmouseover = function() // gets replaced by the UI_PopupMenu object
		{
			if ( !sd.mPopupMenu )
			{
				t.style.margin = '0px';
				sd.mPopupMenu = new UI_PopupMenu( link );	
				sd.mPopupMenu.SetData( t );
				sd.mPopupMenu.Format( undefined, 1, 0 );
			}
			sd.mPopupMenu.Show( true );
		};
		
		t.style.display = 'none';
	}
	
	if ( this.mPuzzleSignUpElement )
	{
		var link2 = this.mPuzzleSignUpElement;
//		link2.onmouseover = function()
		link2.onclick = function()
		{
//			if ( !sd.mPopupMenu_SignUp )
			{
				var html = '';
				if ( sd.mbLoggedIn )
				{
					sd.mNotifyButtonNames = new Array();
					var radioName = sd.mPuzzleSignUpElement.id;
					html = '<TABLE border=0 cellpadding=4 cellspacing=6 style="margin:10px;"><TR><TD colspan=2 style="font-weight:bold; border-bottom:1px solid #000000;">Send me a puzzle:</TD></TR>';
					for ( var i=0; i< sd.mNotifyFrequencies.length; ++i )
					{
						var desc = sd.mNotifyFrequencies[i];
						var id = radioName +  '_' + i;
						sd.mNotifyButtonNames[i] = id;
						html += '<TR><TD style="vertical-align:middle;" onclick="SudokuLobby.OnClickNotifyOption('+i+');">';
						html += '<INPUT type="radio" id="'+id+'" name="'+radioName+'"'+(i==sd.mCurrFrequency?' checked':'')+' value="'+i+'"></TD>';
						html += '<TD style="text-align:left; vertical-align:middle;" onclick="SudokuLobby.OnClickNotifyOption('+i+');"><LABEL for="'+id+'">'+ desc +'</LABEL></TD></TR>';
					}
					html += '<TR><TD colspan=2 style="padding-top:4px;"><INPUT id="sd_button_submit" type="button" value="Submit" onclick="SudokuLobby.OnSubmitNotifyOptions(\''+radioName+'\');">&nbsp;<INPUT type="button" value="Cancel" onclick="UI_ShowOverlay(false);"></TD></TR></TABLE>';
				}
				else
				{
					html = '<TABLE border=0 cellpadding=4 cellspacing=6 style="margin:10px;"><TR><TD>You must be <A href="/">logged in</A> to sign up for daily puzzles.</TD></TR><TR><TD style="padding-top:4px;"><INPUT type="button" value=" OK " onclick="UI_ShowOverlay(false);"></TD></TR></TABLE>';
				}
//				sd.mPopupMenu_SignUp = new UI_PopupMenu( link2 );
//				sd.mPopupMenu_SignUp.SetData( html );
//				sd.mPopupMenu_SignUp.Format( undefined, 1, 0 );
			}
//			sd.mPopupMenu_SignUp.Show( true );
			UI_ShowOverlay( true, html );
			sd.mButton_Submit = GetElement( 'sd_button_submit' );
			if ( sd.mButton_Submit )
			{
				sd.mButton_Submit.disabled = true;	
			}
		};
	}
};

SudokuLobby.prototype.PostHandler_Complete = function( response, rr )
{
	this.PostCleanup();			
	UI_ShowOverlay( false );
	var div = this.mPuzzleSignUpElement;
	var args = response.split( '&' );
	if ( args && args[0] == 'ze_ok' && div.id == args[1] )
	{
		var html = '<TABLE><TR><TD style="vertical-align:top; padding-right:4px;">' + GetHTML_StatusIcon(1) + '</TD><TD style="vertical-align:middle;">Daily puzzle request has been submitted.</TD></TR></TABLE>';
		this.mCurrFrequency = this.mNewFrequency;
		this.mNewFrequency = undefined;
		UI_ShowPopupMessage( true, html, 1, 1 );
	}
	else
	{
		GameLobby.prototype.PostHandler_Complete.call( this, response, rr );
	}
};

SudokuLobby.OnClickNotifyOption = function( index )
{
	var lobby = Game.mSingleton;
	if ( !lobby ) { ErrorOutput( 'No lobby object' ); return; }
	
	lobby.mButton_Submit.disabled = (index == lobby.mCurrFrequency);
};

SudokuLobby.OnSubmitNotifyOptions = function ( groupName )
{
	var lobby = Game.mSingleton;
	if ( !lobby ) { ErrorOutput( 'No lobby object' ); return; }

	if ( lobby.mRR ) { ErrorOutput( 'Remote request already pending' ); return; }

	var bDoGenericError = false;

	if ( lobby.mNotifyButtonNames )
	{
		lobby.mNewFrequency = lobby.mCurrFrequency;
		for ( var i = 0; i < lobby.mNotifyButtonNames.length; ++i )
		{
			var el = GetElement( lobby.mNotifyButtonNames[i] );
			if ( el )
			{
				if ( el.checked )
				{
					lobby.mNewFrequency = el.value;
					break;
				}
			}
			else
			{
				ErrorOutput( 'No notify radio button with name ' + lobby.mNotifyButtonNames[i] );
			}
		}
		
		if ( lobby.mNewFrequency != lobby.mCurrFrequency )
		{		
			var reqTxt = groupName + '=' + lobby.mNewFrequency;
			this.mRR = RemoteRequest.NewRequest( '/notifications.pl', reqTxt, {mCompletion:{obj:lobby,funcName:'PostHandler_Complete'}, mTimeout:{obj:lobby,funcName:'PostHandler_Timeout'}}, true );
			if ( !(this.mRR && this.mRR.Issue()) )
			{
				bDoGenericError = true;
				lobby.PostCleanup();
			}
			else
			{
				UI_ShowOverlay( true, '<DIV style="margin:10px;">Sending request...</DIV>' );
			}
		}	
		else
		{	
			UI_ShowOverlay( false );
			DebugOutput( 'no changes' );
		}
	}
	else
	{
		ErrorOutput( 'No lobby radio button names array' );
		bDoGenericError = true;
	}	
	
	if ( bDoGenericError )
	{
		UI_ShowOverlay( false );
		UI_ShowGenericErrorPopup();		
	}
};


/*
SudokuLobby.LevelOptionOnClick = function ( id )
{
	var e = GetElement( id );
	DebugOutput( 'clicked: ' + id );
};
*/
function ZE_LobbySetup( type, tabPrefix, variationPrefix )
{
	var lobby = new SudokuLobby();
	
	lobby.mType = type;
	
	lobby.mTabElements = GetElements( tabPrefix, 1 ); 
	lobby.mVariationElements = GetElements( variationPrefix, 1 );
}

function ZE_WriteLobby( parent, bLoggedIn, unused )
{
	if ( !Game.mSingleton ) { ErrorOutput( 'No game singleton object created' ); return; }
	
	Game.mSingleton.mbLoggedIn = bLoggedIn == 1 ? true : false;
	Game.mSingleton.SetupPage( parent );
}
