/**
 * Подключение фильтров
 * 
 * @param	string	tableId
 * @param	hash	options
 * 					Возможные параметры:	array	filterList		Список возможных фильтров
 * 											string	filterActive	Активный фильтр
 * 					
 */
var RM_Etrain_Schedule_Filter = function (tableId, options)
{
	this.tableId = tableId;
	this.filterList = [];
	this.filterActive = null;
	this.classes = {};
	this.disableFilters = {};

	if ( options )
		for ( var i in options )
			this[i] = options[i];

	if ( this.filterList.length )
		for ( var i in this.filterList )
			this.bindEvent( this.filterList[i] );
};


/**
 * Установить событие
 * 
 * @param	string	filterType
 */
RM_Etrain_Schedule_Filter.prototype.bindEvent = function (filterType)
{
	var obSelf = this;
	if (this.disableFilters[filterType])
		return false;
	var linkObject = $('#filter-' + filterType);
	this.addClass(linkObject, filterType);
	$(linkObject).bind('click', function(){
		obSelf.filter(this, filterType);
		return false;
	});
};


/**
 * Фильтровать
 * 
 * @param	object	obElement
 * @param	string	filterType
 */
RM_Etrain_Schedule_Filter.prototype.filter = function( obElement, filterType )
{
	$('#' + this.tableId + ' tbody tr').css('display', 'none');

	if ($(obElement).attr( 'rel' ) != 'filtered')
	{
		this.filterActive = filterType;
		$(obElement).attr('rel', 'filtered');

		$('#' + this.tableId + ' tbody tr[rel=filter-' + filterType + ']').css('display', '');
		$('.filtersPages').hide();

		if (!$('#' + this.tableId + ' tbody tr:visible').length)
			$('#no-' + filterType + '-result').css('display', '');
	}
	else
	{
		this.filterActive = null;
		this.hideNoResult();
		
		$('#' + this.tableId + ' tbody tr[rel=]').css('display', '');
		$('.filtersPages').show();
	}
	this.togglePrice();
	this.resetInactiveFilters();
};


/**
 * Сбросить все неактивные фильтры
 */
RM_Etrain_Schedule_Filter.prototype.resetInactiveFilters = function()
{
	for ( var i in this.filterList )
		if ( !this.filterActive || ( this.filterActive && this.filterList[i] != this.filterActive ) )
			$('#filter-' + this.filterList[i]).removeAttr( 'rel' );
};


/**
 * Спрятать все надписи об отсутствующих результатах
 */
RM_Etrain_Schedule_Filter.prototype.hideNoResult = function()
{
	for ( var i in this.filterList )
		$('#no-' + this.filterList[i] + '-result').css( 'display', 'none' );
};


/**
 * Показать/спрятать цену при включенных фильтрах
 */
RM_Etrain_Schedule_Filter.prototype.togglePrice = function ()
{
	if ($('#openFullPriceLink').attr('href') == '#')
	{
		if (this.priceLink == undefined)
		{
			this.priceLink = $('<a/>', {
				'href'	: url.get('etrain.info'),
				'id'	: 'infoPriceLink',
				'style'	: 'display:none'
			});
			this.priceLink.html(lang.get('etrain.common.ticket_price'));
			$('#openFullPriceLink').parent().append(this.priceLink);
		}
		this.filterActive ? $('#openFullPriceLink').hide() : $('#openFullPriceLink').show();
		this.filterActive ? $('#infoPriceLink').show() : $('#infoPriceLink').hide();
	}
}


/**
 * Повесить CSS классы на ссылку фильтрации
 */
RM_Etrain_Schedule_Filter.prototype.addClass = function (obElement, filterType)
{
    $(obElement).addClass(this.classes[filterType]);
}
