(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
                var curList = base.$el.find("a.current").attr("href").substring(1),
                
                // 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 = base.$el.find(".list-wrap"),
                    curListHeight = $allListWrap.height();
                $allListWrap.height(curListHeight);
                                        
                if ((listID != curList) && ( base.$el.find(":animated").length == 0)) {
                                            
                    // Fade out current list
                    base.$el.find("#"+curList).fadeOut(base.options.speed, function() {
                        
                        // Fade in new list on callback
                        base.$el.find("#"+listID).fadeIn(base.options.speed);
                        
                        // 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");
                            
                    });
                    
                }   
                
                // 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));
        });
    };
    
	
	
	
	
	/*
	
	Organic Tabs Slider
	
	*/


    $.organicTabsSlider = function(el, options) {
    
        var base = this;
        base.$el = $(el);
        base.$list = base.$el.find('.list-wrap');
        base.$nav = base.$el.find(".nav");
        
        var zIndexRatio = 100,
            topZIndex = 0;
                
        base.init = function() {
        
            base.options = $.extend({},$.organicTabsSlider.defaultOptions, options);

            // fixes the list wrap into a static height
            base.$list.css('height', base.$list.height() + 'px').css('overflow', 'hidden');

            // gives every list item their own z-index
            var listParts = base.$list.children('div'),
                partCount = listParts.length,
                topZIndex = (partCount + 1) * zIndexRatio;

            listParts.each(function (i, el) {
                // last items have the higher zIndex
                var zIndex = (i + 1)* zIndexRatio;
                $(this)
                    .css({
                        'z-index': zIndex,
                        'display': 'block',
                        'opacity': (i == 0) ? 1 : 0,
                        'background': 'white',
                        'position': 'absolute'
                    })
                    .data('base-z-index', zIndex);
            });
            
            // Accessible hiding fix
            base.$list.children("div.hide").css({
                "top": 0,
                "left": 0
            }); 

            // Global instances of the variable containing the current tab
            var $current = base.$nav.find('.current');
            $current.data('target', $('#' + $current.attr('rel').substring(1)));
            
            base.$nav.find("li > a").click(function() {
            
                var 
                
                // List moving to
                    $newList = $(this),
                    
                // Figure out ID of new list
                    listID = $newList.attr("href").substring(1);

                if ($current) {
                    if ($current.get(0) == $newList.get(0))
                        return;

                    // removes the 'current' class from the tab and fades out the target
                    $current.stop().removeClass('current');
                    $current.data('target').stop().css('z-index', $current.data('target').data('base-z-index')).animate({'opacity': 0}, base.options['speed']);
                }

                $current = $newList;

                // makes sure this tab has a target
                if (!$current.data('target')) {
                    $current.data('target', $('#' + listID));
                }

                // No target, no animation
                if (!$current.data('target')) {
                    return;
                }

                // Fades in the target and changes the class of the tab
                $current.addClass('current');
                $current.data('target').stop().css({
                    'display': 'block',
                    'z-index': topZIndex,
                    'position': 'absolute'
                }).animate({'opacity': 1}, base.options['speed'], function () {
                    if ($.browser.msie) $(this).get(0).style.removeAttribute('filter');
                });
                
                // Changes the height of the list to the height of the new target
                base.$list.stop().animate({'height': $current.data('target').height() + 'px'}, base.options['speed']);
                
                return false;
            });
            
        };
        base.init();
    };
    
    $.organicTabsSlider.defaultOptions = {
        "speed": 300
    };
    
    $.fn.organicTabsSlider = function(options) {
        return this.each(function() {
            (new $.organicTabsSlider(this, options));
        });
    };
    
})(jQuery);


