// cFilenameList - Class to hold list of filenames - Copyright (C) 2010 Charles A Upsdell, All Rights Reserved; www.upsdell.com
//


// Class cFilenameList

cFilenameList.prototype.$name = 'cFilenameList';

function cFilenameList ( names, directory, prefixes, suffixes, id, options )
{
this.aFile = new Array( );
var ix = 0;
if ( names instanceof Array ) for ( var i = 0; i < names.length; ++i )
  {
	var jx = names[i].indexOf('..');
	if ( (jx > 0) && ('0123456789'.indexOf(names[i].charAt(0)) != -1) )
	  {
		var j1 = names[i].substring(0,jx-1);
		var j2 = names[i].substring(jx+2).toString();
		for ( j = j1 ; j <= j2; ++j )
		  {
			var name = j.toString();
			while ( name.length < j2.length )
				name = '' + '0' + name;
			this.aFile[ix] = new Object( name );
			this.aFile[ix++].name = name;
		  }
	  }
	else
	  {
		this.aFile[ix] = new Object( names[i] );
		this.aFile[ix++].name = names[i];
	  }
  }
this.directory = directory;
this.aPrefix = new Array();
if ( prefixes instanceof Array )
	this.aPrefix = prefixes;
else
	this.aPrefix[0] = prefixes;
this.aSuffix = new Array();
if ( suffixes instanceof Array )
	this.aSuffix = suffixes;
else
	this.aSuffix[0] = suffixes;
if ( this.aPrefix.length != this.aSuffix.length )
	this.$name.alert( '', 'prefixes length != suffixes length', this.aPrefix.length, this.aSuffix.length );
this.id = id;
if ( options !== undefined )
  {
	if ( (options.indexOf('textSplit') != -1) )
		this.textSplit();
	if ( (options.indexOf('randomStack') != -1) )
		this.randomStack();
  }
this.aProperty = [ 'name' ];
}

cFilenameList.prototype.add = function ( value )
{
var $name = 'add';
if ( typeof(this.aProperty) == 'undefined' )
	this.$name.alert( $name, 'this.aProperty not defined' );
else if ( this.aProperty.length < arguments.length )
	this.$name.alert( $name, 'Too many arguments', arguments.length );
var length = this.aFile.length;
this.aFile[length] = new Object();
for ( var i = 0; i < this.aProperty.length; ++i )
	this.aFile[length][this.aProperty[i]] = arguments[i];
}

cFilenameList.prototype.addProperties = function ( properties )
{
for ( var i = 0; i < arguments.length; ++i )
	this.aProperty[i] = arguments[i];
}

cFilenameList.prototype.name = function ( ix, affix )
{
var rv = undefined;
if ( ix === undefined )
	ix = 0;
if ( affix === undefined )
	affix = 0;
if ( (ix >= 0) && (ix < this.aFile.length) && (affix >= 0) && (affix < this.aPrefix.length) )
  {
	rv = this.aFile[ix].name;
	if ( rv.indexOf('/') == -1 )
		rv = this.aPrefix[affix] + rv;
	if ( rv.indexOf('.') == -1 )
		rv += this.aSuffix[affix];
	if ( this.directory !== undefined )
		rv = this.directory + rv;
  }
return rv;
}

cFilenameList.prototype.dump = function ()
{
var rv = '';
for ( var i = 0; i < this.aFile.length; ++i )
  {
	for ( var property in this.aFile[i] )
		if ( property.charAt(0) != '$' ) rv += property + '=' + this.aFile[i][property] + ' ';
	rv += '\n';
  }
return rv;
}

cFilenameList.prototype.random = function ( affix )
{
return this.name( this.randomIx(), affix );
}

cFilenameList.prototype.randomIx = function ()
{
if ( (typeof(this.stack) != 'undefined') && (this.stack instanceof cRandom) )
	return this.stack.draw();
else 
	return Math.floor( this.aFile.length * Math.random() );
}

cFilenameList.prototype.randomStack = function ()
{
this.stack = new cRandomizedStack( this.aFile.length );
this.stack.randomize();
}

cFilenameList.prototype.text = function ( ix, property )
{
var rv = undefined;
if ( ix === undefined )
	ix = 0;
if ( property === undefined )
	property = 'text';
if ( (ix >= 0) && (ix < this.aFile.length) && (typeof(this.aFile[ix][property]) != 'undefined') )
	rv = this.aFile[ix][property];
return rv;
}

cFilenameList.prototype.textSplit = function ()
{
for ( var i = 0; i < this.aFile.length; ++i )
  {
	var ixText = this.aFile[i].name.indexOf(' ');
	if ( ixText == -1 )
	  {
		this.aFile[i].text = undefined;
	  }
	else
	  {
		this.aFile[i].text = this.aFile[i].name.substring( ixText+1 );
		this.aFile[i].name = this.aFile[i].name.substring( 0, ixText );
	  }
  }
}



