<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-03-23
// $Id: index.php,v 1.5 2008/05/10 11:46:50 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
if(file_exists("mainfile.php")){
  include_once "mainfile.php";
}elseif("../../mainfile.php"){
  include_once "../../mainfile.php";
}
include_once "function.php";
/*-----------function區--------------*/


function show_cate($csn,$passwd){
	global $xoopsDB,$xoopsUser,$xoopsModule,$xoopsModuleConfig,$xoopsTpl,$xoopsOption;



  $jquery=get_jquery();

  //以流水號取得某筆tad_gallery_cate資料
  $cate=get_tad_gallery_cate($csn);

  //可觀看相簿
  $ok_cat=chk_cate_power();

  //密碼檢查
  if(!empty($csn)){
    if(empty($passwd) and !empty($_SESSION['tadgallery'][$csn])){
      $passwd=$_SESSION['tadgallery'][$csn];
  	}

  	$sql = "select csn,passwd from ".$xoopsDB->prefix("tad_gallery_cate")." where csn='{$csn}'";
    $result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  	list($ok_csn,$ok_passwd)=$xoopsDB->fetchRow($result);
  	if(!empty($ok_csn) and $ok_passwd!=$passwd)redirect_header($_SERVER['PHP_SELF'],3, sprintf(_TADGAL_NO_PASSWD_CONTENT,$cate['title']));

  	if(!empty($ok_passwd) and empty($_SESSION['tadgallery'][$csn])){
  		$_SESSION['tadgallery'][$csn]=$passwd;
  	}


    //檢查相簿觀看權限
  	if(!in_array($csn,$ok_cat)){
  		redirect_header($_SERVER['PHP_SELF'],3, _TADGAL_NO_POWER_TITLE,sprintf(_TADGAL_NO_POWER_CONTENT,$cate['title'],$select));
  	}
  }



  //呈現資料預設值
  $data="";

  //找出分類下所有相片
  $sql = "select * from ".$xoopsDB->prefix("tad_gallery")." where csn='{$csn}' order by photo_sort , post_date";
  $result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

  while(list($sn,$db_csn,$title,$description,$filename,$size,$type,$width,$height,$dir,$uid,$post_date,$counter,$exif)=$xoopsDB->fetchRow($result)){

    $data.="
    <div class='PhotoCate' onClick=\"location.href='{$_SERVER['PHP_SELF']}?sn={$sn}';\">
    <img src='".get_pic_url($dir,$sn,$filename,"s")."' alt='$title' title='$title' class='thumb' width=500>
    </div>
    ";
  }
  
  $main="
  <div id='main'>
    <p>{$data}</p>
  </div>
  <p style='clear:both;'></p>";
  return $main;
}



