(function() {

    Type.registerNamespace('SCOTT.CSC.Tabs');

    var initTabs = function(selector) {
        $(selector).each(function() {

            var context = $(this);

            var timeout = 250;

            $('.Content > div', context).each(function() {
                var height = $(this).height();
                $(this).height(height + 'px');
            }).hide();

            var differenceHeight = $('.Content', context).height() - $('.Content > div:visible', context).height();

            $('a.Tab', context).click(function() {
                var href = $(this).attr('href');
                var idHash;

                var visibleTabs = $('.Content > div:visible', context);
                if (visibleTabs.length > 0) {
                    idHash = '#' + visibleTabs.get(0).id;
                }
                else {
                    idHash = '';
                }

                if (href !== window.location.hash || href != idHash) {

                    var visibleHeight = 0;
                    if (visibleTabs.length > 0) {
                        visibleHeight = $('.Content > div:visible', context).height();
                    }
                    var hiddenHeight = $('div' + href, context).height();

                    $('.Content', context).height((visibleHeight + differenceHeight) + 'px');

                    var showContent = function() {
                        $('a.Tab', context).
                        removeClass('Active').
                        filter('a.Tab[href="' + href + '"]').
                        addClass('Active');

                        $('.Content', context).
                        animate({ height: hiddenHeight + 'px' }, timeout * 2, function() {
                            $(href, context).fadeIn(timeout, function() {
                                $('.Content', context).height('auto');
                            });
                        });
                    }

                    var hideContent = function() {
                        $('.Content > div:visible', context).
                        fadeOut(timeout, showContent);
                    }

                    if (visibleTabs.length > 0) {
                        hideContent();
                    }
                    else {
                        showContent();
                    }

                }

                $(this).blur();

                return false;
            });

            $('.Content a.Tabber', context).click(function() {
                var href = $(this).attr('href');
                $('a.Tab[href="' + href + '"]', context).click();
            });

            if (window.location.hash !== '') {
                $('a.Tab[href="' + window.location.hash + '"]', context).click();
            }
        });
    };


    SCOTT.CSC.Tabs.InitTabs = initTabs;

} ());