<?php 
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")));

$recordsPerPage = 15;     
$pageNum = 1;   
if(isset($_GET['p'])) {   
	$pageNum = $_GET['p'];   
 	settype($pageNum, 'integer');   
}
$offset = ($pageNum - 1) * $recordsPerPage;
?>
<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 -->';
	
if (have_posts($_REQUEST['tags'])) {
    //$and = " AND ( poll_post_date BETWEEN  '$lastmonth' AND '$today' ) ";
	//$other_and = " AND ( other_poll_post_date BETWEEN  '$lastmonth' AND '$today' )  ";
	 
    $tags = $_REQUEST['tags'];
	
	include 'includes/states_list.php';
	$state_short = '';
	$state = '';
	foreach($state_list as $key => $s) {
		if($s != '' && strpos($tags, $s) === 0) {
			$state_short = $key;
			$state = $s;
		}
	}

	$category = trim(str_replace($state, '', urldecode($tags)));

	$charts = &DBQuery("select * from chart_locations where page = 'swing_state' and chart_type = '$category' and state = '$state_short'");
	if($charts->num_rows > 0) {
		//include 'load_chart.php';
		$charts = $charts->fetch_assoc();

		$poll = &DBQuery("select poll_candidate_1_id,poll_candidate_2_id from poll where poll_election_type = '$category' and poll_geography = '$state_short' order by poll_id desc limit 1");
		$poll = mysqli_fetch_assoc($poll);

		$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?state='.$state_short.'&category='.$category.'&cand1='.$cand1['name'].'&cand2='.$cand2['name'].'&year='.$charts['date_year'].'"
			 });
			trumpClintonChart.render();
		 })
		</script>';
		echo '<div class="entry-content">
				<div id="loadChart" style="max-width: 100%"></div>
				<div class="chart_footer" style="margin-bottom: 30px;">
					<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?state='.$state_short.'&category='.$category.'&cand1='.$cand1['name'].'&cand2='.$cand2['name'].'&year='.$charts['date_year'].'&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>';
	}


	$sql = "
	SELECT which, poll_id, poll_state, cur_timestamp, poll_permalink, poll_post_date, YEAR(poll_post_date) AS year,  MONTH(poll_post_date) AS month FROM (
	SELECT 'poll' AS which, cur_timestamp, poll_id AS poll_id, poll.poll_geography AS poll_state, poll_permalink AS poll_permalink, poll_post_date AS poll_post_date FROM poll WHERE exclude = 0 AND ( poll_tag_1 = '$tags' OR poll_tag_2 = '$tags' OR poll_tag_3 = '$tags' OR poll_tag_4 = '$tags' OR poll_tag_5 = '$tags' ) 
	UNION
	SELECT 'other_poll' AS which, cur_timestamp, other_poll_id AS poll_id, other_poll.other_poll_geography AS poll_state, other_poll_permalink AS poll_permalink, other_poll_post_date AS poll_post_date FROM other_poll WHERE exclude = 0 AND ( other_poll_tag_1 = '$tags' OR other_poll_tag_2 = '$tags' OR other_poll_tag_3 = '$tags' OR other_poll_tag_4 = '$tags' OR other_poll_tag_5 = '$tags' )  
	) AS fa
	ORDER BY fa.poll_post_date DESC, fa.cur_timestamp DESC, fa.poll_id DESC	
	LIMIT $offset, $recordsPerPage";

	$result = &DBQuery($sql);
	$ads_count = 0;
	while ($row = $result->fetch_assoc()){ 
		$folder = "election-polls-" . $row['month'] . "-" . $row['year'];
		$filename = $row['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['which'] == 'poll') {
							$permalink = $row['poll_permalink'];
							$title = the_title($row['poll_id'],1,0);
							$sql_which = "SELECT poll_candidate_2_result, poll_candidate_1_result FROM poll WHERE poll_id = " . $row['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['poll_id']);	
								$first_title_photo = the_title_photo_1($row['poll_id']);
							} else {
								$second_title_photo = the_title_photo_1($row['poll_id']);	
								$first_title_photo = the_title_photo_2($row['poll_id']);
							}
							$spread = the_spread($row['poll_id']);
							$tags = the_tags($row['poll_id']);
							$img = $row['poll_state'];
							$state = '<img src="https://www.pollheadlines.com/images/'. $img . '.gif" />';
						} elseif ($row['which'] == 'other_poll') {
							$permalink = $row['poll_permalink'];
							$title = the_title_other($row['poll_id']);
							$first_title_photo = the_title_photo_other($row['poll_id'],1);
							$second_title_photo = the_title_photo_other($row['poll_id'],0);
							$spread = the_spread_other($row['poll_id']);
							$tags = the_tags_other($row['poll_id'],1,0);
							$img = $row['poll_state'];
							$state = '<img src="https://www.pollheadlines.com/images/'. $img . '.gif" />';
						}
						?>
    					<h1 class="title"><a href="https://www.pollheadlines.com/<?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 
$tags = $_REQUEST['tags'];
$sql = "SELECT COUNT(poll_id) AS num FROM (
	SELECT poll_id AS poll_id FROM poll WHERE exclude = 0 AND ( poll_tag_1 = '$tags' OR poll_tag_2 = '$tags' OR poll_tag_3 = '$tags' OR poll_tag_4 = '$tags' OR poll_tag_5 = '$tags' ) 
	UNION
	SELECT other_poll_id AS poll_id FROM other_poll WHERE exclude = 0 AND ( other_poll_tag_1 = '$tags' OR other_poll_tag_2 = '$tags' OR other_poll_tag_3 = '$tags' OR other_poll_tag_4 = '$tags' OR other_poll_tag_5 = '$tags' ) 
	) AS fa";
$result = &DBQuery($sql);
$row = $result->fetch_assoc();
$numrows = $row['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&tags=$tags\">$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&tags=$tags\">$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&tags=$tags\">$page</a></div>";
		}
	}
}

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

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

echo "<br /><div class=\"pagingDiv\">
$first
$prev
$nav
$next
$last</div>";

} // end .if
else {
	include('includes/no-results.php'); 
}	
?>
</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'); 
?>			