/**
 * fichier de class js pour gérer les listes du module de recherche
 */	
$(document).ready( myLoadSearch );

function myLoadSearch()
{
	// calcul du nb resultats en fonction des params du form
	xajax_xjsGetNbResults( 'init' , $('#box_search_form').serialize() ); 
	
	// création des observateurs
	$( '#theme' ).bind('change', updateParams );
	$( '#pays' ).bind('change', updateParams );
	$( '#continent' ).bind('change', updateParams );
	$( '#mois' ).bind('change', updateParams );
	$( '#typologie' ).bind('change', updateParams );
	$( "#slider-range" ).bind( "slidestop", updateParams);

	
}

function updateParams() 
{
	// update des listes deroulantes
	xajax_xjsUpdateParams( $(this).attr('id') , $('#box_search_form').serialize() );
	
	// calcul du nb resultats en fonction des params du form
	xajax_xjsGetNbResults( $(this).attr('id') , $('#box_search_form').serialize() );
}

function initMultiselect( context ){
	var arrOptions = {
	        selectAll: true,
	        selectAllText: 'Tout selectionner',
	        noneSelected: 'Check some boxes!',
	        oneOrMoreSelected: '*'
	    }

	if( ( context == 'all' || context == 'continent' ) && $('#box_search_form #continent').length )$('#box_search_form #continent').multiSelect( $.extend(arrOptions, {noneSelected: 'Choisissez un continent'}) , $.proxy( updateParams , $('#continent') ) );
	if( ( context == 'all' || context == 'pays' ) && $('#box_search_form #pays').length )$('#box_search_form #pays').multiSelect( $.extend(arrOptions, {noneSelected: 'Choisissez un pays'}) , $.proxy( updateParams , $('#pays') ) );
	if( ( context == 'all' || context == 'mois' ) && $('#box_search_form #mois').length )$('#box_search_form #mois').multiSelect( $.extend(arrOptions, {noneSelected: 'Quand partez-vous ?'}) , $.proxy( updateParams , $('#mois') ) );
	if( ( context == 'all' || context == 'theme' ) && $('#box_search_form #theme').length )$('#box_search_form #theme').multiSelect( $.extend(arrOptions, {noneSelected: 'Une route selon vos envies'}) , $.proxy( updateParams , $('#theme') ) );
	if( ( context == 'all' || context == 'typologie' ) && $('#box_search_form #typologie').length )$('#box_search_form #typologie').multiSelect( $.extend(arrOptions, {noneSelected: 'A chacun sa route'}) , $.proxy( updateParams , $('#typologie') ) );
	
}

/**
 * activation du multiselect
 */
;(function($) {
	$(document).ready(function() {
		initMultiselect( 'all' );
	});
})(jQuery);

/**
 * activation du tri des resultats de package
 */
;(function($) {
	$(document).ready(function() {
		if( !$('.p-resultats #ordreSearch').length )return;
		
		// on ecoute le click sur un des liens du sous menu
		$('.p-resultats #ordreSearch').change(function(){
			location.href = '/resultats.html?ordreSearch=' + $(this).val() ;
		});
	});
})(jQuery);
