/*
* Copyright (C) 2009 Joel Sutherland.
* Liscenced under the MIT liscense
*/

(function($) {
	$.fn.filterable = function(settings) {
		settings = $.extend({
			useHash: true,
			animationSpeed: 1000,
			show: { width: 'show', opacity: 'show' },
			hide: { width: 'hide', opacity: 'hide' },
			useTags: true,
			tagSelector: '#portfolio-filter a',
			selectedTagClass: 'current',
			allTag: 'all'
		}, settings);
		
		return $(this).each(function(){
		
			/* FILTER: select a tag and filter */
			$(this).bind("filter", function( e, tagToShow ){
				if(settings.useTags){
					$(settings.tagSelector).removeClass(settings.selectedTagClass);
					$(settings.tagSelector + '[href=' + tagToShow + ']').addClass(settings.selectedTagClass);
				}
				$(this).trigger("filterportfolio", [ tagToShow.substr(1) ]);
			});
		
			/* FILTERPORTFOLIO: pass in a class to show, all others will be hidden */
			$(this).bind("filterportfolio", function( e, classToShow ){
				if(classToShow == settings.allTag){
					$(this).trigger("show");
				}else{
					$(this).trigger("show", [ '.' + classToShow ] );
					$(this).trigger("hide", [ ':not(.' + classToShow + ')' ] );
				}
				if(settings.useHash){
					location.hash = '#' + classToShow;
				}
			});
			
			/* SHOW: show a single class*/
			$(this).bind("show", function( e, selectorToShow ){
				//alert(selectorToShow);
				$(this).children(selectorToShow).animate(settings.show, settings.animationSpeed);
			});
			
			$(this).bind("showSearch", function( e, selectorToShow ){
				//alert(selectorToShow);
				console.log(selectorToShow);
				//selectorToShow = selectorToShow.replace("'", "\\'");
				console.log(selectorToShow);
				$(this).children("." + selectorToShow).animate(settings.show, settings.animationSpeed);
				$("div[neighborhood*=\"" + selectorToShow + "\"]").animate(settings.show, settings.animationSpeed);
				$("div[t*=\"" + selectorToShow + "\"]").animate(settings.show, settings.animationSpeed);
				
				selectorToShow = selectorToShow.replace("'", "");
				$(this).children("." + selectorToShow).animate(settings.show, settings.animationSpeed);
				$("div[neighborhood*=\"" + selectorToShow + "\"]").animate(settings.show, settings.animationSpeed);
				$("div[t*=\"" + selectorToShow + "\"]").animate(settings.show, settings.animationSpeed);
			});
			
			$(this).bind("showNT", function( e, nToShow, tToShow ){
				//alert("div[neighborhood='" + nToShow + "'][t='" + tToShow + "']");
				$("div[neighborhood='" + nToShow + "'][t='" + tToShow + "']").animate(settings.show, settings.animationSpeed);
			});
			
			$(this).bind("showNeighborhood", function( e, selectorToShow ){
				//alert(selectorToShow);
				//alert("Neighborhood: " + "div[neighborhood='" + selectorToShow + "']");
				$("div[neighborhood='" + selectorToShow + "']").animate(settings.show, settings.animationSpeed);
			});
			
			$(this).bind("showType", function( e, selectorToShow ){
				//alert(selectorToShow);
				//alert("Type: " + "div[t='" + selectorToShow + "']");
				//$("div[t='" + selectorToShow + "']").animate(settings.show, settings.animationSpeed);
				$("div[t]").each(function(i){
					$types = $(this).attr("t");
					
					if($types.indexOf(selectorToShow) != -1)
						$(this).animate(settings.show, settings.animationSpeed);
				});
			});
			
			$(this).bind("filterVisibleType", function( e, selectorToShow ){
				//alert(selectorToShow);
				//alert("Type: " + "div[t='" + selectorToShow + "']");
				$(".barBox[t!='" + selectorToShow + "']:visible").animate(settings.hide, settings.animationSpeed);
			});
			
			$(this).bind("filterVisibleNeighborhood", function( e, selectorToShow ){
				//alert(selectorToShow);
				//alert("Neighborhood: " + "div[neighborhood='" + selectorToShow + "']");
				$(".barBox[neighborhood!='" + selectorToShow + "']:visible").animate(settings.show, settings.animationSpeed);
			});
			
			/* SHOW: hide a single class*/
			$(this).bind("hide", function( e, selectorToHide ){
				$(this).children(selectorToHide).animate(settings.hide, settings.animationSpeed);	
			});
			
			/* SHOW: hide a all classes*/
			$(this).bind("hideAll", function(e){
				$(this).children().animate(settings.hide, settings.animationSpeed);	
			});
			
			/* ============ Check URL Hash ====================*/
			if(settings.useHash){
				if(location.hash != '')
				{
					if((location.hash).indexOf("search=") == -1)
					{
						//alert("Location.hash: " + location.hash);
						var $theSub = (location.hash).substr(1);
						//alert("Substring: " + $theSub);
						var blah = getClassName($theSub);
						//alert(blah);
						$(this).trigger("filter", [ '#' + blah ]);
					}else{
						//We've come from the search bar box!
						var $theSub = (location.hash).substr(8);
						var $theSplit = new Array();
						$theSplit = $theSub.split("-");
						
						if($theSplit[0] == null)
						{
							$theSplit = new Array();
							$theSplit[0] = $theSub;
							//alert($theSplit);
						}
						$(this).trigger('hideAll');
						
						for(i = 0; i < $theSplit.length; i++)
						{
							trueName = getClassName($theSplit[i]);
							$(this).trigger("show", [ '.' + trueName ] );
						}
					}
				}else
					$(this).trigger("filter", [ '#' + settings.allTag ]);
			}
			
			/* ============ Setup Tags ====================*/
			if(settings.useTags){
				$(settings.tagSelector).click(function(){
					$('#portfolio-list').trigger("filter", [ $(this).attr('href') ]);
					
					$(settings.tagSelector).removeClass('current');
					$(this).addClass('current');
				});
			}
		});
	}
})(jQuery);
