
require_once("../includes/mysql.php");
require_once("../includes/fastjson.php");

$hive_names = GetData('h'); // Possible array of hive names
$results_per_page = GetData('r');
$width = GetData('w');
$page_num = GetData('p');
$sort_by = GetData('s');
$start = GetData('start');

// Settings
$script_url = 'http://www.twerq.com/onsite';

if(!$start) {
	$start = 0;
}

if(!$page_num) {
	$page_num = 1;
}

if(! $width) {
	$width = 180;
}

if(! $sort_by) {
	$sort_by = 'score';
}

// Read through the list of hive names they sent, find ones that actually exist.
$count = 0;
$hive_array = array();
$results_array = array();
$id_in = array();

foreach($hive_names as $key=>$val) {
	$results = mysql_query("SELECT * FROM hives WHERE name='$val'") or die(mysql_error());
	$obj = mysql_fetch_object($results);
	
	if($obj->id > 0) {
		$hive_array[$key]['name'] = $obj->name;
		$hive_array[$key]['id'] = $obj->id;
		array_push($id_in, $hive_array[$key]['id']);
	}
}

// Get the list of results for each hive that we have.
$tmp_query = mysql_query("SELECT * FROM hive_shortlist WHERE hive_id IN (".implode(',',$id_in).") AND moderator_removed!='1' ORDER BY $sort_by DESC") or die(mysql_error());
while($tmp_obj = mysql_fetch_object($tmp_query)) {
	if(strlen($tmp_obj->title) > 0 && strlen($tmp_obj->address) > 0) {
		$results_array[$count]['title'] = $tmp_obj->title;
		$results_array[$count]['url'] = $tmp_obj->address;
		$results_array[$count]['search_type'] = $tmp_obj->search_engine;

		$count++;
	}
}

$total_hives = sizeof($hive_array);
$total_results = sizeof($results_array);
$total_pages = ceil($total_results/$results_per_page);
$offset = ($page_num - 1) * $results_per_page;

$results_array = array_slice($results_array, $offset, $results_per_page);

//$links = "<ul class=\"onsite_list\" style=\"list-style: none;\">";
$links = "";;
foreach($results_array as $key=>$array) {
	switch($array['search_type']) {
		//Web results
		case 1:
			$class = "web";
			break;
		case 2:
			$class = "web";
			break;
		case 6:
			$class = "web";
			break;
		//Images
		case 3:
			$class = "image";
			break;
		//RSS Searches
		case 5:
			$class = "rss";
			break;
		//Stories
		case 4:
			$class = "story";
			break;
		//Videos
		case 7:
			$class = "video";
			break;
	}
	
	$array['title'] = strip_tags($array['title']);
	$summary_offset_ratio = 9;
	$summary_length = ($width/$summary_offset_ratio);
	$title_summary = substr($array['title'], 0, $summary_length);
	if(strlen($array['title']) > $summary_length) {
		$title_summary .= "...";
	}

	$link_width = $width - 5;

	$links .= <<<EOT
		<div style="clear: both; margin-left: 5px;">
			<a style="width: 100%" href="$array[url]" title="$array[title]" alt="$array[title]" class="$class menu_links">$title_summary</a>
		</div>
EOT;
}
//$links .= "</ul>";

//$links .= "<div style=\"margin-top: 9px; margin-bottom: 2px; text-align: center;\"><img src=\"$script_url/images/gray_sep.gif\"></div>";

if($total_pages > 1) {
//	$links .= GetPaging();
}



if($total_hives == 1) {
	$hive_heading = "<a style=\"color: blue;\" href=\"http://www.twerq.com/splash.html?query=h:{$hive_array[0][name]}\">{$hive_array[0][name]}</a>";
} else {
	$tmp_array = array();
	foreach($hive_array as $array) {
		array_push($tmp_array, "h:$array[name]");
	}
	$hive_heading = "<a style=\"color: blue;\" href=\"http://www.twerq.com/splash.html?query=" . implode('++', $tmp_array) . "\">Related Hives</a>";
}

