#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################

#print STDERR "Using local version of gull_contig_clone !";

# $id$
# $Log: gull_contig_clone.pm,v $
# Revision 1.16  2000/03/24  11:16:11  dn1
# *** empty log message ***
#
# Revision 1.15  1999/08/18  10:07:48  dn1
# *** empty log message ***
#
# Revision 1.14  1999/05/13 12:29:50  dn1
# *** empty log message ***
#
# Revision 1.13  1999/05/13 11:42:50  dn1
# *** empty log message ***
#
# Revision 1.12  1999/05/06 16:39:22  dn1
# *** empty log message ***
#
# Revision 1.11  1999/03/22 18:23:54  dn1
# *** empty log message ***
#

# DN1
#######################################
# Clone selection from contig -options
#######################################
use strict;

my $entrytitle = 'Lightgoldenrod';
my $entrycolour = 'white';
my $BITMAP_DIR = "$ENV{'BADGER'}/lib/gull/bitmaps";
my $db;
#Default printer
my $printer = $ENV{PRINTER};



package My_choice_Window;

my $dbh;

sub new {

    my ($type, $mw, $clone, $project_status, $given_project);
    ($type, $mw, $clone, $db, $project_status, $given_project, $dbh) = @_;


#    warn "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ $project_status \n";

    my $self = {};
    
    if ($project_status eq 'Cannot determine'){
	$self->{project_status_history_available} = 'no';
    } else {
	$self->{project_status_history_available} = 'yes';
    }

    $self->{Original_mw} = $mw;
    $self->{clone} = $clone;

    # New 
    $self->{given_project} = $given_project;
    # New 

    bless($self, $type);

    # Set up the screen object
    $self->makeWindow($mw, $clone);
    $self->makeFrames1();
    $self->makeButtons();

    return $self;
}

sub makeWindow {
    my ($self, $mw, $clone) = @_;
    $self->{mainWindow} = $mw->Toplevel;
    $self->{mainWindow}->configure(cursor  => 'hand2', title => $clone);
    $self->{mainWindow}->wm('minsize', 150, 240); 
    $self->{mainWindow}->wm('maxsize', 150, 240);
    my $f = $self->{mainWindow}->Frame(-height => 240, -width => 150);
    $f->pack(); 
    $self->{mainWindow}->idletasks();
    $self->{mainWindow}->packPropagate(0);
    $self->{mainWindow}->idletasks();
    $f->packForget();

    ##########
    # Bitmaps
    ##########
    # up button
    $self->{mainWindow}->Bitmap('up',
				-file => "$BITMAP_DIR/uarrw.xbm",
				-maskfile => "$BITMAP_DIR/uarrw.xbm"
				);
    # down button
    $self->{mainWindow}->Bitmap('down',
				-file => "$BITMAP_DIR/darrw.xbm",
				-maskfile => "$BITMAP_DIR/darrw.xbm"
				);
}

sub makeFrames1 {
    my ($self) = @_;
    $self->{button_frame} = $self->{mainWindow}->Frame()->pack(side => 'top', fill => 'x');
}

sub makeFrames2 {
    my ($self) = @_;
    $self->{title_frame} = $self->{mainWindow}->Frame()->pack(-side => 'top');
    $self->{menu_frame} = $self->{mainWindow}->Frame()->pack(side => 'top', fill => 'x');
    $self->{main_frame} = $self->{mainWindow}->Frame(-relief => 'groove', -borderwidth => '6')->pack(-side => 'top', -expand => 'yes', -fill => 'both');

    # Need a bottom section for some buttons plus a next/prev clone section
    $self->{main_frame_top} = $self->{main_frame}->Frame()->pack(-side => 'top');
    #$self->{main_frame_bottom} = $self->{main_frame}->Frame(-relief => 'groove', -borderwidth => '6')->pack(-side => 'bottom');
    $self->{main_frame_bottom} = $self->{main_frame}->Frame()->pack(-side => 'bottom');

    # Split the main screen into 2
    $self->{main_frame_top_left} = $self->{main_frame_top}->Frame()->pack(-side => 'left');
    $self->{main_frame_top_right} = $self->{main_frame_top}->Frame()->pack(-side => 'right');
}

