/*
	Additional methods for Strings used in IGN scripts.

	*** IGN_Core.js must be executed prior to executing this script

	Author: OldManLink (Peter Hugosson-Miller)
	Creation date: 2004-07-02
	Version: 1.0
*/

Number.wulColours = new Array();
Number.wulColours['-'] = 'green';
Number.wulColours['x'] = 'blue';
Number.wulColours['+'] = 'red';

Number.rankColours = new Array();
Number.rankColours['-'] = 'red';
Number.rankColours['x'] = 'blue';
Number.rankColours['+'] = 'green';

Number.signPadding = new Array();
Number.signPadding['-'] = 'i';
Number.signPadding['x'] = 'i';
Number.signPadding['+'] = '';

Number.prototype.signCharacter = function()
{/*
	Return a single character String containing one of '-x+', depending
	on whether I am negative, zero or positive.
*/
	var tSign = 'x';
	if(this < 0)
	{
		tSign = '-';
	}
	else
	{
		if(this > 0)
		{
			tSign = '+';
		}
	}

	return tSign;
}


Number.prototype.stringWithDecimals = function(decimalCount)
{/*
	Return a String representation of myself with the required number of
	decimal digits visible.
*/
	var tSign = this.signCharacter();
	if (this >= 0)
		tSign = '';
	var tString = '';
	var tRounder = 0;
	var tCount = decimalCount;
	if (decimalCount != 0)
	{
		tCount += 1;
		tRounder = 5 * Math.pow(10, 0 - tCount);
	}

	var tRounded = Math.abs(this) + tRounder;
	tString += tRounded + '.';
	var tDecimalPos = tString.search(/\./);
	return tSign + tString.substr(0, tDecimalPos + tCount);

}


Number.prototype.rankMarkup = function(padCount, signColours, decimalCount)
{/*
	Return a String representation of myself suitable for displaying in
	a ranking list in an IGN markup message.

*/
	var tSign = this.signCharacter();
	var tString = Math.abs(this).stringWithDecimals(decimalCount);
	tString = tSign + tString.markupPadded(padCount, '0', false);
	tString = tString.markupPair('color=' + signColours[tSign]);
	return ''.markupPadded(1, Number.signPadding[tSign], true) + tString;
}


Number.prototype.commatize = function()
{/*
	Return a String representation of myself with commas added as the
	thousands separators.
*/
	var tArray = new Array('0');
	var tIndex = 0;
	var tNumber = this;
	while (tNumber > 0)
    {
    	tArray[tIndex] = '' + tNumber%1000;
    	tNumber=Math.floor(tNumber/1000);
    	tIndex++;
    }

    tArray = tArray.reverse();
    for (var i in tArray)
    {
    	if ( i > 0) //padding zeros
    	{
    		while (tArray[i].length < 3)
    		{
    			tArray[i] = '0' + tArray[i];
    		}
    	}
    }

    return tArray.join();

}
