package DADA::Template::Widgets;

use lib qw(./ ../ ./dada ../dada ./DADA ../DADA ./DADA/perllib ../DADA/perllib); 

use DADA::Config qw(!:DEFAULT);  
use DADA::App::Guts; 
use CGI; 
my $q = new CGI; 
   $q->charset($DADA::Config::HTML_CHARSET);

	my $dbi_handle; 

if($DADA::Config::SUBSCRIBER_DB_TYPE =~ m/SQL/ || $DADA::Config::ARCHIVE_DB_TYPE =~ m/SQL/ || $DADA::Config::SETTINGS_DB_TYPE =~ m/SQL/){        
	require DADA::App::DBIHandle; 
    $dbi_handle = DADA::App::DBIHandle->new; 
}


my $wierd_abs_path = __FILE__; 
   $wierd_abs_path =~ s/^\///g;
   
my $First_Guess; 

my $Templates; 

if(! $DADA::Config::ALTERNATIVE_HTML_TEMPLATE_PATH ){ 

	eval { require File::Spec };
	
	if(!$@){

	   $Templates  =  File::Spec->rel2abs($wierd_abs_path);
	   $Templates  =~ s/Widgets\.pm$//g;
	   $Templates  =~ s/\/$//; # cut off the first slash, if it's there; 
	   $Templates .= '/templates';
	   
	   $First_Guess = $Templates; 
	   
	}elsif($@){

	 warn "$DADA::Config::PROGRAM_NAME warning: File::Spec isn't working correctly: ". $@;
	 warn 'You may want to setup the, "$DADA::Config::ALTERNATIVE_HTML_TEMPLATE_PATH " Config variable!';
	 
	} else{ 
	
		$Templates = $DADA::Config::ALTERNATIVE_HTML_TEMPLATE_PATH ;
    	$First_Guess = $Templates; 

	}
	
}else{ 
	
	$Templates = $DADA::Config::ALTERNATIVE_HTML_TEMPLATE_PATH ;
    $First_Guess = $Templates; 

}
		
my $second_guess_template  = $wierd_abs_path; 
   $second_guess_template  =~ s/Widgets\.pm$//g;
   $second_guess_template  =~ s/\/$//;
   $second_guess_template .= '/templates';
   $second_guess_template  = '/' . $second_guess_template;

require Exporter; 
@ISA    = qw(Exporter);  
@EXPORT = qw( 
 
global_list_sending_checkbox_widget

templates_dir

list_popup_menu
list_popup_login_form
list_popup_subscription_form

default_screen
send_email_screen
send_url_email_screen

login_switch_widget

screen

absolute_path


);

use strict; 
use vars qw( @EXPORT );

my %Global_Template_Variables = (

NO_ONE_SUBSCRIBED      => $DADA::Config::NO_ONE_SUBSCRIBED , 
GOOD_JOB_MESSAGE       => $DADA::Config::GOOD_JOB_MESSAGE, 
ROOT_PASS_IS_ENCRYPTED => $DADA::Config::ROOT_PASS_IS_ENCRYPTED, 
PROGRAM_NAME           => $DADA::Config::PROGRAM_NAME, 
PROGRAM_URL            => $DADA::Config::PROGRAM_URL,
S_PROGRAM_URL          => $DADA::Config::S_PROGRAM_URL,
SHOW_ADMIN_LINK        => $DADA::Config::SHOW_ADMIN_LINK,

SIGN_IN_FLAVOR_NAME    => $DADA::Config::SIGN_IN_FLAVOR_NAME, 
DISABLE_OUTSIDE_LOGINS => $DADA::Config::DISABLE_OUTSIDE_LOGINS, 

ADMIN_FLAVOR_NAME      => $DADA::Config::ADMIN_FLAVOR_NAME, 
SHOW_HELP_LINKS        => $DADA::Config::SHOW_HELP_LINKS,
HELP_LINKS_URL         => $DADA::Config::HELP_LINKS_URL, 
MAILPROG               => $DADA::Config::MAILPROG,
FILES                  => $DADA::Config::FILES, 
VER                    => $DADA::Config::VER, 
FCKEDITOR_URL          => $DADA::Config::FCKEDITOR_URL, 
GLOBAL_LIST_SENDING    => $DADA::Config::GLOBAL_LIST_SENDING, 

GIVE_PROPS_IN_HTML            => $DADA::Config::GIVE_PROPS_IN_HTML, 
GIVE_PROPS_IN_SUBSCRIBE_FORM  => $DADA::Config::GIVE_PROPS_IN_SUBSCRIBE_FORM, 
GIVE_PROPS_IN_ADMIN           => $DADA::Config::GIVE_PROPS_IN_ADMIN, 

DEFAULT_ADMIN_SCREEN          => $DADA::Config::DEFAULT_ADMIN_SCREEN, 
(
    ($DADA::Config::CPAN_DEBUG_SETTINGS{HTML_TEMPLATE} == 1) ? 
        (debug => 1, ) :
        ()
), 
                    

); 