sub bottom_buttons {
    my ($self) = @_;
    $self->{up_button} = $self->{main_frame_bottom}->Button( 
							    -state => 'disabled',
							    -image => 'up', 
							    -command => sub { $self->goup();}
							    )->pack(-side => 'left');

    $self->{down_button} = $self->{main_frame_bottom}->Button( 
							      -state => 'disabled',
							      -image => 'down', 
							      -command => sub { $self->godown();}
							      )->pack(-side => 'left');

    $self->{which_entry} = $self->{main_frame_bottom}->Entry(
				      -bg => $entrycolour, 
				      -width => 10, 
				      -textvariable => \$self->{where}, 
				      -state => 'disabled'
				      )->pack(-side => 'left');

}

sub godown {
    my ($self) = @_;
    
#    warn "DEBUG !!! $self->{this_hit} vs. $self->{total_hits} \n";
    unless ($self->{this_hit} == 1){

#	warn "yup ! In here ! warn $self->{type}\n";
	
	
	if ($self->{type} eq '  Marker info'){
	    $self->{this_hit}--;
	    $self->{markeralias_value}->configure (-textvariable => \$self->{markers}->{($self->{this_hit})}->{markeralias});      
	    $self->{markername_value}->configure (-textvariable => \$self->{markers}->{($self->{this_hit})}->{markername});     
	    $self->setwhere();
	}





	if ($self->{type} eq '  Overlap info'){
	    $self->{this_hit}--;

	    ############
	    #&var_monitor::unmonitor(\$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION});
	    ############

	    $self->{clonename_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{CLONENAME}); 
	    $self->{overlaps_clone_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{OVERLAPS_CLONE});      

#	    warn "&&&&&&&&&&&&&&&&&&&&&&&&&& $self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} \n";
	    $self->{confirmation_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION});
	    $self->{size_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{SIZE_KB});
	    $self->{variation_check_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{VARIATION_CHECK});

	    $self->{olr_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{OLR});

	    $self->setwhere();
	    $self->confirmation_check();
	    my $go = sub {
#		warn "inside ---> GO $self \n";
		$self->testytest;
	    };
	    &var_monitor::monitor(\$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION}, 'confirmation', $go);
	}



	
	if ($self->{type} eq '  Project info'){
	    $self->{this_hit}--;
	    $self->{projectname_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{PROJECTNAME}); 
	    $self->{team_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{TEAM_OWNER});      
	    $self->{status_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{STATUS}); 

	    $self->{status_remark_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{STATUS_REMARK}); 
	    $self->{project_remark_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{PROJECT_REMARK}); 

	    $self->{current_finisher_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{FINISHER});
	    $self->setwhere();   
	}


    }
    
}

sub goup {
    my ($self) = @_;
#    warn "DEBUG !!! $self->{this_hit} vs. $self->{total_hits} \n";
    unless ($self->{this_hit} == $self->{total_hits}){
	
#	warn "yup ! In here ! warn $self->{type}\n";

	if ($self->{type} eq '  Marker info'){
	    $self->{this_hit}++;
	    $self->{markeralias_value}->configure (-textvariable => \$self->{markers}->{($self->{this_hit})}->{markeralias});
	    $self->{markername_value}->configure (-textvariable => \$self->{markers}->{($self->{this_hit})}->{markername});  
	    $self->setwhere();
	}

	if ($self->{type} eq '  Overlap info'){

	    ############# fucks things up ---> need to take a look >!<
	    #&var_monitor::unmonitor(\$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION});
	    #############

	    $self->{this_hit}++;
	    $self->{clonename_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{CLONENAME}); 
	    $self->{overlaps_clone_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{OVERLAPS_CLONE});      

#	    warn "&&&&&&&&&&&&&&&&&&&&&&&&&& $self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} \n";
	    $self->{confirmation_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION}); 
	    $self->{size_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{SIZE_KB});
	    $self->{variation_check_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{VARIATION_CHECK});

	    $self->{olr_value}->configure (-textvariable => \$self->{overlaps}->{($self->{this_hit})}->{OLR});

	    $self->setwhere();
	    $self->confirmation_check();
	    my $go = sub {
#		warn "inside ---> GO $self \n";
		$self->testytest;
	    };
	    &var_monitor::monitor(\$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION}, 'confirmation', $go);
	}
	
	if ($self->{type} eq '  Project info'){
	    $self->{this_hit}++;
	    $self->{projectname_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{PROJECTNAME}); 
	    $self->{team_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{TEAM_OWNER});      
	    $self->{status_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{STATUS}); 
	    
	    $self->{status_remark_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{STATUS_REMARK}); 
	    $self->{project_remark_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{PROJECT_REMARK}); 

	    $self->{current_finisher_value}->configure (-textvariable => \$self->{projects}->{($self->{this_hit})}->{FINISHER});

	    $self->setwhere(); 
	}
    }
}

