
function GnxFileManager(oidFile)
{
	if (typeof(oidFile) != "undefined")
	{
		this.oidFile = oidFile;
	} else {
		this.oidFile = 0;
	}
	
	
	this.int2bigInt = GnxFileManager_int2bigInt;
	this.copy = GnxFileManager_copy;
	this.copyInt = GnxFileManager_copyInt;
	this.addInt = GnxFileManager_addInt;
	this.multInt = GnxFileManager_multInt;
	this.divInt = GnxFileManager_divInt;
	this.dup = GnxFileManager_dup;
	this.isZero = GnxFileManager_isZero;
	this.str2bigInt = GnxFileManager_str2bigInt;
	this.bigInt2str = GnxFileManager_bigInt2str;


	this.getBit = GnxFileManager_getBit;
	this.getURL = GnxFileManager_getURL;
	this.getURLFileDownloader = GnxFileManager_getURLFileDownloader;
	this.getPATH = GnxFileManager_getPATH;
	this.getSerial = GnxFileManager_getSerial;
	this.getCodeFileType = GnxFileManager_getCodeFileType;
	this.getFileSize = GnxFileManager_getFileSize;
	this.getFileName = GnxFileManager_getFileName;
	this.getFileExt = GnxFileManager_getFileExt;
	this.getURLFileName = GnxFileManager_getURLFileName;
	
	this.GnxFileDownloader = "http://file.nexon.com/Isolation/file/download/filedownloader.aspx?oidfile=";

	this.bpe = 0;
	this.mask = 0;
	this.radix = this.mask + 1;
	
	this.digitsStr='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-';
	for (this.bpe=0; ( 1<<( this.bpe+1 ) ) > ( 1<<this.bpe ); this.bpe++);
	this.bpe >>= 1;
	this.mask = (1 << this.bpe) - 1;
	this.radix = this.mask + 1;
	this.one = this.int2bigInt( 1, 1, 1 );

	this.t = new Array(0);
	this.s6 = this.t;
	
	this.getStrFileSize = GnxFileManager_getStrFileSize;
	this.getStrFileSizeType = GnxFileManager_getStrFileSizeType;
}

function GnxFileManager_int2bigInt(t, bits, minSize) {   
	var i, k;
	k = Math.ceil( bits / this.bpe ) + 1;
	k = minSize > k ? minSize : k;
	buff = new Array(k);
	this.copyInt( buff, t );

	return buff;
}

function GnxFileManager_copy(x, y) {
	var i;
	var k = x.length < y.length ? x.length : y.length;

	for (i=0; i<k; i++)
		x[i]=y[i];

	for (i=k; i<x.length; i++)
		x[i]=0;
}

function GnxFileManager_copyInt(x, n) {
	var i, c;

	for (c=n, i=0; i<x.length;i++) {
		x[i] = c & this.mask;
		c >>= this.bpe;
	}
}

function GnxFileManager_addInt(x,n) {
	var i, k, c, b;

	x[0] += n;
	k = x.length;
	c = 0;

	for (i=0; i<k; i++) {
		c += x[i];
		b = 0;
		if ( c < 0) {
			b =- (c >> this.bpe);
			c += b * this.radix;
		}
		x[i] =c & this.mask;
		c = ( c >> this.bpe ) - b;
		if (!c) return;
	}
}

function GnxFileManager_multInt(x, n) {
	var i,k,c,b;
	if (!n)
		return;
	k = x.length;
	c = 0;
	for (i=0; i<k; i++) {
		c += x[i] * n;
		b = 0;
		if (c < 0) {
			b =- (c >> this.bpe);
			c += b * this.radix;
		}
		x[i] = c & this.mask;
		c = (c >> this.bpe) - b;
	}
}

function GnxFileManager_divInt(x, n) {
	var i ,r=0, s;
	for (i=x.length-1; i>=0; i--) {
		s = r * this.radix + x[i];
		x[i] = Math.floor(s / n);
		r = s % n;
	}

	return r;
}

function GnxFileManager_dup(x) {
	var i;
	buff=new Array(x.length);
	this.copy(buff,x);

	return buff;
}