my %Global_Template_Options = (

		#debug            => 1, 		
		path              => [$DADA::Config::ALTERNATIVE_HTML_TEMPLATE_PATH , $DADA::Config::TEMPLATES , $second_guess_template, $Templates, 'templates', 'Templates/templates', 'DADA/Templates/templates', '../DADA/Templates/templates'],
		die_on_bad_params => 0,	
		loop_context_vars => 1, 
										
);

											
=pod

=head1 Name

DADA::Template::Widgets

=head1 Description

Holds commonly used HTML 'widgets'

=head1 Subroutines

=cut


=pod

=head2 list_popup_menu

returns a popup menu holding all the list names as labels and 
list shortnames as values

=cut
sub templates_dir {
	return $First_Guess;
}

sub list_popup_menu { 
	my %args = (-show_hidden      => 0,
				-name             => 'list',
				-empty_list_check => 0, 
			    @_); 
	my $labels = {}; 
	
	require DADA::MailingList::Settings; 
		   $DADA::MailingList::Settings::dbi_obj = $dbi_handle;
	
	my @lists = available_lists(-Dont_Die => 1); 

	 
	return ' ' if !@lists;
	
	my $l_count = 0; 
	
	# This needs its own method...
		foreach my $list( @lists ){
			my $ls = DADA::MailingList::Settings->new(-List => $list); 
			my $li = $ls->get; 
			next if $args{-show_hidden} == 0 && $li->{hide_list} == 1; 
			$labels->{$list} = $li->{list_name};
			$l_count++;
		}
		my @opt_labels = sort { uc($labels->{$a}) cmp uc($labels->{$b}) } keys %$labels;
	#								#	
	
	if($l_count <= 0 && $args{-empty_list_check} == 1){ 
	
	    return undef; 
	}
	
	
	return $q->popup_menu( -name    => $args{-name}, 
						   -id      => $args{-name}, 
						  '-values' => [@opt_labels],
						   -labels   => $labels,
						   -style    => 'width:200px'); 
}




sub list_popup_login_form { 
	
	my %args = (-show_hidden => 0, -auth_state => undef, @_); 
	
	require HTML::Template::Expr;

	my $list_popup_menu = list_popup_menu(-name   	            => 'admin_list', 
				   		                  -show_hidden          => $args{-show_hidden},
				   		                  -empty_list_check     => 1, 
				   		                  
				   		                 );
				   		                 
	my $template = HTML::Template::Expr->new(%Global_Template_Options, 
											 filename          => 'list_popup_login_form.tmpl',
											);
	   $template->param(
	    %Global_Template_Variables, 
	    list_popup_menu => $list_popup_menu, 
	    auth_state      => $args{-auth_state},
	    );
	                
	return $template->output();		   		      
				   		      
}




