<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: play.php,v 1.2 2008/05/14 01:22:58 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include "header.php";
$xoopsOption['template_main'] = "play_tpl.html";
/*-----------function區--------------*/

//播放
function play($get_psn=""){
	global $xoopsDB,$xoopsModuleConfig,$xoopsUser;

  $file=get_tad_player($get_psn);
  $ok_cat=chk_cate_power();
  
  $user_group=array();
  if($xoopsUser){
    $user_group=$xoopsUser->getGroups();
	}
  
  $enable_group_arr=explode(",",$file['enable_group']);
	$same=array_intersect($enable_group_arr,$user_group);
  if((!empty($file['pcsn']) and !in_array($file['pcsn'],$ok_cat)) or (!empty($file['enable_group']) and empty($same))){
		redirect_header("index.php",3,sprintf(_MD_TADPLAYER_NO_POWER,$file['title']));
	}

	add_counter($get_psn);
	
	$h1_title="<a href='index.php?pcsn={$file['pcsn']}&op=playlist' style='color: rgb(51,102,255);'><img src='images/playlist.png' alt='playlist.png, 5.6kB' title='Playlist' border='0' height='48' width='48' align='absmiddle' hspace=3></a>";


	$play_code=play_code($file,$get_psn,"single");

  

  if($xoopsModuleConfig['show_border']=='1'){
		$main=div_3d("",$play_code,"corners","",$xoopsModuleConfig['border_color'])."";
	}else{
    $main="<h2>{$h1_title}{$file['title']}<h2>
		$play_code";
	}
	
	$all['pcsn']=$file['pcsn'];
	$all['media']=$play_code;
	$all['title']=$file['title'];
	$all['content']=$file['content'];
	
	if($xoopsUser){
		$getGroups=$xoopsUser->getGroups();
		$in_group=array_intersect($xoopsModuleConfig['show_code'],$getGroups);
	}else{
    $in_group=array();
	}
	
	$all['code']="";
	
	if(sizeof($in_group) > 0){
	  $width=(empty($file['width']))?$xoopsModuleConfig['width']:$file['width'];
		$height=(empty($file['height']))?$xoopsModuleConfig['height']:$file['height'];
		$all['code']="
		<script type='text/javascript'>
		var XmlHttp;

		function startRequest(w,h,b,l){
		  if(window.ActiveXObject){
		   XmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
		  }else if(window.XMLHttpRequest){
				XmlHttp = new XMLHttpRequest();
			}

			XmlHttp.onreadystatechange = handleStateChange;
			XmlHttp.open('GET','".XOOPS_URL."/modules/tad_player/mk_code.php?psn={$get_psn}&w='+w+'&h='+h+'&b='+b+'&l='+l,true);
			XmlHttp.send(null);
		}

		function handleStateChange(){
			if(XmlHttp.readyState==4){
				if(XmlHttp.status == 200){
				document.getElementById('code_results').innerHTML=XmlHttp.responseText;
				}
			}
		}
		</script>
		<form action='' method='get' name='myForm' onSubmit='return false;'>
		"._MD_TADPLAYER_WIDTH.": <input type='text' name='width' value='{$width}' size=3> x
		"._MD_TADPLAYER_HEIGHT.": <input type='text' name='height' value='{$height}' size=3>
		"._MD_TADPLAYER_BORDER.": <select name='border'>
		<option value='0'>"._MD_TADPLAYER_NO."</option>
		<option value='1'>"._MD_TADPLAYER_YES."</option>
		</select>
		"._MD_TADPLAYER_ALIGN.": <select name='align'>
		<option value=''>"._MD_TADPLAYER_FLOAT_NO."</option>
		<option value='left'>"._MD_TADPLAYER_FLOAT_LEFT."</option>
		<option value='center'>"._MD_TADPLAYER_FLOAT_CENTER."</option>
		<option value='right'>"._MD_TADPLAYER_FLOAT_RIGHT."</option>
		</select>
		<input type='hidden' name='op' value='make_code'>
		<input type='hidden' name='psn' value='{$psn}'>
		<input type='hidden' name='pcsn' value='{$file['pcsn']}'>
		<input type='button' value='"._MD_TADPLAYER_SHOW_CODE."' onClick='javascript:startRequest(document.myForm.width.value,document.myForm.height.value,document.myForm.border.value,document.myForm.align.value)'>
		</form>
		<div id='code_results'></div>";
	}
	

	$all['width']=$file['width'];

	return $all;
}


