//****************************************************************************
var selected_domain_category = null;
var default_domain_category  = null;
var selected_domain          = null;
var domain_max_chunks        = 3;
var domain_limit_index       = 1;

// RPC invoker for the categories list
function get_domain_categories() {

    // abort if in teXt mode
    if ( get_value( 'x', 'home' ) != 'home' ) return;

    var parameters = new Array();
    parameters.push( 'rpc_class=domain_cats' );
    parameters.push( 'category_index=' + escape( get_value( 'c', 0 ) ) );
    send_rpc( parameters, add_category_list );
    return;
}

// RPC invoker for the domain results alpha links
function get_alpha_links_for( category_index, key ) {
    var parameters = new Array();
    parameters.push( 'rpc_class=domain_alpha_links' );
    parameters.push( 'category_index=' + escape( category_index ) );
    parameters.push( 'chunks_per_pg=' + escape( window.domain_max_chunks ) );
    parameters.push( 'limit_index=' + escape( window.domain_limit_index ) );
    parameters.push( 'alpha_key=' + escape( key ) );
    send_rpc( parameters, add_alpha_links );
    return;
}

// RPC invoker for the domains list
function get_domains_for( category_index, start ) {

    // abort if in teXt mode
    if ( get_value( 'x', 'home' ) != 'home' ) return;
 
    // var rpc_class = 'domain_search';
    var rpc_class = 'domain_browse';
    if ( typeof( category_index ) == 'undefined' ) {
        category_index = get_value( 'c', 0 );
    }
    if ( typeof( start ) == 'undefined' ) {
        start = get_value( 's', 0 );
    }

    var rpc_id = window.current_rpc_id[ rpc_class ];
    if ( start == 0 ) {
        // new occurances of this class will take priority over older ones
        rpc_id = unique_id();
    }
    var parameters = new Array();
    parameters.push( 'rpc_class=' + rpc_class );
    parameters.push( 'category_index=' + escape( category_index ) );
    parameters.push( 'limit_index=' + escape( window.domain_limit_index ) );
    parameters.push( 'rpc_id=' + escape( rpc_id ) );
    parameters.push( 'start=' + escape( start ) );
    send_rpc( parameters, add_domain_list );
    return;
}

// DOM builder for the category list ( a.k.a 'callback' )
function add_category_list( categories ) {

    if ( !categories || !categories.innerHTML ) {
        logit( 'null category data received' );
        return;
    }

    var domain_cats_column = domElement( 'domain_cats_column' );
    if ( domain_cats_column ) {
        domain_cats_column.innerHTML = categories.innerHTML;
        // logit( 'added categories: ' + categories.innerHTML );
    }
    else {
        logit( 'failed to get category column ' );
    }
    if ( window.default_domain_category == null ) {
        window.default_domain_category = categories.default_category;
        highlight_category( categories.default_category );
    }
    return;
}

// DOM builder for the domains list ( a.k.a 'callback' )
function add_domain_list( domains ) {

    if ( !domains ) return false;

    // verify this invokation is in response to the most current request
    if ( domains.rpc_id != window.current_rpc_id[domains.rpc_class] ) {
        // abort: another rpc of the same class has taken priority
        return;
    }

    var current_limit = window.limits[ window.domain_limit_index ];
   
    var results_per_page = (
        window.domain_max_chunks * current_limit
    );

    var current_chunk_number = (
        ( 
            Math.round( domains.start / current_limit )
            % 
            window.domain_max_chunks
        )
        + 1
    );
 
    var current_page_number = (
        Math.floor( 
            ( 
                parseInt( domains.start ) + parseInt( results_per_page ) 
            )
            / results_per_page 
        )
    );

    var max_start_index = (
        current_page_number * results_per_page
    );

    // issue the purge directive if this is the first data chunk
    // for the current rpc
    var purge_directive = null;
    if ( current_chunk_number == 1 ) {
        if (
            window.domain_max_chunks 
            <
            ( domains.total_found / current_limit ) 
        ) {

            generate_page_links(
                domains.start,
                current_page_number,
                results_per_page,
                domains.total_found,
                '/home?c=' + domains.category_index
            );    
        }  
        purge_directive = 'purge';
    }

    // add the current result set to the ui
    add_list_items( 
        'domain', 
        add_checkout_links( domains ), 
        purge_directive 
    );

    // go back for more, if there is any
    if ( 
         domains.next         > 0 
      && domains.next         < max_start_index 
      && current_chunk_number < window.domain_max_chunks
    ) {
        // use the proposed next as the start of the next chunk
        get_domains_for( 
            domains.category_name, 
            domains.next 
        );
    }
    else {
        // this rpc is no longer active
        window.current_rpc_id[domains.rpc_class] = null;

        logit( 'completed final chunk for rpc call: ' + domains.rpc_id );
       
        if ( domains.total_found > 0 
          && domains.total_found > results_per_page
        ) {
            // initiate alpha link requests
            get_alpha_links_for( domains.category_index, '0' );
        }
    }
    return;
}