sub list_popup_subscription_form { 


	require HTML::Template::Expr;

	my %args = (-show_hidden => 0,
				-name        => 'list',
				-email       => undef, 
				-set_flavor  => 'subscribe',
				@_); 
	
	$args{-set_flavor} = 'u' 
		if $args{-set_flavor} eq 'unsubscribe'; 
	
	my $list_popup_menu = list_popup_menu(%args);
	
	my $email    = $args{-email};
	my $template = HTML::Template::Expr->new(
											%Global_Template_Options, 
											filename          => 'list_popup_subscription_form.tmpl',
										   );
										   
	   $template->param(
	   					%Global_Template_Variables,
	   					list_popup_menu => $list_popup_menu,
	                    email           => $args{-email},
	                    set_flavor      => $args{-set_flavor},
	                   ); 
		
	return $template->output();
	
}



sub global_list_sending_checkbox_widget { 
	
	my $list = shift || undef; 
	
	require DADA::MailingList::Settings;
		   $DADA::MailingList::Settings::dbi_obj = $dbi_handle;

	my @available_lists = available_lists(); 
	my @f_a_lists; 
	
	foreach(@available_lists){ 
		next if $_ eq $list; 
		push(@f_a_lists, $_); 
	}
	
	my %list_names; 
	
	foreach(@f_a_lists){ 
		my $ls = DADA::MailingList::Settings->new(-List => $_); 
		my $li = $ls->get; 
		$list_names{$_} = $_ . ' (' . $li->{list_name} . ')';
	}
	
	
	
	
	return  $q->checkbox_group(-name      => 'alternative_list',
							  '-values'  => [@f_a_lists],
							   -linebreak =>'true',
							   -labels    => \%list_names,
							   -columns   => 3, 
							 );				 
}





sub default_screen { 
	
	my %args = (
				-show_hidden        => undef,
				-name               => undef,
				-email              => undef, 
				-set_flavor         => undef,
				-error_invalid_list => 0, 
				@_
				); 
	
	require HTML::Template::Expr;
	require DADA::MailingList::Settings;
	       $DADA::MailingList::Settings::dbi_obj = $dbi_handle;

	use DADA::MailingList::Archives; 
	       $DADA::MailingList::Archives::dbi_obj = $dbi_handle;
	       


	
	my @list_information = ();
	
	my $reusable_parser = undef; 
	
	# Keeps count of how many visible lists are printed out; 
	my $l_count = 0;
	

	my $labels = {}; 
	foreach my $l( available_lists() ){
			my $ls = DADA::MailingList::Settings->new(-List => $l); 
			my $li = $ls->get; 
			next if $li->{hide_list} == 1; 
			$labels->{$l} = $li->{list_name};
			$l_count++;
		}
	my @list_in_list_name_order = sort { uc($labels->{$a}) cmp uc($labels->{$b}) } keys %$labels;
		
		
	        
	#foreach my $list(available_lists(-In_Order => 1)){
	 
	 foreach my $list(@list_in_list_name_order){	 
		my $ls = DADA::MailingList::Settings->new(-List => $list); 
		my $all_list_info = $ls->get; 
		
		my $ah = DADA::MailingList::Archives->new(-List => $all_list_info, (($reusable_parser) ? (-parser => $reusable_parser) : ())); 
		
		if($all_list_info->{hide_list} ne "1"){ # should we do this here, or in the template?          
	        
	        
	        $l_count++; 
	        
			my $tmpl_list_information = {}; 
			
				
			my $html_info = $all_list_info->{info};
			   $html_info = webify_plain_text($html_info);
		
			# Just trying this out...
		
			for($all_list_info->{list_owner_email}, 
				$all_list_info->{admin_email},
				$all_list_info->{discussion_pop_email},
			){  
				if($_){ 
					my $look_e      = quotemeta($_);
					my $protected_e = entity_protected_str($_); 					
					   $html_info   =~ s/$look_e/$protected_e/g;
				}
			}
			#/ end that...		

               	
			   $tmpl_list_information->{uri_escaped_list}     = uriescape($all_list_info->{list});
			   $tmpl_list_information->{list_name}            = $all_list_info->{list_name};
			   $tmpl_list_information->{info}                 = $all_list_info->{info};
			   $tmpl_list_information->{html_info}            = $html_info;
			
			   my $ne      = $ah->newest_entry; 
			   my $subject = $ah->get_archive_subject($ne); 
			   
			   $tmpl_list_information->{newest_archive_blurb}   = $ah->message_blurb(); 
			   $tmpl_list_information->{newest_archive_subject} = $subject; 
			   $tmpl_list_information->{show_archives}          = $all_list_info->{show_archives}; 
			
			
			push(@list_information, $tmpl_list_information);
			
			$reusable_parser = $ah->{parser} if ! $reusable_parser; 

		
		}	
	}
	
	my $visible_lists = 1; 
    if($l_count == 0){ 
        $visible_lists = 0; 
    }

	
	
	my $list_popup_menu = list_popup_menu(-email      => $args{email}, 
											  -list       => $args{list}, 
											  -set_flavor => $args{set_flavor},
											  );
																	
	my $template = HTML::Template::Expr->new(
											%Global_Template_Options, 
											filename          => 'default_screen.tmpl',
										   );
	   $template->param(
	   					%Global_Template_Variables,
	   					
	   					SHOW_ADMIN_LINK    => $DADA::Config::SHOW_ADMIN_LINK,
	   					list_popup_menu    => $list_popup_menu,
	                    email              => $args{-email},
	                    set_flavor         => $args{-set_flavor},
	                    list_information   => \@list_information,
	                    
	                    visible_lists      => $visible_lists, 
	                    
	                    error_invalid_list => $args{-error_invalid_list}, 
	                    
	                   
	                   ); 
		
	return $template->output();

}




