/* YAML Tabs - jQuery plugin for accessible, unobtrusive tabs
 * Build to seemlessly work with the CCS-Framework YAML (yaml.de) not depending on YAML though
 * @requires jQuery v1.0.3 http://blog.ginader.de/dev/yamltabs/index.php
 * Copyright (c) 2007 Dirk Ginader (ginader.de)
 * Dual licensed under the MIT and GPL licenses: http://www.opensource.org/licenses/mit-license.php http://www.gnu.org/licenses/gpl.html
 * Version: 1.1.1*/
(function($) {   
    $.fn.extend({
        getUniqueId: function(p){
            return p + new Date().getTime();
        },
        accessibleTabs: function(config) {
            var defaults = {
                wrapperClass: 'content',
                currentClass: 'current',
                tabhead: 'h6',
                tabbody: '.tabbody',
                fx:'show', // can be "fadeIn", "slideDown", "show"
                fxspeed: 'normal', // speed (String|Number): "slow", "normal", or "fast") or the number of milliseconds to run the animation
                currentInfoText: 'current tab: ',
                currentInfoPosition: 'prepend', // Definition where to insert the Info Text. Can be either "prepend" or "append"
                currentInfoClass: 'current-info' // Class to apply to the span wrapping the CurrentInfoText
            };
            var options = $.extend(defaults, config);
            var o = this;
            return this.each(function() {
                var el = $(this);
                var list = '';
                var contentAnchor = o.getUniqueId('accessibletabscontent');
                var tabsAnchor = o.getUniqueId('accessibletabs');
                $(el).wrapInner('<div class="'+options.wrapperClass+'"></div>');

                $(el).find(options.tabhead).each(function(i){
                    var id = '';
                    if(i === 0){
                        id =' id="'+tabsAnchor+'"';
                    }
                    list += '<li><a'+id+' href="#'+contentAnchor+'">'+$(this).text()+'</a></li>';
                    $(this).remove();
                });
                $(el).prepend('<ul>'+list+'</ul>');
                $(el).find(options.tabbody).hide();
                $(el).find(options.tabbody+':first').show().before('<'+options.tabhead+'><a tabindex="0" class="accessibletabsanchor" name="'+contentAnchor+'" id="'+contentAnchor+'">'+$(el).find("ul>li:first").text()+'</a></'+options.tabhead+'>');
                $(el).find("ul>li:first").addClass(options.currentClass)
                .find('a')[options.currentInfoPosition]('<span class="'+options.currentInfoClass+'">'+options.currentInfoText+'</span>');

                $(el).find('ul>li>a').each(function(i){
                    $(this).click(function(event){
                        event.preventDefault();
                        $(el).find('ul>li.current').removeClass(options.currentClass)
                        .find("span."+options.currentInfoClass).remove();
                        $(this).blur();
                        $(el).find(options.tabbody+':visible').hide();
                        $(el).find(options.tabbody).eq(i)[options.fx](options.fxspeed);
                        $( '#'+contentAnchor ).text( $(this).text() ).focus();
                        $(this)[options.currentInfoPosition]('<span class="'+options.currentInfoClass+'">'+options.currentInfoText+'</span>')
                        .parent().addClass(options.currentClass);
                        
                    });
                });
            });
        }
    });
})(jQuery);