//找出選單
function get_cate_play($get_psn="",$size=23){
	global $xoopsDB,$xoopsModuleConfig;
	$file=get_tad_player($get_psn);
	
	$sql = "select a.psn,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 where a.pcsn='{$file['pcsn']}' order by a.sort, a.post_date desc";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

  $option="";
	while(list($psn,$title,$cate_title)=$xoopsDB->fetchRow($result)){
	  $selected=($psn==$get_psn)?"selected":"";
	  $option.="<option value='{$psn}' $selected>$title</option>\n";
	}
	
	$xoopsModuleConfig['height']-=15;
	$height=$xoopsModuleConfig['height']+40;
	
	$cate_select=get_tad_player_cate_option(0,0,$file['pcsn']);
	$select="
	<form action='' method='post'>
	<select id='main_opt' name='main_opt' onchange='getList(this)' style='width:150px;'>
	$cate_select
	</select><br>
	<select id='sub_opt' name='sub_opt' size=$size style='width:150px;height:{$height}px' onChange=\"window.location.href='{$_SERVER['PHP_SELF']}?psn=' + this.value\" >
	$option
	</select>
	</form>";


  return $select;
}

/*-----------執行動作判斷區----------*/
$op=(empty($_REQUEST['op']))?"":$_REQUEST['op'];
$psn=(empty($_REQUEST['psn']))?"":intval($_REQUEST['psn']);
$pcsn=(empty($_REQUEST['pcsn']))?"":intval($_REQUEST['pcsn']);

switch($op){

	default:
	if(empty($psn))header("location:index.php");
	$main=play($psn);
	break;
}

/*-----------秀出結果區--------------*/
$admin_link="index.php";


if ($xoopsUser) {
  $module_id = $xoopsModule->getVar('mid');
  $isAdmin=$xoopsUser->isAdmin($module_id);
}


$lang['home']=_MD_HOMEPAGE;
$lang['list']=_MA_TADPLAYER_LIST;
$lang['hot']=_MA_TADPLAYER_HOT;
$lang['admin']=_TO_ADMIN_PAGE;
$lang['modify']=_MD_TADPLAYER_MODIFY_MEDIA;

include XOOPS_ROOT_PATH."/header.php";
$xoopsTpl->assign( "css" , "<link rel='stylesheet' type='text/css' media='screen' href='module.css' />
<script type='text/javascript' src='class/ajax.js'></script>
<script type='text/javascript' src='class/chained_selects.js'></script>") ;
$xoopsTpl->assign( "select" , get_cate_play($psn)) ;
$xoopsTpl->assign( "title" , $main['title']) ;
$xoopsTpl->assign( "width" , $main['width']) ;
$xoopsTpl->assign( "media" , $main['media']) ;
$xoopsTpl->assign( "code" , $main['code']) ;
$xoopsTpl->assign( "content" , $main['content']) ;
$xoopsTpl->assign( "admin_link" , $admin_link) ;
$xoopsTpl->assign( "hot_media" , hot_media()) ;
$xoopsTpl->assign( "pcsn" , $main['pcsn']) ;
$xoopsTpl->assign( "psn" , $psn) ;
$xoopsTpl->assign( "admin" , $isAdmin) ;
$xoopsTpl->assign( "op" , $op) ;
$xoopsTpl->assign( "lang" , $lang) ;

include_once XOOPS_ROOT_PATH.'/include/comment_view.php';
include_once XOOPS_ROOT_PATH.'/footer.php';

?>
