<?php

/* if($_GET['category'] === 'latest-polls')
	header('Location: /'); */

require_once('functions.php');
include('header.php'); 

$today = date("Y-m-d");
$lastmonth = date("Y-m-d", mktime(0, 0, 0, date("m")-1, date("15"),   date("Y")));
$amonth = date("Y-m-d", mktime(0, 0, 0, date("m")-3, date("15"),   date("Y")));

$recordsPerPage = 20;     
$pageNum = 1;   
if(isset($_GET['p'])) {   
	$pageNum = $_GET['p'];   
 	settype($pageNum, 'integer');   
}   
$offset = ($pageNum - 1) * $recordsPerPage;

$sql = "SELECT category_id, category_title FROM category WHERE cat_permalink = '" . $_REQUEST['category'] . "'";
$result = &DBQuery($sql); 
$row = $result->fetch_assoc();
$category = $_REQUEST['category'];
$category_id = $row['category_id'];
$archive = @$_REQUEST['archive'];
?>
<div id="main-area">
	<?php
	$top = '<div class="entry"><div class="entry-top"><div class="entry-content">';
	$bottom = ' </div> <!-- end .entry-content --></div> <!-- end .entry-top --></div> <!-- end .entry -->';

	$and = '';
	$other_and = '';
	$this_archive = '';

	if(isset($archive)) { 
		$this_archive = "&archive=$archive";
		$stamp = strtotime($archive);
		$this_month = date("Y-m", $stamp);
		$and = " AND ( poll.poll_post_date like '$this_month%' ) ";
		$other_and = " AND ( other_poll.other_poll_post_date like '$this_month%') ";
	} else {
			switch ($row['category_id']) {
			case 1:
				// Latest Poll
				//$and = " AND ( poll.poll_post_date BETWEEN  '$amonth' AND '$today' ) ";
				//$other_and = " AND ( other_poll.other_poll_post_date BETWEEN  '$amonth' AND '$today' ) ";
				break;
			case 3:
				// 2012 GOP Primary
				$and = '';
				$other_and = '';
				echo $top;
				create_chart_xml('2012', 'US', '2012 GOP Primary', 'Romney', 0, 'Santorum', 0, 'Gingrich', 'Paul');
				echo $bottom;
				break;
			case 42:
				// 2016 GOP Primary
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/gop-pre.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 43:
				// 2016 Democratic Primary
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/democrat-pre.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 8:
				// Health Care
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/obama-health-care.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" >  </iframe>	
				<?php
				echo $bottom;
				break;
			case 9:
				// Economy
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/poll-economy.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 10:
				// Foreign Policy
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/foreign-policy-poll.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 11:
				// Direction of Country
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/direction-of-country-poll.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 39:
				// Hillary Clinton Favorable
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/hillary-favorable-poll.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
				
			case 12:
				// Obama Favorable
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/obama-favorable-poll.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 13:
				//Congress Job Approval
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/congress-job-approval-poll.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" >  </iframe>	
				<?php
				echo $bottom;
				break;
			case 14:
				//Romney Favorable
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/romney-favorable-poll.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 17:
				// Obama vs. Romney
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="/Charts/chart-obama-vs-romney-2012.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 29:
				// Generic Congressional Race
				$and = '';
				$other_and = '';
				echo $top; 
				?>
				<iframe name=stbbox src="/generic-congressional-race-poll.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 15: 
				//Florida 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-FL.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 16: 
				//Ohio 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-OH.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 18: 
				//Nevada 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-NV.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 19: 
				//Colorado 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-CO.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 20: 
				//New Mexico 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-NM.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 21: 
				//Virginia 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-VA.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 22: 
				//North Carolina 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-NC.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 23: 
				//Pennsylvania 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-PA.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 24: 
				//Michigan 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-MI.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 25: 
				//Wisconsin 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-WI.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 26: 
				//New Hampshire 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-NH.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 27: 
				//Iowa 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-IA.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 28: 
				//Missouri 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-MO.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 30: 
				//Arizona 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/Charts/chart-obama-vs-romney-2012-AZ.php" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
			case 31: 
				//Same Sex marriage 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/same-sex-marriage.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;			
			case 50: 
				//Trump vs. Clinton 
				$and = '';
				$other_and = '';
				echo $top;
				?>
				<iframe name=stbbox src="https://www.pollheadlines.com/trump-clinton.html" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" width="600" height="395" > </iframe>	
				<?php
				echo $bottom;
				break;
		}
	}

	$charts = &DBQuery("select * from chart_locations where (page = 'category' OR page = 'Other Category') and chart_type = '$row[category_title]'");

	if($charts->num_rows > 0) {
		//include 'load_chart.php';
		$charts = $charts->fetch_assoc();

		$query = "select poll_candidate_1_id,poll_candidate_2_id from poll where poll_election_type = '$row[category_title]' order by poll_id desc limit 1";
		if($charts['page'] == 'Other Category')
			$query = "select other_poll_positive_opinion,other_poll_negative_opinion from other_poll where other_poll_type = '$charts[other_chart_type]' order by other_poll_id desc limit 1";

		$poll = &DBQuery($query);
		$poll = mysqli_fetch_assoc($poll);

		$cand1 = array();
		$cand2 = array();
		if($charts['page'] == 'Other Category') {
			$cand1['name'] = $poll['other_poll_positive_opinion'];
			$cand2['name'] = $poll['other_poll_negative_opinion'];
		} else {
			$cand1 = &DBQuery("select name from candidate where candidate_id = '$poll[poll_candidate_1_id]'");
			$cand1 = mysqli_fetch_assoc($cand1);
			$cand2 = &DBQuery("select name from candidate where candidate_id = '$poll[poll_candidate_2_id]'");
			$cand2 = mysqli_fetch_assoc($cand2);
		}

		echo '<script type="text/javascript" src="dgchart/fusioncharts.js"></script>
		<script type="text/javascript" src="dgchart/fusioncharts.charts.js"></script>
		
		<script type="text/javascript">
		  FusionCharts.ready(function(){
			var trumpClintonChart = new FusionCharts({
				"type": "zoomline",
				"renderAt": "loadChart",
				"width": "100%",
				"height": "370",
				"dataFormat": "xmlurl",
				"dataSource": "load_chart.php?poll_type='.$row['category_title'].'&cand1='.ucwords($cand1['name']).'&cand2='.ucwords($cand2['name']).'&year='.$charts['date_year'].'&page='.$charts['page'].'&table_name='.$charts['table_name'].'"
			 });
			trumpClintonChart.render();
		 })
		</script>';
		echo '<div class="entry-content">
				<div id="loadChart" style="max-width: 100%"></div>
				<br /><br />
				<div class="chart_footer" style="margin-bottom: 30px; margin-top: -33px;">
					<div class="box1" ><a href="#" title="" class="flip">Embed</a></div>
					<div class="box2">This graph reflects average of polls, not the results of a single poll. <strong><a href="https://www.pollheadlines.com/about_Poll_Headlines.php#chart" target="_parent">more info!</a></strong></div>
				</div>
				<!-- Embed Begin -->
				<div class="panel new_embed">
					<div class="small1">
						To use our chart on your blog or website, copy and paste the embed code below into your website
					</div>
					<a href="#" class="close">X</a>
					<form>
						<textarea name="targetme" rows="3" cols="65" onClick="javascript:this.form.targetme.focus();this.form.targetme.select();">&lt;iframe name=stbbox src=&quot;https://www.pollheadlines.com/embed_charts.php?poll_type='.$row['category_title'].'&cand1='.ucwords($cand1['name']).'&cand2='.ucwords($cand2['name']).'&year='.$charts['date_year'].'&page='.$charts['page'].'&tn='.$charts['table_name'].'&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot; width=&quot;600&quot; height=&quot;418&quot; &gt; &lt;/iframe&gt;</textarea>
					</form>
				</div>
			</div><br /><br />';
	}

	$and_category = '';
	if(!@$_GET['archive'])
	$and_category = 'AND category_poll.cp_category_id =' . $category_id;

	$sql_0 = "
	SELECT which, cur_timestamp, poll_id, poll_state, poll_permalink, poll_post_date, cp_category_id, priority, YEAR(poll_post_date) AS year,  MONTH(poll_post_date) AS month  FROM (
	SELECT 'poll' AS which, cur_timestamp, poll.poll_id AS poll_id, poll.poll_geography AS poll_state, poll.poll_permalink AS poll_permalink, poll.poll_post_date AS poll_post_date, category_poll.cp_category_id AS cp_category_id, poll.priority AS priority FROM poll, category_poll WHERE category_poll.cp_poll_id = poll.poll_id AND poll.exclude = 0 AND category_poll.flag = 'poll' $and_category $and
	UNION
	SELECT 'other_poll' AS which, cur_timestamp, other_poll.other_poll_id AS poll_id, other_poll.other_poll_geography AS poll_state, other_poll.other_poll_permalink AS poll_permalink, other_poll.other_poll_post_date AS poll_post_date, category_poll.cp_category_id AS cp_category_id, other_poll.priority AS priority FROM other_poll, category_poll WHERE category_poll.cp_poll_id = other_poll.other_poll_id AND other_poll.exclude = 0 AND category_poll.flag = 'other_poll' $and_category $other_and
	) AS fa
	ORDER BY fa.poll_post_date DESC, fa.priority ASC, fa.cur_timestamp DESC, fa.poll_id DESC
	LIMIT $offset, $recordsPerPage";

	$result_0 = &DBQuery($sql_0);
	$ads_count = 0;
	while ($row_0 = $result_0->fetch_assoc()){
		$folder = "election-polls-" . $row_0['month'] . "-" . $row_0['year'];
		$filename = $row_0['poll_permalink'];
		$ads_count++;
	?>	
		
	<div class="entry">
		<div class="entry-top"> 
			<div class="entry-content">
                <div class="entry-container">
                    <div class="entry-title">
						<?php

						if ($row_0['which'] == 'poll') {
							$permalink = $row_0['poll_permalink'];
							$title = the_title($row_0['poll_id'],1,0);
							$sql_which = "SELECT poll_candidate_2_result, poll_candidate_1_result FROM poll WHERE poll_id = " . $row_0['poll_id'];
							$result_which = &DBQuery($sql_which);
							$row_which = $result_which->fetch_assoc();
							if ( $row_which['poll_candidate_1_result'] >= $row_which['poll_candidate_2_result']) {
								$second_title_photo = the_title_photo_2($row_0['poll_id']);	
								$first_title_photo = the_title_photo_1($row_0['poll_id']);
							} else {
								$second_title_photo = the_title_photo_1($row_0['poll_id']);	
								$first_title_photo = the_title_photo_2($row_0['poll_id']);
							}
							$spread = the_spread($row_0['poll_id']);
							$tags = the_tags($row_0['poll_id']);
							$img = $row_0['poll_state'];
							$state = '<img src="/images/'. $img . '.gif" />';
						} elseif ($row_0['which'] == 'other_poll') {
							$permalink = $row_0['poll_permalink'];
							$title = the_title_other($row_0['poll_id'],1,0);
							$first_title_photo = the_title_photo_other($row_0['poll_id'],1);
							$second_title_photo = the_title_photo_other($row_0['poll_id'],0);
							$spread = the_spread_other($row_0['poll_id']);
							$tags = the_tags_other($row_0['poll_id']);
							$img = $row_0['poll_state'];
							$state = '<img src="/images/'. $img . '.gif" />';
						}
						?>
    					<h1 class="title"><a href="/<?php echo $folder . "/" . $filename; ?>.html"><?php echo $title; ?></a></h2>
                    </div>  <!-- end .entry-title -->

                    <div id="second-photo" class="entry-photo">
					<?php  echo $second_title_photo; ?>
            		</div> <!-- end .entry-photo -->
 
            		<div id="first-photo" class="entry-photo">
					<?php  echo $first_title_photo; ?>
            		</div> <!-- end .entry-photo -->

                    <div class="entry-states">
					<?php  echo $state; ?> 
            		</div> <!-- end .entry-states -->
                </div><!-- end .entry-container -->
                <?php	
				echo $tags;
				echo $spread;
                ?>
                <div class="clear"></div>
            </div> <!-- end .entry-content -->
		</div> <!-- end .entry-top -->
	</div> <!-- end .entry -->
<?php

	if($ads_count%6 == 0) {
		echo '<div class="mobile_resp_ad">';
		echo file_get_contents('mobile_ads.txt');
		echo '</div>';
	}

} // end .while 

