//---------------------- GLOBALS ----------------------------//
var kTxt_WordZoo			= 'Word Zoo';

var kTxt_AdjacentLetters 	= 'Adjacent Letters';
var kTxt_LeaveEmpty			= 'Leave Empty';
var kTxt_Replace			= 'Replace';
var kTxt_Fixed				= 'Fixed';

var kDropNewKeyboardControls		= 'Move Left = &larr;&nbsp;&nbsp; Move Right = &rarr;&nbsp;&nbsp; Drop Down = &darr;';

var gLetterValues = [
1,3,3,2,1, 	//A-E
4,2,4,1,8, 	//F-J
5,1,3,1,1, 	//K-O
3,10,1,1,1,	//P-T
1,4,4,8,4, 	//U-Y
10			//Z
];

var kLetterValueClass 	= 'wg_letter_value';
var kLetterValueClassHigh = 'wg_letter_value_high';

var kMinWordLen 	= 2;
var kMaxWordLen 	= 15;
var kScriptDictionaryLimit = kMaxWordLen;

Game.mWordLists = new Array();
Game.mWordListsProg = new Array();
Game.mWordLengthFreq = new Array();

Game.mGameName								= kTxt_WordZoo.toUpperCase();
WordGameVariation.eType_Classic 			= 1;
WordGameVariation.eType_Multiplayer 		= 2;
WordGameVariation.eType_Arcade 				= 3;
WordGameVariation.eType_ArcadeMultiplayer 	= 4;

gbMultiplayer = ((gType !== undefined && gType == WordGameVariation.eType_Multiplayer) || Search_GetValue('mp') == 1) ? true : false; // REVISIT -- what about mp arcade?
var gbArcade = ((gType !== undefined && gType == WordGameVariation.eType_Arcade) || Search_GetValue('arcade') == 1) ? true : false; // REVISIT -- what about mp arcade?

// REVISIT -- pass this from perl
GameVariation.GetTypeName = function ( typeID )
{	
	if ( typeID === undefined ) { typeID = gType; }
	if ( typeID == WordGameVariation.eType_Classic ) 			{ return 'Classic'; }
	if ( typeID == WordGameVariation.eType_Multiplayer ) 		{ return 'Multiplayer'; }
	if ( typeID == WordGameVariation.eType_Arcade ) 			{ return 'Arcade'; }
	if ( typeID == WordGameVariation.eType_ArcadeMultiplayer ) 	{ return 'Arcade Multiplayer'; }
};

WordGameVariation.prototype = new GameVariation();
WordGameVariation.prototype.constructor = WordGameVariation;
function WordGameVariation() 
{
	GameVariation.call( this );

	this.mLobbyPage 	= 'word-game-lobby.pl';
	this.mRoomPage 		= 'word-game-room.pl';
	this.mGamePage 		= 'word-game.pl';
	this.mRulesPage		= 'game-rules.pl';
	this.mScoresPage	= 'game-scores.pl';

	this.mDesc 		= '';
	
	this.mbAdjacency = false;
	this.mbReplace	= true;
	this.mbDrop		= false;
	this.mbDropNew	= false;
	this.mKeyControlsDesc 	= ''; 
	
	this.mStartNumRows = undefined;
	this.mStartNumCols = undefined;
		
	this.mTimedID		= undefined;
	this.mMPID			= undefined;
	this.mArcadeID		= undefined;
	this.mArcadeTimedID = undefined;
	
	this.mCategory = undefined;
	this.mNumPlayers = undefined;
	
	this.IsMultiplayerType = function ()
	{
		return (this.mType == WordGameVariation.eType_Multiplayer) ? true : false;
	};
	
	this.IsArcadeType = function ()
	{
		return (this.mType == WordGameVariation.eType_Arcade) ? true : false;
	};
	
	this.IsMultiplayer = function ()
	{
		return (this.mCurVariation && this.mCurVariation == this.mMPID ? true : false);
	};
	
	this.IsArcade = function ()
	{
		return ( this.mCurVariation && (this.mCurVariation == this.mArcadeID || this.mCurVariation == this.mArcadeTimedID) ) ? true : false;
	};
		
	this.IsTimed = function ( id )
	{
		if ( id === undefined ) { id = this.mCurVariation; }
		return (id && (id == this.mTimedID || id == this.mArcadeTimedID) ? true : false);
	};
	
	this.SetCurrentID = function ( v )
	{
		GameVariation.prototype.SetCurrentID.call( this, v );
		gbMultiplayer = this.IsMultiplayer();
		gbArcade = this.IsArcade();
	};
		
	this.GetGameArgs = function ()
	{
		var args = GameVariation.prototype.GetGameArgs.call( this );
		if ( this.IsMultiplayer() )
		{
			args += '&cat=' + (this.mCategory?this.mCategory:'2') + '&nump=' + (this.mNumPlayers?this.mNumPlayers:'2');
		}
		return args;
	};
	
	this.DetermineID = function ( bTimed )
	{
		var id = -1;
		if ( bTimed )
		{
			if ( this.IsArcadeType() )
			{
				id = this.mArcadeTimedID;
			}
			else
			{
				id = this.mTimedID;
			}
		}
		else
		if ( this.IsMultiplayerType() )
		{
//			if ( gbLoggedIn )
			{
				id = this.mMPID;
			}
			//REVISIT -- arcade mp
		}
		else
		{
			if ( this.IsArcadeType() )
			{
				id = this.mArcadeID;
			}
			else
			{
				id = this.mID;	
			}
		}
		return id;
	};
}

