<?

class Youtube
{
	var $dom, $idvideo, $title, $video;

	function videoTitle()
	{
		return $this->title;
	}
	
	function pagination()
	{
		return true;
	}
		
	function tag($tag, $page = 1) 
	{
		if ($page <= 0) $page = 1;
		
		$start = ($page-1)*10 + 1;
		$tag = str_replace(' ', '+', $tag);
		$feed = ('http://gdata.youtube.com/feeds/api/videos?vq='.$tag.'&start-index='.$start.'&max-results=10&orderby=updated&alt=rss');
		$this->dom = getFeed($feed);
	}
	
	function video($id) 
	{
		$this->idvideo = $id;
		$feed = ("http://gdata.youtube.com/feeds/api/videos/".$id);
		$this->dom = getFeed($feed);
		$this->video = $this->dom->getElementsByTagName('entry')->item(0);
		$this->title = $this->video->getElementsByTagName('title')->item(0)->textContent;
	}
	

	function player()
	{
		?>
		<object width="425" height="355">
			<param name="movie" value="http://www.youtube.com/v/<?=$this->idvideo?>" />
			<param name="wmode" value="transparent" />
			<embed src="http://www.youtube.com/v/<?=$this->idvideo?>" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed>
		</object>
		<?
	}
	
	function view($tag)
	{
		$videos = $this->dom->getElementsByTagName('item');
		
		foreach ($videos as $video)
		{
			$id = $video->getElementsByTagName('guid')->item(0)->textContent;
			$id = explode('/', $id);
			$id = $id[6];
			
			echo '<div class="video">';
			$title = $video->getElementsByTagName("title")->item(0)->textContent;
				echo '<p><a href="'.BASE_URL.'/0/video/'.$tag.'/'.$id.'.html">'.$title.'</a></p>';
				
				$fotos = $video->getElementsByTagNameNS("http://search.yahoo.com/mrss/", "thumbnail");
				$tiempo = $video->getElementsByTagNameNS("http://gdata.youtube.com/schemas/2007", "duration");
				$tiempo = $tiempo->item(0)->getAttribute("seconds");
				
				$i = 0;
				echo '<div class="img">';
				foreach ($fotos as $foto)
				{
					$url = $foto->getAttribute("url");
					echo '<a href="'.BASE_URL.'/0/video/'.$tag.'/'.$id.'.html"><img src="'.$url.'" alt="'.$title.'"/></a>';
					$i++;
					if ($i > 2) break;
				}
				echo '</div>';
				
				echo '<p class="links"><a href="'.BASE_URL.'/0/video/'.$tag.'/'.$id.'.html">'._VIEW.'</a> '._DURATION.': '.minutes($tiempo).'</p>';
				
				echo '<p>'.cut($video->getElementsByTagName("description")->item(0)->textContent).'</p>';
				
				$tags = $video->getElementsByTagName("category");
				
				echo '<div class="tags">'; echo 'tags: ';
				foreach ($tags as $palabra)
				{
					if (strpos($palabra->textContent, "http") === FALSE)
						echo '<a href="'.BASE_URL.'/0/tag/'.$palabra->textContent.'.html">'.$palabra->textContent.'</a> ';
				}
				echo '</div>';
				
			echo '</div>';
			
		}
	}
	
	function videoInfo()
	{	
			$id = $this->video->getElementsByTagName("id")->item(0)->textContent;
			$id = explode("/", $id);
			$id = $id[6];
			
			echo '<div class="video">';
				$title = $this->video->getElementsByTagName("title")->item(0)->textContent;
				
				$tiempo = $this->video->getElementsByTagNameNS("http://gdata.youtube.com/schemas/2007", "duration");
				$tiempo = $tiempo->item(0)->getAttribute("seconds");
				
				echo '<p class="links">'._DURATION.': '.minutes($tiempo).'</p>';
				
				echo '<p>'.$this->video->getElementsByTagName("description")->item(0)->textContent.'</p>';
				
				$tags = $this->video->getElementsByTagNameNS("http://www.w3.org/2005/Atom", "category");
				
				echo '<div class="tags">'; echo 'tags: ';
				foreach ($tags as $palabra)
				{
					if (strpos($palabra->getAttribute("term"), "http") === FALSE)
						echo '<a href="'.BASE_URL.'/0/tag/'.$palabra->getAttribute("term").'.html">'.$palabra->getAttribute("term").'</a> ';
				}
				echo '</div>';
				
			echo '</div>';
	}
}

?>