function getFilterSlug(curid){
	var curregx = /-/;
	var dashpos = curid.search(curregx);
	return curid.substring(0,dashpos);
}

function getInner(curid){
	var curregx = /duration-inner/;
	var dashpos = curid.search(curregx);
	return dashpos > -1;
}

function filterResults(){
	// turn everybody off
	$('.frame').hide();
	$('#noneavail').hide();
	var filters = new Array("river","exp","duration","rating");
	var filtered = new Array();
	
	
	// debug only 
	//$('div.heading').html('');
	// check to see which filters are NOT set to all, put them in filtered
	for( var i in filters){
		if( !$('a.check#'+filters[i]+'-all' ).hasClass("ischecked") ){
			filtered.push(filters[i]);
			//$('div.heading').append('filtering for: '+filters[i]);
			//$('div.heading').append('<br />');
		}				  
	}
	//$('div.heading').append('<br />');
	
	if( filtered.length == 0 ){  // no filters are on, better show everybody!
		$('.frame').show();
	}
	else {
		// with only the filtered categories, filter away
		for (var j in filtered){
			if (j == 0){
				$('a.check.ischecked[id^='+filtered[j]+']').each(function(){
	//				$('.frame:visible').not('.'+$(this).attr("id")).hide();
					$('.frame.'+$(this).attr("id")).show();
					//$('div.heading').append('subcats: '+$(this).attr("id"));
					//$('div.heading').append('<br />');
		
				});
			}
			else{
				$('a.check[id^='+filtered[j]+']').not('.ischecked').each(function(){
					$('.frame:visible.'+$(this).attr("id")).hide();
					//$('div.heading').append('hiding: '+$(this).attr("id"));
					//$('div.heading').append('<br />');
		
				});
			}
		}
		
		// if no trips are showing, better let the user know
		if( !$('.frame').is(":visible") ){
			$('#noneavail').show();
		}
		
		
	}

}

$(document).ready(function() {
   
   
	$('#mycarousel li').show();
   
   $('a.quest').click(function(){							 
			$('.c-thinbox').hide();
			$(this).next('.c-thinbox').show();
			return false;
	});
   
   $('a.closethinbox').click(function(){							 
			$(this).parents('.c-thinbox').hide();
			return false;
	});
   
   $('h3.faq a').click(function(){							 
			$(this).parent().next('.faqa').slideToggle();
			return false;
	});
	
	$('#mycarousel').jcarousel(
    	{
    		auto: 5,
    		scroll: 1,
			wrap: "both"
    	}
    );
	
	
    $('#nav li.active').parent().parent().addClass('line');
    $('.box-holder li').hover(
    	function(){
    		$(this).find('h3').addClass('hovered');
    		$(this).find('h4').css('color','#ff8b00');
    	},
    	function(){
    		$(this).find('h3').removeClass('hovered')
    		$(this).find('h4').css('color','#fff');
    	}
    );
    $('.box-holder li .box a').hover(
    	function(){
    		$(this).addClass('hovered');
    	},
    	function(){
    		$(this).removeClass('hovered')
    	}
    );
    $('a.check').click(function(){ 
			var curid = $(this).attr("id");
			var curslug = getFilterSlug(curid);
			var isInner = getInner(curid);
			if( $(this).hasClass("ischecked") ){ // check it OFF
			
				if( curid == curslug+'-all'){	// uncheck the ALL 
					$('a.check[id^='+curslug+']').addClass("ischecked");
					$('a.check[id^='+curslug+']').next('span').addClass("isdark");
					$(this).removeClass("ischecked");
					$(this).next('span').removeClass("isdark");
				}
				else{	// uncheck one inside a category
					$(this).removeClass("ischecked");
					$(this).next('span').removeClass("isdark");
					if(isInner){ // uncheck all children of the inner
						$(this).next().next().next('.inner').children('a.check').removeClass("ischecked");		
						$(this).next().next().next('.inner').children('span').removeClass("isdark");		
					}
					if( !$('a.check[id^='+curslug+']').hasClass("ischecked")){  // none are checked, better recheck the ALL
						$('a.check#'+curslug+'-all').addClass("ischecked");
						$('a.check#'+curslug+'-all').next('span').addClass("isdark");
					}
					
				}
			
			}
			else{	// check it ON
				
				if( curid == curslug+'-all'){	// checked the ALL 
					$('a.check[id^='+curslug+']').removeClass("ischecked");
					$('a.check[id^='+curslug+']').next('span').removeClass("isdark");
					$(this).addClass("ischecked");
					$(this).next('span').addClass("isdark");
				}
				else{	// checked one inside a category
					$('a.check#'+curslug+'-all').removeClass("ischecked"); // uncheck the ALL first
					$('a.check#'+curslug+'-all').next('span').removeClass("isdark");
					$(this).addClass("ischecked");
					$(this).next('span').addClass("isdark");
					$('.frame.'+curid).show();
					
					if(isInner){ // uncheck all children of the inner
						$(this).next().next().next('.inner').children('a.check').addClass("ischecked");		
						$(this).next().next().next('.inner').children('span').addClass("isdark");		
					}

				}
			
			}
			
			// now that every check has the correct state, let's filter.
			filterResults();
			
			return false;
				
    	});
	
});
