/************************************
BROWSE
Written by Ethan Gruber, ewg4x@virginia.edu
Library: jQuery
Description: ajax for the browse page.  When a category is selected
it queries solr and returns the unique facets and displays them in #query_list.
Additionally, the term list is hidden once a term is selected, but it can be rendered
visible again by clicking a button.
************************************/

$(function () {
	$('.browse_category') .click(function () {
		$('.browse_category_selected') .each(function () {
			$(this) .attr('class', 'browse_category');
		});
		$(this) .attr('class', 'browse_category_selected');
		var name = $(this) .children('.category_link') .attr('name');
		var category = $(this) .children('.category_link') .attr('id');
		var query = category + ':[* TO *]';		
		$('#query_list') .show();
		$.get('get_browse_results', {
			q : query, fl: category, mode: 'browse', facet: 'true', 'facet.sort': 'false', 'facet.limit': '-1', 'facet.sort': category
		},
		function (data) {
			//get data for query terms
			$('#query_list') .html(data);
			$('#term_list') .fadeIn('slow');
			$('#term_list') .attr('class', 'term_list_visible');
			$('.query_term') .click(function () {
				$('#term_list') .attr('class', 'term_list_visible');
				$('#term_list') .hide();
				$('.term_header') .show();
				var query = $(this) .attr('id');
				var term = $(this) .attr('name');
				$('#term_list') .attr('class', 'term_list_hidden');
				$('.browse_term a') .each(function () {
					if ($(this) .attr('class') == 'query_term_selected') {
						$(this) .attr('class', 'query_term');
					}
				});
				$(this) .attr('class', 'query_term_selected');
				$.get('search_results', {
					q : query, start:0, rows:10, ajax: true
				},
				function (data) {
					$('#search') .html(data);
					$('.term_header') .html(name + ': ' + term);
				});
			});
			$('#toggle_browse') .show();
			$('#toggle_browse') .click(function () {
				if ($('#term_list') .attr('class') == 'term_list_visible') {
					$('#term_list') .fadeOut('slow');
					$('#term_list') .attr('class', 'term_list_hidden');
					$('.term_header') .show();
				} else if ($('#term_list') .attr('class') == 'term_list_hidden') {
					$('#term_list') .fadeIn('slow');
					$('#term_list') .attr('class', 'term_list_visible');
					$('.term_header') .hide();
				}
			});
		});
	});
});