<?php

//global $Debug; $Debug=true;
class VideoView extends Slot {

	var $ModuleID = 3;
	var $SectionID = 55;
	var $describe = array();
	var $curPageNumber = 0;	

	function __construct($pquery) {
		parent::__construct($pquery);
		$this->Cache = 0;
		$this->Passing = TRUE;
	}
	
	function init() {
		if(!empty($this->Target[2]) && !isset($this->Target[3]) && !is_numeric($this->Target[2]) && is_AlphaNumDefis(stripslashes($this->Target[2]),1,1024) ) {
			$this->Action->processEvent($this->SectionID,'open_url',array('url_name' => $this->Target[2]),$this);
			if(!empty($this->PreparedTemplates['_error'])) header('Location: '.$this->Home.'/video'.$this->SID);		else $this->Scheme = 'story';
		} else { 
				switch($this->Scheme) {
					case 'video':
						$this->Action->processEvent($this->SectionID,'getlistvideo',array(),$this); 
					break;
					default:
						$request = array('conditions' => array(),'pageRecordsQty' => '20'    , 'sortColumn'=>'date' , 'sortType' =>'DESC'    );
						$request['curPageNumber'] = empty($this->LocalTarget[0]) || !validate(intval($this->LocalTarget[0]))? 1 : intval($this->LocalTarget[0]+1);
						$this->curPageNumber = ($request['curPageNumber'] == 0)? $request['curPageNumber']: $request['curPageNumber']-1;
						$this->Action->processEvent($this->SectionID,'open',$request,$this);
						$this->getPageControl($this->Home.'/video', '', '&nbsp');
					break;
				}
		}
		$this->setSchemeTemplate($this->Scheme);
	}

	function fillElements() {
		if(!empty($this->describe)) {
			$this->TempFillEls['describe'][0]['top'][0]['sys_name'] = strtoupper($this->describe['sys_name']);
			foreach($this->describe['contents'] as $key => $value) {
				$this->TempFillEls['describe'][0]['descr'][$key] = array(
					'top_name' => !empty($value['name'])? array(array('header' => strtoupper($value['name']))) : array(),
					'text' => $value['content_langs'][0]['value'],
				);
			}
		}
		switch($this->Scheme) {
			default: $this->fillDefault(); break;
		}
	}

	function fillDefault() { //printArray($this->PreparedTemplates);exit;
		$this->story = $this->Action->getTemplate($this->SectionID,'open_story');
		$i=1;
		foreach($this->PreparedTemplates['curPage'] as $pkey => $pval) {
			if(is_array($pval['story_langs']))		{ 
						$this->TempFillEls['list'][] = array(
							'image' => makeImage($this->ImagePath.'/video/'. $pval['id'].'/big.jpg', $this->ImageWPath.'/video/'. $pval['id'].'/big.jpg'	),
							'title' => $pval['story_langs'][0]['title'],
							'video_link' => !empty($pval['url'])?$pval['url']:'#v'.$i,
							'video_index' => 'video'.$i,
						);
						if (empty($pval['url'])) {	//mp4 files
							$this->TempFillEls['list_videos_mp4'][] = array(
								'image' => $this->ImageWPath.'/video/'. $pval['id'].'/big.jpg',
								'image_urlencode' => rawurlencode($this->ImageWPath.'/video/'. $pval['id'].'/big.jpg'),
								'video_index' => 'video'.$i,
								'linkTomp4' => $this->ImageWPath.'/video/'. $pval['id'].'/video.mp4',
								'linkTomp4_urlencode' => rawurlencode($this->ImageWPath.'/video/'. $pval['id'].'/video.mp4'),
								//'element_index' => $i>1?($i-1):'',
							);
						} else {			// youtube video
							$link_embed = str_replace('youtube.com/watch?v=', 'youtube.com/embed/', $pval['url']);
							$link_embed = str_replace('youtu.be/', 'youtube.com/embed/', $link_embed);
							$this->TempFillEls['list_videos_link'][] = array(
								'video_index' => 'video'.$i,
								'link' => $link_embed,
							);
						}
			}
			$i++;
		}
		$this->PreparedTemplates = $this->Action->clearTemplate($this->SectionID, 'open');
		if(!count($this->TempFillEls['list'])) header('Location: '.$this->Home.$this->SID);
	}
	
}
?>