function ZE_SetupVariation( index, type, name, rulesDivID, id, idTimed, idMP, idArcade, idArcadeTimed, shortDesc, adj, replace, drop, dropNew, startRows )
{
	if ( index < 0 ) { return; }
	if ( !GameVariation.mVariations ) 
	{ 
		ErrorOutput( 'No variations array setup' ); return; 
	}
	if ( !GameVariation.mVariations[index] ) 
	{ 
		GameVariation.mVariations[index] = new WordGameVariation(); 
	}
	var v = GameVariation.mVariations[index];

	v.mIndex 		= index;
	v.mType			= type;
	v.mName 		= name;
	v.mElementID	= 'wg_variation_' + name;
	v.mDesc			= shortDesc;

	v.mbAdjacency 	= ConvertToInt(adj);
	v.mbReplace		= ConvertToInt(replace);
	v.mbDrop		= ConvertToInt(drop);
	v.mbDropNew		= ConvertToInt(dropNew);

	v.mKeyControlsDesc 	= (v.mbDropNew?('<SPAN style="font-weight:bold;">Keyboard Controls</SPAN><BR>'+kDropNewKeyboardControls):'');

	if ( ConvertToInt(startRows) > -1 ) { v.mStartNumRows = ConvertToInt(startRows); }
	
	v.mID 			= id;
	v.mTimedID 		= idTimed;
	v.mMPID			= idMP;
	v.mArcadeID		= idArcade;
	v.mArcadeTimedID = idArcadeTimed;
		
	v.mRulesDivID	= rulesDivID;
	
	if ( v.mTimedID == -1 )
	{
		v.mbTimeable = false;
	}
}

function WG_FancifyText( t, c, space_c, id, bCenterTable )
{
	if (!t) {return '';}
	if (!id) { id=''; }
	if ( bCenterTable != true ) { bCenterTable = false; }
	var s = 'style="margin-left:' + (bCenterTable?'auto;"':'0px;"');
	var f = '<TABLE id="' + id + '" ' + s + '><TR>';
	var tlc = t.toLowerCase();
	for ( var i=0; i<t.length; ++i ) 
	{
		var val = t.charAt(i);
		f += '<TD><DIV class="' + c + (val==' '? ' ' + space_c:'') + '" style="position:relative;"><TABLE border=0 cellpadding=0 cellspacing=0 height="100%"><TR><TD style="vertical-align:middle;">' + (val==' '?'&nbsp;':val) + '</TD></TR></TABLE>' + WG_GetHTML_LetterValue( val ) + '</DIV></TD>';
	}
	f += '</TR></TABLE>';
	return f;
}

Game.FancifyText = function( t, c, space_c, id, bCenterTable )
{
	return WG_FancifyText( t, c, space_c, id, bCenterTable );
};

function GetHTML_LetterValuesIcon( numDots, bHighlight )
{
	var imgW = 19;
	var w = (numDots * 3) + 1;
	var left = w - imgW; // -(imgW - w)
	var top = bHighlight ? -3 : 0;
	
	var t = '<DIV style="position:absolute; right:0px; bottom:1px; margin-right:1px; width:' + w + 'px;">';
	t += GetHTML_ImageWithOffset( w, 4, left, top, '/img/letter_values.gif', imgW, 7, 'wg_letter_value' );
	t += '</DIV>';
	return t;
}

