<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: tad_player.php,v 1.2 2008/05/14 01:22:58 tad Exp $
// ------------------------------------------------------------------------- //
define("_TAD_PLAYER_UPLOAD_URL",XOOPS_URL."/uploads/tad_player/");
define("_TAD_PLAYER_FLV_URL",XOOPS_URL."/uploads/tad_player/flv/");
define("_TAD_PLAYER_IMG_URL",XOOPS_URL."/uploads/tad_player/img/");

//區塊主函式 (影音播放器區塊1說明)
function tad_player($options){
	global $xoopsDB;
	if(empty($options[0])){
    $sql = "select * from ".$xoopsDB->prefix("tad_player")." order by rand() limit 0,1";
	}elseif(substr($options[0],0,4)=="pcsn"){
	  $sn=explode("_",$options[0]);
	  $sql = "select * from ".$xoopsDB->prefix("tad_player")." where pcsn='{$sn[1]}' order by rand() limit 0,1";
	}else{
		$psn=$options[0];
		$sql = "select * from ".$xoopsDB->prefix("tad_player")." where psn='$psn'";
	}
	
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	$file=$xoopsDB->fetchArray($result);
	
  $width=$options[1];
	$height=$options[2]+20;
	$all_width=$width;
	$all_height=$height;

	if(empty($file['location']) and !empty($file['youtube'])){
  	$media=$file['youtube'];
	}elseif(substr($file['location'],0,4)=='http'){
    $media=$file['location'];
	}else{
    $media=_TAD_PLAYER_FLV_URL."{$file['psn']}_{$file['location']}";
	}

  if(empty($file['image'])){
    $image=XOOPS_URL."/modules/tad_player/images/no_pic.png";
	}elseif(substr($file['image'],0,4)=="http"){
    $image=$file['image'];
	}else{
    $image=_TAD_PLAYER_IMG_URL.$file['image'];
	}
  //$media=_TAD_PLAYER_UPLOAD_URL."{$file['psn']}_{$file['location']}";
  //$image=(empty($file['image']))?"no_pic.png":"{$file['psn']}_{$file['image']}";
  $pic="s1.addVariable('image','{$image}');";
  $width_js="s1.addVariable('width','{$width}');";
  $height_js="s1.addVariable('height','{$height}');";
  $autoplay=($options[3]==1)?"true":"false";
    
	$block="
	<script type='text/javascript' src='".XOOPS_URL."/modules/tad_player/class/jquery.js'></script>
	<script type='text/javascript' src='".XOOPS_URL."/modules/tad_player/class/jw_player/swfobject.js'></script>
<div id='tad_player_block_container{$file['psn']}'>this will be replaced by the SWF.</div>
<script type='text/javascript'>
	var s1 = new SWFObject('"._TAD_PLAYER_UPLOAD_URL."mediaplayer.swf','mediaplayer','{$all_width}','{$all_height}','6');
	s1.addParam('allowfullscreen','true');
	{$width_js}
	{$height_js}
	s1.addVariable('file','{$media}');
	{$pic}
	s1.addVariable('backcolor','0x000000');
	s1.addVariable('frontcolor','0xFFFFFF');
	s1.addVariable('lightcolor','0xFFFF00');
	s1.addVariable('screencolor','0xFCFCFC');
	s1.addVariable('rotatetime','60');
	s1.addVariable('overstretch','fit');
	s1.addVariable('showicons','true');
	s1.addVariable('shownavigation','true');
	s1.addVariable('showstop','false');
	s1.addVariable('showdigits','total');
	s1.addVariable('usefullscreen','true');
	s1.addVariable('autostart','{$autoplay}');
	s1.addVariable('repeat','{$options[4]}');
	
	s1.write('tad_player_block_container{$file['psn']}');
</script>";
	return $block;
}

//區塊編輯函式
function tad_player_edit($options){
	global $xoopsDB;
	$seled0_0=($options[0]=="")?"selected":"";
	$chked3_0=($options[3]=="0")?"checked":"";
	$chked3_1=($options[3]=="1")?"checked":"";
	$chked4_0=($options[4]=="false")?"checked":"";
	$chked4_1=($options[4]=="true")?"checked":"";


	$sql = "select a.psn,a.pcsn,a.title,b.title from ".$xoopsDB->prefix("tad_player")." as a left join ".$xoopsDB->prefix("tad_player_cate")." as b on a.pcsn=b.pcsn order by a.post_date desc";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	
	$select="<select name='options[0]'>
	<option value='0'>"._MB_TADPLAYER_RANDOM_PLAY."</option>";
	$old_pcsn=0;
	while(list($psn,$pcsn,$title,$cate_title)=$xoopsDB->fetchRow($result)){
	  $selected=($options[0]==$psn)?"selected":"";
	  
	  if($old_pcsn!=$pcsn){
			$select.="<option value='pcsn_{$pcsn}' $selected>[{$cate_title}] "._MB_TADPLAYER_RANDOM_PLAY."</option>\n";
			$old_pcsn=$pcsn;
		}
		$select.="<option value='{$psn}' $selected>[{$cate_title}] {$title}</option>\n";
	}
  $select.="</select>";

	$form="
	"._MB_TADPLAYER_TAD_PLAYER_EDIT_BITEM0."
	$select<br>
	"._MB_TADPLAYER_TAD_PLAYER_EDIT_BITEM1."
	<INPUT type='text' name='options[1]' value='{$options[1]}'><br>
	"._MB_TADPLAYER_TAD_PLAYER_EDIT_BITEM2."
	<INPUT type='text' name='options[2]' value='{$options[2]}'><br>
	"._MB_TADPLAYER_TAD_PLAYER_EDIT_BITEM3."
	<INPUT type='radio' $chked3_1 name='options[3]' value='1'>"._MB_TADPLAYER_AUTOPLAY."
	<INPUT type='radio' $chked3_0 name='options[3]' value='0'>"._MB_TADPLAYER_DONT_AUTOPLAY."<br>
	"._MB_TADPLAYER_TAD_PLAYER_EDIT_BITEM4."
	<INPUT type='radio' $chked4_1 name='options[4]' value='true'>"._MB_TADPLAYER_REPEAT."
	<INPUT type='radio' $chked4_0 name='options[4]' value='false'>"._MB_TADPLAYER_DONT_REPEAT."
	";
	return $form;
}

?>
