<?php

if (!isset($gCms)) exit;


if (! $this->CheckPermission('Use Play'))
{
	return $this->DisplayErrorPage($id, $params, $returnid,$this->Lang('accessdenied'));
}

include_once dirname(__FILE__).'/class.player.php';
include_once dirname(__FILE__).'/class.record.php';

$playerid = isset($params['playerid']) ? $params['playerid'] : -1;
$active_tab = isset($params['active_tab']) ? $params['active_tab'] : false;


if (isset($player))
{
}
elseif ($playerid=='new')
	$player = new pPlayer;
else {
	$player = new pPlayer();
	if (! $player->loaddb($playerid))
		return $this->DisplayErrorPage($id, $params, $returnid,$this->Lang('error_unknownplayer'));
}


echo $this->CreateFormStart($id,"saveplayer",$returnid);
echo $this->CreateInputHidden($id, "playerid", $playerid);


echo $this->StartTabHeaders();
echo $this->SetTabHeader('params',$this->Lang('params'), ('params' == $active_tab));
echo $this->SetTabHeader('template',$this->Lang('template'), ('template' == $active_tab));
if ($player->checkfile() && $player->width >0 && $player->height >0)
	echo $this->SetTabHeader('preview',$this->Lang('preview'), ('preview' == $active_tab));
echo $this->EndTabHeaders();

echo $this->StartTabContent();
echo $this->StartTab('params', $params);

$paramlist = array();
foreach ($player->paramlist as $param)
{
	if (!isset($player->params[$param]))
		$paramlist[$param] = $param;
}


$this->smarty->assign('nametext', $this->lang('name'));
$this->smarty->assign('nameinput', $this->createinputtext($id,'name',$player->name,30,255,($player->islocked() ? 'readonly="readonly"' :"")));
$this->smarty->assign('locationtext', $this->lang('location'));
$this->smarty->assign('locationinput', $this->createinputtext($id,'location',$player->location,30,255,($player->islocked() ? 'readonly="readonly"' :"")));
if (!$player->islocked())
	$this->smarty->assign('browse', $this->createlink($id,'browse',$returnid, $this->lang('browse'),array('curdir'=>dirname($player->location),'browse'=>'flash'),'',false,false,'onclick="window.open(this.href,\'browser\',\'width=500,height=500\');return false;"'));
$this->smarty->assign('widthtext', $this->lang('width'));
$this->smarty->assign('widthinput', $this->createinputtext($id,'width',$player->width,20));
$this->smarty->assign('heighttext', $this->lang('height'));
$this->smarty->assign('heightinput', $this->createinputtext($id,'height',$player->height,20));

$this->smarty->assign('paramsstart',$this->CreateFieldsetStart( $id, 'paramsstart', $this->lang('flashvars')));
$this->smarty->assign('paramsend',$this->CreateFieldsetEnd());

$param_inputs = array();
if (!$player->islocked())
{
	$this->smarty->assign('paramlist', "<p>".$this->lang('values')."&nbsp;".$this->lang('values_help')." :</p>".
				"<p>".$this->createinputtext($id,'paramlist',implode(";",$player->paramlist),80,500));
}

$entry = new stdclass();
$entry->label = $this->lang('name');
$entry->input = $this->lang('value');
$param_inputs[] = $entry;

foreach ($player->params as $name=>$value)
{
	$entry = new stdclass();
	$entry->label = $name;
	$entry->input = $this->createinputtext($id,'player_param_'.$name,$value,20);
	$entry->clear = '<a href="" onclick="document.getElementById(\''.$id.'player_param_'.$name.'\').value=\'\';return false;">'.$this->lang('clear').'</a>';
	$param_inputs[] = $entry;
}
$entry = new stdclass();
if (count($player->paramlist)>0)
	$entry->label = $this->CreateInputDropdown($id, 'player_newparam_name', $paramlist);
else
	$entry->label = $this->CreateInputText($id, 'player_newparam_name',"",15);
$entry->input = $this->createinputtext($id,'player_newparam_value','',20);
$param_inputs[] = $entry;

$this->smarty->assign_by_ref('playerparams', $param_inputs);

$this->smarty->assign('submit', $this->CreateInputSubmit($id, 'submit', $this->lang('submit')));
$this->smarty->assign('apply', $this->CreateInputSubmit($id, 'apply', $this->lang('apply')));
$this->smarty->assign('cancel', $this->CreateInputSubmit($id, 'cancel', $this->lang('cancel')));



echo $this->processtemplate('player.tpl');


echo $this->EndTab();

if (true) //!$player->islocked())
{
	echo $this->StartTab('template', $params);
	echo '<textarea id="'.$id.'template" name="'.$id.'template" '.($player->islocked() ? 'readonly="readonly"' :'').' >';
	echo $player->template;
	echo '</textarea>';
	//echo $this->CreateTextArea(false, $id, $player->template, 'template');
	echo '<div class="pageoverflow"><p class="pagetext">&nbsp;</p><p class="pageinput">';
	echo $this->CreateInputSubmit($id, 'submit', $this->lang('submit'));
	echo $this->CreateInputSubmit($id, 'apply', $this->lang('apply'));
	echo $this->CreateInputSubmit($id, 'cancel', $this->lang('cancel'));
	echo '</p></div>';
	echo $this->EndTab();
}
else
	echo $this->CreateInputHidden($id, "template", $player->template);

if ($player->checkfile() && $player->width >0 && $player->height >0)
{
	echo $this->StartTab('preview', $params);
	echo $this->ShowPlayerTemplate($id, $returnid, $player->template, $player->location, 0, $player->width, $player->height, $player->params);
	echo $this->EndTab();
}
echo $this->EndTabContent();

echo $this->CreateFormEnd();

?>