Dresolr = { };
Dresolr.variables = { 
	eIDurl: '?eID=tx_dresolr_eid',
    lang: {
        nl: {
            header: 'Top 4 resultaten'       
        }
    },
    keyPressed: 0
};

jQuery(document).ready(function() {
	
	// Trigger search actions
	//jQuery('#dossierSearchSubmit').click(function(){ Dresolr.querySolr(); return false; });	
});

/**
 * Search looks for checkboxes with the class "tx_dresolr_kbfilter",
 * then creates a query and calls the solr eID, which will return a JSON string
 */
Dresolr.querySolr = function() {
    this.variables.keyPressed = parseInt(this.variables.keyPressed) + 1;
    setTimeout("Dresolr.doQuerySolr("+this.variables.keyPressed+');', 500);
}
Dresolr.doQuerySolr = function(keyInt) {
    if (keyInt != this.variables.keyPressed) return;

	kbFilters = this.getKbFilters();

    var date = new Date().getTime();

	// Make ajax call to solr eID
	jQuery.ajax({
		type: 'POST',
		url: Dresolr.variables.eIDurl,
		data: {
			tx_dresolr_pi1: {
				kbFilters: kbFilters,
				query: jQuery('#dossierSearchInput').val(),
                currentPage: Dresolr.variables.currentPage
			},
            r: date
		},
		beforeSend: function() {
			jQuery('#dossierSearchResults').html('<div class="spinner"></div>');
		},
		success: function(data) {
			Dresolr.showResults(data);
		}
	});	
}

/**
 * Fill results in div id: tx_dresolr_pi1_results
 */
Dresolr.showResults = function(results) {		
	jQuery('#dossierSearchResults').html(results);
}

/**
 * Find all checkboxes with the class "tx_dresolr_kbfilter"
 */
Dresolr.getKbFilters = function() {
	var kbFilters = new Array();

	jQuery("input.kbfilter:checked").each(function() {
		kbFilters.push(jQuery(this).val());
	});    
    
    return kbFilters;
}

// Hide empty facets
jQuery(document).ready(function() {
    jQuery('#filters h3').each(function() {
       if(jQuery(this).next().children().length == 0) {
           jQuery(this).parent().hide();
       }
    });
})
