<%perl>;
my $order_vals = [ map { [ $_ => $_ ] } 1..@contribs ];
my ($i, @sort_array);

my $order_sub = sub {
    return unless $_[1] eq 'order';
    my $id = $_[0]->get_id;
    push @sort_array, qq{"$widget|reorder_$id"};
    $m->scomp('/widgets/profile/select.mc',
              name => "$widget|reorder_$id",
              disp => '',
              value => ++$i,
              options => $order_vals,
              useTable => 0,
              js => qq{onChange="reorder(this, 'theForm')"}
        )
};
my $uri = $r->uri;
my $role_sub = sub {
    my ($val, $c) = @_;
    my $role = $media->get_contributor_role($c);
    $val .= qq{ (Role: <a href="$uri?media_prof|assoc_contrib_cb=} .
      $c->get_id . qq{">$role</a>)} if $role;
    $val;
};

$m->comp('/widgets/listManager/listManager.mc',
         object    => 'contrib',
         userSort => 0,
         def_sort_field => 'order',
         title     => 'Current Contributors',
         objs      => \@contribs,
         profile   => undef,
         alter     => { type => $role_sub },
         fields    => [qw(lname fname type order)],
         field_titles => { order => 'Order' },
         field_values => $order_sub,
         select =>  ['Delete', "$widget|delete_id"],
         addition  => '',
        );
</%perl>
<script language="javascript">
var selectOrderNames = new Array(<% join ', ', @sort_array %>);
</script>
<p></p>

<& /widgets/search/search.mc,
   object       => 'contrib',
   field        => 'lname',
   type         => 'dual',
   use_form_tag => 0
&>
<p></p>
<& /widgets/listManager/listManager.mc,
   object    => 'contrib',
   title     => 'Choose Contributors',
   select    => undef,
   profile   => $contrib_profile,
   exclude  	=> \@existing_contribs,
   fields    => [qw(lname fname type)],
   constrain => { no_grp_id => Bric::Biz::Person->INSTANCE_GROUP_ID },
   addition  => '',
   behavior => 'expand',
&>

<p></p>
<input type="image" src="/media/images/<% $lang_key %>/save_red.gif" border="0" name="<% $widget %>|save_contrib_cb" value="Save" />
<input type="image" src="/media/images/<% $lang_key %>/save_and_stay_lgreen.gif" border="0" name="<% $widget %>|save_and_stay_contrib_cb" value="Save and Stay" />
<input type="image" src="/media/images/<% $lang_key %>/return_dgreen.gif" border="0" name="<% $widget %>|leave_contrib_cb" />

<%args>
$widget
</%args>

<%init>
my $media = get_state_data($widget, 'media');
my @existing_contribs = map { $_->get_id } $media->get_contributors;
my @contribs = $media->get_contributors();
</%init>

<%once>
my $contrib_profile = sub {
    my ($o,$flags) = @_;
    unless ($flags->{'featured'}) {
        return ['Associate', $r->uri, "media_prof|assoc_contrib_cb=".$o->get_id];
    } else {
        return ['Un-Associate', $r->uri, "media_prof|unassoc_contrib_cb=".$o->get_id];
    }
};
</%once>
