// ********************************************************
// Main menu

function nav_node_toggle( node ) {

    node.children('span.nav-node-img').toggleClass('nav-node-img-opened');
    node.siblings('span.nav-node-span').toggle();

}

function menu_leaf_on_click( uri ) {
    var matches = /page=([^&]+)/.exec( uri );
    if( matches ) {
        var page = matches[ 1 ];
        $( '.page-content' ).html(
            '<div class="ajax-content" id="page-content" uri="' + uri + '" page="' + page + '"/>'
        );
        get_one_ajax_content( $( '#page-content' ) );
    }
};


// ********************************************************
// AJAX processing

function replace_contents_with_spinner( target ) {
    target.empty();
    target.text( 'Processing...' );
    target.append( '<img src="images/spinner.gif" alt="(processing)"/>' );
}

function get_one_ajax_content( target ) {
    target.removeClass( 'ajax-content' );
    replace_contents_with_spinner( target );
    target.show();
    target.load(
        target.attr( 'uri' ),
        function() {
            // Fetch any nested ajax tabs
            get_one_ajax_content( $(this).find( '.tabcontent div.selected[@uri]' ) );
        }
    );
}

// ********************************************************
// Tab sets

function tabset_select (id, pos, max) {
    for (var i = 1; i <= max; i++) {
        var tab  = $("#" + "tabset-tab-"  + id + "-" + i);
        var body = $("#" + "tabset-body-" + id + "-" + i);

        if (i == pos) {
            tab.attr("class", "selected");
            body.attr("class", "selected");
            if( body.attr( 'uri' ) ) { 
                get_one_ajax_content( body );
            }
        }
        else {
            tab.attr("class", "notselected");
            body.attr("class", "notselected");
        }
    }
}



// ********************************************************
// Expanding tables

function show_row( row ) {
    var shown = false;
    var type = $.browser.msie ? 'inline' : 'table-row';
    if( row.css( 'display' ) != type ) {
        row.css( 'display', type );
        shown = true;
    }
    return shown;
}

function open_toggleable( toggleable ) {
    if( show_row( toggleable ) ) {
        // Call AJAX, if specified
        var ajax_target = toggleable.children( 'td[@uri]' ).eq( 0 );
        if( ajax_target.size() > 0 ) {
            if( ajax_target.attr( 'uri' ) ) {
                get_one_ajax_content( ajax_target );
            }
        }
    }
}

function toggler_on_click( button, toggleable_id, action ) {
    var toggleable = $( '#' + toggleable_id );
    
    if( toggleable.size() > 0 ) {
        toggle_visibility_widgets( button.parent() );
        if( action == 'show' ) {
            open_toggleable( toggleable );
        } else if( action == 'hide' ) {
            toggleable.hide();
        }
    }
};




// ********************************************************
// Form processing

function process_errors( parent ) {
    var num_errors = 0;
    parent.find( '.error' ).each( function() {
        num_errors++;
        var formid = $(this).attr( 'formid' );
        var target = $( 'form#' + formid );
        if( target ) {
            var message = $( '<div class="error">Internal error.</div>' );
            message.prependTo( target );
        }
    } );
    parent.find( '.error-input' ).each( function() {
        num_errors++;
        var formid = $(this).attr( 'formid' );
        var inputname = $(this).attr( 'inputname' );
        var target = $( 'form#' + formid + ' :input[@name="' + inputname + '"]' );
        if( target ) {
            $(this).insertAfter( target );
            $(this).show( 'normal' );
        }
    } );
    return num_errors;
};

// Add a new row to the top of an expanding table.
function add_to_list( result ) {
    var list = result.parents( '.tr-add' ).eq( 0 ).parents( 'table' ).eq( 0 );
    var rows = result.find( '.toggler.tr-new' );  // possibly more than one
    if( list.size() > 0 && rows.size() > 0 ) {
        var top_row = list.find( '.tr.toggler:first' );
        rows.each( function() {
            var row = $(this);
            row.insertBefore( top_row );
            var toggleable = row.nextAll( '.toggleable:first' );
            toggleable.insertAfter( row );
            show_row( row );
        } );
    }
}

function form_on_submit( target, uri ) {
    var spinner = $( '<div class="spinner"/>' );
    replace_contents_with_spinner( spinner );
    spinner.insertAfter( target );
    
    var input = {};
    $( ':input', target ).each( function() {
        input[ this.name ] = this.value;
    } );
    // Clear form messages.
    $( '.error-input' ).remove();
    $( '.success' ).remove();
    
    var ajax_receiver = $( '<div/>' );
    ajax_receiver.load(
        uri,
        input,
        function() {
            spinner.hide();
            var num_errors = process_errors( $(this) );
            if( num_errors == 0 ) {
                target.replaceWith( $(this) );
                $(this).find( '.success' ).slideDown();
                add_to_list( $(this) );
            }
            get_one_ajax_content( $(this).find( '.ajax-content[uri]' ) );
        }
    );
};

// ********************************************************
// Misc functions

function openwindow( url, width, height ) {
    var win = window.open(
        url,
        "features",  // window name
        "toolbar=1,location=1,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,top=50,left=50,width="+width+",height="+height
    );
    win.focus();
}

// The parent should contain a .hider and a .shower.
function toggle_visibility_widgets( parent ) {
    var togglers = parent.children( '.hider|.shower' );
    if( togglers.eq( 0 ).css( 'display' ) == 'none' ) {
        togglers.eq( 1 ).hide();
        togglers.eq( 0 ).css( 'display', 'inline' );
    } else {
        togglers.eq( 0 ).hide();
        togglers.eq( 1 ).css( 'display', 'inline' );
    }
}
