// JavaScript Document
var paged = 1;

(function(jQuery) {
	jQuery.fn.figfilters = function(settings) {
		
		function _initialize(){
			jQuery('#filter-menu .cat-item a, a.tag-item').each(function(i){
				jQuery(this).click(function(){
					jQuery('#filter-menu .cat-item a, a.tag-item').each(function(ii){
						if(ii!=i){
							if(!jQuery(this).hasClass("tag-item")){
								jQuery(this).parent().removeClass("current-cat");
							} else {
								jQuery(this).removeClass("current-tag");
							}
						} else {
							if(!jQuery(this).hasClass("tag-item")){
								jQuery(this).parent().addClass("current-cat");
							} else {
								jQuery(this).addClass("current-tag");
							}
						}
					});
					getAjaxRequest(this);
					return false;
				});
			});
			jQuery('#filter-menu #tags').click(function(){
				jQuery('#tags-list').slideToggle("fast");
				return false;
			});
			jQuery('#tags-list a').click(function(){
				return false;
			});
		}
		
		function gatherInlineFilters(){
			jQuery('.fig-links .cat-item').each(function(i){
				jQuery(this).click(function(){
					var href = jQuery(this).attr("href");
					jQuery('#filter-menu .cat-item a, a.tag-item').each(function(){
						if(jQuery(this).attr("href")==href){
							jQuery(this).parent().addClass("current-cat");
						} else {
							jQuery(this).parent().removeClass("current-cat");
							jQuery(this).removeClass("current-tag");
						}
					});
					getAjaxRequest(this);
					return false;
				});
			});
		}
		
		function getAjaxRequest(t){
			var href = jQuery(t).attr("href");
			jQuery('#more-fig').removeClass("off");
			jQuery('#more-fig a').attr("href",href).addClass("loading");
			paged = 1;
			if(!jQuery('#filter-menu').hasClass("loading")){
				jQuery('#filter-menu').addClass("loading");
				jQuery('#fig-list-container').removeAttr("style");
				jQuery("#fig-list").slideUp("slow", function(){
					jQuery(this).html('');
					jQuery.ajax({
						type: "GET",
						url: href,
						data: "fig-filter=1&filter-paged="+paged,
						success: function(msg){
							jQuery('#filter-menu').removeClass("loading");
							jQuery("#fig-list").html(msg).slideDown("slow", function(e){
								jQuery(document).scrollTo('#content-filter',300);
							});
							jQuery('#more-fig a').removeClass("loading");
						}
					});	
				});
				
			}	
		}
		
		_initialize();
		//gatherInlineFilters();

	};
})(jQuery);