/**
 * Common functions for content pages.
 * JQuery 1.2.6 required see http://jquery.com/
 * JQuery UI core and tabs extensions required see http://ui.jquery.com/
 *
 * @author Devin Weaver <weaver.devin (at) gmail.com>
 * Copyright (c) 2008 Devin Weaver
 */

// Extend jQuery browser detection to include iPhone.
jQuery.browser.iphone = /iphone/.test(navigator.userAgent.toLowerCase());

/**
 * Scan a page for special anti-apammed email addresses and format them
 * into real email addresses. Any element wit class parse-addy will be scanned.
 * The format is USER (at) DOMAIN [LINK TEXT]
 * Ex. &lt;span class="parse-addy"%gt;foo (at) domain.com&lt;/span&gt;
 * Ex. &lt;span class="parse-addy"&gt;foo(at)domain.com&lt;/span&gt;
 */
// scanForEmails() {{{
function scanForEmails() {
    $('.parse-addy').each(function (i) {
        var text = $(this).text();
        var matches = text.match(/(.+?)\s*\(at\)\s*([a-zA-Z.-_:0-9]+)(.*)/);
        if (matches[3] != '')
        {
            text = matches[3];
        }
        $(this).html('<a href="mailto:' + matches[1] + '@' + matches[2] + '" class="email">' + text + "</a>");
    });
}
// }}}

/**
 * DEPRICATED *
 * This function is deprecated. Use initTOC instead.
 *
 * Initialize any tabs for the page. It uses a div of id tablist with
 * in which is an ul set.
 * See http://docs.jquery.com/UI/Tabs for more info.
 * @param tab the selected tab to start with. default tab 0.
 */
// initTabs() {{{
function initTabs(tab) {
    // Pass this onto new function
    initTOC();
}
// }}}

/**
 * Build dynamic sublists for TOC like menues.
 */
// initTOC() {{{
function initTOC() {
    // Older versions of Safari do not handle tabs properly. iPhone does but looks bad.
    if ( (jQuery.browser.safari && jQuery.browser.version.substr(0,3) < 500) || jQuery.browser.iphone )
        return;


    // Find list items representing folders and
    // style them accordingly.  Also, turn them
    // into links that can expand/collapse the
    // tree leaf.
    $('ul.toclist').find('li > ul').each(function(i) {
        // Find this list's parent list item.
        var parent_li = $(this).parent('li');

        // Style the list item as folder.
        parent_li.addClass('tocsublist');
        parent_li.addClass('tocsublist-closed');

        // Temporarily remove the list from the
        // parent list item, wrap the remaining
        // text in an anchor, then reattach it.
        var sub_ul = $(this).remove();
        parent_li.wrapInner('<a class="tocsublistaction"/>').find('a').click(function() {
            // Make the anchor toggle the leaf display.
            sub_ul.slideToggle('normal');

            parent_li.toggleClass('tocsublist-closed').toggleClass('tocsublist-open');
        });
        parent_li.append(sub_ul);

        sub_ul.addClass('tocsublist').hide();
    });


    var start_div_found = false;
    var url_ref_tag = null;

    // Check the url ref tag if any.
    var index = location.href.indexOf('#');

    if (index > 0)
        url_ref_tag = location.href.substring( index );

    $('#tablist').find('li > a').each(function(i) {
        if ( $(this).hasClass('tocsublistaction') )
            return;

        var div_id = $(this).attr('href');

        // Ignore list items that are not referencing local content divs
        if (div_id.charAt(0) != '#')
            return;

        // see if this is the start div and if so leave it shown. Set flag.
        if ( !start_div_found
            && (url_ref_tag == null || url_ref_tag == div_id) )
        {
            start_div_found = true;
            $(div_id).addClass('tab-is-displayed');

            $('#subtitle').html('<h2>' + $(div_id).children('.title:first').text() + '</h2>');
        }
        else if (url_ref_tag != div_id)
        {
            $(div_id).hide();
        }

        $(this).click(function() {
            var other_div = $('div.tab-is-displayed');

            $('#subtitle').html('<h2>' + $(div_id).children('.title:first').text() + '</h2>');

            $(div_id).addClass('tab-is-displayed');

            if (other_div !== null && other_div != '')
            {
                other_div.removeClass('tab-is-displayed');

                // Animate the transition
                other_div.slideUp('normal', function() {
                    $(div_id).slideDown('normal');
                });
            }
            else
            {
                $(div_id).show();
            }

            return false;
        });

        $(div_id + ' > .title').hide();
    });
}
// }}}

/* vim:set et sw=4 fdm=marker: */

