	// Custom sorting plugin
	(function($) {
		$.fn.sorted = function(customOptions) {
			var options = {
				reversed: false,
				by: function(a) { return a.text(); }
			};
			$.extend(options, customOptions);
			$data = $(this);
			arr = $data.get();
			arr.sort(function(a, b) {
			var valA = options.by($(a));
			var valB = options.by($(b));
			if (options.reversed) {
				return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;				
			} else {		
				return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;	
			}
			});
			return $(arr);
		};
	})(jQuery);


	/* ------------------------------------------------------------------------------------------ */
	// Quicksand
	$(document).ready(function() {
		//$('#projekte-container').css('z-index', '-999');
		//$('#projekte-container').css('visibility', 'hidden');
		// get the first collection
		var $applications = $('#projekte-container');
		// clone applications to get a second collection
		var $data = $applications.clone();
		
		$('.projekte-filter-button').click(function(e) {
			$('.projekte-filter-button').removeClass('active');
			$(this).toggleClass('active');
			
			if ($(this).attr("id") == 'alle' || $(this).attr("id") == 'all') {
			  	var $filteredData = $data.find('li');
			} else {
				//var $filteredData = $data.find('li[data-type=' + $(this).attr("id") + ']');
				//var $filteredData = $data.find('li[data-type=' + $(this).attr("id") + ']');
				var $filteredData = $data.find('li[data-type*=' + $(this).attr("id") + ']');
			}
			
			
			//alert(li.attributes['data-type']);
			//alert($('#projekte-container li').attr("data-type"));
			//$($applications + 'li').attr("title");
			
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					//return $(v).find('span').text().toLowerCase();
					return $(v).find('h3').attr('id').toLowerCase();
				}
			});
			// finally, call quicksand
			$applications.quicksand($sortedData, {
				duration: 800,
				easing: 'easeInOutQuad'
			}, 	function() { // callback function
					// callback function
					
					$('a img').not('#logo img, .loupe, .home-teaser-img a img').mouseover(function() {
						$(this).stop(true, true).fadeTo(250, 0.8); 
					}).mouseout(function(){
						$(this).stop(true, true).fadeTo(100, 1); 
					});
				}
			);
			
			e.preventDefault();
		})


		var $_GET = {};
		
		document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
			function decode(s) {
				return decodeURIComponent(s.split("+").join(" "));
			}
		
			$_GET[decode(arguments[1])] = decode(arguments[2]);
		});
		
		if($_GET['cat']){
			$('.projekte-filter-button').removeClass('active');
			$('#'+$_GET['cat']).toggleClass('active');
			
			//var $filteredData = $data.find('li[data-type=promotion]');
			//var $filteredData = $data.find('li[data-type*='+$_GET['cat']+']');
			var $filteredData = $data.find('li[data-type*=' + $_GET['cat'] + ']');
			
			
			
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h3').attr('id').toLowerCase();
					//return $(v).find('span').text().toLowerCase();
				}
			});
			// finally, call quicksand
			$applications.quicksand($sortedData, {
				duration: 0,
				easing: 'easeInOutQuad'
			}, 	function() { // callback function
					// callback function
					$('#projekte-container').css('visibility', 'visible');
					
					$('a img').not('#logo img, .loupe, .home-teaser-img a img').mouseover(function() {
						$(this).stop(true, true).fadeTo(250, 0.8); 
					}).mouseout(function(){
						$(this).stop(true, true).fadeTo(100, 1); 
					});
				}
			);
		}else{
			$('#projekte-container').css('visibility', 'visible');
		}
		
	});
	/* ------------------------------------------------------------------------------------------ */