sub makeTitles {
    my ($self) = @_;
    $self->{clone_label} = $self->{title_frame}->Label(font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
						      text => '  Clone:')->pack(-side => 'left');
    $self->{clone_entry} = $self->{title_frame}->Entry(
					 -state => 'disabled',
					 -width => 15,
					 -font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
					 -bg => $entrycolour,
					 -textvariable => \$self->{clone},
					 )->pack(side => 'left');
    $self->{data_type} = $self->{title_frame}->Label(font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
						     text => "$self->{type}")->pack(-side => 'right');
}

sub makeMenu {
    my ($self) = @_;
    $self->{menu} = $self->{menu_frame}->Menubutton(bg => 'grey77', text => 'File', underline => 0);

    # File
    $self->{fileMenu1} = $self->{menu_frame}->Menubutton(bg => 'grey77', text => 'File', underline => 0);
    $self->{fileMenu1}->configure(-menu => $self->{fileMenu1} ->Menu(tearoff => 0));
    $self->{fileMenu1}->command(-label => 'Quit', 
				underline => 0,
				command => sub{ 
				    $self->{mainWindow}->destroy();
				});
    $self->{fileMenu1}->pack(-side => 'left');


}

sub makeButtons {
    my ($self) = @_;

    $self->{Marker_info_button} = $self->{button_frame}->Button(-font => '-*-clean-*-r-*-*-14-*-*-*-*-*-*-*', -text => 'Marker info', -state => 'active', 
								-command => sub { 
								    # code
								    $self->{type} = '  Marker info';
								    $self->button_away();
								    
								})->pack(-side => 'top', -fill => 'x');

    $self->{Overlap_info_button} = $self->{button_frame}->Button(-font => '-*-clean-*-r-*-*-14-*-*-*-*-*-*-*', -text => 'Overlap info', -state => 'active',
								 -command => sub {
								     # code
								     $self->{type} = '  Overlap info';
								     $self->button_away();

								 })->pack(-side => 'top', -fill => 'x');
    $self->{Project_info_button} = $self->{button_frame}->Button(-font => '-*-clean-*-r-*-*-14-*-*-*-*-*-*-*', -text => 'Project info', -state => 'active', 
								 -command => sub {
								     # code
								     $self->{type} = '  Project info';
								     $self->button_away();

								 })->pack(-side => 'top', -fill => 'x');

    
    $self->{Clon_info_button} = $self->{button_frame}->Button(-font => '-*-clean-*-r-*-*-14-*-*-*-*-*-*-*', -text => 'Clone info', -state => 'active', 
							      -command => sub {
								  unless ($self->{clone} eq ''){
								      #my ($return) = system "gull_clone -auto $self->{clone} &";
								  #if ($return){warn $!; }
								      
								      #NEW
								      my $child;
								      $self->{Clon_info_button}->configure(-state => 'disabled', -text => "Please wait", -background => 'Lightgoldenrod');
								      $child = Tk::IO->new(-linecommand => sub {$self->{Clon_info_button}->configure(-state => 'normal', -text => "Clone info", -background => 'light grey');}, -childcommand => sub {$child->wait()});
								      my $return = $child->exec("gull_clone -auto $self->{clone}");




							      }
							      })->pack(-side => 'top', -fill => 'x');

    #####################################################
    # Try adding Project and Clone History buttons here
    #####################################################
    $self->{Clone_his_button} = $self->{button_frame}->Button(-font => '-*-clean-*-r-*-*-14-*-*-*-*-*-*-*', -text => 'Clone history', -state => 'active', 
								-command => sub { 

								    my $code = sub {
								    };
#								    my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
								    my $status_history = status->get_history($dbh, $self->{clone});
#								    $dbh->disconnect();
								    status->start($self->{Original_mw}, $self->{clone}, $status_history, $db, $code, 'Clone', 'no');
								    $self->{mainWindow}->destroy();

								})->pack(-side => 'top', -fill => 'x');


    $self->{Project_his_button} = $self->{button_frame}->Button(-font => '-*-clean-*-r-*-*-14-*-*-*-*-*-*-*', -text => 'Project history', -state => 'disabled', 
							      -command => sub { 
								  # code
								  my $code = sub {
								  };
#								  my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
								  #print STDERR "SENDING $self->{given_project}\n";

								  my $status_history = status->get_project_history($dbh, $self->{given_project});
#								  $dbh->disconnect();
								  #status->start($self->{Original_mw}, $self->{clone}, $status_history, $db, $code, 'Project', 'no');
								  status->start($self->{Original_mw}, $self->{given_project}, $status_history, $db, $code, 'Project', 'no');
								  $self->{mainWindow}->destroy();
								  
							      })->pack(-side => 'top', -fill => 'x');
    
    $self->{Cancel_button} = $self->{button_frame}->Button(-font => '-*-clean-*-r-*-*-14-*-*-*-*-*-*-*', -text => 'Cancel', -state => 'active', 
							   -command => sub { 
							       # code
							       $self->{mainWindow}->destroy();
							   })->pack(-side => 'top', -fill => 'x');

    if ($self->{project_status_history_available} eq 'yes'){
	$self->{Project_his_button}->configure(-state => 'active');
    }

}

