(function($) {
	
	$.filtering = function(element, options) {
		var settings = {};

		element.data('filtering', this);
		
		var el = element;
		var $el = $(el);

		
		var current_index = 0;
		var last_index = 0;
		var total_slides = 0;
		var filters = [];
		var active_filter, active_category;
		
		init = function(element, options) {
		     
//try{ console.log('filtering'); }catch(e){}

			//settings = $.extend({}, $.filtering.defaultOptions, options, $.fn.filtering.notify); 

			attachEvents();
			
			active_filter = '';
			active_category = 'all';
			
			setHighlight();
			setCheckboxesRed();
			setCheckboxesWhite();
			//clearCheckboxesRed();
			//clearCheckboxesWhite();
		};
		attachEvents = function() {

			$('#winelist-nav').find('li a').bind('click', function(e){
//try{ console.log('click '+$(this).attr('rel')); }catch(err){}
					
					//e.stopImmediatePropagation();
					//e.preventDefault();
					
					applyFilter( $(this).attr('rel'), $(this).parent().find('ul').attr('id') );
					
					return false;
			});
			
			$('#winelist-nav').find('input[type=checkbox]').click(function(e){
//try{ console.log('click '+$(this).attr('rel')); }catch(err){}
					
					//e.stopImmediatePropagation();
					//e.preventDefault();
					
					/*if ($(this).attr('checked')){
						$(this).attr('checked',false);
					} else {
						$(this).attr('checked',true);
					}*/
					
					applyFilter( $(this).attr('rel'), $(this).parent().parent().attr('id') );
					
					//return false;
			});
		};

		applyFilter = function(new_filter, parent_group) {
	
//try{ console.log('applyFilter '+new_filter+' '+parent_group); }catch(e){}

			active_filter = new_filter;
			
			switch(new_filter){
				case 'all':
				
					//show all
					$('#all-whites').show().find('div').show(1);
					$('#all-reds').show().find('div').show(1);
					$('#whites-subnav').hide(1);
					$('#reds-subnav').hide(1);
					
					setCheckboxesRed();
					setCheckboxesWhite();
					active_category = "all";
					
					$('#all-reds').find('div').not(':visible').show();
					$('#all-whites').find('div').not(':visible').show();
					
					break;
				case 'all-reds':
					//show all reds and subnav
					$('#all-reds').show(1);//.find('div').show();
					$('#reds-subnav').fadeIn();
					//$('#all-reds').find('div').not(':visible').show();
					
					//hide all whites and subnav
					$('#all-whites').hide(1);
					$('#whites-subnav').hide(1);
					
					//handle checkboxes
					//setCheckboxesRed();
					//clearCheckboxesWhite();
					//updateWinelist(parent_group);
					active_category = 'all-reds';
					break;
				case 'all-whites':
					//show all whites and subnav
					$('#all-whites').show(1);//.find('div').show();
					$('#whites-subnav').fadeIn();
					//$('#all-whites').find('div').not(':visible').show();
					
					//hide all reds and subnav
					$('#all-reds').hide(1);
					$('#reds-subnav').hide(1);
					
					//handle checkboxes
					//setCheckboxesWhite();
					//clearCheckboxesRed();
					//updateWinelist(parent_group);
					active_category = 'all-whites';
					break;
				default:
					//make sure we can show what we need
					//$('#all-whites').show(1);//.find('div').hide();
					//$('#all-reds').show(1);//.find('div').hide();
					
//try{ console.log('default  '+ new_filter +' '+ parent_group   ); }catch(e){}
					
					var section = 'all-reds';
					if (parent_group == 'whites-subnav'){
						section = 'all-whites';
					}
					
try{ console.log('! '+ $('#'+parent_group).find('input:checkbox').length +' '+parent_group); }catch(e){}
					
					$('#'+parent_group).find('input:checkbox').each( function(){


						if ( $(this).is(':checked') ){
							
try{ console.log('!! '+ $('#'+section).find('.'+$(this).attr('rel') ).length +' '+$(this).attr('rel') ); }catch(e){}

							$('#'+section).find('.'+$(this).attr('rel') ).show();				
						} else {
							
							$('#'+section).find('.'+$(this).attr('rel') ).hide();
						}
					});
					
					//show it
					//$('.'+new_filter).show('fast');
					
//try{ console.log('whites '+  $('#all-whites').find('div:visible').length  ); }catch(e){}
//try{ console.log('reds   '+  $('#all-reds').find('div:visible').length  ); }catch(e){}
					
					/*//show hide red
					if ( $('#all-whites').find('div:visible').length < 1){
						$('#all-whites').hide();
					}
					if ( $('#all-reds').find('div:visible').length < 1){
						$('#all-reds').hide();
					}*/
					//updateWinelist(parent_group);
					break;
					
			}
				

			setHighlight(); 
		};
		
		setCheckboxesRed = function(){
			$('#reds-subnav').find('input:checkbox').attr('checked',true);
		};
		
		setCheckboxesWhite = function(){
			$('#whites-subnav').find('input:checkbox').attr('checked',true);
		};
		
		clearCheckboxesRed = function(){
			$('#reds-subnav').find('input:checkbox').attr('checked',false);
		};
		
		clearCheckboxesWhite = function(){
			$('#whites-subnav').find('input:checkbox').attr('checked',false);
		};
		
		updateWinelist = function(parental){
						
			var other = 'whites-subnav';
			var section = 'all-whites';
			if (parental == 'reds-subnav'){
				other = 'reds-subnav';
				section = 'all-reds';
			}
			
try{ console.log('updateWinelist  '+$('#winelist-nav').find('input:checkbox').length + ' '+parental+' '+section+' '+$('#'+section).is(':visible')); }catch(e){}


	
			//show sections for checked varietals
			$('#'+parental).find('input:checkbox').each(function(){
				
				if ( $(this).is(':checked')){
					
//try{ console.log('checkboxes  '+ $(this).attr('rel') + ' '+section); }catch(e){}
						
					$('#'+section).find('.'+$(this).attr('rel')).show();

				} else {
					$('.'+$(this).attr('rel')).hide();
				}
			});
			
			
		};
		
		setHighlight = function(){
			
try{ console.log('setHighlight '+active+' '+$('#winelist').find('a[rel="'+active+'"]').length  ); }catch(e){}

			$('#winelist-nav').find('a').removeClass('active');
			$('#winelist-nav').find('a[rel="'+active_category+'"]').addClass('active');

			
			/*$('#winelist-nav').find('li a').each(function(){
				
				var $this = $(this);
				
				if ($this.attr('rel') === active_filter){
					$this.addClass('active');
				} else {
					$this.removeClass('active');
				}
			});*/
		};

		init(element, options);
	};

	$.fn.filtering = function(options) {                   
		return this.each( function() {
			(new $.filtering($(this), options));              
		});        
	};


	$.filtering.defaultOptions = {
		//filter_list_id:''
	};

})(jQuery);