var gNumToDots = [0,0,1,2,3,4,4,4,5,5,6];

function WG_GetHTML_LetterValue( val ) 
{
	if ( val == ' ' ) { return ''; }
	var lcchar = val.toLowerCase();
	var val = gLetterValues[lcchar.charCodeAt(0)-97];
	//var t = val;
	//return '<SPAN class="wg_letter_value" style="margin-right:-2px;">' + t + '</SPAN>';
	/*
	t = '<TABLE border=0 cellpadding=0 cellspacing=1 style="position:absolute; right:1px; bottom:1px; margin-right:0px;"><TR style="height:2px;">';
	for ( var i=gNumToDots[val]-1; i>=0;--i ) 
	{
		var c = i<4?kLetterValueClass:kLetterValueClassHigh;
		t += '<TD class="' + c + '"><DIV style="width:2px; height:2px;"></DIV></TD>';
	}
	t += '</TR></TABLE>';
	*/
	var t = '';
	var num = gNumToDots[val];
	if ( num )
	{
		t = GetHTML_LetterValuesIcon( num, false );
	}
	return t;
}

function WG_GetNextFile()
{
	for ( var i = kMinWordLen; i <= kScriptDictionaryLimit; ++i ) 
	{ 
		if ( Game.mWordListsProg && !Game.mWordListsProg[i] )
		{
			Game.mWordListsProg[i] = new Object();
			Game.mWordListsProg[i].mbLoaded = false;
			GetFile( '/txt/wg_' + (i<10?'0':'') + i.toString() + '_letter_words.txt', WG_SetFileData );
			break;
		}
	}
}

function WG_SetFileData( name, data ) 
{
	var s = name.match(/[0]?([0-9]+)/);  //DebugOutput( s[1] );
	try
	{
		if ( Game.mWordListsProg && Game.mWordListsProg[ s[1] ] )
		{
			Game.mWordLists[ s[1] ] = data.split("\n");
			Game.mWordListsProg[ s[1] ].mbLoaded = true;
		}
		else
		{
			ErrorOutput( 'No word list object?' );
		}
	}
	catch(e)
	{
		// anying to worry about?
	}
	window.setTimeout( 'WG_GetNextFile()', 1 );
}

function WG_AllFilesLoaded()
{
	for ( var i=kMinWordLen; i<=kScriptDictionaryLimit; ++i ) 
	{
		if ( !(Game.mWordListsProg[i] &&Game.mWordListsProg[i].mbLoaded) )
		{
			return false;
		}
	}
	return true;
}

function WG_CalculateBonus( score, length )
{
	if ( length <=4 || !score ) { return 0; }
	
	return Math.floor( score * 0.2 * (Math.pow(1.25,length-4)) ); // 4 == WordGame.kMinWordLength
}

function WG_WriteBonusScoreTable()
{
	var scoreMin = 5;
	var scoreMax = 40;
	var rows = scoreMax - scoreMin + 1;
	
	var html = '<DIV class="bonus_table_header">WORD ZOO BONUS TABLE</DIV>';
	html += '<TABLE border=0 cellpadding=0 cellspacing=0 style="padding:10px;">';
	html += '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD><TD colspan=11 class="small" style="padding-bottom:10px;">word length</TD>';
	html += '<TR><TD rowspan='+rows+' class="small" style="padding-right:10px; vertical-align:top; padding-top:260px; line-height:12px;">w<BR>o<BR>r<BR>d<BR><BR>r<BR>a<BR>w<BR><BR>s<BR>c<BR>o<BR>r<BR>e</TD><TD></TD>';
	for ( var length = 5; length <= 15; ++length )
	{
		html += '<TD style="padding:2px; border-bottom:1px solid #000000; border-top:1px solid #000000; font-weight:bold;">'+ length +'</TD>';
	}
	html += '</TR>';
	for ( var score = scoreMin; score < scoreMax; ++score )
	{
		html += '<TR><TD style="padding:4px; border-right:1px solid #000000; border-left:1px solid #000000; font-weight:bold;">'+score+'</TD>';
		for ( var length = 5; length <= 15; ++length )
		{
			var bonus = score < length ? '--' : WG_CalculateBonus( score, length );
			html += '<TD style="padding:4px;">'+ bonus +'</TD>';
		}
		html += '</TR>';
	}
	html += '</TABLE>';
	document.write( html );
}
