

//console.log('filterBlock.js');

function initFilterBlocks() {

	//console.log('initFilterBlocks()');

	if( typeof(ajaxRedirection)!='undefined' && ajaxRedirection==true )
		return;

	_initFilterBlockFolding();
	_initDateSettingBlocks();

}

function _initFilterBlockFolding() {

	var filterBlock, title;

	filterBlock = $('.filterBlock');
	title = filterBlock.find('H4');

	title.click( _onFilterBlockTitleClicked );
	title.addClass('closed');
	title.css('cursor', 'pointer');

	title.hover(
		function() {$(this).addClass('hover')},
		function() {$(this).removeClass('hover')}
	);
	
}


function _onFilterBlockTitleClicked() {

	var clickedTitle, filterBlock, filterBlockContent, filterBlockContentIsVisible;

	clickedTitle = $(this);
	filterBlock = clickedTitle.parent().parent().parent();
	filterBlockContent = filterBlock.find('.content');
	filterBlockContentIsVisible = (filterBlockContent.css('display')=='none') ? false : true ;


	if( filterBlockContentIsVisible ) {

		// Hide !
		if( ! cdtRegistry.IE6 )
			filterBlockContent.slideUp( {duration: 300, easing: 'easeOutQuint'} );
		else
			filterBlockContent.hide();

		clickedTitle.addClass('closed');clickedTitle.removeClass('opened');
		filterBlock.removeClass('filterBlockWithBg');

	} else {

		// Show !
		if( ! cdtRegistry.IE6 )
			filterBlockContent.slideDown( {duration: 400, easing: 'easeOutBack'} );
		else
			filterBlockContent.show();

		clickedTitle.addClass('opened');clickedTitle.removeClass('closed');
		filterBlock.addClass('filterBlockWithBg');

	}

}

function _initSlider(element, name, value, limits, labels, step)
{

	$(element).each(function(){
	
		var input = $('<input type="hidden" name="'+name+'" value="'+value+'"/>');
		var slider = $('<div class="slider"/>');
		var label = $('<div class="label"/>');
	
		$(this).append(input);
		$(this).append(slider);
		$(this).append(label);
		
		slider.slider({
			min: limits[0],
			max: limits[1],
			value: value,
			step: step,
			slide: function(event, ui){
				input.val(ui.value);
				var i = ui.value > 1 ? 1 : 0;
				label.text(labels[i].split('[value]').join(ui.value));
			}
		});
		
		var i = value > 1 ? 1 : 0;
		label.text(labels[i].split('[value]').join(value));
		
	});

}

function _initRangeSlider(element, names, values, limits, labelTexts, step)
{

	$(element).each(function(){
	
		var minInput = $('<input type="hidden" name="'+names[0]+'" value="'+values[0]+'"/>');
		var maxInput = $('<input type="hidden" name="'+names[1]+'" value="'+values[1]+'"/>');
		
		if(values[1] == -1) values[1] = limits[1];
		
		var slider = $('<div class="slider"/>');
		var label = $('<div class="label"/>');
	
		$(this).append(minInput);
		$(this).append(maxInput);
		$(this).append(slider);
		$(this).append(label);
		
		slider.slider({
			range: true,
			min: limits[0],
			max: limits[1],
			values: values,
			step: step,
			slide: function(event, ui){
				minInput.val(ui.values[0]);
				
				if(ui.values[1] == limits[1]) maxInput.val(-1);
				else maxInput.val(ui.values[1]);
				
				if(ui.values[0] == limits[1]){
					label.text(labelTexts[1].split('[min]').join(ui.values[0]));
				}else{
					label.text(labelTexts[0].split('[min]').join(ui.values[0]).split('[max]').join(ui.values[1]));
				}
			}
		});
		
		label.text(labelTexts[0].split('[min]').join(values[0]).split('[max]').join(values[1]));
		
	});

}

function _initDateSettingBlocks() {

	var $dateFilterTypeBlocks;

	$dateFilterTypeBlocks = $('.filterBlock .content .dateFilterTypeBlock');

	//console.log('$dateFilterTypeBlocks.length='+$dateFilterTypeBlocks.length);

	if( $dateFilterTypeBlocks.length==0 )
		return;

	$dateFilterTypeBlocks.each( function() {

		var $currentDateFilterTypeBlock, $currentDateSettingBlock, $currentDateInput;
		var $datesTypesInputs, $customDateInput;

		$currentDateFilterTypeBlock = $(this);

		if( $currentDateFilterTypeBlock.hasClass('initialized') ) {
			//console.log('Date settings block already initialized...');
			//$currentDateFilterTypeBlock.find('INPUT[name="when"], LABEL').css( {backgroundColor: 'yellow'} );
			return;
		}

		$currentDateFilterTypeBlock.addClass('initialized');

		//console.log('A new date settings block is gonna be initialized...');

		$currentDateSettingBlock = $currentDateFilterTypeBlock.parents('.content').find('.dateSettingBlock');

		$currentDateInput = $currentDateSettingBlock.find('.dateInput');
		$currentDateInput.datepicker( {duration: 150} );

		$datesTypesInputs = $currentDateFilterTypeBlock.find('INPUT[name="when"], LABEL');
		$customDateInput = $datesTypesInputs.filter('INPUT[value="custom"]');

		//$datesTypesInputs.css( {backgroundColor: 'red'} );
		
		$datesTypesInputs.click( function() {

			var customDateInputRequired;
			var dateSettingBlockIsVisible;

			customDateInputRequired = ( $customDateInput.attr('checked')==true ) ? true : false ;
			dateSettingBlockIsVisible = ($currentDateSettingBlock.css('display')=='none') ? false : true ;

			if( customDateInputRequired && ! dateSettingBlockIsVisible )
				$currentDateSettingBlock.fadeIn('normal');
			else if( ! customDateInputRequired && dateSettingBlockIsVisible )
				$currentDateSettingBlock.fadeOut('fast');

		});

		

		// We may provide a unique ID to the block, which helps the browser to make difference between ".dateSettingBlock" instances...
		if( $currentDateFilterTypeBlock.attr('id').length==0 )
			$currentDateFilterTypeBlock.attr('id', 'dateFilterTypeBlock_' + (new Date()).getTime() );

	});
	

}

