(function($)
{
    // reformat h2

    $.fn.a10_btl_refmt_h2 = function ( separator )
    {
        this.each( function()
        {
            $( 'h2', this ).each ( function()
            {
                txt = $(this).text().split ( / - / );

                if ( txt.length == 2 )
                    txt = txt[0] + ' - '+separator+'<span class="mini">'+txt[1]+'</span>';
                else
                    txt = txt[0];

                $(this).html ( txt );
            });
        });
    };

    // turn h2 into proper link

    $.fn.a10_btl_link_h2 = function()
    {
        txt = $( 'h2', this ).html();

        txt = '<a href="#">'+txt+'</a>';

        $( 'h2', this ).html ( txt );

        $a = $( 'h2 a', this );

        var ef = function()
        {
            $(this).parents ( '.brochure_toc li' ).trigger ( 'touch' );

            return false;
        }

        $a.click ( ef ).keyup ( ef );

        $a2 = $a.clone ( true );
        $a2.html ( '&nbsp;' ).addClass ( 'plus_minus_overlay' );

        $(this).prepend ( $a2 );
    }

    // format one teaser

    $.fn.anu10_broc_toc_leaf = function()
    {
        var li_touch = function()
        {
            if ( this._running == 1 )
                return;

            this._running = 1;

            var li  = this;
            var $li = $(this);

            var _close = function ( $li )
            {
                $( '.extra_link', $li ).slideUp ( 250, function()
                {
                    $( '.richtext', $li ).slideUp ( 250, function()
                    {
                        $li.removeClass ( 'minus' );
                        $li.addClass    ( 'plus'  );

                        $li.get(0)._running = 0;
                    });
                });
            }

            if ( $li.hasClass ( 'plus' ) )
            {
                $( 'li.minus', $li.parent() ).each ( function()
                {
                    if ( $(this).get(0)._running == 1 )
                        return;

                    _close ( $(this) );
                });

                $( '.richtext', $li ).slideDown ( 250, function()
                {
                    $( '.extra_link', $li ).slideDown ( 250, function()
                    {
                        $li.removeClass ( 'plus'  );
                        $li.addClass    ( 'minus' );
                        li._running = 0;
                    });
                });
            }
            else
            {
                _close ( $li );
            }
        }

        this.each ( function()
        {
            $(this).removeClass ( 'minus' );
            $(this).addClass    ( 'plus'  );

            $( '.richtext',   $(this) ).hide();
            $( '.extra_link', $(this) ).hide();

            if ( $( 'h2', $(this) ).height() > 24 )
                $(this).a10_btl_refmt_h2 ( '<br />' );

            $(this).bind ( 'touch', li_touch );

            $(this).a10_btl_link_h2();
        });
    };

    // format all matching pagelists

    $.fn.anu10_broc_toc = function()
    {
        return this.each ( function()
        {
            $( 'li', $(this) ).anu10_broc_toc_leaf();
        });
    };
})( jQuery );

$(function()
{
    // initialize the toc

    $( '.brochure_toc' ).anu10_broc_toc();

    // the first co getting in a different order :-|

    $co     = $( 'body.anu10_brochure #page_body_main_content .co_pos_first.rt_ext_inh' );
    $co_img = $( '.img_container', $co );
    $co_h3  = $( '.richtext h3' );

    if ( $co.size() > 0 && $co_img.size() > 0 && $co_h3.size() > 0 )
    {
        $co_img.before ( $co_h3.clone() );
        $co_h3.remove();
    }

    // headlines on emphasized blocks on a leaf page

    $( '.emphasis' ).a10_btl_refmt_h2 ( '' );

    // rounding corners - got em?

    if ( $quotes = $ ( '.quote' ) )
    {
        $quotes.each(function()
        {
            $(this).css ( 'padding',        0 );

            if ( $ic = $( '.img_container', $(this) ) )
            {
                $img = $( 'img', $ic );
                $rt  = $( '.richtext', $(this) );

                $( ':last-child', $rt ).css ( 'margin-bottom', 0 );

                $ic.css ( 'margin',         0      );

                $rt.css ( 'margin-left', $ic.width() );

                padding = ( 28 + $rt.height() - $img.height() ) / 2;

                $ic.css ( 'height',         $img.height()   )
                   .css ( 'padding-top',    padding         )
                   .css ( 'padding-left',   0               )
                   .css ( 'padding-right',  0               )
                   .css ( 'padding-bottom', 0               );
            }
        });

        if ( $.browser.msie )
        {
            jq161( '.quote .img_container img' ).wrap ( '<div class="inner"></div>' );
            jq161( '.quote .img_container .inner' ).corner ( 'round tl 10px'  );
            jq161( '.quote .img_container       ' ).corner ( 'round bl 10px'  );
            jq161( '.quote .richtext'             ).corner ( 'round 12px'     );
        }
    }

    if (    $( 'body.anu10_brochure_super' ).size() == 1
         || $( 'body.anu10_brocleaf' ).size() == 1
         || $( 'body.anu10_brocleaf_green' ).size() == 1
         || $( 'body.anu10_brochure' ).size() == 1 )
    {
        var $co = $( '#page_body_right_content .co_1' );

        $co.addClass ( 'torn_apart' );

        // we only act if we have an image here

        var $img_container = $( '.img_container', $co );

        if ( $( 'img', $img_container ).size() )
        {
            $img_container.addClass ( 'ghost_img' );
            $img_container.clone().appendTo ( '#page_container' );
            $img_container.remove();

            //$img_container = $( '#page_container .ghost_img' );
        }
    }

});

// correction for the look of the first co if img given