sub send_email_screen { 

	my %args = (-list => undef, 
				-vars => {},
				@_); 

	
	die "no list!" if ! $args{-list};

	require HTML::Template::Expr;
	require DADA::MailingList::Settings; 
		   $DADA::MailingList::Settings::dbi_obj = $dbi_handle;

	
	my $ls = DADA::MailingList::Settings->new(-List => $args{-list}); 
	my $list_info = $ls->get; 
	
	my $precendence_popup_menu = $q->popup_menu(-name    => 'Precedence',
												-values  =>  \@DADA::Config::PRECEDENCES ,
                    							-default =>  $list_info->{precedence});
    my $priority_popup_menu    = $q->popup_menu(-name  =>'Priority',
												-values =>[keys %DADA::Config::PRIORITIES],
												-labels => \%DADA::Config::PRIORITIES,
												-default =>  $list_info->{priority}, 
												);
												
                
	my $template = HTML::Template::Expr->new(
										%Global_Template_Options, 
										filename          => 'send_email_screen.tmpl',
									   );
   $template->param(
   
					%Global_Template_Variables,
					%$list_info,
					%{$args{-vars}},
					precendence_popup_menu => $precendence_popup_menu, 
				   ); 
		
	return $template->output();








}




sub list_page { 


	my %args = (-list           => undef, 
			    -email          => undef, 
				-set_flavor     => undef,
				-error_no_email => undef, 
				

				@_);
	
	$args{-set_flavor} = 'u' 
		if defined($args{-set_flavor}) && $args{-set_flavor} eq 'unsubscribe'; 

				
	require HTML::Template::Expr;
	require DADA::MailingList::Settings; 
		   $DADA::MailingList::Settings::dbi_obj = $dbi_handle;
	
	my $ls = DADA::MailingList::Settings->new(-List => $args{-list}); 
	my $list_info = $ls->get; 
	
	my $tmpl_list_information = {}; 
			
	my $info      = $list_info->{info};
	
	my $html_info = $list_info->{info};
	   $html_info = webify_plain_text($html_info);

# Just trying this out...

	for($list_info->{list_owner_email}, 
	    $list_info->{admin_email},
	    $list_info->{discussion_pop_email},
	){  
		my $look_e      = quotemeta($_);
		my $protected_e = entity_protected_str($_); 
		
		$html_info =~ s/$look_e/$protected_e/g;
	}
#/ end that...		



	   
	my $html_privacy_policy = $list_info->{privacy_policy};
	   $html_privacy_policy =~ s/\n\n/<p>/gio; 
	   $html_privacy_policy =~ s/\n/<br \/>/gio; 

		
	   for (qw(
	          show_archives 
	          list_name 
	          info 
	          privacy_policy
	          )){
	          	$tmpl_list_information->{$_} = $list_info->{$_}; 
	          }
	   
	   $tmpl_list_information->{uri_escaped_list}      = uriescape($list_info->{list});
	   $tmpl_list_information->{info}                  = $info;	
	   $tmpl_list_information->{html_info}             = $html_info;	
	   
	   $tmpl_list_information->{html_privacy_policy}   = $html_privacy_policy;	
	   
	   my $html_archive_list = html_archive_list($args{-list}); 
	   
		my $template = HTML::Template::Expr->new(
											%Global_Template_Options, 
											filename          => 'list_page_screen.tmpl',
										   );
	   $template->param(
			            list                => $args{-list}, 
			            email               => $args{-email}, 
						set_flavor          => $args{-set_flavor}, 
	   					html_archive_list   => $html_archive_list, 
	   					
	   					publish_archives_rss => ($list_info->{publish_archives_rss}) ? 1: 0, 

	
	   					%$tmpl_list_information,
	   					
	   					closed_list => $list_info->{closed_list}, 
	   					error_no_email => $args{-error_no_email}, 
	   					
	   					publish_archives_rss => ($list_info->{publish_archives_rss}) ? 1: 0, 


	   					%Global_Template_Variables,

	                   ); 
		
	return $template->output();
	
}