sub makeEntries_MARKER {
    my ($self) = @_;
    my $a = 'Marker alias';
    $self->{markeralias} = $self->{main_frame_top_left}->Entry(
							       -state => 'disabled',
							       -width => 15,
							       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							       -bg => $entrytitle,
							       -textvariable => \$a,
							       )->pack(side => 'top');
    
    $self->{markeralias_value} = $self->{main_frame_top_right}->Entry(
								     -state => 'disabled',
								     -width => 20,
								     #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								     #-bg => $entrytitle,
								      -textvariable => \$self->{markers}->{($self->{this_hit})}->{markeralias},
								     )->pack(side => 'top');

    my $b = 'Marker name';
    $self->{markername} = $self->{main_frame_top_left}->Entry(
							      -state => 'disabled',
							      -width => 15,
							      #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							      -bg => $entrytitle,
							      -textvariable => \$b,
							      )->pack(side => 'top');
    
    $self->{markername_value} = $self->{main_frame_top_right}->Entry(
								    -state => 'disabled',
								    -width => 20,
								    #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								    #-bg => $entrytitle,
								     -textvariable => \$self->{markers}->{($self->{this_hit})}->{markername},
								    )->pack(side => 'top');
    
}

sub makeEntries_OVERLAP {
    my ($self) = @_;
 
    my $ab = 'Clonename';
    $self->{clonename} = $self->{main_frame_top_left}->Entry(
							     -state => 'disabled',
							     -width => 15,
							     #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							     -bg => $entrytitle,
							     -textvariable => \$ab,
							     )->pack(side => 'top');
    
    $self->{clonename_value} = $self->{main_frame_top_right}->Entry(
								    -state => 'disabled',
								    -width => 20,
								    #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								    #-bg => $entrytitle,
								    -textvariable => \$self->{overlaps}->{($self->{this_hit})}->{CLONENAME},
								    )->pack(side => 'top');



    my $a = 'Overlaps clone';
    $self->{overlaps_clone} = $self->{main_frame_top_left}->Entry(
								  -state => 'disabled',
								  -width => 15,
								  #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								  -bg => $entrytitle,
								  -textvariable => \$a,
								  )->pack(side => 'top');
    
    $self->{overlaps_clone_value} = $self->{main_frame_top_right}->Entry(
									 -state => 'disabled',
									 -width => 20,
									 #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
									 #-bg => $entrytitle,
									 -textvariable => \$self->{overlaps}->{($self->{this_hit})}->{OVERLAPS_CLONE},
									 )->pack(side => 'top');

    my $b = 'Confirmation';
    $self->{confirmation} = $self->{main_frame_top_left}->Entry(
								-state => 'disabled',
								-width => 15,
								#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								-bg => $entrytitle,
								-textvariable => \$b,
								)->pack(side => 'top');

    $self->{confirmation_value} = $self->{main_frame_top_right}->Entry(
								       -state => 'disabled',
								       -width => 20,
								       -textvariable => \$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION},
								       )->pack(side => 'top');




    my $c = 'Size (kb)';
    $self->{size} = $self->{main_frame_top_left}->Entry(
							-state => 'disabled',
							-width => 15,
							#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							-bg => $entrytitle,
							-textvariable => \$c,
							)->pack(side => 'top');
    
    $self->{size_value} = $self->{main_frame_top_right}->Entry(
							       -state => 'disabled',
							       -width => 20,
							       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							       #-bg => $entrytitle,
							       -textvariable => \$self->{overlaps}->{($self->{this_hit})}->{SIZE_KB},
							       )->pack(side => 'top');

    my $d = 'Variation check';
    $self->{variation_check} = $self->{main_frame_top_left}->Entry(
								   -state => 'disabled',
								   -width => 15,
								   #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								   -bg => $entrytitle,
								   -textvariable => \$d,
								   )->pack(side => 'top');

    $self->{variation_check_value} = $self->{main_frame_top_right}->Entry(
									  -state => 'disabled',
									  -width => 20,
									  -textvariable => \$self->{overlaps}->{($self->{this_hit})}->{VARIATION_CHECK},
									  )->pack(side => 'top');

    
    my $e = 'Remark';
    $self->{olr} = $self->{main_frame_top_left}->Entry(
						       -state => 'disabled',
						       -width => 15,
						       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
						       -bg => $entrytitle,
						       -textvariable => \$e,
						       )->pack(side => 'top');

    $self->{olr_value} = $self->{main_frame_top_right}->Entry(
							      -state => 'disabled',
							      -width => 20,
							      -textvariable => \$self->{overlaps}->{($self->{this_hit})}->{OLR},
							      )->pack(side => 'top');  
}

