/**
 * Code by Giancarlo "GM" Moschitta (info@myphp.it) and Negatyve (http://www.negatyve.com)
 * Powered by jQuery (http://jquery.com))
**/
/**
 * Avvia tutti gli script necessari ad una pagina di scelta
**/
function initDigitalChoice()
{
	$( '#advanced-search-form-cat' ).change
	(
		function(){ categorySelectChanged( $( this ) ); }
	);
	$( '#advanced-search-form-send, #advanced-search-form-send-bottom' ).click
	(
		function(){ return searchButtonClicked( $( this ) ); }
	);
	if( $('#advanced-search-form-cat').html() != null )
	{
		categorySelectChanged( $('#advanced-search-form-cat') );
	}
}

/**
 * Chiude tutti i pannelli dei filtri
**/
function collapseAllFiltersPanels()
{
	$( 'div.search-filter' ).each
	(
		function(){ searchFilterClose( $(this) ) }
	);
}

/**
 * Apre tutti i pannelli dei filtri
**/
function expandAllFiltersPanels()
{
	$( 'div.search-filter' ).each
	(
		function(){ searchFilterOpen( $(this) ) }
	);
}

/**
 * Gestisce il click sul pulsante cerca
 *
 * @param button, jQuery, required, Il pulsante cliccato
**/
function searchButtonClicked( button )
{
	var category = $('#advanced-search-form-cat').val();
	if( category != 0 )
	{
		var makers = '';
		var attr = new Array();
		var cookie = new Array();
		$( '.search-filter-body form' ).each
		(
			function()
			{
				var selected = $(this).find(':checked').val();
				var id = $( this ).attr('name');
				id = id.substr( id.lastIndexOf('-') + 1 );
				var select = id + ':';
				select += ( $(this).parent().css('display') == 'block' ? '1' : '0' ) + ':';
				select += ( $(this).parent().find('.list-hide').html() != null ? '1' : '0' ) + ':';
				if( selected != undefined )
				{
					if( id == 'makers' )
					{
						makers = '&mi=' + selected;
					} else {
						attr.push( selected );
					}
					select += selected;
				} else {
					select += '0';
				}
				cookie.push( select );
			}
		);
		var joined = attr.join('|');
		$.cookie( 'adv_choice', category + '<SEP>' + cookie.join('|'), { path: '/' } );
		window.location.href = CURRENT_PAGE_URL + '?gc=1' + makers + '&ci=' + category + ( joined != '' ? '&attr=' + Base64.encode( joined ) : '' );
		return true;
	} else {
		showAlert('Devi selezionare una categoria!');
		return false;
	}
}


/**
 * Assegna i gestori di evento
**/
function assignEvents()
{
	$( '.collapsed .search-filter-head' ).click
	(
		function(){ searchFilterOpen( $(this).parent() ); return false; }
	);
	$( '.expanded .search-filter-head' ).click
	(
		function(){ searchFilterClose( $(this).parent() ); return false; }
	);
	$( '.search-filter-body a.list-show' ).click
	(
		function(){ searchFilterShowList( $(this) ); return false; }
	);
	$( '.search-filter-body a.list-hide' ).click
	(
		function(){ searchFilterHideList( $(this) ); return false; }
	);
	$( '.search-filter-body input' ).click
	(
		function(){ filterValueChanged( $(this) ); }
	);
	$( 'a.expand-all-filters' ).click
	(
		function(){ expandAllFiltersPanels(); return false; }
	);
	$( 'a.collapse-all-filters' ).click
	(
		function(){ collapseAllFiltersPanels(); return false; }
	);
	$( 'a.remove-option' ).unbind().click
	(
		function(){ removeOption( $(this)); return false; }
	);
}


/**
 * Rimuove l'opzione selezionata
 *
 * @param link, jQuery, required, Il link cliccato
**/
function removeOption( link )
{
	var div = link.parent().parent();

	div.find('input').removeAttr('checked');

	link.parent().find('span').html('...');
	link.remove();
}

