/*
* Copyright (C) 2009 Joel Sutherland.
* Liscenced under the MIT liscense
*/

(function($) {

    var myoldi;
    var docheight = $(document).height();

    $.fn.filterable = function(settings) {
        settings = $.extend({
            useHash: true,
            animationSpeed: 800,
            animationSpeedClose: 500,
            expandspeed: 700,
            show: { width: 'show', opacity: 'show' },
            hide: { width: 'hide', opacity: 'hide' },
            useTags: true,
            tagSelector: '#portfolio-filter a',
            selectedTagClass: 'current',
            allTag: 'all'
        }, settings, function() {
            jQuery_1_3_2("#side").css({ "height": docheight - 556 });
        });

        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) {
                var myrows = 0;
                var myoldrows = 0;
                var myi = 0;
                $(this).children(selectorToShow).each(function() {
                    myi++;
                    if (myi == 5) {
                        myrows++;
                        myi = 0;
                    };
                });
                if (myi > 0) {
                    myrows++;
                };
                myi = myrows * 122 + 84 + 100;
                settings.expandspeed = 900 / myrows;
                var myitem = $(this);
                if (myoldrows != myrows) {
                    jQuery_1_3_2("#side").stop().animate({ height: myi }, settings.expandspeed, 'linear', function() {
                        myitem.children(selectorToShow).animate(settings.show, 800, 'linear');
                    });
                } else {
                    myitem.children(selectorToShow).animate(settings.show, settings.animationSpeed, 'linear');
                };
                myoldrows = myrows;
                myoldi = myi;
            });

            /* SHOW: hide a single class*/
            $(this).bind("hide", function(e, selectorToHide) {
                $(this).children(selectorToHide).animate(settings.hide, settings.animationSpeedClose, 'linear');
            });

            /* ============ Check URL Hash ====================*/
            if (settings.useHash) {
                if (location.hash != '')
                    $(this).trigger("filter", [location.hash]);
                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_1_3_2);