(function( $ ){

    jQuery.fn.dtabs = function() {
        return this.each(function() {
            $(this).find('ul.tabs a').click(function(event) {
                var href = $(event.target).attr('href');
                var pos = href.indexOf('#');
                jQuery.activateTab(href);
                if (pos == 0 || (pos > 0 && (window.location.pathname == '' || window.location.pathname == href.substring(0, pos))))
                    return false;
            });

            var a = $(this).find("ul.tabs > li:first > a");
            jQuery.activateTab($(a).attr('href'));

            // activate a tab based on the current anchor
            var url = decodeURI(window.location);
            var pos = url.indexOf("#");
            if (pos >= 0) {
                var id = url.substring(pos);
                if ($(this).find('ul.tabs a[href="' + id + '"]').length > 0) {
                    jQuery.activateTab(id);
                    return false;
                }
            }


        });
    };



    jQuery.activateTab = function(id) {
        var pos = id.indexOf("#");

        if (pos >= 0) {
            id = id.substring(pos);
        }
        var $tab = $(id);
        var $container = $tab.parent('div');
        $container.find('ul.tabs a').removeClass('active');
        $container.find('ul.tabs a[href="' + id + '"]').addClass('active');
        $container.children('div').hide();
        $tab.show();
    }


})( jQuery );