function add_alpha_links( alpha_link ) {

    if ( alpha_link.innerHTML != '' ) {

        var alpha_links = domElement( 'alpha_links' );

        if (0) { // this disabled until a better idea materializes

            var current_limit = window.limits[ window.domain_limit_index ];

            var results_per_page = (
                window.domain_max_chunks * current_limit
            );

            var start = parseInt( get_value( 's', 0 ) );

            var link_regex = new RegExp(
                    '('
                    + '<a[ ]href="[\/]home[\?]c=[0-9]+&amp;s=[0-9]+">'
                    + '&nbsp;'
                    + '[0A-Z]'
                    + '&nbsp;<[\/]a>' +
                    ')',
                    'gi'
                );
            var start_regex = new RegExp( 
                '&amp;s=([0-9]+)">'
                + '(&nbsp;[0A-Z]&nbsp;)'
            );
            
            var links = alpha_link.innerHTML.match( link_regex );

            for ( var index in links ) {

                var link_start = links[index].match( start_regex );

                // This only hits when the current page is an alpha
                // transition boundary.
                if ( link_start[1] = start
                && link_start[1] < parseInt( start + results_per_page ) 
                ) {
                    // Strip the active link to a non-link        
                    alpha_link.innerHTML = alpha_link.innerHTML.replace( 
                        links[index], 
                        link_start[2] 
                    );
                    break;
                }
            }
        }

        // Add the links to the page
        alpha_links.innerHTML = alpha_link.innerHTML;
    }
    return;
}

// enhance results with links to the request page
function add_checkout_links( domains ) {

    var domain_id_regex = new RegExp(/"domain_([\d]+)"/);
    var root_directory = get_root_directory();
    if ( root_directory == 'http:///' ) root_directory = '';

    for ( var n in domains.list_items ) {

        var domain_id_match = domains.list_items[n].match( domain_id_regex );

        if ( domain_id_match && domain_id_match[1] ) {
            domains.list_items[n] = '<a href="' 
                + '?t=request&i='
                + domain_id_match[1] 
                + '">'
                + domains.list_items[n]
                + '</a>';
        }
        else {
            logit( 
                'add_checkout_links: '
                + 'domains.list_items[' + n + '] => ' + domains.list_items[n] 
                + ' -- there was a problem parsing the domain_id.'
            );
        }
    }
    return domains;
}

// category onlick event
function select_category( id ) {
    var category_index = id.replace(/cat_/, '');
    highlight_category( category_index );
    get_domains_for( category_index );
    return;
}

// ui modifier
function highlight_category( category_name ) {

    // this function is kind of obsolete
    return;

    var theUls = document.getElementsByTagName( 'UL' );

    // iterate through all the categories and set theme either on or off
    var theCategoryUl = domElement( 'domain_category_list' );
    if ( !theCategoryUl ) return;

    var theCategoryLis = theCategoryUl.getElementsByTagName( 'LI' );
    if ( !theCategoryLis ) return;

    var category_id = 'cat_';
    category_id += category_name.replace( '%20', ' ' ).replace( ' ', '_' );

    for (var li in theCategoryLis ) {

        if ( theCategoryLis[li].nodeName != 'LI' ) continue;

        if ( theCategoryLis[li].id == category_id ) {
            theCategoryLis[li].style.color          = 'yellow';
            theCategoryLis[li].style.textDecoration = 'none';
            theCategoryLis[li].style.fontWeight     = 'bold';

            return;
        }
    }
    return;
}

// load up the categories list in 10 milliseconds ( during page load )
// setTimeout( 'get_domain_categories();', 10 );
setTimeout( 'get_domains_for();',       10 );
