function Filter(opts) {
	this.opt = opts;
	var html = '';
	this.filter = {column:'', val:''};
	var me;
	this.lock = false;
	this.firstDisplay = true;
	
	this.init = function() {
		if (this.opt.autoDisplaySingleRow == undefined) {
			this.opt.autoDisplaySingleRow = true;
		}
		
		me = this;
		if (this.opt.templateUrl == undefined) {
			this.render();
		} else {
			$.ajax({
				url: this.opt.templateUrl,
				type: 'GET',
				dataType: 'html',
				success: function(data) {
					$('#'+me.opt.div).html(data);
					me.render();
				}
			});
		}
	}
	
	this.render = function(filter) {
		html = '';
		if (filter == undefined) {
			filter = {column: '', val: ''};
		}
		this.filter = filter;
		me = this;
		if (this.firstDisplay) {
			this.firstDisplay = false;
			if (this.opt.noInitialData && this.opt.uri.getUri() == '') {
				return;
			}
		}
		this.getData();
	}
	
	this.getData = function(filter) {
		me = this;
		this.displayLoadingIcon();
		$.ajax({
			url: this.opt.url,
			type: 'POST',
			data: {action: this.opt.action, page: this.opt.page,category: this.opt.category, filter_column: this.filter.column, filter_val: this.filter.val, uri: me.opt.uri.getUri()},
			dataType: 'json',
			success: function(data) {
				me.displayData(data);
			}
		});
	}
	
	this.displayData = function(data) {
		nbData = 0;
		firstId = '';
		selectedN = -1;
		$.each(data, function(i, val){
			nbData++;
			selected = '';
			if (firstId == '') {
				firstId = val.id;
			}
			if (val.__IsSelected == '1') {
				selected = 'filter-selected';
				me.currentSelection = val.id;
				selectedN = nbData - 1;
				if (me.opt.child != undefined) {
					me.opt.child.addFilter({column:me.opt.column, val:val.id});
				}
			}
			html+= '<tr><td><div class="'+me.opt.div+'-item filter-item '+selected+'" id="'+val.id+'">'+val.data+' ('+val.nb_media+')</div></td></tr>';
		});
		this.endRender();
		
		if (selectedN != -1) {
			$('#'+me.opt.div+'-contents tr')[selectedN].scrollIntoView();
		}
		
		$(".filter-item").mouseover(function() {
			$(this).addClass("filter-over");
		}).mouseout(function() {
			$(this).removeClass("filter-over");
		});
		
		$('.'+this.opt.div+'-item').click(function(){
			me.onClick(this);
		});
		
		$('.filter-item').dblclick(function(){return false;});
		
		if (nbData == 1 && this.opt.autoDisplaySingleRow && (this.opt.uri.getUri() == '')) {
			$('#'+me.opt.div+'-contents  *.filter-item:first').trigger('click');
		}
	}
	
	this.onClick = function(target) {
		if (me.lock == true) {
			return;
		}
		me.lock = true;
		me.opt.uri.clear();
		
		if (me.opt.child != undefined) {
			if (me.opt.displayGridBeforeChild) {
				me.opt.grid.render([{column:me.opt.column, val:target.id}]);
			} else {
				me.opt.grid.displayEmptyData();
			}
			me.opt.child.render({column:me.opt.column, val:target.id});
		} else {
			me.opt.grid.render([{column:me.opt.column, val:target.id}, {column:me.filter.column, val:me.filter.val}]);
		}
		setTimeout(function() {
			me.lock = false;
		}, 500);
		
		if (!$(target).hasClass("filter-selected")) {
			$('#'+me.opt.div+'-contents  *.filter-selected').removeClass("filter-selected");
			$(target).addClass("filter-selected");
		}
	}
	
	this.addFilter = function(filter) {
		this.filter = filter;
	}
	
	this.endRender = function() {
		$('#'+this.opt.div+'-contents').html('<table class="filter" width="100%" cellspacing="0" cellpadding="0" id="'+this.opt.div+'-grid">'+html+'</table>');
	}
	
	this.displayLoadingIcon = function() {
		$('#'+this.opt.div+'-contents').html('<div style="text-align: center;vertical-align: bottom;position: relative;top: 40%;"><img src="/_images/loading_2.gif"/></div>');
	}
}
