//JQUERY DOCUMENT
var oTable,oSettings;
/* Custom filtering function */
$.fn.dataTableExt.afnFiltering.push(
	function( oSettings, aData, iDataIndex ) {
		var range = $("#ajout").attr('value');
		
		var dateAjout = new Date(aData[18].replace(/-/g,"/"));
		timeAjout = dateAjout.getTime();
		var dateToday = new Date();
		dateToday.setHours(0);
		dateToday.setMinutes(0);
		dateToday.setSeconds(0);
		var dateSelect = new Date(dateToday.setDate(dateToday.getDate() - range));
		timeSelect = dateSelect.getTime();

		if(range===''){
			return true;
		}else if(timeSelect <= timeAjout){
			return true;
		}
		return false;
	}
);

$(document).ready(function(){
	//Data Table
	//---------------------------------------------------
	$(".dataTables_criteres select").attr("selectedIndex",0);
	$(".dataTables_criteres input[type='text']").attr("value","");
	oTable = $("#emplois").dataTable({
		//Tri initial
		"aaSorting":[[18,'desc'],[2,'asc']],
		// Paramètres Largeur des colonnes automatique
		"bAutoWidth": false,
		//Paramètres Colonnes
		"aoColumns": [
			//ID
			{"bVisible":false},
			//Publication
			{"bVisible":false},
			//Appellation
			{"sWidth":"210px"},
			//Employeur
			{"sWidth":"129px"},
			//Poste(s)
			{"bVisible":false},
			//Scolarité
			{"bVisible":false},
			//Expérience
			{"bVisible":false},
			//Salaire
			{"bVisible":false},
			//Horaire
			{"bVisible":false},
			//Heures
			{"bVisible":false},
			//Statut
			{"bVisible":false},
			//Lieu
			{"bVisible":false},
			//Durée
			{"bVisible":false},
			//Langue
			{"bVisible":false},
			//Disponibilité
			{"bVisible":false},
			//Lien
			{"bVisible":false},
			//Catégorie
			{"bVisible":false},
			//Territoire
			{"sWidth":"100px"},
			//Date
			{"sType":"date","sWidth":"90px"},
			//Consulter
			{"bSortable":false,"bSearchable":false,"sWidth":"65px"}
		],
		// Sorting Class
		"bSortClasses": false,
		// Paramètres Enregistrement de l'état
		"bStateSave": false,
		// Nombre de résultats affichés
		"iDisplayLength": 5,
		//Paramètres Langue
		"oLanguage": {
					"sLengthMenu": 'Afficher <select><option value="5" selected="selected">5</option><option value="10">10</option><option value="15">15</option></select> résultats par page',
					"sZeroRecords": "Aucun résultat à afficher",
					"sInfo": "Résultat _START_ à _END_ | _TOTAL_ au total",
					"sInfoEmtpy": "Aucun résultat à afficher",
					"sInfoFiltered": "",
					"sSearch": "Rechercher: ",
					"sProcessing": "En cours...",
					"oPaginate":{
						"sFirst": "Début",
						"sLast": "Fin",
						"sNext": "Suivant",
						"sPrevious": "Précédent"
					}
				},
		//Paramètres Pagination
		"sPaginationType": "full_numbers",
		//Positionnement des éléments
		"sDom": 'lptip'
	});
	oSettings = oTable.fnSettings();
	// Recherche Section Emplois
	//---------------------------------------------------
	$("#territoires").change(function(){
		if($(this).val()===''){
			$(this).removeClass('critereOn');
		}else{
			$(this).addClass('critereOn');
		}
		oTable.fnFilter($(this).val(),17);
	});
	$("#categories").change(function(){
		if($(this).val()===''){
			$(this).removeClass('critereOn');
		}else{
			$(this).addClass('critereOn');
		}
		oTable.fnFilter($(this).val(),16);
	});
	$("#ajout").change(function(){
		if($(this).val()===''){
			$(this).removeClass('critereOn');
		}else{
			$(this).addClass('critereOn');
		}
		oTable.fnDraw();
	});
	$("#filtre").keyup(function(){
		if($(this).val()===''){
			$(this).removeClass('critereOn');
		}else{
			$(this).addClass('critereOn');
		}
		oTable.fnFilter($(this).val());
	});
	$("#reset").click(function(){
		for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
			oSettings.aoPreSearchCols[ iCol ].sSearch = '';
		}
		oTable.fnFilter('');
		$(".dataTables_criteres select").removeClass('critereOn').find('option:first').attr('selected','selected');
		$("#filtre").removeClass('critereOn').val("");
		return false;
	});
});
