// utility functions for roboprogs.com site
// $Header: $

Roboprog = {}

// max number of article summaries to collect in preview function
Roboprog.MAX_SUMMARIES = 3

// link search sentinel
Roboprog.END = '--end--'

// read in the headings from the given link, display preview
// load_to: id of element to be updated with the preview (list items)
// link:  uri to be read and filtered.
// load_zone:  id of hidden element to be used to buffer upload elements
Roboprog.preview = function( load_to, link, load_zone)
    {
    var buf_el, upd_params, load_el, cur_and_next, items, num_sum

    load_el = $( load_to)
    buf_el = $( load_zone)
    for ( num_sum = 0;
          ( num_sum < Roboprog.MAX_SUMMARIES) && ( link !== Roboprog.END);
        ) // increment below in nested loop

        {
        // alert( 'load ' + link + ' into ' + load_to)

        upd_params = {}
        upd_params[ 'method' ] = 'get'
        upd_params[ 'asynchronous' ] = false
        new Ajax.Updater( load_zone, link, upd_params)
        // TODO:  shuffle elements in DOM
        cur_and_next = Roboprog.collect_summaries( buf_el, link)
        items = cur_and_next[ 0 ]
        items.each( function( el)

            {
            if ( num_sum >= Roboprog.MAX_SUMMARIES)
                {
                return  // === skip (closure pass) ===
                }

            load_el.insert( el)
            num_sum++
            } )  // append each summary list item

        link = Roboprog.get_next_summary_link( link, cur_and_next[ 1 ])
        }  // load each summary

    }// _____

// Generate the link URI to the next page in this series.
//  dubious assumption:  the next page is in the same directory as the current one,
//  and specified with just the file name (basename).
//
// link: the base link to the page from which the data came
// a_els:  array of a[href] elements, hopefully of length 1.
Roboprog.get_next_summary_link = function( cur_link, a_els)
    {
    var a_el, link_tag, slash_idx, new_link

    if ( ( ! a_els) || ( a_els.size() == 0) )
        {
        return Roboprog.END;  // === done ===
        }  // no link tags found?

    link_tag = a_els[ 0 ]
    slash_idx = cur_link.lastIndexOf( '/')
    if ( slash_idx < 0)
        {
        return Roboprog.END;  // === done ===
        }  // do seperator?

    new_link = cur_link.slice( 0, slash_idx) + '/' +
            link_tag.getAttribute( 'href')
    return new_link
    }// _____

// scan for headings and initial paragraphs to put into preview list
// warning:  generated elements don't display under Internet Exploder
// (need to convert from pure function to direct mutator of page DOM - yuck)
//
// buf_el: hidden element from which text is taken
// link: the base link to the page from which the data came
Roboprog.collect_summaries = function( buf_el, link)
    {
    var lis, a_els, anchor, title_els, title_text, a_li, a_href,
            tease_els, tease_span, elipsis, prev_links

    lis = $A( [] )
    buf_el.select( 'div').each( function( a_div)
    
        {
        title_els = a_div.select( 'h3')
        title_text = ( title_els.size() > 0 ) ?
                title_els[ 0 ] : null
        if ( ! title_text)
            {
            return  // === skip === (this pass within a closure)
            }

        tease_els = a_div.select( 'span.tease')
        tease_span = ( tease_els.size() > 0 ) ?
                tease_els[ 0 ] : null
        if ( ! tease_span)
            {
            return  // === skip === (this pass within a closure)
            }

        a_els = a_div.select( 'a')
        anchor = ( a_els.size() > 0 ) ?
                a_els[ 0 ].getAttribute( 'name') : null
        anchor = anchor ?
                ( '#' + anchor) : ''

        a_href = new Element( 'a')
        a_href.setAttribute( 'href', link + anchor)
        a_href.textContent = 'Read:'
        a_href.insert( title_text)
        a_li = new Element( 'li')
        a_li.insert( new Element( 'p') )  // spacer
        a_li.insert( a_href)
        elipsis = new Element( 'p')
        elipsis.textContent = '...'
        a_li.insert( elipsis)
        a_li.insert( tease_span)
        elipsis = new Element( 'p')
        elipsis.textContent = '...'
        a_li.insert( elipsis)
        lis[ lis.size() ] = a_li
        } )  // scan each div

    prev_links = $$( 'a.prev')
    return [ lis, prev_links ]
    }// _____


// *** EOF ***