sub testytest {
    my ($self) = @_;
#    warn "inside testytest -> $self \n";
    $self->confirmation_check();
}

sub makeEntries_PROJECTS {
    my ($self) = @_;

#    my $capture = Data::Dumper::Dumper($self);
#    warn $capture;
#    die;

    my $a = 'Projectname';
    $self->{projectname} = $self->{main_frame_top_left}->Entry(
							       -state => 'disabled',
							       -width => 15,
							       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							       -bg => $entrytitle,
							       -textvariable => \$a,
							       )->pack(side => 'top');
    
    $self->{projectname_value} = $self->{main_frame_top_right}->Entry(
								      -state => 'disabled',
								      -width => 20,
								      #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								      -textvariable => \$self->{projects}->{($self->{this_hit})}->{PROJECTNAME},
								      #-bg => $entrytitle,
								      )->pack(side => 'top');

    my $b = 'Team';
    $self->{team} = $self->{main_frame_top_left}->Entry(
							-state => 'disabled',
							-width => 15,
							#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							-bg => $entrytitle,
							-textvariable => \$b,
							)->pack(side => 'top');
    
    $self->{team_value} = $self->{main_frame_top_right}->Entry(
							       -state => 'disabled',
							       -width => 20,
							       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							       #-bg => $entrytitle,
							       -textvariable => \$self->{projects}->{($self->{this_hit})}->{TEAM_OWNER},
							       )->pack(side => 'top');
    my $c = 'Status';
    $self->{status} = $self->{main_frame_top_left}->Entry(
							  -state => 'disabled',
							  -width => 15,
							  #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							  -bg => $entrytitle,
							  -textvariable => \$c,
							  )->pack(side => 'top');
    
    $self->{status_value} = $self->{main_frame_top_right}->Entry(
								 -state => 'disabled',
								 -width => 20,
								 #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								 #-bg => $entrytitle,
								 -textvariable => \$self->{projects}->{($self->{this_hit})}->{STATUS},
								 )->pack(side => 'top');

#### some new stuff ####
    my $d = 'Status Remark';
    $self->{status_remark} = $self->{main_frame_top_left}->Entry(
								 -state => 'disabled',
								 -width => 15,
								 #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								 -bg => $entrytitle,
								 -textvariable => \$d,
								 )->pack(side => 'top');
    
    $self->{status_remark_value} = $self->{main_frame_top_right}->Entry(
									-state => 'disabled',
									-width => 20,
									#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
									#-bg => $entrytitle,
									-textvariable => \$self->{projects}->{($self->{this_hit})}->{STATUS_REMARK},
									)->pack(side => 'top');

    
my $e = 'Project Remark';
    $self->{project_remark} = $self->{main_frame_top_left}->Entry(
								  -state => 'disabled',
								  -width => 15,
								  #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								  -bg => $entrytitle,
								  -textvariable => \$e,
								  )->pack(side => 'top');
    
    $self->{project_remark_value} = $self->{main_frame_top_right}->Entry(
									-state => 'disabled',
									-width => 20,
									#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
									#-bg => $entrytitle,
									-textvariable => \$self->{projects}->{($self->{this_hit})}->{PROJECT_REMARK},
									)->pack(side => 'top');

    my $f = 'Current Finisher';
    $self->{current_finisher} = $self->{main_frame_top_left}->Entry(
								    -state => 'disabled',
								    -width => 15,
								    #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								    -bg => $entrytitle,
								    -textvariable => \$f,
								    )->pack(side => 'top');
    
    $self->{current_finisher_value} = $self->{main_frame_top_right}->Entry(
									-state => 'disabled',
									-width => 20,
									#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
									#-bg => $entrytitle,
									-textvariable => \$self->{projects}->{($self->{this_hit})}->{FINISHER},
									)->pack(side => 'top');


##### end of new stuff

    
    $self->{info_button} = $self->{main_frame_bottom}->Button(
							      -state => 'active',
							      -width => 10,
							      -text => 'More info',
							      -command => sub {

								  unless ($self->{projects}->{($self->{this_hit})}->{PROJECTNAME} eq ''){
#								  my ($return) = system "gull_proj -auto $self->{projects}->{($self->{this_hit})}->{PROJECTNAME} &";
#								  if ($return){warn $!; }

								      #NEW
								      my $child;
								      $self->{info_button}->configure(-state => 'disabled', -text => "Please wait", -background => 'Lightgoldenrod');
								      $child = Tk::IO->new(-linecommand => sub {$self->{info_button}->configure(-state => 'normal', -text => "More info", -background => 'light grey');}, -childcommand => sub {$child->wait()});
								      my $return = $child->exec("gull_proj -auto $self->{projects}->{($self->{this_hit})}->{PROJECTNAME}");


							      }
							      })->pack(side => 'top');
}



