<?php
define("PAGE_USES_GMAPS",true);
define("PAGE_USES_FACEBOX",true);
include_once("includes/header.php");

require_once("includes/classes/Commenter.inc.php");
$Commenter['rep'] = new Commenter(13,$user_data);
$Commenter['party'] = new Commenter(3,$user_data);
$Commenter['years_in_office'] = new Commenter(4,$user_data);
$Commenter['committees'] = new Commenter(5,$user_data);
$Commenter['address'] = new Commenter(6,$user_data);
$Commenter['url'] = new Commenter(7,$user_data);
$Commenter['youtube_url'] = new Commenter(12,$user_data);
$Commenter['twitter_url'] = new Commenter(11,$user_data);
$Commenter['donors_orgs'] = new Commenter(8,$user_data);
$Commenter['donors_industries'] = new Commenter(9,$user_data);
$Commenter['donors_sectors'] = new Commenter(10,$user_data);
$Commenter['words'] = new Commenter(17,$user_data);
$Commenter['email'] = new Commenter(44,$user_data);


$Commenter['historical'] = new Commenter(42,$user_data);


require_once("includes/classes/CongressPerson.inc.php");
$CongressPerson = new CongressPerson($user_data['district_ID']);
$representative = $CongressPerson->getRepresentative();
?>



