/*!
 * jQuery JavaScript tabs Plugin v1.0
 *
 * Copyright 2011, Stephan Schröter
 *
 */

(function( $ ){

    var methods = {
        
        init : function( options ) {
            
            var settings = {
                showFirstTabContent: true
            }
            
            return this.each(function(){
                
                if ( options ) {
                    $.extend( settings, options );
                }
                
                var obj = $(this);
                
                obj.tabs('generateTabSelectorList', settings);
                obj.tabs('setTabSelectorSize', settings);
                obj.tabs('hideTabContent', settings);
                obj.tabs('showFirstTabContent', settings);
                obj.tabs('handleTabs', settings);
                
                
            });
            
        },
        
        
        // generateTabSelectorList
        generateTabSelectorList : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                obj
                    .prepend('<ul class="tab-selector"></ul>')
                ;
                
                obj
                    .find('div.tab-content')
                    .each(function(i){
                        
                        var hash = 'tab_'+ new Date().getTime() +'-'+ i +'';
                        
                        $(this)
                            .attr('id',hash)
                            .children('h1.tab-title')
                            .hide()
                        ;
                        
                        obj
                            .children('ul.tab-selector')
                            .append('<li><a href="#'+ hash +'">'+ $(this).children('h1.tab-title').text() +'<span></span></a></li>')
                        ;
                        
                    })
                ;
                
            });
            
        },
        
        
        // setTabSelectorSize
        setTabSelectorSize : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                var width = obj.find('ul.tab-selector').width() / obj.find('ul.tab-selector li a').length - ( obj.find('ul.tab-selector li a').outerWidth() - obj.find('ul.tab-selector li a').width() );
                
                var adPixels = Math.floor( ( width - Math.floor(width) ) * obj.find('ul.tab-selector li a').length );
                
                obj
                    .find('ul.tab-selector li a')
                    .each(function(i){
                        
                        $(this)
                            .css({
                                'width':''+ Math.floor(width) +'px'
                            })
                        ;
                        
                        if (i+1 == obj.find('ul.tab-selector li a').length) {
                            
                            $(this)
                                .css({
                                    'width':''+ (Math.floor(width)+adPixels) +'px'
                                })
                            ;
                            
                        }
                        
                    })
                ;
                
                
            });
            
        },
        
        // handleTabs
        handleTabs : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                obj
                    .find('ul.tab-selector li a')
                    .click(function(){
                        
                        obj
                            .find('ul.tab-selector li a')
                            .removeClass('active')
                        ;
                        
                        $(this)
                            .addClass('active')
                        ;
                        
                        obj.tabs('hideTabContent', settings);
                        
                        obj
                            .find('div.tab-content'+ this.hash)
                            .show()
                        ;
                        
                        return false;
                        
                    })
                ;
                
            });
            
        },
        
        // hideTabContent
        hideTabContent : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                obj
                    .find('div.tab-content')
                    .hide()
                ;
                
            });
            
        },
        
        // showFirstTabContent
        showFirstTabContent : function( settings ) {
            
            return this.each(function(){
                
                if (settings.showFirstTabContent) {
                
                    var obj = $(this);
                    
                    obj
                        .find('ul.tab-selector li a:first')
                        .toggleClass('active')
                    ;
                    
                    obj
                        .find('div.tab-content:first')
                        .show()
                    ;
                
                }
                
            });
            
        }
        
    };

    $.fn.tabs = function( method ) {
    
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tabs' );
        }
    
    };

})( jQuery );