//觀看某一張照片
function view_pic($sn=""){
	global $xoopsDB,$xoopsUser,$xoopsModule,$xoopsModuleConfig,$isAdmin;

	//所有分類名稱
	$cate_all=get_tad_gallery_cate_all();

	$sql = "select csn,title,description,filename,size,type,width,height,dir,uid from ".$xoopsDB->prefix("tad_gallery")." where sn='{$sn}'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	list($csn,$title,$description,$filename,$size,$type,$width,$height,$dir,$uid)=$xoopsDB->fetchRow($result);

	if(!empty($csn)){
		$ok_cat=chk_cate_power();
		if(!in_array($csn,$ok_cat)){
		 	header("location:{$_SERVER['PHP_SELF']}");
		}
	}

	//找出上一張或下一張
  $pnp=get_pre_next($csn,$sn);

	$back_btn=(!empty($pnp['pre']))?" id='pre_photo' onClick=\"location.href='{$_SERVER['PHP_SELF']}?sn={$pnp['pre']}#photo_top'\" ":"";
	$next_btn=(!empty($pnp['next']))?" id='next_photo' onClick=\"location.href='{$_SERVER['PHP_SELF']}?sn={$pnp['next']}#photo_top'\" ":"";

	$title=(empty($title))?$filename:$title;

  //計數器
	add_tad_gallery_counter($sn);

  //縮圖
	if(($width/$height)>2){
	  $mpic=getimagesize(get_pic_url($dir,$sn,$filename,"m","dir"));
	  $pic180="<marquee width='{$xoopsModuleConfig['thumbnail_m_width']}' behavior='scroll' height='{$mpic[1]}' direction=left scrolldelay=0 scrollamount=3><a href='".get_pic_url($dir,$sn,$filename)."'><img src='".get_pic_url($dir,$sn,$filename,"m")."'  alt='$title' class='instant itiltnone icolorFCFCFC' /></a></marquee>";
	}else{

    if($width > $height){
      $new_wh=round($xoopsModuleConfig['thumbnail_m_width'] * $height / $width , 0);
      $pcss="width:{$xoopsModuleConfig['thumbnail_m_width']}px;height:{$new_wh}px;";
      $pacss="width:{$new_wh}px;height:{$new_wh}px;";
    }else{
      $new_wh=round($xoopsModuleConfig['thumbnail_m_width'] * $width / $height , 0);
      $new_wh_a=$new_wh/2;
      $pcss="width:{$new_wh}px;height:{$xoopsModuleConfig['thumbnail_m_width']}px;";
      $pacss="width:{$new_wh_a}px;height:{$xoopsModuleConfig['thumbnail_m_width']}px;";
    }

    $photo=get_pic_url($dir,$sn,$filename,"m");
    $pic180="
    <table id='view_photo' style='{$pcss}background-image:url({$photo});background-position: top center;	background-repeat: no-repeat;	background-color: black;'>
    <tr>
    <td $back_btn style='{$pacss}background-position: top center;	background-repeat: no-repeat;border:0px;'></td>
    <td $next_btn style='{$pacss}background-position: top center;	background-repeat: no-repeat;border:0px;'></td></tr>
    </table>
  ";
	}

  $data="
  <table id='main' style='width:100%;'>
  <tr><td align='center'><a name='photo_top'>{$pic180}</a></td></tr>
  <tr><td style='color:#E0E0E0;text-align:center'>{$title}</td></tr>
  <tr><td style='color:#E0E0E0'>".nl2br($description)."</td></tr>
  </table>
  ";


	return $data;
}


//更新人氣資料到tad_gallery中
function add_tad_gallery_counter($sn=""){
	global $xoopsDB;
 	$sql = "update ".$xoopsDB->prefix("tad_gallery")." set `counter`=`counter`+1 where sn='{$sn}'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error());

}


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

$sn=(isset($_REQUEST['sn']))?intval($_REQUEST['sn']) : 0;
$csn=(isset($_REQUEST['csn']))?intval($_REQUEST['csn']) : 0;
$passwd=(isset($_POST['passwd']))?$_POST['passwd'] : "";


$jquery=get_jquery();


switch($_REQUEST['op']){

	default:
	if(!empty($sn)){
		$main=view_pic($sn);
    $pic=get_tad_gallery($sn);
    $csn=$pic['csn'];
	}else{
		$main=show_cate($csn,$passwd);
	}
	break;
}

//分類下拉選單
$cate_option=get_tad_gallery_cate_option(0,0,$csn);

$jquery=get_jquery();
/*-----------秀出結果區--------------*/
echo "
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
  <head>
  <meta http-equiv='content-type' content='text/html; charset="._CHARSET."'>
  <title></title>
  $jquery
  <script type='text/javascript'>
    $(document).ready(function(){
      var w=$(document.body).width();
      var thumb_w=(w/3)+30;
      var pic_w=thumb_w-90;
      $('.thumb').css('width',thumb_w);
      $('.PhotoCate').css('width',pic_w).css('height',pic_w);
    });
  </script>
  <style>
    body,td,div,#cate_menu option,a{
      font-size:56px;
      text-decoration:none;
      border:none;
    }

    .PhotoCate{
    	margin: 5px;
    	float: left;
    	width: 350px;
    	height: 350px;
    	overflow:hidden;
    	position: relative;
    	cursor: pointer;
    	background-color: rgb(0,0,0);
    	border:10px solid #202020;
    }

  </style>
  </head>
  <body style='background-color:black;'>

  <select style='width:100%;font-size:56px;' onChange=\"window.location.href='{$_SERVER['PHP_SELF']}?csn=' + this.value\" id='cate_menu'>$cate_option</select>
  $main

  </body>
</html>
";

?>