sub admin { 

	my %args = (
	            -login_widget            => $DADA::Config::LOGIN_WIDGET,
	            -no_show_create_new_list => 0, 
				-cgi_obj                 => '', 
				@_,
			   ); 
	
	my $login_widget = $DADA::Config::LOGIN_WIDGET;
	
    # Why is this so BIG?!
    if($args{-login_widget} eq 'text_box'){ 
        $login_widget = 'text_box';
    } elsif($DADA::Config::LOGIN_WIDGET eq 'popup_menu'){ 
        $login_widget = 'popup_menu';
    } elsif($DADA::Config::LOGIN_WIDGET eq 'text_box') { 
        $login_widget = 'text_box';	
    } else { 
        warn "'\$DADA::Config::LOGIN_WIDGET' misconfigured!";
    }
	
	my @available_lists = available_lists();
	
    $DADA::Config::LIST_QUOTA = undef if strip($DADA::Config::LIST_QUOTA) eq '';
	my $list_max_reached = 0; 

    if(
      ($DADA::Config::LIST_QUOTA) && 
      (($#available_lists + 1) >= $DADA::Config::LIST_QUOTA)
     ) { 
      
	   $list_max_reached = 1;
     }	
	
	
	my $list_popup_menu = list_popup_menu(-name   	        => 'admin_list', 
										  -show_hidden      => 0,
										  -empty_list_check => 1, 
										 );
	
	if(!$list_popup_menu){ 
	    $login_widget = 'text_box'; # hey Zeus that's a lot of switching aboot. 
	}
		
	my $auth_state; 
	
	if($DADA::Config::DISABLE_OUTSIDE_LOGINS == 1){ 
        require DADA::Security::SimpleAuthStringState;
        my $sast =  DADA::Security::SimpleAuthStringState->new;  
           $auth_state = $sast->make_state; 
	}
	
	
        my $logged_in_list_name = undef; 
        my ($admin_list, $root_login, $checksout) = check_list_security(-cgi_obj         => $args{-cgi_obj},  
                                                                        -Function        => 'admin',
                                                                        -manual_override => 1
                                                                      );
        if($checksout == 1){ 
            require DADA::MailingList::Settings; 
            $DADA::MailingList::Settings::dbi_obj = $dbi_handle;
            my $l_ls             = DADA::MailingList::Settings->new(-List => $admin_list); 
            my $l_li             = $l_ls->get(); 
            $logged_in_list_name = $l_li->{list_name};
        }
    
    
	require HTML::Template::Expr;
	my $template = HTML::Template::Expr->new(%Global_Template_Options, 
											 filename          => 'admin_screen.tmpl',
											);
		   $template->param(
	   					%Global_Template_Variables,
	   					login_widget            => $login_widget, 
	   					list_popup_menu         => $list_popup_menu,
	   					list_max_reached        => $list_max_reached, 
	   					auth_state              => $auth_state, 
	   					show_other_link         => _show_other_link(),  
	   					no_show_create_new_list => $args{-no_show_create_new_list}, 
	   					
	   					logged_in_list_name     => $logged_in_list_name, 

	                   ); 
		
	return $template->output();

}



sub _show_other_link { 

    require DADA::MailingList::Settings; 
    $DADA::MailingList::Settings::dbi_obj = $dbi_handle;
    
    
    # Basically, if there's at least one list that's hidden, we show the 
    # More... link. 
        
    foreach my $list(available_lists(-Dont_Die => 1) ){
        my $ls = DADA::MailingList::Settings->new(-List => $list); 
        my $li = $ls->get; 
        return 1
            if $li->{hide_list} == 1; 
	}
			
    return 0; 
    
}




sub html_archive_list { 

	# god, what a mess...
	my $list = shift; 
	my $t    = "";
	
	require HTML::Template::Expr;
	require DADA::MailingList::Archives; 
	require DADA::MailingList::Settings;
		   $DADA::MailingList::Settings::dbi_obj = $dbi_handle;

	
	my $ls = DADA::MailingList::Settings->new(-List => $list); 
	my $li = $ls->get; 
	
	my $archive = DADA::MailingList::Archives->new(-List => $li); 
	my $entries = $archive->get_archive_entries(); 
	
	
	if(defined($entries->[0])) { 
	

        my ($begin, $stop) = $archive->create_index(0);
        my $i;
        my $stopped_at = $begin;
        my $num = $begin;
        
        $num++; 
        my @archive_nums; 
        my @archive_links; 
        my $th_entries = []; 
    
        
        
        # iterate and save
        for($i = $begin; $i <=$stop; $i++){ 
            my $link; 
            
            if(defined($entries->[$i])){
                
                my ($subject, $message, $format, $raw_msg) = $archive->get_archive_info($entries->[$i]); 
                        
                # this is so atrocious.
                my $date = date_this(-Packed_Date   => $entries->[$i],
                -Write_Month   => $li->{archive_show_month},
                -Write_Day     => $li->{archive_show_day},
                -Write_Year    => $li->{archive_show_year},
                -Write_H_And_M => $li->{archive_show_hour_and_minute},
                -Write_Second  => $li->{archive_show_second});
    
    
                my $entry = { 				
                        id               => $entries->[$i], 
    
                        date             => $date, 
                        subject          => $subject,
                       'format'          => $format, 
                        list             => $list, 
                        uri_escaped_list => uriescape($list),
                        PROGRAM_URL      => $DADA::Config::PROGRAM_URL, 
                        message_blurb    => $archive->message_blurb(-key => $entries->[$i]),
                    }; 
                
                $stopped_at++;
                push(@archive_nums, $num); 
                push(@archive_links, $link); 
                $num++;
    
    
                push(@$th_entries, $entry); 
                    
            }
        } 
    
        my $ii; 
        
        for($ii=0;$ii<=$#archive_links; $ii++){ 
    
            my $bullet = $archive_nums[$ii];
            
            #fix if we're doing reverse chronologic 
            $bullet = (($#{$entries}+1) - ($archive_nums[$ii]) +1) 
                if($li->{sort_archives_in_reverse} == 1);
    
            # yeah, whatever. 
            $th_entries->[$ii]->{bullet} = $bullet; 
            
        }
    
        $t .= screen(-screen => 'archive_list_widget.tmpl', 
                     -vars => {
                                entries              => $th_entries,
                                list                 => $list, 
                                list_name            => $li->{list_name}, 
                                publish_archives_rss => ($li->{publish_archives_rss}) ? 1: 0, 
                                index_nav            => $archive->create_index_nav($stopped_at), 
                                search_form          => ( ($li->{archive_search_form} eq "1") && (defined($entries->[0])) ) ? $archive->make_search_form($li->{list}) : ' ', 
                               }
                    );  
 
	}
	
	return $t; 

}




sub login_switch_widget { 

	my $args = shift; 
	
	die "no list!" if ! $args->{-list};
	
	require DADA::MailingList::Settings; 
		   $DADA::MailingList::Settings::dbi_obj = $dbi_handle;

	my $location = $q->self_url || $DADA::Config::S_PROGRAM_URL . '?flavor=' . $args->{-f}; 

    require  DADA::App::ScreenCache; 
    my $c  = DADA::App::ScreenCache->new; 
    
    if($c->cached('login_switch_widget')){ 
        my $lsw = $c->pass('login_switch_widget');
           $lsw =~ s/\[LOCATION\]/$location/g; 
           return $lsw; 
      }

    my $scrn; 
    
#	require DADA::MailingList::Settings; 
#		   $DADA::MailingList::Settings::dbi_obj = $dbi_handle;

	my @lists = available_lists(-dbi_handle => $dbi_handle); 
	my %label = (); 
	
	foreach my $list( @lists ){
			my $ls = DADA::MailingList::Settings->new(-List => $list); 
			my $li = $ls->get; 
			$label{$list} = $li->{list_name} . ' (' . $list . ')'; 
			
	}
	
	$label{$args->{-list}} = '----------'; 
	
	if($lists[1]){ 
		$scrn = $q->start_form(-action => $DADA::Config::S_PROGRAM_URL, 
							  -method => "post",
							  ) . 
			   $q->popup_menu(-style   => 'width:75px', 
							  -name    => 'change_to_list', 
							  -value   => [@lists], 
							  -default => $args->{-list},
							  -labels  => {%label}, 
							  ) . 
			   $q->hidden(-name => 'location', 
						  -value => '[LOCATION]',
						 ) . 
			   $q->hidden(-name      => 'flavor', 
						   -value    => 'change_login',
						   -override => 1,
				 ) . 
	
			   $q->submit(-value => 'switch', -class=>'plain') .
			   $q->end_form(); 
	}else{ 
		$scrn = '';
	}
	
	$c->cache('login_switch_widget', \$scrn);
	
	$scrn =~ s/\[LOCATION\]/$location/g; 
	
    return $scrn; 
}




sub screen { 

	my %args = (-screen => undef, 
				-list   => undef, #nothin' doin'?!
		   		-vars   => {},
		    	-expr   => 0, 
		    	-data   => undef, 
		    	@_
		       ); 
		       
	die "no screen! or data" if ! $args{-screen} && ! $args{-data};

	my $template; 
	
	if($args{-expr}){ 
	
		if($args{-screen}){ 
	
			require HTML::Template::Expr;
			 $template = HTML::Template::Expr->new(%Global_Template_Options, 
													 filename => $args{-screen},
													);
		}elsif($args{-data}){ 
		
			require HTML::Template::Expr;
			$template = HTML::Template::Expr->new(%Global_Template_Options, 
												  scalarref => $args{-data},
													);
		}else{ 
			warn "what are you trying to do?!"; 
		}
		
   }else{ 
   
   	if($args{-screen}){ 

   		require HTML::Template;
		$template = HTML::Template->new(%Global_Template_Options, 
										filename => $args{-screen},
							   );

	}elsif($args{-data}){ 

   		require HTML::Template;
		$template = HTML::Template->new(%Global_Template_Options, 
										scalarref => $args{-data},
							   );
							   					   
   		}else{ 
			warn "what are you trying to do?!"; 
		}
   }
   
   
   $template->param(   
					%Global_Template_Variables,
					%{$args{-vars}},
				   ); 
				   
	if($args{-list}){ 
		$template->param('list', $args{-list}); 
	}
				   
	return $template->output();
}


sub file_path { 

    my $fn   = shift; 
    if(!$fn){ 
        die "You did not pass a filename as the sole argument!!!"; 
    }
    my $path = undef; 
    
    foreach my $path(@{$Global_Template_Options{path}}){ 
        if(-e $path . '/' . $fn){ 
            return $path . '/' . $fn;
        }
    }
}

1;




=pod

=head1 COPYRIGHT

Copyright (c) 1999-2007 Justin Simoni 
http://justinsimoni.com 
All rights reserved. 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

=cut