$sql_1 = "
SELECT COUNT(poll_id) AS num FROM (
SELECT poll.poll_id AS poll_id FROM poll, category_poll WHERE category_poll.cp_poll_id = poll.poll_id AND poll.exclude = 0 AND category_poll.flag = 'poll' $and_category $and
UNION 
SELECT other_poll.other_poll_id AS poll_id FROM other_poll, category_poll WHERE category_poll.cp_poll_id = other_poll.other_poll_id AND other_poll.exclude = 0 AND category_poll.flag = 'other_poll' $and_category $other_and
) AS fa";

$result_1 = &DBQuery($sql_1);
$row_1 = $result_1->fetch_assoc();
$numrows = $row_1['num'];  
$maxPage = ceil($numrows/$recordsPerPage);   


$nav = '';
$threeDots = true;
$pageNumbers = array();
for($page = 1; $page <= $maxPage; $page++) {

	if(!in_array($page, $pageNumbers)) {
		if(($pageNum > 4 && $page < 3) ||
			$pageNum == 4 && $page == 1) {
			array_push($pageNumbers, $page);
			$nav .= "<div class='pNo pageNo'><a href=\"?p=$page&category=$category$this_archive\">$page</a></div>";
		}
	}

	if($pageNum > 5 && $page == 3) {
		$nav .= "<div class='pNo pageNo'>...</div>";
	}

	if(!in_array($page, $pageNumbers)) {
		if(($page >= ($pageNum-2) && $page <= ($pageNum+2)) ||
			$pageNum == ($page-2)
		) {
			array_push($pageNumbers, $page);
			if ($page == $pageNum) $nav .= "<div class='pNo pageNo activePageNo'>$page</div>";
			else $nav .= "<div class='pNo pageNo'><a href=\"?p=$page&category=$category$this_archive\">$page</a></div>";
		}
	}

	if($page >= ($pageNum+2) && $maxPage > ($pageNum+4) && $threeDots) {
		$nav .= "<div class='pNo pageNo'>...</div>";
		$threeDots = false;
	}

	if(!in_array($page, $pageNumbers)) {
		if($pageNum <= ($maxPage-3) && $page > ($maxPage-2)) {
			array_push($pageNumbers, $page);
			$nav .= "<div class='pNo pageNo'><a href=\"?p=$page&category=$category$this_archive\">$page</a></div>";
		}
	}
}

if ($pageNum > 1) {
	$page = $pageNum - 1;
	$prev = "<div class='pNo pageNo'><a href=\"?p=$page&category=$category$this_archive\"><strong><</strong></a></div>";
	$first = "<div class='pNo pageNo'><a href=\"?p=1&category=$category$this_archive\"><strong><<</strong></a></div>";
} else {
   $prev  = '';
   $first = '';
}

if ($pageNum < $maxPage) {
   $page = $pageNum + 1;
   $next = "<div class='pNo pageNo'><a href=\"?p=$page&category=$category$this_archive\"><strong>></strong></a></div>";
   $last = "<div class='pNo pageNo'><a href=\"?p=$maxPage&category=$category$this_archive\"><strong>>></strong></a></div>";
} else {
   $next = '';
   $last = '';
}

echo "<br /><div class=\"pagingDiv\">
$first
$prev
$nav
$next
$last</div>";
?>
</div> <!-- end #main-area -->
<script type="text/javascript">
	$(document).ready(function() {
		$(".flip").click(function() {
			$(".panel").slideToggle("slow");
		});
		$(".close").click(function() {
			$(".panel").slideToggle("slow");
		});
	});
</script>
<?php 
include('sidebar.php');
include('footer.php'); 
?>			
