RWS.Search.PostFilters=
{
	Init:function()
	{
		this.mList = new Array();
		this.menuTpl = new Ext.XTemplate (
			'<tpl for=".">',
				'<div class = "postfilterItem">',
					'<span class = "font14" style = "text-align:right; position:relative; right:0; float:right">{count}</span>',					
					'<span class = "font14">',
							'{[ COMP.fitStringToWidth(values.name.toString(), 235, "font14")]}',
					'</span>',
				'</div>',
			'</tpl>'
		);
		this.recType = Ext.data.Record.create([{name:'name',type:'string'}, {name:'count',type:'number'}]);
	}
	,Add : function(name, str, anyMatch, reapplication)
	{
		reapplication = reapplication || false;
		
		// Default to false
		anyMatch = anyMatch || false;
		var fIdStr = str;
		if(fIdStr == '[blank]')
			fIdStr = /^$/;
		
		var filterID = name + '\t' + fIdStr + '\t' + anyMatch;
		
		if (!reapplication)
		{
			this.mList.add(filterID);
			var listCopy = this.mList.clone();
			RWS.Bread.AddCrumb(L(25082) + " " + RWS.Search.PostFilters.ConvertFilterName(name) + ": " + str, function() { RWS.Search.PostFilters.ReapplyFilters(listCopy); }); // Filter by
		}

		var stores = RWS.Search.Results.stores;
		stores.temp.removeAll();
		stores.temp.add(stores.main.getRange(0));
		stores.temp.filter(name, fIdStr, anyMatch);
		
		stores.main.removeAll();
		stores.main.add(stores.temp.getRange(0));
		
		if (!reapplication)
		{
			pageResults('start');
		}
		RWS.Layout.SearchPostFilters.loadData();
	}
	
	,ConvertFilterName : function(name)
	{
		switch (name)
		{
			case 'author':			return L(4085);
			case 'subjects':		return L(3693);
			case 'series':			return L(3692);
			case 'awards':			return L(12500);
			case 'year':			return L(4170);
			case 'medium':			return L(4090);
			case 'statusString':	return L(4121);
		}
		return name;
	}

	,Remove : function(name, str, anyMatch)
	{
		// Default to false
		anyMatch = anyMatch || false;

		for (var i=this.mList.length-1; i>=0; i--)
			if (this.mList[i] == (name + '\t' + str + '\t' + anyMatch))
				this.mList.remove(i);
		
		this.ReapplyFilters();
	}
	
	,ReapplyFilters : function(newList)
	{
		if (newList) this.mList = newList;
		
		this.Reset(false);

		for (var i=0; i<this.mList.length; i++)
		{
			var params = this.mList[i].split('\t');
			this.Add(params[0], params[1], params[2]=='true', true);
		}
			
		pageResults('start');
	}
	
	,Reset : function(display)
	{
		display = display || false;
		
		this.mList.remove(0,-1);
		RWS.Search.Results.stores.main.removeAll();
		RWS.Search.Results.stores.main.add(RWS.Search.Results.stores.original.getRange(0));

		if (display)
			pageResults('start');
	}
	
	// Everything in a particular list. eg: All Authors.
	// Also include a counter.
	// This needs to be rewritten using a Store for efficiency.
	,LoadFilterList : function(name, store)
	{
		store.removeAll();
		
		// Walk through all items in the current (filtered) selection,
		// and assemble our list. The 'find' function doesn't work
		// with empty strings so make a substitution.
		for (var i=0; i<RWS.Search.Results.stores.main.getCount(); i++)
		{
			var rec = RWS.Search.Results.stores.main.getAt(i);
			var matchStr = rec.data[name];
			if (matchStr=="") matchStr="[blank]";
			var existsAt = store.find('name',matchStr,0,false,true);
			if (existsAt == -1)
				store.add(new RWS.Search.PostFilters.recType({name:matchStr, count:1}));
			else
				store.getAt(existsAt).data.count++;
		}
		
		// Sort by the name field.
		store.sort('name','ASC'); // was 'count', 'DESC'
	}
};

RWS.Search.PostFilters.Init();

function CreatePostFilter(title,id,store)
{
	RWS.Search.PostFilters.LoadFilterList(title, store);
}

if (window.COMP) COMP.Broadcast('scriptLoaded_rws_postfilters');