<?php defined( '_BIGNIGHT_EXEC' ) or die( 'Restricted access' );?>
<?php
	global $user;
	global $DB;
	
	$limit = get_page_limit();
//	$sql = "SELECT SQL_CALC_FOUND_ROWS e.*, att_cnt, z.city, z.state_prefix AS state, z.lat, z.lon FROM events e LEFT JOIN zip_code z ON e.zip=z.zip_code JOIN users u ON e.username=u.username LEFT JOIN (SELECT COUNT(event_id) AS att_cnt, event_id FROM attending GROUP BY event_id) a ON e.id=a.event_id WHERE e.time<" . (time()+14*24*3600) . " AND e.time>" . time() . " GROUP BY e.id ORDER BY time ASC {$limit};";
	
	$ip = getIP();
	// $zip = getZipByIP($ip);
	$zip = DEFAULT_ZIP;

	$sql = "SELECT SQL_CALC_FOUND_ROWS e.*,u.name FROM events e JOIN users u ON e.username=u.username LEFT JOIN zip_code z ON e.zip=z.zip_code WHERE time<" . (time()+14*24*3600) . " AND  time>" . (time()-24*3600);
	if(!empty($user["lat"])&&!empty($user["lon"])){
		$lat = $user["lat"];
		$lon = $user["lon"];
	} else {
		$pos = get_zip_pos($zip);
		$lat = $pos["lat"];
		$lon = $pos["lon"];
	}
	if(!empty($lat)&&!empty($lon)) $sql .= " AND (z.zip_code IS NULL OR (ABS(z.lat-{$lat})<0.2 AND ABS(z.lon-{$lon})<0.2))";
	$sql .= " ORDER BY time ASC {$limit};";
	
	$result = $DB->qry($sql);
	$total = $DB->qry("SELECT FOUND_ROWS();",3);
	
	search_results($result,$total);

	featured_events(empty($user["zip"]) ? $zip : $user["zip"]);

?>