	// 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() {
		// get the first collection
		var $applications = $('#projekte-container');
		// clone applications to get a second collection
		var $data = $applications.clone();
		var filterName = 'alle';
		
		$('.projekte-filter li a').click(function(e) {
			e.preventDefault();
			filterName	= this.href.substr(this.href.indexOf('#')+1);
			document.location.hash = filterName
			
			$('.projekte-filter li a').removeClass('active');
			$(this).toggleClass('active');
			
			if (filterName == 'alle') {
			  	var $filteredData = $data.find('li[data-type]');
			} else {
				var $filteredData = $data.find('li[data-type*=' + filterName + ']');
			}
			
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h3').attr('id').toLowerCase();
				}
			});
			// finally, call quicksand
			$applications.quicksand($sortedData, {
				duration: 800,
				easing: 'easeInOutQuad'
			}, 	function() { // callback function
					// Mouseover Effect für Bilder mit Hyperlinks
					$('a img').not('#header img, .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]);
		});
		
		
		var tempUrl = location.href;
		
		//alert(tempUrl.indexOf('/filter/'));
		//alert(tempUrl.indexOf('/show/'));
		
		if(tempUrl.substring(tempUrl.length, tempUrl.length-1) == '/'){
			tempUrl = tempUrl.substring(0, tempUrl.length-1);
		}
			tempUrl = tempUrl.split("/");
			
		var part_num=0;
		while (part_num < tempUrl.length){
			part_num+=1;
			if(tempUrl[part_num] == 'show' && tempUrl[part_num+1].length > 0){
				$_GET['cat'] = tempUrl[part_num+1];
			}
		}
		
		
			
		
		if($_GET['cat'] || (location.hash.slice(1) && location.hash.slice(1) != 'alle')){
			if(location.hash.slice(1).length > 0){
				$_GET['cat'] = location.hash.slice(1);
			}
			
						
			$('.projekte-filter li a').removeClass('active');
			//$('#'+$_GET['cat']).toggleClass('active');
			$('a[href="#'+$_GET['cat']+'"]').toggleClass('active');
			
			var $filteredData = $data.find('li[data-type*=' + $_GET['cat'] + ']');
			
			
			
			var $sortedData = $filteredData.sorted({
				by: function(v) {
					return $(v).find('h3').attr('id').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');
		}
		
		$('#projekte-container a').live('click',function(){
			//$(this).attr('href', $(this).attr('href') + '#' + location.hash.slice(1));
			//$(this).attr('href', $(this).attr('href') + '?show=' + location.hash.slice(1));
			if(location.hash.slice(1) != 'alle' && location.hash.slice(1).length > 0){
				var tempUrl = $(this).attr('href');
				if(tempUrl.substring(tempUrl.length, tempUrl.length-1) == '/'){
					tempUrl = tempUrl.substring(0, tempUrl.length-1);
				}
				if(tempUrl.match(/filter\S{2}/)){
					$(this).attr('href', tempUrl + '/show/' + location.hash.slice(1)+'/');	
				}else{
					$(this).attr('href', tempUrl + '/filter/show/' + location.hash.slice(1)+'/');
				}
			}
		});			
	});
	/* ------------------------------------------------------------------------------------------ */
