/**
 * YAML Tabs - jQuery plugin for accessible, unobtrusive tabs
 * Made to seemlessly work with the CCS-Framework YAML (yaml.de)
 * @requires jQuery v1.0.3
 *
 * http://blog.ginader.de/dev/yamltabs/
 *
 * 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
 * 
 * History:
 * * 1.0 initial release
 * * 1.1 
 * * * rewrite to use "fn.extend" structure
 * * * added check for existing ids on the content containers to use to proper anchors in the tabs
 */

(function($) {
    /*
    // test what has focus on the page
    window.addEventListener('focus',function(e) { 
    	    var evt = e || window.event;
        	var tgt = evt.target || evt.srcElement;
        	if(window.console && window.console.log){
                console.log('focus');
            	console.log(tgt);
        	}
    },true);
    // */
    
    
    $.fn.extend({
        getUniqueId: function(p){
            return p + new Date().getTime();
        },
        yamltabs: function(options) {
            var defaults = {
                wrapperClass: 'content', // Classname to apply to the div that is wrapped around the original Markup
                tabhead: 'h4', // Tag or valid Query Selector of the Elements to Transform the Tabs-Navigation from (originals are removed)
                tabbody: '.text', // Tag or valid Query Selector of the Elements to be treated as the Tab Body
                fx:'show', // can be "fadeIn", "slideDown", "show"
                fxspeed: null // speed (String|Number): "slow", "normal", or "fast") or the number of milliseconds to run the animation
            };
            var options = $.extend(defaults, options);
            var o = this;
            return this.each(function() {
                var el = $(this);
                var list = '';

                // Tab-Content Container erzeugen ...
                var contentAnchor = o.getUniqueId('yamltabscontent');
                var tabsAnchor = o.getUniqueId('yamltabs');
                $(el).wrapInner('<div class="'+options.wrapperClass+'"></div>');

                // Tabreiter aus vorgegebenen Elementen erzeugen ...
                $(el).find(options.tabhead).each(function(i){
                    //var anchor = o.getUniqueId('yamltab');
                    var id='';
                    if(i===0){
                        id=' id="'+tabsAnchor+'"';
                    }
                    list+= '<li><a'+id+' href="#'+contentAnchor+'">'+$(this).text()+'</a></li>';
                    $(this).remove();
                    //$(this).replaceWith('<a name="'+anchor+'" id="'+anchor+'" tabindex="0"></a>');
                });

                // Liste vervollständigen ...
                $(el).prepend('<ul class="tabs-nav floatbox">'+list+'</ul>');
                $(el).find(options.tabbody).hide();
                $(el).find(options.tabbody+':first').show().before('<a tabindex="0" class="yamltabsanchor" name="'+contentAnchor+'" id="'+contentAnchor+'"></a>');
                $(el).find("ul>li:first").addClass('current');		

                // Verhalten der Tabreiter hinzufÃ¼gen ...
                $(el).find('ul>li>a').each(function(i){
                    $(this).click(function(event){
                        event.preventDefault();
                        $(el).find('ul>li.current').removeClass('current');
                        $(this).blur().parent().addClass('current');
                        $(el).find(options.tabbody+':visible').hide();
                        $(el).find(options.tabbody).eq(i)[options.fx](options.fxspeed);
                        //console.log('#'+anchor);
                        o.lastActiveTab = this;
                        $( '#'+contentAnchor ).html('<'+options.tabhead+'>'+$(this).text()+'</'+options.tabhead+'>').focus();
                    });
                });
                $('#'+contentAnchor).click(function(event){
                    event.preventDefault();
                    $(o.lastActiveTab).focus();
                });
            });
        }
    });
})(jQuery);
