var searching = false;
var attemptedSearch = false;

function FilterSearch()
{
	if(searching == false)
	{
		searching = true;
		$('.ajaxLoader').slideDown();
	    $('[name=filterForm]').submit();
	}
	else
	{
		attemptedSearch = true;
	}
}

function BindPagination()
{
	$('div.pagination a').click(function() {
		var href = $(this).attr('href');
		href = href.replace(/\//, '');
		$('[name=filterOffset]').val(href);
		
		FilterSearch();
		return false;
	});
	
	$('a.tagClass').click(function() {
		$('[name=clearAll]').click();
		$('ul.tagMinis li a:contains(' + $(this).text() + ')').click();
	});
}

$(function() {
    $('[name=filterForm]').ajaxForm(function(html) {
    	$('.filterReplace').html(html);
    	$.getScript("/system/application/assets/js/common/sifr-config.js");
    	BindPagination();
    	searching = false;
    	if(attemptedSearch == false) $('.ajaxLoader').slideUp();
    	if(attemptedSearch == true)
    	{
    		attemptedSearch = false;
    		FilterSearch();
    	}
    });	
    
	// bind clear all
	$('[name=clearAll]').click(function() {
		$('.tagMinis li.sel').click();	
	});
	// bind check all
	$('[name=checkAll]').click(function() {
		$('.tagMinis li:not(.sel)').click();	
	});

	// Bind all band toggles
	$('ul.allBandMinis li').click(function() {
		var checkedCount = $('ul.allBandMinis li.sel').length;
		
		if(checkedCount == 0)
		{
			$(this).addClass("sel");
			
			// update individual members
			$('ul.bandMinis li.sel').removeClass('sel');
			$('div.bandToggles input:checkbox[checked=true]').attr('checked', false);
			
			$('div.allBandToggles input:checkbox').attr('checked', true);
			
			FilterSearch();
		}
	});

	// Bind types
	$('ul.typeMinis li').click(function() {
		var index = $('ul.typeMinis li').index(this);
		
		$('ul.typeMinis li.sel').removeClass('sel');
		$(this).addClass("sel");
		
		$('div.typeToggles input:checkbox').attr('checked', false);
		$('div.typeToggles input:checkbox:eq(' + index + ')').attr('checked', true);
		
		FilterSearch();
	});

	// Bind band members
	$('ul.bandMinis li').click(function() {
		var index = $('ul.bandMinis li').index(this);

		if($(this).hasClass('sel'))
		{
			// Unselect it
			$('div.bandToggles input:checkbox:eq(' + index + ')').attr('checked', false);
			$(this).removeClass("sel");

			// How many band members left checked
			var checkCount = $('div.bandToggles input:checkbox[checked=true]').length;

			if(checkCount == 0)
			{
				$('ul.allBandMinis li').addClass('sel');
				$('div.allBandToggles input:checkbox').attr('checked', 'true');
			} 
		}
		else
		{
			// select it
			$('div.bandToggles input:checkbox:eq(' + index + ')').attr('checked', true);
			$(this).addClass("sel");

			$('ul.allBandMinis li.sel').removeClass('sel');
			$('div.allBandToggles input:checkbox').attr('checked', false);
		}
		
		FilterSearch();
	});
	
	$('ul.tagMinis li').click(function() {
		var index = $('ul.tagMinis li').index(this);
		
		if($(this).hasClass('sel'))
		{
			$('div.tagToggles input:checkbox:eq(' + index + ')').attr('checked', false);
			$(this).removeClass("sel");
		}
		else
		{
			$('div.tagToggles input:checkbox:eq(' + index + ')').attr('checked', true);
			$(this).addClass("sel");
		}
		
		FilterSearch();
	});
 
	$('input[name=filterSearch]').keyup(function() {
		var length = $('[name=filterSearch]').val().length;
		if(length>2 || length == 0) FilterSearch();
	});

	$('a.tagClass').click(function() {
		$('[name=clearAll]').click();
		$('ul.tagMinis li a:contains(' + $(this).text() + ')').click();
	});
});