<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Video Player - by Duktee
 *
 * @package		Video Player
 * @version		Version 2.5
 * @author		Benjamin David
 * @copyright	Copyright (c) 2011 - Duktee
 * @license		http://duktee.com/addons/video-player/license
 * @link		http://duktee.com/addons/video-player/
 *
 */

class Videoplayer {

	var $return_data = '';

	/**
	 * Constructor
	 *
	 */
	function Videoplayer()
	{
			
		$this->EE =& get_instance();
		
		// load videoplayer library
		
		$this->EE->load->library('videoplayer_lib');
		
		
		// constructor calls details()
		
		$this->details();
	}

	// --------------------------------------------------------------------
	
	/**
	 * Parameters
	 *
	 * @access	private
	 * @return	void
	 */
	
	private function _parameters()
	{
		$params = array();
		
		if(isset($this->EE->TMPL))
		{
			$params = array(
				'url' 			=> $this->EE->TMPL->fetch_param('url'),
				'width'			=> $this->EE->TMPL->fetch_param('width'),
				'height' 		=> $this->EE->TMPL->fetch_param('height'),
				
				'src' 			=> $this->EE->TMPL->fetch_param('src'), 		// deprecated since v1.2
				'error' 		=> $this->EE->TMPL->fetch_param('error')		// deprecated since v2.5
			);
		}
		
		return $params;
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Check if video exists
	 *
	 * @access	public
	 * @return	bool
	 */
	function video_exists()
	{
		$url = $this->EE->TMPL->fetch_param('url');
		
		return $this->EE->videoplayer_lib->video_exists($url);
	}
	
	// --------------------------------------------------------------------	
	
	/**
	 * Front Endpoint for Ajax Calls
	 *
	 * @access	public
	 * @return	tagdata
	 */
	
	function ajax()
	{
		$this->EE->load->library('videoplayer_lib');
		
		$this->EE->videoplayer_lib->endpoint();
	}

	// --------------------------------------------------------------------

	/**
	 * Details
	 *
	 * @access	public
	 * @return	tagdata
	 */
	function details()
	{
		$out = "";

		if(isset($this->EE->TMPL->tagdata))
		{
			$tagdata = $this->EE->TMPL->tagdata;
			$params = $this->_parameters();
			
			$this->video = $this->EE->videoplayer_lib->get_video($params);
			$video = $this->video;
	
	
			if($tagdata)
			{
				// rendering pair
				
				if($video['video_found'])
				{
									
					// date format
					
					$local_date = $video['date'];
					
					if (preg_match_all("#".LD."date format=[\"|'](.+?)[\"|']".RD."#", $tagdata, $matches))
					{
						foreach ($matches['1'] as $match)
						{
							$tagdata = preg_replace("#".LD."date format=.+?".RD."#", $this->EE->localize->decode_date($match, $local_date), $tagdata, 1);
						}
					}
	
				}
				
				$conditionals = $this->EE->functions->prep_conditionals($tagdata, $video);
				
				$out = $this->EE->functions->var_swap($conditionals, $video);				
			}
			else
			{
			
				// rendering single tag
				
				if($video['video_found'])
				{
					$out = $video['embed'];				
				}
				else
				{
					$out = $video['error'];
				}
			}
		}

		return $this->return_data = $out;
	}

	// --------------------------------------------------------------------

	/**
	 * Single - alias for details()
	 *
	 * @access	public
	 * @return	tagdata
	 */
	 
	function single()
	{
		return $this->details();
	}
	
	// --------------------------------------------------------------------

	/**
	 * Pair - alias for details()
	 *
	 * @access	public
	 * @return	tagdata
	 */
	 
	function pair()
	{
		return $this->details();
	}

}

/* END Class */

/* End of file mod.videoplayer.php */
/* Location: ./system/expressionengine/third_party/videoplayer/mod.videoplayer.php */