<div id="representative">
	<h2>
		<?=$representative['first_name'].' '.$representative['middle_name'].' '.$representative['last_name'];?> 
		<?php
			if(strlen($representative['party_abbreviated'])>0)
				echo "(".$representative['party_abbreviated'].")";
		?>
		<?=$Commenter['rep']->getLink();?></h2>
	</h2>
	<?=$Commenter['rep']->getSupportingHTML();?>
	<div id="column-left">
	<?php
		//Check to make sure the file exists on this server, otherwise, fetch copy from govtrack.use
		if((int)curl_get_header('http://'.$_SERVER['SERVER_NAME'].'/assets/photos/'.$representative['gov_track_ID']."-200px.jpeg")==200){
		?>
			<img src="assets/photos/<?=$representative['gov_track_ID'];?>-200px.jpeg" id="photo" width="200" height="244">
		<?php
		}
		else{
		?>
			<img src="http://www.govtrack.us/data/photos/<?=$representative['gov_track_ID'];?>-200px.jpeg" id="photo" width="200" height="244">		
		<?php
		}
		?>


		<h3>Political Party<?=$Commenter['party']->getLink();?></h3><?=$Commenter['party']->getSupportingHTML();?>
		<P>
			<?=$representative['party'];?>
		</P>
		


		<h3>Year of Original Election</h3>
		<p>
			<?=date("Y",$representative['startTS']);?>
		</p>

		<h3>Year of Next Election</h3>
		<p>
			<?=date("Y",$representative['endTS']);?>
		</p>

		<h3>Years in Office<?=$Commenter['years_in_office']->getLink();?></h3><?=$Commenter['years_in_office']->getSupportingHTML();?>
		<p>
			<?=$representative['yearsInOffice'];?>
		</p>
		
		
		<h3>Committees<?=$Commenter['committees']->getLink();?></h3><?=$Commenter['committees']->getSupportingHTML();?>
		<ul id="committees">
			<?php
			
			foreach($representative['committees'] as $committee){
				?>
					<li><?=$committee;?></li>
				<?php
			}
			?>
		</ul>

		<h3>Congressional Phone #</h3>
		<p>
			<input type="text" value="<?=phone_integer_to_string($representative['phone']);?>">
		</p>

		<h3>Address<?=$Commenter['address']->getLink();?></h3><?=$Commenter['address']->getSupportingHTML();?>
		<p>
			<?=$representative['congress_office_address'];?><br/>
			Washington, D.C.
		</p>

		
		<?php
		if(strlen($representative['email'])>4){ //Don't bother displaying this for everyone with a message about none available, it's rare that it is
		?>
			<h3>Official Email Address<?=$Commenter['email']->getLink();?></h3><?=$Commenter['email']->getSupportingHTML();?>
			<p>
				<input type="text" value="<?=$representative['email'];?>">
			</p>	
		<?php
		}
		?>	

		<h3>Official RSS Feed</h3>
		<p>
			<input type="text" value="<?php
				if(strlen($representative['official_rss_url'])>1)
					print $representative['official_rss_url'];
				else
					print "No RSS feed available.";
			?>">
		
		</p>
		
		
		<h3>Official URL<?=$Commenter['url']->getLink();?></h3><?=$Commenter['url']->getSupportingHTML();?>
		<p>
			<input type="text" value="<?=$representative['official_url'];?>">
		</p>
		
		
		
		<h3>Contact Form URL</h3>
		<p>
			<input type="text" value="<?=$representative['contact_form_url'];?>">
		</p>
		
		
		<h3>Twitter URL<?=$Commenter['twitter_url']->getLink();?></h3><?=$Commenter['twitter_url']->getSupportingHTML();?>
		<p>
			<input type="text" value="<?php
			
			if(strlen($representative['twitter_ID'])>1)
				print "http://www.twitter.com/".$representative['twitter_ID'];
			else
				print "Not on Twitter.";
			?>">
		</p>
		
		
		<h3>Youtube URL<?=$Commenter['youtube_url']->getLink();?></h3><?=$Commenter['youtube_url']->getSupportingHTML();?>
		<p>
			<input type="text" value="<?php
			if(strlen($representative['youtube_url'])>1)
				print $representative['youtube_url'];
			else
				print "Not on Youtube.";
			?>">
		</p>
	

		<?php
		include_once("includes/classes/CapitolWords.inc.php");
		$CapitolWords = new CapitolWords($representative['gov_track_ID'],20);
		?>
		
		<h3><?=date("Y");?> Most Spoken Words
			<?=$Commenter['words']->getLink();?>
		</h3>
		<?=$Commenter['words']->getSupportingHTML();?>

		<table cellspacing="0" id="words">
			<thead>
				<tr>
					<th style="text-align:left">Word</th>
					<th style="text-align:right">Times Spoken in Congress</th>
				</tr>
			</thead>
			<tbody>
			<?php
			foreach($CapitolWords->words as $word=>$word_count){
			?>
			<tr>
				<td><?=ucfirst($word);?></td>
				<td style='text-align:right'><?=$word_count;?></td>
			</tr>
			<?php
			
			}
			?>
			</tbody>
		</table>


		<p id="words">
		
			This information was sourced from <a href="about_data.php">CapitolWords</a> and is current as of today.
		
		</p>


			
	</div>
	<div id="column-right">

		<div id="map">
			<?php
				get_district_google_map($user_data['state'],$user_data['district'],"100%","355px",8);
			?>
		</div>
		
		<a name="organizations"></a>
		<h3>Top 10 Donors by Organization 
			<?=$Commenter['donors_orgs']->getLink();?>
			<?php if(!isset($_GET['organization'])){?> - <a href="?limit=50&organizations=true#organizations">Click Here for More Organizations</a> <?php } ?></h3>
		
		<?=$Commenter['donors_orgs']->getSupportingHTML();?>
		
		<table cellspacing="0" id="organizations">
			<thead>
				<tr>
					<th style="text-align:left">Donor Organization</th>
					<th style="text-align:right">Amount</th>
				</tr>
			</thead>
			<tbody>
		<?php
		$limit = 10;
		if(isset($_GET['organizations']) && (int)$_GET['limit']>0)
			$limit = (int)$_GET['limit'];
		$result = mysql_query("SELECT contributor,amount FROM Contributors WHERE is_organization_flag=1 AND rep_ID = ".$representative['ID']." ORDER BY amount DESC LIMIT $limit");
		while($row=mysql_fetch_assoc($result)){
		
			?>
			<tr>
				<td><?=$row['contributor'];?></td>
				<td style='text-align:right'>$<?=number_format($row['amount'],0);?></td>
			</tr>
			<?php
		
		}
		?>
			</tbody>
		</table>
		
		<a name="industries"></a>
		<h3>Top 10 Donors by Industry 
			<?=$Commenter['donors_industries']->getLink();?>
			<?php if(!isset($_GET['industry'])){?> - <a href="?limit=50&industries=true#industries">Click Here for More Industries</a> <?php } ?></h3>
		
		<?=$Commenter['donors_industries']->getSupportingHTML();?>
		
		<table cellspacing="0" id="industries">
			<thead>
				<tr>
					<th style="text-align:left">Donor</th>
					<th style="text-align:right">Amount</th>
				</tr>
			</thead>
			<tbody>
		<?php
		$limit = 10;
		if(isset($_GET['industries']) && (int)$_GET['limit']>0)
			$limit = (int)$_GET['limit'];
		$result = mysql_query("SELECT contributor,amount FROM Contributors WHERE is_industry_flag=1 AND rep_ID = ".$representative['ID']." ORDER BY amount DESC LIMIT $limit");
		while($row=mysql_fetch_assoc($result)){
		
			?>
			<tr>
				<td><?=$row['contributor'];?></td>
				<td style='text-align:right'>$<?=number_format($row['amount'],0);?></td>
			</tr>
			<?php
		
		}
		?>
			</tbody>
		</table>
		
		<a name="sectors"></a>
		<h3>Top 10 Donors by Sector 
			<?=$Commenter['donors_sectors']->getLink();?>
			<?php if(!isset($_GET['sector'])){?> - <a href="?limit=50&sectors=true#sectors">Click Here for More Sectors</a> <?php } ?></h3>
		
		<?=$Commenter['donors_sectors']->getSupportingHTML();?>
		
		<table cellspacing="0" id="sectors">
			<thead>
				<tr>
					<th style="text-align:left">Donor</th>
					<th style="text-align:right">Amount</th>
				</tr>
			</thead>
			<tbody>
		<?php
		$limit = 10;
		if(isset($_GET['sectors']) && (int)$_GET['limit']>0)
			$limit = (int)$_GET['limit'];
		$result = mysql_query("SELECT contributor,amount FROM Contributors WHERE is_sector_flag=1 AND rep_ID = ".$representative['ID']." ORDER BY amount DESC LIMIT $limit");
		while($row=mysql_fetch_assoc($result)){
		
			?>
			<tr>
				<td><?=$row['contributor'];?></td>
				<td style='text-align:right'>$<?=number_format($row['amount'],0);?></td>
			</tr>
			<?php
		
		}
		?>
			</tbody>
		</table>

		<p>
			
			Please note that the above information is as reported to the FEC and the organizations, industries and sectors are of the people who donated money and so, may not necessarily represent those companies.
		
		</p>


		<script type="text/javascript">
			/* Table Striping */
			$('table#organizations tr:odd,table#industries tr:odd,table#sectors tr:odd, table#words tr:odd').css({'background-color' : '#f2f8ff'});
		</script>
		
		<h3>Historical Representation<?=$Commenter['historical']->getLink();?></h3><?=$Commenter['historical']->getSupportingHTML();?>
		<table id="history" cellspacing="0">
			<thead>
				<tr>
					<th>Representative</th>
					<th colspan="2">Representation</th>
				</tr>
			</thead>
			<tbody>
		<?php
		$result = mysql_query("SELECT party,YEAR(start_date) as 'start',YEAR(end_date) as 'end',CONCAT(first_name,' ',last_name) as 'name_concat' FROM Representatives WHERE district_ID = ".$representative['district_ID']." ORDER BY end_date DESC");
		
		$switch = 1;
		while($person = mysql_fetch_assoc($result)){
			if(isset($last_party) && trim(strtoupper($person['party'])) != strtoupper($last_party))
				$switch *=-1;
			
			if($switch<0)
				echo "<tr><td>".$person['name_concat']."</td><td style='text-align:right'><b>".$person['party']."</b></td><td>".$person['start']." - ".$person['end']."</td></tr>";
			else
				echo "<tr class='switch'><td>".$person['name_concat']."</td><td style='text-align:right'>".$person['start']." - ".$person['end']."</td><td><b>".$person['party']."</b></td></tr>";
			
			$last_party = trim($person['party']);
		}
		?>
			</tbody>
		</table>
	</div>


	

</div>














<?php
include_once("includes/footer.php");
?>