var significantDigits = 3;

function nxtCmpFolder( a, b )
{
   if (a.normalizedName() < b.normalizedName())
      return -1
   if (a.normalizedName() > b.normalizedName())
      return 1
   // a must be equal to b
   return 0
}

function getParentFolder( path )
{
	pf = path.substr( 0, path.indexOf( "\\" ) );
	alert( path );
}

function nxtFolder( name, count )
{
	this.name  		= name;
	this.path		= "";
	this.count 		= count;
	this.subFolders 	= new Array();
	this.parentFolder 	= null;

	this.filename		= "";



	this.calcPath = function()
		{
			if ( this.parentFolder.name != "virtual root" )
			{
				this.path = this.parentFolder.name + "\\" + this.name + "\\";
			}
			else
			{
				this.path = this.name + "\\";
			}
		}


	this.addSubFolder = function( name, count )
		{
			this.subFolders[ this.subFolders.length ] = new nxtFolder( name, count );
			this.subFolders[ this.subFolders.length - 1 ].parentFolder = this;
			this.subFolders[ this.subFolders.length - 1 ].calcPath();
			return this.subFolders[ this.subFolders.length - 1 ];
		}
	
	this.render = function( style )
		{
			if ( this.parentFolder.name == "virtual root" )
			{
				document.write( "<tr valign=top>" );
				document.write(	"<td id=punkt_" + this.count + "menu onmouseout=\"menu('out','punkt_" + this.count + "menu')\" onmouseover=\"menu('over','punkt_" + this.count + "menu')\" >" );
				document.write(	"<img id=punkt_" + this.count + " onclick=\"lag('punkt_" + this.count + "lag',document.images['punkt_" + this.count + "'])\" src=\"grafik/global/blank.gif\" width=\"6\" border=0>\n" );
				document.write(	"<b onclick=\"lag('punkt_" + this.count + "lag',document.images['punkt_" + this.count + "']);top.indhold.location='ViewFileContent.asp?strFolder=" + escape(this.path) + "&strFile='\">" + this.name +"</b>\n" );
				document.write(	"<div id=punkt_" + this.count + "lag style=\"display: none\">\n" );

				this.renderSubFolders( );

				document.write(	"</div>" );
				document.write(	"<hr size=1 noshade>\n" );
				document.write(	"</td>" );
				document.write(	"<td width=\"10\"><img src=\"grafik/global/blank.gif\" width=\"10\" height=\"1\" alt=\"\" border=\"0\"></td>\n" );
				document.write(	"</tr>" );
			}
			else
			{
				document.write( "<img src=\"grafik/global/menu_pil.gif\" width=\"7\" height=\"9\" alt=\"\" border=\"0\"> <a href=\"ViewFileContent.asp?strFolder=" + escape(this.path) + "&strFile=" + this.filename + "\" target=\"indhold\"><span class=\"" + this.getClassName() + "\">" + this.name + "</span></a><br>\n" );
			}
		}

	this.renderSubFolders = function()
		{
			this.subFolders.sort( nxtCmpFolder );
			for( var i=0; i < this.subFolders.length; i++)
			{
				this.subFolders[i].render();
			}
		}

	this.normalizedName = function()
		{
			var strIndex = this.name.substr( 0, this.name.indexOf( " " ) );
			var strIndexArray = new Array();
			var curIndex = 0;


			strIndexArray[curIndex] = "";
			for ( var i=0; i < strIndex.length; i++ )
			{
				if ( strIndex.charAt(i) != "." )
				{
					strIndexArray[curIndex] += strIndex.charAt(i);
				}
				else 
				{
					curIndex ++;
					strIndexArray[curIndex] = "";
				}
			}

			strIndex = "";
			var curLength = 0;
			for ( var i=0; i < strIndexArray.length; i++)
			{
				curLength = strIndexArray[i].length;
				if ( curLength < significantDigits )
				{
					prefix = "";
					for ( var j=curLength; j < significantDigits; j++ )
					{
						prefix += "0"; 
					}
					strIndexArray[i] = prefix + strIndexArray[i];
				}
				strIndex += strIndexArray[i] + ".";
			}

			return strIndex + this.name.substr( this.name.indexOf( " " ), this.name.length );
		}

	this.getClassName = function()
		{
			var tempName = this.name.substr( 0, this.name.indexOf( " " ) );
			var clsName = "";
			var doContinue = true;
			var curIndex = 0;
		
			for ( var i=0; i < tempName.length && doContinue; i++ )
			{
				if ( tempName.charAt(i) != "." )
				{
					clsName += tempName.charAt(i);
				}
				else 
				{
					curIndex ++;
					if (curIndex < 2)
					{
						clsName += "_";
					}
					else
					{
						doContinue = false;
					}
				}
			}


			return "folder_" + clsName;
		}

}