if($total_hives > 1) {
	$tmp_array = array();
	foreach($hive_array as $array) {
		$cur = "<a style=\"color: blue;\" href=\"http://www.twerq.com/splash.html?query=h:$array[name]\">$array[name]</a>";
		array_push($tmp_array, $cur);
	}

	$hive_name_list = <<<EOT
		<div style="margin-bottom: 4px;">
EOT;

	$hive_name_list .= implode(', ', $tmp_array) . "</div>";
}

$result_count = "Results 1-$results_per_page of $total_results";


$html = file_get_contents("onsite.html");
$html = str_replace('%result_count%', $result_count, $html);
$html = str_replace('%search_width%', ($width-37), $html);
$html = str_replace('%hive_names%', $hive_name_list, $html);
$html = str_replace('%hive_heading%', $hive_heading, $html);
$html = str_replace('%script_url%', $script_url, $html);
$html = str_replace('%results%', $links, $html);
$html = str_replace('%width%', $width, $html);

//print $html;
$html = str_replace("\n", '', $html);
$html = json($html);

#################################################
function GetData($var) {
	if(is_array($_REQUEST[$var])) {
		$tmp_array = array();
		foreach($_REQUEST[$var] as $key=>$val) {
			$tmp_array[$key] = mysql_real_escape_string(strip_tags(stripslashes($val)));
		}
		return $tmp_array;
	}

	return mysql_real_escape_string(strip_tags(stripslashes($_REQUEST[$var])));
}

#################################################
function GetPaging() {
	global $start;
	global $total_results;
	global $results_per_page;
	global $total_pages;
	global $page_num;
	global $hive_array;
	global $script_url;
	global $sort_by;

	$max_page_numbers = 7;

	$page_url = "$script_url/onsite_paging.php?s=$start&s=$sort_by";
	foreach($hive_array as $cur) {
		$page_url .= "&h[]=$cur[name]";
	}
	
	//Current Page.
	$current_page = $page_num;

	//Next & Previous Page.
	$next_page = $current_page+1;
	$previous_page = $current_page == 1 ? 1:$current_page-1;

	//Determine whether or not we'll be displaying the next and previous images.
	$display_previous = $current_page == 1 ? 'none':'inline';
	$display_next = $current_page == $total_pages ? 'none':'inline';

	//Calculate our starting and ending pages.
	$start_page = $current_page-1;

	//Make sure we always show the correct number of pages.
	if( ($total_pages-$start_page) < $max_page_numbers ) {
		$start_page = ($total_pages - $max_page_numbers) + 1;
	}

	//Make sure we never have negative pages.
	if($start_page < 1 || $max_page_numbers >= $total_pages) {
		$start_page = 1;
	}
	
	$end_page = ($start_page-1) + $max_page_numbers;	
	if($end_page > $total_pages) {
		$end_page = $total_pages;
	}	

	$page_number = $start_page;

	$html .= "<div class=\"onsite_paging\">";

	$html .= "<table border=\"0\" style=\"border: collapse;\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\"><tr>";
	
	/* PREVIOUS BUTTON */
	$html .= <<<EOT
		<td>
			<a href="javascript:;" onclick="OnsiteChangePage('$page_url&p=$previous_page');" style="">
				<img class="previous-page-button" border="0" src="$script_url/images/back.gif" style="display: $display_previous "></a>
		</td>
		<td style="padding-bottom: 7px;">
EOT;

	/* CREATE PAGING LINKS */
	for($i = $start_page; $i <= $end_page; $i++) {
		if($current_page == $page_number) {
			$html .= "&nbsp;$page_number ";
		} else {
			$html .= "<a href=\"javascript:OnsiteChangePage('$page_url&p=$page_number');\">$page_number</a> ";
		}

		$page_number++;
	}

	/* NEXT BUTTON */
	$html .= <<<EOT
				</td>
				<td>
					<a href="javascript:;" onclick="OnsiteChangePage('$page_url&p=$next_page');" style="">
					<img class="next-page-button" border="0" src="$script_url/images/next.gif" style="display: $display_next;"></a>
				</td>
			</tr>
		</table>
EOT;
	$html .= "<div style=\"clear: both;\"></div>";
	$html .= "</div>";

	return $html;
}



document.write($html);