function GnxFileManager_isZero(x) {
	var i;
	for (i=0;i<x.length;i++)
		if (x[i])
			return 0;

	return 1;
}

function GnxFileManager_str2bigInt(s, base, minSize) {
	var d, i, j, x, y, kk;
	var k = s.length;

	if (base == -1) {
		x = new Array(0);

	for (;;) {
		y = new Array( x.length + 1 );
		for (i=0; i < x.length; i++)
			y[i+1] = x[i];
		y[0] = parseInt(s, 10);
		x = y;
		d = s.indexOf(',', 0);
		if (d < 1) 
			break;
		s = s.substring(d + 1);
		if (s.length == 0)
			break;
		}
		if ( x.length < minSize ) {
			y = new Array( minSize );
			this.copy( y, x);
			return y;
		}

		return x;
	}

	x = this.int2bigInt(0, base * k, 0);

	for (i=0; i<k; i++) {
		d = this.digitsStr.indexOf( s.substring( i, i+1 ), 0 );
		
		if (base <= 36 && d >= 36)  //convert lowercase to uppercase if base<=36
			d -= 26;
		
		if ( d < base && d >= 0 ) {   //ignore illegal characters
			this.multInt(x, base);
			this.addInt(x, d);
		}
	}

	for (k=x.length; k>0 && !x[k-1]; k--); //strip off leading zeros
	k = minSize > k+1 ? minSize : k+1;
	y = new Array(k);
	kk = k < x.length ? k : x.length;

	for (i=0; i<kk; i++)
		y[i] = x[i];

	for (; i<k ; i++)
		y[i] = 0;

	return y;
}

function GnxFileManager_bigInt2str(x,base) {
	var i, t, s = "";

	if (this.s6.length != x.length) 
		this.s6 = this.dup(x);
	else
		this.copy(this.s6, x);

	if (base == -1) {
		for (i=x.length-1; i>0; i--)
			s += x[i] + ',';
		s += x[0];
	}
	else {
		while ( !this.isZero(this.s6) ) {
			t = this.divInt(this.s6, base);
			s = this.digitsStr.substring(t, t+1) + s;
		}
	}

	if (s.length == 0)
		s = "0";

	return s;
}

function GnxFileManager_getBit(sValue, nCutStartBit, nCutLengthBit)
{
	var n8Value = this.str2bigInt(sValue, 10);
	var nBit = this.bigInt2str(n8Value , 2);
	var nRetValue = 0;
	for (var i=nBit.length; i < nCutStartBit + nCutLengthBit; i++)
	{
		nBit = "0" + nBit;
	}

	for (var i=nBit.length - nCutStartBit; i > nBit.length - ( nCutStartBit + nCutLengthBit ); i--)
	{
		if (nBit.substr(i-1, 1) == "1")
		{
			nRetValue += (1 << ( (nBit.length - nCutStartBit) - i) );
		}
	}

	return nRetValue;
}

function GnxFileManager_getURL(oidFile)
{
	
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}

	if (oidFile == 0)
	{
		return 0;
	}
	
	var strURL = Private_getURL( this.getBit( oidFile, 0, 1 ) ) + "/" + Private_intToStr(this.getCodeFileType(oidFile), 3);
	var strSerial = Private_intToStr(this.getSerial(oidFile), 10);
	
	strURL += "/" + Private_intToStr(Private_getFileDirectory(this.getFileSize(oidFile)), 3)
	strURL += "/" + strSerial.substr(0, 3) + "/" + strSerial.substr(3, 2) + "/" + strSerial.substr(5, 2);
	
	return strURL;
}

function GnxFileManager_getURLFileDownloader(oidFile)
{
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}

	if (oidFile == 0)
	{
		return 0;
	}

	var strURL = this.GnxFileDownloader + oidFile;

	return strURL;
}