/**
 * Gestisce il cambio di valore di un gruppo di radiobuttons
 *
 * @param radio, jQuery, required, Il radio button cliccato
**/
function filterValueChanged( radio )
{
	var maxLen = 40;
	var label = radio.parent().find('label[for=' + radio.attr('id') + ']').html();
	label = html_entities_decode( label );
	if( label.length > maxLen )
	{
		label = label.substr(0, maxLen - 3 ) + '...';
	}
	label = html_entities_encode( label );

	var footer = radio.parent().parent().parent().parent().find('.search-filter-foot');
	footer.find('span').html( label );
	if( footer.find('a').html() == null )
	{
		$('<a href="#" class="remove-option" title="Rimuovi" />')
		.html('<span class="hidden">[X]</span>')
		.click( function(){ removeOption( $(this)); return false; } )
		.prependTo( footer );
	}
}

/**
 * Mostra tutta la lista dei filtri di ricerca
 *
 * @param link, jQuery, required, Il link cliccato
**/
function searchFilterShowList( link )
{
	link.parent().find('.more').removeClass('hidden');
	link
	.unbind()
	.removeClass()
	.addClass( 'list-hide' )
	.click( function(){ searchFilterHideList( $(this) ); return false; } )
	.attr( 'title', 'Nascondi' )
	.find( 'span' ).html( 'Nascondi' );
}

/**
 * Nasconde parte della lista dei filtri di ricerca
 *
 * @param link, jQuery, required, Il link cliccato
**/
function searchFilterHideList( link )
{
	link.parent().find('.more').removeClass('hidden').addClass( 'hidden' );
	link
	.unbind()
	.removeClass()
	.addClass( 'list-show' )
	.click( function(){ searchFilterShowList( $(this) ); return false; } )
	.attr( 'title', 'Mostra tutti' )
	.find( 'span' ).html( 'Mostra tutti' );
}

/**
 * Espande un filtro di ricerca
 *
 * @param filter, jQuery, required, Il div del filtro
**/
function searchFilterOpen( filter )
{
	filter
	.find( '.search-filter-head' )
	.unbind()
	.click( function(){ searchFilterClose( $(this).parent() ); return false; } )
	.find( 'a' )
	.attr( 'title', 'Chiudi' )
	.find( 'span' ).html( 'Chiudi' );

	filter.find( '.search-filter-body' ).show();/*slideDown();*/
	filter.removeClass().addClass( 'search-filter' ).addClass( 'expanded' );
}

/**
 * Collassa un filtro di ricerca
 *
 * @param filter, jQuery, required, Il div Del filtro
**/
function searchFilterClose( filter )
{
	filter
	.find( '.search-filter-head' )
	.unbind()
	.click( function(){ searchFilterOpen( $(this).parent() ); return false; } )
	.find( 'a' )
	.attr( 'title', 'Espandi' )
	.find( 'span' ).html( 'Espandi' );

	filter.find( '.search-filter-body' ).hide();/*slideUp();*/
	filter.removeClass().addClass( 'search-filter' ).addClass( 'collapsed' );
}

/**
 * Gestisce il cambio di elemento del select delle categorie
 *
 * @param select, jQuery, required, Il riferimento al select
**/
function categorySelectChanged( select )
{
	var category = select.val();
	showSearchButton( false );
	$('.one-col').empty();
	if( category != 0 )
	{
		showAjaxLoader( $('#advanced-search-form'), 624, 435 );
		$.ajax
		(
			{
				type: 'POST',
				url: SITE_PATH + 'a/ajax.php',
				data: 'c=scelta&m=getFilters&category=' + category,
				success: function( result )
				{
					hideAjaxLoader( $('#advanced-search-form') );
					if( ajaxError( result ) )
					{
						return false;
					}
					$('.one-col').html( result );
					assignEvents();
					showFiltersButtons( result != '' );
					showSearchButton( true );
				}
			}
		);
		//$('#advanced-search-form-send, #advanced-search-form-send-bottom').removeClass('disabled');
	} else {
		showFiltersButtons( false );
		//$('#advanced-search-form-send, #advanced-search-form-send-bottom').removeClass('disabled').addClass('disabled');
	}
}

/**
 * Mostra il pulsante di se richiesto
 *
 * @param show, boolean, required, True per mostrare il pulsante
**/
function showSearchButton( show )
{
	if( show )
	{
		$('#advanced-search-form-send').show();
	} else {
		$('#advanced-search-form-send').hide();
	}
}

/**
 * Mostra i pulsanti se richiesto
 *
 * @param show, boolean, required, True per mostrare i pulsanti
**/
function showFiltersButtons( show )
{
	if( show )
	{
		$('.search-send, .filters-control-buttons').show();
	} else {
		$('.search-send, .filters-control-buttons').hide();
	}
}

$( document ).ready( initDigitalChoice );
