jQuery(document).ready(function($) { 
	var $ = jQuery,
		gallery = $('.gallery'),
		galleryItems = gallery.find('.gallery-item'),
		galleryTags = [];

	gallery.find('a').attr('rel','gallery').colorbox({title:'Examples of our architectural photography', maxWidth:"95%", maxHeight:"95%"});
	gallery.before('<div id="gallery-filters">Filter:</div>').find('br').remove();
	
	galleryItems.each(function(){
		galleryTags.push(this.className.match(/tag-(.*)/)[1]);
	}).wrapAll('<div class="gallery-wrapper">');
	
	var galleryWrapper = gallery.find('.gallery-wrapper');	
	var galleryTags = unique(galleryTags.sort());
	
	$(galleryTags).each(function(i,e){
		$('<a>',{
			'text':e,
			'href':'#',
			'class':'gallery-filter',
			data:{'tag':e},
			click: function(){
				var e=$(this), tag=$(this).data('tag');
				e.siblings('.filter-current').removeClass('filter-current');
				e.addClass('filter-current');
				galleryItems.hide().filter('.tag-'+tag).show();			
				return false;
			}}
		).appendTo('#gallery-filters');
	});
	
	$('<a>',{'text':'all', 'href':'#', 'class':'filter-current gallery-filter',
		click: function(){
			var e=$(this), tag = $(this).data('tag');
			e.siblings('.filter-current').removeClass('filter-current');
			e.addClass('filter-current');
			galleryItems.show();			
			return false;
		}
	}).appendTo('#gallery-filters');
	
	
	
	function unique(a){
		var r = new Array(); 
		o:for(var i = 0, n = a.length; i < n; i++){ 
		for(var x = 0, y = r.length; x < y; x++){ if(r[x]==a[i]) continue o; }
	      r[r.length] = a[i];
		}
		return r;
	}
});

