(function($) {

    $.organicTabs = function(el, options) {

        var base = this;
        base.$el = $(el);
        base.$nav = base.$el.find(".nav");

        base.init = function() {

            base.options = $.extend({},$.organicTabs.defaultOptions, options);

            // Accessible hiding fix
            $(".hide").css({
                "position": "relative",
                "top": 0,
                "left": 0,
                "display": "none"
            }); 

            base.$nav.delegate("li > a", "click", function() {

                // Figure out current list via CSS class
				if(base.$el.find("a.current").length != 0){
                	var curList = base.$el.find("a.current").attr("href").substring(1);
				}
				if(base.$el.find("a.current").length == 0){
                	var curList = 'none';
				}

                // List moving to
                    $newList = $(this),

                // Figure out ID of new list
                    listID = $newList.attr("href").substring(1),

                // Set outer wrapper height to (static) height of current inner list
                    $allListWrap = $(".list-wrap"),
                    curListHeight = $allListWrap.height();
                $allListWrap.height(curListHeight);

                if ((listID != curList) && ( $.find(":animated").length == 0)) {

                    // Fade out current list
                    $("#"+curList).fadeOut(base.options.speed, function() {

                        // Fade in new list on callback
                        $("#"+listID).fadeIn(base.options.speed);

						$(".list-wrap").css("z-index", "1000");
						$("#"+listID).css("z-index", "1000");
						$("#FBScreen").css("z-index", "-1000");

                        // Adjust outer wrapper to fit new list snuggly
                        var newHeight = base.$el.find("#"+listID).height();
                        $allListWrap.animate({
                            height: newHeight
                        });

                        // Remove highlighting - Add to just-clicked tab
                        base.$el.find(".nav li a").removeClass("current");
                        $newList.addClass("current");

                    });

                } 
				if ((listID == curList) && $("#"+curList+":animated")) {

                    // Fade out current list
                    $("#"+curList).fadeOut(base.options.speed, function() {

						$(".list-wrap").css("z-index", "-1000");
						$("#FBScreen").css("z-index", "1000");

                        // Remove highlighting - Add to just-clicked tab
                        base.$el.find(".nav li a").removeClass("current");
                        $newList.addClass("current");

                    });

                }   

                // Don't behave like a regular link
                // Stop propegation and bubbling
                return false;
            });

        };
        base.init();
    };

    $.organicTabs.defaultOptions = {
        "speed": 300
    };

    $.fn.organicTabs = function(options) {
        return this.each(function() {
            (new $.organicTabs(this, options));
        });
    };

})(jQuery);