function GnxFileManager_getPATH(oidFile)
{
	
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}

	if (oidFile == 0)
	{
		return 0;
	}
	
	var strPATH = Private_getPATH( this.getBit( oidFile, 0, 1 ) ) + "\\" + Private_intToStr(this.getCodeFileType(oidFile), 3);
	var strSerial = Private_intToStr(this.getSerial(oidFile), 10);

	strPATH += "\\" + Private_intToStr(Private_getFileDirectory(this.getFileSize(oidFile)), 3)
	strPATH += "\\" + strSerial.substr(0, 3) + "\\" + strSerial.substr(3, 2) + "\\" + strSerial.substr(5, 2);

	return strPATH;
}

function GnxFileManager_getFileName(oidFile)
{
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}
	if (oidFile == 0)
	{
		return 0;
	}

	return oidFile + "." + Private_getFileExt(this.getCodeFileType(oidFile));
}

function GnxFileManager_getURLFileName(oidFile)
{
	return this.getURL(oidFile) + "/" + this.getFileName(oidFile);
}

function GnxFileManager_getFileExt(oidFile)
{
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}
	if (oidFile == 0)
	{
		return 0;
	}

	return Private_getFileExt(this.getCodeFileType(oidFile));
}

function GnxFileManager_getSerial(oidFile)
{
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}
	if (oidFile == 0)
	{
		return 0;
	}

	return this.getBit(oidFile, 0, 31)
}

function GnxFileManager_getCodeFileType(oidFile)
{
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}
	if (oidFile == 0)
	{
		return 0;
	}

	return this.getBit(oidFile, 52, 10)
}

function GnxFileManager_getFileSize(oidFile)
{
	if (typeof(oidFile) == "undefined")
	{
		oidFile = this.oidFile;
	}
	if (oidFile == 0)
	{
		return 0;
	}

	return this.getBit(oidFile, 32, 20)
}

function GnxFileManager_getStrFileSize()
{	
	if ( this.oidFile == 0 )
		return "0 Kbyte";
	else
	{
		return this.getBit( this.oidFile, 32, 10 ) +'.' +this.getBit(this.oidFile, 53, 6) +" " +this.getStrFileSizeType();
	}
}

function GnxFileManager_getStrFileSizeType()
{	
	var strFileSizeType = this.getBit( this.oidFile, 42, 2 )
	if ( strFileSizeType == 1 )
		return "Kbyte";
	else if( strFileSizeType == 2 )
	{
		return "Mbyte";
	}
	else if( strFileSizeType == 3 )
	{
		return "Gbyte";
	}
	else
	{
		return "";
	}
}














function Private_getFileExt(nCodeFileType)
{
	strCodeFileType = "gif,jpg,jpge,bmp,png,zip,gz,txt,alz,ppt,doc,xls,hwp,test,html,htm,csv,wmv,ksv,sav,wma,rar,pdf,exe,mp3,a00,swf,asf,avi,crydmp,xml";

	strCodeFileType = strCodeFileType.toLowerCase();
	arrCodeFileType = strCodeFileType.split(",");

	if (arrCodeFileType.length <= ( nCodeFileType - 1))
	{
		return "";
	}

	return arrCodeFileType[nCodeFileType-1];
}

function Private_getFileDirectory(nFileSize)
{
	if (nFileSize >= 100)
	{
		return 100;
	}

	return nFileSize;
}

function Private_intToStr(sNumber, nSize)
{
	sNumber += "";
	for (var i=sNumber.length; i<nSize; i++)
	{
		sNumber = "0" + sNumber;
	}

	return sNumber;
}

function Private_getPATH(nBit)
{
	var sRetValue = "";
	if (nBit == 0)
	{
		sRetValue = "\\\\192.168.7.3\\share01\\Data01\\GnxFile";
	} else if ( nBit == 1 )
	{
		sRetValue = "\\\\192.168.7.4\\share02\\Data02\\GnxFile";
	}
	
	return sRetValue;
}

function Private_getURL(nBit)
{
	var sRetValue = "";

	if (nBit == 0)
	{
		sRetValue = "http://storage.nx.com/Data01/GnxFile";
	} else if ( nBit == 1 )
	{
		sRetValue = "http://storage.nx.com/Data02/GnxFile";
	}
	
	return sRetValue;
}