sub button_away {
    my ($self) = @_;

    $self->{Marker_info_button}->destroy();
    $self->{Overlap_info_button}->destroy();
    $self->{Project_info_button}->destroy();
    $self->{Cancel_button}->destroy();
    $self->{button_frame}->destroy();
    $self->{mainWindow}->wm('minsize', 500, 290); 
    $self->{mainWindow}->wm('maxsize', 500, 290);
    $self->{mainWindow}->packPropagate(1);
    $self->{mainWindow}->idletasks();

    $self->{this_hit} = 0;
    $self->{total_hits} = 0;
    $self->setwhere();
    $self->makeFrames2();
    $self->makeTitles();
    $self->makeMenu();
    $self->bottom_buttons();

    # Our entryboxes and labels depend upon the user choice
    if ($self->{type} eq '  Marker info'){
	# Need to get relevant marker information
#	my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
	my $markers = marker->get_markers($dbh, $self->{clone});
	$self->{markers} = $markers;
	my @count = keys(%$markers);
#	warn ($#count + 1);
	$self->{total_hits} = ($#count + 1);
	unless ($self->{total_hits} == 0){ 
	    unless ($self->{total_hits} == 1){
		$self->turn_up_and_down_on();
	    }
	    $self->{this_hit} = 1;
	    $self->setwhere();
	}
#	$dbh->disconnect();
	$self->makeEntries_MARKER();
    }


    if ($self->{type} eq '  Overlap info'){
	# Need to get relevant overlap information
#	my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
	my $overlaps = overlap->get_overlaps($dbh, $self->{clone});

	$self->{overlaps} = $overlaps;
	my @count = keys(%$overlaps);
#	warn ($#count + 1);
	$self->{total_hits} = ($#count + 1);
	unless ($self->{total_hits} == 0){ 
	    unless ($self->{total_hits} == 1){
		$self->turn_up_and_down_on();
	    }
	    $self->{this_hit} = 1;
	    $self->setwhere();
	}
#	$dbh->disconnect();
	$self->makeEntries_OVERLAP();
    }




    if ($self->{type} eq '  Project info'){
	# Need to get relevant overlap information
#	my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
	my $projects = project->get_projects($dbh, $self->{clone});

	$self->{projects} = $projects;

	my @count = keys(%$projects);
#	warn ($#count + 1);
	$self->{total_hits} = ($#count + 1);
	unless ($self->{total_hits} == 0){ 
	    unless ($self->{total_hits} == 1){
		$self->turn_up_and_down_on();
	    }
	    $self->{this_hit} = 1;
	    $self->setwhere();
	}
#	$dbh->disconnect();
	$self->makeEntries_PROJECTS();
    }

}

sub setwhere {
	my ($self) = @_;
	$self->{where} = "$self->{this_hit}" . " of " . "$self->{total_hits}";
    } 

sub turn_up_and_down_on {
    my ($self) = @_;
    $self->{up_button}->configure(-state=>'active');
    $self->{down_button}->configure(-state=>'active');


#    warn "%%%%%% $self->{total_hits} %%%%%%% \n";
}

sub off_size_var {
    my ($self) = @_;
    $self->{variation_check_value}->configure( -state => 'disabled');
    $self->{size_value}->configure( -state => 'disabled');
}

sub on_size_var {
    my ($self) = @_;
#    warn "inside on_size_var ---> trying to turn on variation and size editing \n"; 
    $self->{variation_check_value}->configure( -state => 'disabled');
    $self->{size_value}->configure( -state => 'normal');
}

sub confirmation_check {
    my ($self) = @_;
#    warn "got inside confirmation_check !!! $self \n";
#    warn "$self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} \n";

    if ($self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} eq 'Overlap confirmed'){
	$self->on_size_var();
    } 

    if ( ($self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} eq 'Not checked') || ($self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} eq 'Overlap refuted') ){
	$self->{overlaps}->{($self->{this_hit})}->{VARIATION_CHECK} = 'No variation found';
    }
    if ($self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} eq 'Overlap refuted'){
	$self->{overlaps}->{($self->{this_hit})}->{SIZE_KB} = 0;
    }

    if ($self->{overlaps}->{($self->{this_hit})}->{CONFIRMATION} ne 'Overlap confirmed'){
	$self->off_size_var();
    }

}

