var ns_chunkfinder = new Class({
	
	element: {},
	
	current_filter: '',
	current_value: '',
	
	initialize: function(in_element){
		
		this.element = $(in_element);
		
		//	run setup
		this.setup();
	},
	
	setup: function(){
		
	},
	
	filter: function(in_key, in_value){
		
		this.current_filter = in_key;
		this.current_value = in_value;
		
		var fxi = 0;
		
		if(!in_value){
			
			//	show all
			this.element.getElements('.box').each(function(el,i){
				if(el.hasClass('box') && el.hasClass('hide')){
					el.setStyles({'left': 1000 - (Math.random() * 3000) + 'px', 'top': '-150px', 'display': 'block'});
					if(el.fx) el.fx.cancel();
					el.fx = new Fx.Morph(el, {duration: 1000, onComplete: function(){ el.removeClass('hide'); }}).start({'opacity': 1});
					fxi++;
				}
			});
			
		} else {
		
			//	show applied selection
			this.element.getElements('.'+in_key+'_'+in_value).each(function(el,i){
				if(el.hasClass('box') && el.hasClass('hide')){
					el.setStyles({'left': 1000 - (Math.random() * 3000) + 'px', 'top': '-150px', 'display': 'block'});
					if(el.fx) el.fx.cancel();
					el.fx = new Fx.Morph(el, {duration: 1000, onComplete: function(){ el.removeClass('hide'); }}).start({'opacity': 1});
					fxi++;
				}
			});
			
			//	hide others
			this.element.getElements(':not(.'+in_key+'_'+in_value+')').each(function(el,i){
				if(el.hasClass('box') && !el.hasClass('hide') && !el.hasClass('back')){
				 	el.addClass('hide');	
					if(el.fx) el.fx.cancel();
					el.fx = new Fx.Morph(el, {duration: 1000, onComplete: function(){ el.setStyle('display', 'none'); }}).start({'opacity': 0});
					fxi++;
				}
			});
			
		}

		//	add to footer
		

		//	move to top/left of the grid
		new Fx.Scroll('bottom', {duration: 1000}).toTop();

		//	re-arrange masonry
		(function(){ this.element.my_mason.masonryArrange(); }).bind(this).delay(1250);
	},
	
	sort: function(in_sort_function){
		mason = this.element.my_mason;
		if(in_sort_function == 'sort_random'){
			for(i=0;i<5;i++){
				mason.bricks.sort(this[in_sort_function].bind(this));
			}	
		} else {
			mason.bricks.sort(this[in_sort_function].bind(this));
		}
		mason.masonryArrange(); 
	},
	
	sort_reverse: function(a, b){ 
		return -1	
	},
	
	sort_random: function(a,b){
		return Math.round(Math.random()*2)-1;
	}
	
});

var my_chunkfinder = {};

window.addEvent('masonready', function(){
	my_chunkfinder = new ns_chunkfinder('chunkfinder');
	window.fireEvent('chunkfinderready');	
});
