//------------------------------------------------------------------------------
// Evil Evil Global Vars
//------------------------------------------------------------------------------
var existing = '' ;
var profile_json =
    'http://www.genomics.purdue.edu/~ltl/cgi-bin/submission/get_full_list.cgi' ;
var pi_list =
    'http://www.genomics.purdue.edu/~ltl/cgi-bin/submission/get_pi_list.cgi' ;
var aPI ;
var aChip = new Array(0) ;

//------------------------------------------------------------------------------
// Kinda the "Main()" for this. Runs when the page is fully loaded.
//------------------------------------------------------------------------------
$(function() {

    $( '<select/>').attr( 'id'   , 'pi_list' )
                   .attr( 'name' , 'pi_list' )
                   .attr( 'size' , '5' )
                   .append( '<option> Not Listed </option>')
                   .prependTo( '#primary_investigator_fieldset' )
                   .change( function () {
//alert( $( '#pi' ).val() ) ;
                    var id = $( this ).attr( 'value' ) ;
                    if ( id == 'Not Listed' ) {
                    $( '#pi'    ).attr( 'value' , '' ) ;
                    $( '#lab_director' ).attr( 'value' , '' ) ;
                    $( '#lab_director_email' ).attr( 'value' , '' ) ;
                    $( '#department'  ).attr( 'value' , item.dept ) ;
                        }
                    else {
                        $.each( aPI , function ( i , item ) {
                            if ( item.id == id ) {

                    $( '#pi'    ).attr( 'value' , item.id ) ;
                    $( '#lab_director' ).attr( 'value' , item.name ) ;
                    $( '#lab_director_email' ).attr( 'value' , item.email ) ;
                    $( '#department'  ).attr( 'value' , item.dept ) ;
                                }
                            } ) ;
                        }
                    } ) ;
    fill_array() ;
    $( '#toggle_service' ).hide() ;
    $( '#count'  ).change( function() { fill_array() ; } ) ;
    $( '#winnow' ).keyup( function() { fill_array() ; } ) ;
    $( '#delete' ).click( function () {
        return confirm( 'Are you sure you want to delete this profile?' ) ;
        } ) ;


    $( '.toggle_all' ).click( function () {
        var id   = $(this).attr('id') ;
        var name = $(this).attr('name') ;
            $( '#service' ).attr( 'value' , name ) ;
        if ( id == 'toggle_all' ) {
            $( '#toggle_all' ).hide() ;
            $( '#toggle_service' ).show() ;
            }
        else {
            $( '#toggle_all' ).show() ;
            $( '#toggle_service' ).hide() ;
            }
        fill_array() ;
        } ) ;

    $( '#edit_submit' ).click( function() {
        $( '#edit_profile').attr( 'value' ,  $( '#profile' ).attr('value') ) ;
        } ) ;

    $.getJSON( pi_list, function( data ) {
        aPI = data.items ;
        var x = Array(0);
        $.each( data.items, function( i, item ) {
            x.push( item.id ) ;
            } ) ;
        $.each( x , function( i, profile ) {
            if ( i < data.size ) {
                $( '<option/>' ).append( profile )
                               .appendTo( '#pi_list' )
                               ;
                }
            } ) ;
        $( 'select' ).each(function () {
            $( this ).children( 'option:first' ).each( function () {
                $( this ).attr( 'selected' , 'selected' ) ;
                } ) ;
            } ) ;
        } ) ;

    } ) ;
//------------------------------------------------------------------------------

//==============================================================================
// Functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
function set_title ( ) {
    var currentTime = new Date()
    var time = currentTime.toLocaleString() ;
    $( document ).attr( 'title' , 'aChip.length: ' + aChip.length + ' ' + time )
    //setTimeout( set_title() , 50000 )
    }
    //--------------------------------------------------------------------------

//------------------------------------------------------------------------------
function fill_array ( ) {
    var count     = $( '#count'   ).attr( 'value' ) ;
    var service   = $( '#service' ).attr( 'value' ) ;
    var substring = $( '#winnow'  ).attr( 'value' ) ;
    if ( typeof(substring) == "undefined" ) {
        substring = '' ;
        }
    $( '#profile' ).empty() ;
    aChip = [] ;

    $.getJSON( profile_json ,
        { service:service , count:count , substring:substring } ,
        function( data ) {
            $.each( data.profiles, function( i, profile ) {
                aChip.push( profile ) ;
                } ) ;
            $( '#profile' ).empty() ;
            if ( aChip.length < 1 ) {
                $( '<option/>' ).append( 'No Profiles Available' )
                                .prependTo( '#profile' )
                                ;
                }
            $.each( aChip.reverse() , function( i, profile ) {
                if ( i < data.size ) {
                    $( '<option/>' ).append( profile )
                                    .prependTo( '#profile' )
                                    ;
                    }
                } ) ;
            $( 'select' ).each(function () {
                $( this ).children( 'option:first' ).each( function () {
                    $( this ).attr( 'selected' , 'selected' ) ;
                    } ) ;
                } ) ;
            } ) ;
    }
    //--------------------------------------------------------------------------