sub load_confirmation_list {
    my ($self) = @_;
#    my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
    my $con_list = confirmation_choice->get_list($dbh);
    my $next;
    foreach $next (@$con_list){
	$self->{confirmation_value}->insert('end', $next);
    }
#    $dbh->disconnect();
}

sub load_variation_list {
    my ($self) = @_;
#    my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
    my $var_list = variation_choice->get_list($dbh);
    my $next;
    foreach $next (@$var_list){
	$self->{variation_check_value}->insert('end', $next);
    }
#    $dbh->disconnect();
}

sub try_save_overlap {
    my ($self) = @_;
#    warn "Now trying to save entry !@!! \n";
    # What's the data were playing with here !!!!

    # $self->{overlaps}->{($self->{this_hit})

    #my $test = $self->{overlaps}->{($self->{this_hit})};
    #my $capture = Data::Dumper::Dumper($test);
    #warn $capture;

    # We should first test to see whether this table entry is locked
    # PK is on clonename and overlaps_clone
#    my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
    my $hit = $self->{overlaps}->{($self->{this_hit})};
#    warn "Now trying to lock this overlap entry... \n";
    my ($lock) = overlap->lock_entry($hit,$dbh);


#    warn "lock ---> $lock \n";

    ##########################################################################
    # If $lock is not zero then there was a problem locking the table entries
    ##########################################################################
    if ($lock > 0){
	main::popup ($self->{mainWindow}, "WARN", ["Sorry ! This clone_overlap entry is already locked. Please try again later.\n"]);
#	$dbh->disconnect();
	return();

      } else {
	###############################################
	# Try to update the entry with the new details
	##############################################
#	warn "Now trying to change this overlap entry... \n";
	my (@problems) = overlap->change_entry($hit,$dbh);
	if ($problems[0] ne 'End'){
	    #########################
	    # Was there a problem >? 
	    #########################
	    my $title = "Problems >!<";
	    &main::viewer($printer, $self->{mainWindow}, $title, \@problems);
#	    $dbh->disconnect();
	    return;
	}
    }

#    $dbh->disconnect();

}

1;



