<?php
    global $selected;

    $campaign_id = $selected;
 
 
    $about_url  = variable_get('dw_campaigns_derby_about_np_link', '#');
    $give_url   = variable_get('dw_campaigns_derby_general_donation_link', '');
    $derby_url  = '';
    //$leaderboard_url  = '/dw/campaign/current/leaderboard';

    $leaderboard_url  = '/dw/campaign/current/leaderboard';
    if($selected != -1 && $selected != '' && !(arg(2) == 'give' && arg(3) == 'general-donation')) {
   
        $leaderboard_url  = dw_campaigns_get_campaign_path($selected, '/dw/campaign', 'leaderboard');
    }

    // TODO - lookup homepage campaign - this may need to be changed to be like 'leaderboard' with current
    $toplocations_url  = '/dw/walking/toplocations';

    
    $active     = 'active-path';
    
    $home_class         = '';
    $leaderboard_class  = '';
    $account_class      = '';

    $body_class         = dw_campaigns_make_body_class();
    if($body_class == 'dw-walking') {
        $home_class         = $active;
    } elseif (arg(3) == 'leaderboard') {
         $drupal_id      = _dw_campaigns_contact_id_get_user($pcp->contact_id);
         if($drupal_id == $user->uid) {
             $hasPCP = TRUE;
         }
     }
 } else {
     $campaign       = dw_campaigns_get_selected_campaign();
     $campaign_id    = $campaign->nid;
     
     $hasPCP = false;   
     $my_location    = '-1';
 
     if($campaign == '') {
         $search_url     = '/dw/campaign/current/search';
     } else {
         $search_url     = dw_campaigns_get_campaign_path($campaign_id, '/dw/campaign', 'search');    
         }
 
     $pcps = _dw_campaigns_get_pcp_by_drupal_id($user);
 
     // todo - fix this so it works 'next year'
     foreach($pcps as $key => $pcp) {
        $hasPCP = TRUE;
        $dummy = new stdClass;
        $dummy->id = $pcp->contribution_page_id;
        $myCampaign = dw_campaigns_get_campaign_from_contribution_page($dummy);
        if(!is_null($myCampaign)) {
            $my_location = $myCampaign->nid;
        }
        break;
     }
    }

    $sql_count = "select count(*) from donations_as_$num_per_page";

    //$result = db_query("select * from donations_as " . tablesort_sql($headers));
    $result = pager_query("select * from donations_as_$num_per_page " . tablesort_sql($headers), $num_per_page, 0, $sql_count);

    $rows = array();
    while ($db_row = db_fetch_object($result)) {
        $rows[] = array(
            'data' => array(
                array('data' => '<a><img src="' . $db_row->photo . '" width="25">' . $db_row->name . '</a>', 'class' => 'name'),
                array('data' => dw_campaigns_force_decimal($db_row->amount, $campaign->field_dw_currency['0']['value']), 'class' => 'amount'),
                array('data' => $db_row->location, 'class' => 'location'),
                array('data' => dw_campaigns_format_date($db_row->donationdate), 'class' => 'date'),
                array('data' => $db_row->note, 'class' => 'message'),
            )    
        );
    }

    echo '<h2>' . $thisUser->displayname . ' Donors</h2>';
    echo theme('table', $headers, $rows);
    // if we are showing the big page, enable paging
    if($num_per_page!=3) {
        echo theme('pager', NULL, $num_per_page, 0);
        echo '<a href="' . dw_campaigns_get_campaign_path($campaign->nid, '/dw/users/' . $thisUser->name) . '" class="see-all">' . t('return to fundraising page') . '</a>';
    } else {    
        echo '<a href="' . dw_campaigns_get_campaign_path($campaign->nid, '/dw/users/' . $thisUser->name,  '/supporters') . '" class="see-all">' . t('see all') . '</a>';
    }
    
        $leaders        = _dw_campaigns_campaign_leaders($campaign, $show_cnt);
        $campaign_id    = $campaign->nid;
    } else {
        $leaders        = _dw_campaigns_all_leaders($show_cnt);
    }
    

?>

<h2><?php echo t('Top Fundraisers'); ?></h2>
<?php
    if($show_cnt<10) {
        $leaderboard_path       = '/dw/campaign/current/leaderboard';
        
        if(!is_null($campaign_id)) {
            $leaderboard_path   = dw_campaigns_get_campaign_path($campaign_id, '/dw/campaign', 'leaderboard');
        }
?>
        <a href="<?php echo $leaderboard_path;?>" class="see-all"><?php echo t('see all'); ?></a>
<?php
    }

    if(!empty($location_string) && $selected != -1) {
        echo '<span class="for-locname">' . t('For !city', array('!city' =>  $location_string)) . '</span>';
    }
?>
<div class="fundraising-leaders">
    <ul>
<?php
    if($leaders) {
        $campaigns = array();
                        <th><?php echo t('Details'); ?></th><td><?php echo $details; ?></td>
                    </tr>
                    <?php
                    if(!is_null($event_url) && $mode_type != 'walking') {
                    ?>
                    <tr class="link">
                        <th><?php echo t('Register'); ?></th><td>
                        <a href="<?php echo $event_url;?>" target="_blank"><?php echo t('Register for Event'); ?></a>
                        </td>
                    </tr>
                    <?php
                    }
                    ?>
                </table>
            </div>

            <div class="get-involved donate-box">
                <h2><?php echo t('Get Involved'); ?></h2>
                <p><?php echo t('Please consider helping us in one of the following ways:'); ?></p>
                <ul>
                    <li><?php echo t('1. Create a Fundraising page of your own if you don\'t already have one. !urlstart Get Started Now! !urlend', array('!urlstart' => '<a href="' . $get_started_url . '">', '!urlend' => '</a>')); ?> </li>
                    <li><?php echo t('2. Volunteer at our event.  !urlstart Get More Info !urlend', array('!urlstart' => '<a href="' . $get_more_info_url . '">', '!urlend' => '</a>')); ?></li>
                    <li><?php echo t('3. Tell your friends and family about our organization and event. Use the Spread the Word links.'); ?></li>
                </ul>
            </div>
            
            <div class="donated-nav">
                <a class="btn" href="<?php echo dw_campaigns_get_campaign_path($campaign->nid, '/dw/users/' . $thisUser->name); ?>"><?php echo t('Take me back to the Fundraiser\'s page'); ?></a>
                <a class="btn" href="/dw/"><?php echo t('Take me to the Home Page'); ?></a>
            </div>