// Sorter JS
// by ejik ;)

// clases vars
var web = ".category-web";
var mobile = ".category-mobile";
var ui = ".category-ui";
var dtp = ".category-print";
var others = ".category-others";

var checkInterval;
var uncheckInterval;

// behaviour
function check(target) {
   clearInterval(checkInterval);
   uncheck(target);
   
   $tBoxes =  $(".list a:not(.inactive)").not(target);
   var itemsNr = $tBoxes.length;
   function quecheck() {
	   $tBoxes.eq(i).addClass("inactive");
	   $tBoxes.eq(i).children().animate({opacity: "0.15"}, 750, "easeOutCubic");
	   i++;
	   if (i >= itemsNr) {
		   clearInterval(checkInterval);
	   }
   };
   var i = 0;
   checkInterval = setInterval(quecheck, 150);
};
function uncheck(target) {
	clearInterval(uncheckInterval);	
	
	if (target) {
		$aBoxes = $(".list a.inactive").filter(target);
	} else {
		$aBoxes = $(".list a.inactive");
	};
	var itemsNr = $aBoxes.length;
	function queuncheck() {
		$aBoxes.eq(i).removeClass("inactive");
		$aBoxes.eq(i).children().animate({opacity: "1"}, 750, "easeInCubic");
		i++
		if (i >= itemsNr) {
			clearInterval(uncheckInterval);
		};
	};
	var i = 0;
	uncheckInterval = setInterval(queuncheck, 150);
};
function selectBtn(button) {
	$(button).addClass("selected");
	$("#menu li").not(button).removeClass("selected");
};

// events
$(document).ready(function() {
		  					 $("#all").click(function() {
								  uncheck();
								   selectBtn(this);
													});
							 $("#web").click(function() {
  								 check(web)
								  selectBtn(this);
														});
							 $("#mobile").click(function() {
								 check(mobile);
								  selectBtn(this);								 
														 });
							 $("#ui").click(function() {
								 check(ui);
								  selectBtn(this);								 
													 });
							 $("#print").click(function() {
								 check(dtp);
								  selectBtn(this);								 
														});
							 $("#others").click(function() {
							     check(others);
								  selectBtn(this);								 
														 });
						   });