/*
* Copyright (C) 2009 Joel Sutherland.
* Licenced 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' },
          easing: 'linear',
          useTags: true,
          tagSelector: '.portfolio-filter a',
          selectedTagClass: 'current',
          allTag: 'all',
          async: true
          }, 
      settings
      );
    
    var setup = function() {
      var $this = $(this);

      // -------------------------------------------------------------------------
      var onClick = function(e) {
        // console.log("BEGIN $.fn.filterable.onClick '%s'", this.href);
        //e.stopPropagation = true;
        var $link = $(this);

        $this.trigger("filter", [ $link.attr('href') ]);
        $(settings.tagSelector).removeClass('current');
        $link.addClass('current');
        // console.log("END $.fn.filterable.onClick '%s'", this.href);
      };
      
      // -------------------------------------------------------------------------
      var onFilter = function(e, tagToShow) {
        // console.log("BEGIN $.fn.filterable.onFilter - tagToShow : %s", tagToShow);

        //e.stopPropagation = true;
        if(settings.useTags){
          $(settings.tagSelector).removeClass(settings.selectedTagClass);
          $(settings.tagSelector + '[href=' + tagToShow + ']').addClass(settings.selectedTagClass);
        }
        $this.trigger("filterportfolio", [ tagToShow.substr(1) ]);

        // console.log("END $.fn.filterable.onFilter - tagToShow : %s", tagToShow);
      };
    
      // -------------------------------------------------------------------------
      var onFilterportfolio = function(e, classToShow) {
        // console.log("BEGIN $.fn.filterable.onFilterportfolio - classToShow = '%s'", classToShow);

        //e.stopPropagation = true;
        //var $this = $(this);

        if(classToShow == settings.allTag){
          var evt_type = settings.async ? "hide" : "show";
          $this.trigger(evt_type); 
        }
        else{
          var selectorToShow = '.' + classToShow;
          var selectorToHide = ':not(.' + classToShow + ')';
          if (settings.async) {
            $this.trigger("hide", [null, selectorToShow]); 
          }
          else {
            $this.trigger("show", [selectorToShow]);
            $this.trigger("hide", [selectorToHide]);
          }
        }
        
        if(settings.useHash){
          location.hash = '#' + classToShow;
        }
        // console.log("END $.fn.filterable.onFilterportfolio - classToShow = '%s'", classToShow);
      };
    
      // -------------------------------------------------------------------------
      var onShow = function(e, selectorToShow) { 
	// console.log("BEGIN $.fn.filterable.onShow - selectorToShow = '%s'", selectorToShow);
        //e.stopPropagation = true;
        $this.children(selectorToShow).animate(settings.show, settings.animationSpeed, settings.easing); 
	
        // console.log("END $.fn.filterable.onShow - selectorToShow = '%s'", selectorToShow);
      };
      
      // -------------------------------------------------------------------------
      var onHide = function(e, selectorToHide, selectorToShow) {
        //debugger;
        // console.log("BEGIN $.fn.filterable.onHide - selectorToHide : '%s'", selectorToHide);
        //e.stopPropagation = true;
        if (settings.async) {
          var children = $this.children(selectorToHide);
          var thenShow = function(e) {
            // setTimeout(function() {$this.trigger("show", [selectorToShow]);}, settings.animationSpeed / 2);
            setTimeout(function() {$this.trigger("show", [selectorToShow]);}, settings.animationSpeed);
          };
          children.slice(0,-1).animate(settings.hide, settings.animationSpeed, settings.easing);
          children.slice(-1).animate(settings.hide, settings.animationSpeed, settings.easing, thenShow);
        }
        else {
          $this.children(selectorToHide).animate(settings.hide, settings.animationSpeed, settings.easing);
        }
        // console.log("END $.fn.filterable.onHide - selectorToHide : '%s'", selectorToHide);
      };
      
      // -------------------------------------------------------------------------
      $this.bind("filter", onFilter);
      $this.bind("filterportfolio", onFilterportfolio);
      $this.bind("show", onShow);
      $this.bind("hide", onHide);
      
      if(settings.useHash){
        /*
        var tag = location.hash != '' ? location.hash : '#' + settings.allTag;
        $this.trigger("filter", [ tag ]);
        */
        if (location.hash != '') {
          $this.trigger("filter", [ location.hash ]);
        }
      }
      
      if(settings.useTags){
        $(settings.tagSelector).click(onClick);
      }
    };
    
    // -------------------------------------------------------------------------
    return $(this).each(setup);
  };
})(jQuery);

/*
$(document).ready(function(){
	
	$('#portfolio-list').filterable();

});
*/


