<?php
/**
 * Created by PhpStorm.
 * User: ValekS
 * Date: 16.02.14
 * Time: 18:12
 */
define('_IN_JOHNCMS', 1);

require('../incfiles/core.php');

$search = isset($_REQUEST['s']) ? functions::checkout(trim($_REQUEST['s'])) : false;

// подключение языкового файла
$lng_yt = core::load_lng('you_tube');
// вывод титлов страницы
$textl = $lng_yt['search_v'].' - '.$search;

require('../incfiles/head.php');

require('classes/youtube.php');

$p = isset($_REQUEST['p']) ? functions::checkout(trim($_REQUEST['p'])) : '';
$order = isset($_REQUEST['order']) ? functions::checkout(trim($_REQUEST['order'])) : 'relevance';

echo '<div class="phdr">'.$lng_yt['search_v'].'</div>';

if ($search) {
	
	$s = str_replace(' ', '+', $search);
	
	// Поисковый запрос
	$youtube = new YouTube($s, $kmess, $order, $p);
	$data = $youtube->search();
	$info = $youtube->info();
	
	echo '<div class="gmenu">'.$lng_yt['text_search'].' <i>'.functions::checkout($search).'</i><br/>'.$lng_yt['result'].' <b>'.$info['count'].'</b></div>';
	echo '<div class="topmenu">'.$lng_yt['filtr'].' '.($order == 'relevance' ? $lng_yt['filtr_rel'] : '<a href="results?s='.$s.'&amp;order=relevance" title="'.$lng_yt['filtr_rel_title'].'">'.$lng_yt['filtr_rel'].'</a>').' | '.
	($order == 'date' ? $lng_yt['filtr_pub'] : '<a href="results?s='.$s.'&amp;order=date" title="'.$lng_yt['filtr_pub_title'].'">'.$lng_yt['filtr_pub'].'</a>').' | '.
	($order == 'viewCount' ? $lng_yt['filtr_view'] : '<a href="results?s='.$s.'&amp;order=viewCount" title="'.$lng_yt['filtr_view_title'].'">'.$lng_yt['filtr_view'].'</a>').' | '.
	($order == 'rating' ? $lng_yt['filtr_rate'] : '<a href="results?s='.$s.'&amp;order=rating" title="'.$lng_yt['filtr_rate_title'].'">'.$lng_yt['filtr_rate'].'</a>').'</div>';
	
	////////////////////////////
	/////Вывод Результатов//////
	////////////////////////////
	for ($i = 0; $i < $info['res']; ++$i) {
		
		// Запрос инфы с поиска видео
		$video_id = $data[$i]['id'];
		$video_name = htmlspecialchars($data[$i]['title']);
		$video_author = htmlspecialchars($data[$i]['username']);
		$video_desc = htmlspecialchars($data[$i]['description_short']);

		echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
			
		if (!$is_mobile) {
			echo '<table width="100%"><tr><td width="30%"><a href="watch?v='.$video_id.'&amp;n='.$video_name.'" title="'.$video_name.'"><img src="http://i1.ytimg.com/vi/'.$video_id.'/mqdefault.jpg" alt="Image" width="185" /></a></td>';
			echo '<td width="70%"><h3><a href="watch?v='.$video_id.'&amp;n='.$video_name.'" title="'.$video_name.'">'.$video_name.'</a></h3>';
			echo '<p><b>'.$lng_yt['author'].'</b> '.$video_author.' | <b>'.$lng_yt['add'].'</b> '.$data[$i]['date'].' | <b>'.$lng_yt['view'].'</b> '.$data[$i]['views'].'</p><p><i>'.$video_desc.'</i></p>';
		} else {
			echo '<table width="100%"><tr><td width="30%"><a href="watch?v='.$video_id.'&amp;n='.$video_name.'" title="'.$video_name.'"><img src="http://i1.ytimg.com/vi/'.$video_id.'/mqdefault.jpg" alt="Image" width="120" height="72" /></a></td>';
			echo '<td width="70%"><a href="watch?v='.$video_id.'&amp;n='.$video_name.'" title="'.$video_name.'">'.$video_name.'</a><br/>';
			echo '<p><b>'.$lng_yt['author'].'</b> '.$video_author.' | <b>'.$lng_yt['add'].'</b> '.$data[$i]['date'].' | <b>'.$lng_yt['view'].'</b> '.$data[$i]['views'].'</p>';
		}
		
		echo '</td></tr></table></div>';
	
	}
	
	////////////////////////////
	/////////Постраничка////////
	///////////////////////////
	if ($info['next'] || $info['prev']) {
		echo '<div class="topmenu">';
		
		if (!$info['prev']) {
			echo '<span class="currentpage"><b>Предыдущая</b></span> ';
		} else {
			echo '<a class="pagenav" href="results?s='.$s.($order ? '&amp;order='.$order : '').'&amp;p='.$info['prev'].'">Предыдущая</a> ';
		}
		
		if (!$info['next']) {
			echo '<span class="currentpage"><b>Следующая</b></span>';
		} else {
			echo '<a class="pagenav" href="results?s='.$s.($order ? '&amp;order='.$order : '').'&amp;p='.$info['next'].'">Следующая</a>';
		}
		
		echo '</div>';
	}

	echo '<div class="phdr"><a href="index">'.$lng_yt['back'].'</a></div>';
}

require('../incfiles/end.php');