var QuickSearchFilter = Class.create({
	initialize: function(json)
	{
		if(Object.isUndefined(json))
		{
			console.log("Filter: no JSON provided");
			return;
		}
		
		Object.extend(this, json);
	},
	
	render: function(value)
	{
		this.dropDownContainer = Builder.node('div');
		
		var tbl = (typeof(this.table) != 'string') ? this.table.join('-') : this.table; 
		
		this.labelElem = Builder.node('label', {'for': tbl}, this.label);
		this.dropDownContainer.insert(this.labelElem);
		
		this.dropDownSelect = Builder.node('select', {className: 'qs-filter', name: tbl});
		this.dropDownSelect.observe('change', this.filterChanged.bindAsEventListener(this));
		this.dropDownContainer.insert(this.dropDownSelect);
		
		var blank_option = Builder.node('option', {value: -1}, "Please select...");
		this.dropDownSelect.insert(blank_option);
		
		if(value == -1)
		{
			// Empty dependant
			this.dropDownSelect.addClassName('qs-dependant');
		}
		else if(Object.isUndefined(value) || Object.isUndefined(this.itemsLookup[value]))
		{
			if(Object.isUndefined(value))
			{
				// If no value then we must not be a dependant so we should show all items!
				this.items.each( function(item) {
					var ttl = item.title.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
					var option = Builder.node('option', {value: item.id}, ttl);
					this.dropDownSelect.insert(option);
				}.bind(this));
			}

			this.dropDownSelect.value = -1;
		}
		else
		{
			// We're a dependant so only show the relevant items
			this.itemsLookup[value].each( function(item) {
				var ttl = item.title.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
				var option = Builder.node('option', {value: item.id}, ttl);
				this.dropDownSelect.insert(option);
			}.bind(this));
			
			this.dropDownSelect.addClassName('qs-dependant');
			
			this.dropDownSelect.value = -1;
		}
		
		if(this.hasDependant())
		{
			var cont = Builder.node('div');
			cont.insert(this.dropDownContainer);
			
			this.dependant.render($F(this.dropDownSelect));
			cont.insert(this.dependant.getSurface());
			this.surface = cont;
		}
		else
			this.surface = this.dropDownContainer;
	},
	
	filterChanged: function(event)
	{
		if(this.hasDependant())
		{
			var value = $F(this.dropDownSelect);
			
			if(!Object.isUndefined(value))	
			{
				var child = this.dependant.dropDownContainer;

				if(Object.isElement(child))
					child.remove();
					
				this.dependant.render(value);
				this.surface.insert(this.dependant.getSurface());
			}
		}
		
		document.fire('quicksearch:clear');
		document.fire('quicksearch:changed');
	},
	
	getSurface: function()
	{
		return this.surface;
	},
	
	addDependant: function(filter)
	{
		this.dependant = filter;
		this.dependant.cacheItems();
	},
	
	hasDependant: function()
	{
		return !Object.isUndefined(this.dependant);
	},
	
	// Function for dependants to be able to sort their results based on parents selected id
	cacheItems: function()
	{
		this.itemsLookup = new Object();
		
		this.items.each(function(item){
			if(item.parent_id)
			{
				if(Object.isUndefined(this.itemsLookup[item.parent_id]))
					this.itemsLookup[item.parent_id] = new Array();
					
				this.itemsLookup[item.parent_id].push(item);
			}
		}.bind(this));
	},
	
	getTags: function()
	{
		var value = $F(this.dropDownSelect);
		var tags = new Array();
		
		if(value && value > 0)
		{
			var str = '';

			str = (this.tag_prefix.length)? this.tag_prefix + ":":this.tag_prefix;

			str += this.dropDownSelect.options[this.dropDownSelect.selectedIndex].text;
			
			if(this.hasDependant())
			{
				var d_tags = this.dependant.getTags();
				str += ":" + d_tags;

				// tags = tags.concat(d_tags);
			}
			
			tags.push(str);
		}

		return tags;
	}
});
