function SaltPTable(element,options)
{
	// initialise the elements
	this.element			= $(element);
	this.propertycontainer	= $('<div class="PropertyContainer"></div>');
	this.propertycontainer.insertAfter(this.element);
	var id					= this.element.attr('id');
	if(!id)
	{
		this.id='SaltPTable-'+SaltPTable.it++;
	}
	this.id = id;
	
	// initialise the variables
	this.tree				= {};
	this.treeTitles			= [];
	this.selectedTree		= [];
	this.depth				= 0;
	this.selectedDepth		= 0;
	
	// initialise the settings
	this.hasHeaderRow		= options.hasHeaderRow==true ? true : false;
	
	SaltPTable.objects[this.id] = this;
	
	this.init();
}
SaltPTable.it = 1;
SaltPTable.objects = {};
SaltPTable.getTablByName = function(name)
{
	return SaltPTable.objects[name];
}
$pr = SaltPTable.prototype;

$pr.init = function()
{
	var p = this;
	
	// get the header row
	$('tr:first td',this.element).each(function()
	{
		// push the title into the titles field
		if(p.hasHeaderRow)
		{
			p.treeTitles.push($(this).text());
		}
		// increment the depth
		p.depth++;
	});
	
	// loop through the rows and assemble the tree
	var i =0;
	$('tr').each(function()
	{
		var op = $(this);
		if(i>0 || !p.hasHeaderRow)
		{
			var tds = $('td',op);
			var j = 0;
			var subtree = [];
			tds.each(function()
			{
				var optd = $(this);
				var tree = p.getCategoryByTree(subtree);
				
				// if we're at the prescribed depth, make the final record and stop processing the each
				if(j == p.depth-1)
				{
					tree.items.push(optd.html());
					return false;
				}
				
				subtree.push(optd.text());
				
				j++;
			});
		}
		i++;
	});
	
//	var option1 = this.generateDropdown();
//	this.element.after(option1);

	this.regenerate();
	
	// delete the table
	this.element.css('display','none');
}

$pr.regenerate = function()
{
	$('select.SPDrill_'+this.id).remove();
	
	var i = -1;
	do
	{
		i++
		var selectedOption = this.selectedTree[i] ? this.selectedTree[i] : '';
		this.element.before(this.generateDropdown(i,selectedOption));
	} while(i < this.selectedTree.length)
	
	this.generateItems();
	
	$(window).trigger('resize');
}

$pr.generateItems = function()
{
	var tree = this.getSelectedTree(this.selectedTree.length);
	this.propertycontainer.empty();
	var propertylist = $('<ul class="propertylist"></ul>').appendTo(this.propertycontainer);
	for(var item in tree.items)
	{
		propertylist.append('<li>'+tree.items[item]+'</li>');
	}
}

$pr.setSelectedTree = function(depth,option)
{
	this.selectedTree[depth] = option;
	
	this.selectedTree.splice(depth+1,this.selectedTree.length - depth);
	
	this.regenerate();
}

$pr.getSelectedTree = function(depth)
{
	var tree = this.tree;
	
	for(var i = 0; i < depth; i++)
	{
		if(!this.selectedTree[i])
		{
			break;
		}
		tree = tree[this.selectedTree[i]];
	}
	
	return tree;
}

$pr.generateDropdown = function(depth,option)
{
	this.selectedDepth = depth;
	
	var rtn = $('<select class="depth_'+depth+'_ SPDrill_'+this.id+' SPSelecter"><option value="0">Select</option></select>');
	var p = this;
	rtn.change(function()
	{
		var op = $(this).attr('value');
		if(op != 0)
		{
			var vars = $(this).attr('class').split('_');
			var l = vars[1];
			p.setSelectedTree(l,op);
//			p.regenerate(l,op,parent);
		}
	});
	
	var selectedOption = this.getSelectedTree(depth);
	
	var length = 0;
	for(var id in selectedOption)
	{
		var op = selectedOption[id];
		if(typeof op.items != 'undefined')
		{
			length++;
			var title	= op.title;
			var id		= id;
			
			if(option == id)
			{
				rtn.append('<option selected="selected" value="'+id+'">'+title+'</option>');
			} else
			{
				rtn.append('<option value="'+id+'">'+title+'</option>');
			}
			
		}
	}
	
	if(length == 0)
	{
		return '';
	}
	
	return rtn;
}

$pr.getCategoryByTree = function(items)
{
	var base = this.tree;
	for(var i=0;i<items.length;i++)
	{
		var title = items[i];
		var friendly = title.replace(/[^a-zA-Z0-9]+/g,'');
		
		if(typeof base[friendly] == 'object')
		{
			base = base[friendly];
		} else
		{
			base[friendly] = {};
			base = base[friendly];
			base['title'] = title;
			base['items'] = [];
		}
	}
	
	return base;
}

delete $pr;


	$.fn.SaltPTable = function(options)
	{
		this.each(function()
		{
			var settings =
			{
				hasHeaderRow: false
			};
			
			if(options)
			{
				$.extend(settings, options);
			}
			
			var spt = new SaltPTable($(this),settings);
		});
	};