<?php
include_once("auth.php");
include_once "db/dbElemento.class.php";
include_once "base/imagem.class.php";
include_once("include/css.inc.php");

if ($_REQUEST['campo']) $_SESSION['campo']=$_REQUEST['campo'];
$file= $_SERVER['PHP_SELF'] ."?popup=" . $_REQUEST['popup']."&campo=" . $_SESSION['campo'];


print $campo;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//PT">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<title>Imagens</title>
	<link rel="STYLESHEET" type="text/css" class="cssfile">
	<!--[if IE]><link class="cssfile_ie" rel="STYLESHEET" type="text/css" ><![endif]-->
	<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script>
var campo="<?=$_SESSION['campo'] ?>";
var file="<?= $file ?>";


function escolhe(url, alt){
	pai=eval("window.opener.document.dataform." + campo);
	pai.value=url;

	//window.opener.document.bgColor='white';
	self.close();
}

$(document).ready(function(){

	cssfile = "css/" + "<?=$css?>" + ".css";
	cssfile_ie = "css/" + "<?=$css?>" + "_ie.css";
	$(".cssfile").attr("href", cssfile);
	$(".cssfile_ie").attr("href", cssfile_ie);

	var offset = "<?=$_REQUEST['offset'] ?>";
	var extra = "";
	var idCat = "<?=$_REQUEST['idCat'] ?>";
	
	$("#navselect").change(function () {
		offset = $('#navselect option:selected').val();
		extra = $("#navextra").val();	
		document.location.href=file + "&offset=" + offset + "&idCat" + idCat + extra;
		//return false;
	});

	$(".navidcat").change(function(){
		idCat =  $(".navidcat :selected").val();
		document.location.href = file + "&idCat="+ idCat + "&offset=" + offset+ extra ;	
	}); 

	$(".selectMedia").change(function(){
		var id = $(this).attr("id");
		idCat =  $("#"+ id +" :selected").val();
		document.location.href = file + "&idCat="+ idCat + "&offset=" + offset+ extra ;		
	}); 
	
});
</script>
</head>

<body >
<form enctype="multipart/form-data" name="dataform" id="dataform" action="<?=$file?>" method="POST">
<?
class imagemPopup extends imagem{
	
	function imagemPopup(){
		$this->popup = 1;
		$this->req=$_REQUEST;
		$this->req['limit']=20;

		$this->idCat=$this->req['idCat'];
		$this->tipo="imagem";
		
		if ($this->req['cmd']=="Novo"){ $this->novo();}
		else if ($this->req['cmd']=="Guardar"){ $this->guardar();}
		else $this->listar();
	}
	
	function guardar($id=""){
		$db= new dbElemento;
		$id_novo = parent::guardar($id);
		$linha = $db->getLinha($this->tipo, $id_novo);
		$img = "netimages/image/".$linha['imagem']; //novo
		?>
		<script type="text/javascript">
			escolhe("<?=$img ?>", "<?=$linha['titulo'] ?>");
		</script>	
		<?
	}

	function listar(){
		
		$db= new dbElemento;
		/*if (!$this->req['idCat']){
			$temp=$db->get("select min(id) as IDCAT from imagemCategoria");
			$this->req['idCat']=$temp[0]['IDCAT'];
			$this->idCat=$this->req['idCat'];
		}*/		
		$extra = "&popup=" . $_REQUEST['popup']."&campo=" . $_SESSION['campo'] . "&idCat=".$this->req['idCat'];

		$campos=array("Nº"=>"id","Imagem"=>"imagem","Titulo"=>"titulo","Palavras"=>"palavras", "IdCat"=>"idCat");
        $dados=$db->getLista("imagem", $this->idCat, $campos , "imagem", $this->req['ordem'] ,$this->req['offset'], $this->req['limit'], $this->req['procurar'], $this->req['filtro'] );
        $this->req['total']=$db->getTotal("imagem", $this->idCat, $this->req['procurar'], $this->req['filtro']);
		
		$toolbar= new toolbar(); 
		$toolbar->botao("Novo");
		$toolbar->nav($this->req['offset'], $this->req['limit'], $this->req['total'], $extra);
		$toolbar->ini();
		$toolbar->categoria("imagem", "categoria", $this->idCat, true);
		$toolbar->procurar($this->req['procurar']); 
		$toolbar->botao("procurar", 90);
		$toolbar->fim();
		
		$this->dados=$dados;
		$this->tabela();
	}
	

	function tabela(){
        $max= sizeof($this->dados);
		$i=0;
		?><table><?
		if($this->dados)	
			foreach($this->dados as $t){
                $linha=($i % 4)?"1":"0";
                if (($i % 2)==0) echo '<tr class="linha' . $linha . '">';
				echo "<td>";
				$this->imageTable($t);	
				if (($i % 2)==1)  echo "</td></tr>\n"; else   echo  "</td>";     
				$i++;
    		}
    	else echo "<br>Não tem registos nesta categoria.";
		?></table><?
    }
		
	function desc($label, $value, $link){
	?><table width="220" border="0">
      <tr>
        <td width="70" class="miniLabel"><?=$label?>:</td>
        <td width="150"><?=$link?><?=$value?></a></td>
      </tr>
    </table>    
	<? }
	
	
	function imageTable($t){	

		$db= new dbElemento;
		$temp=$db->getLinha("imagemCategoria", $t['idCat']);
		
		$image_name="../netimages/image/thumb/". $t["imagem"];
		$ori_image="../netimages/image/". $t["imagem"];
		
		$size=@getimagesize($ori_image);
		$largura=$size[0];
		$altura=$size[1];
		if (!(file_exists($image_name))){
			$img=new image();
			$thumb=$img->thumbnail2($ori_image,$image_name, 100,100);
		} else 
			$thumb=$image_name;
		
		$prepath="../";
		if (strstr($ori_image,$prepath)) {$ori_image=substr($ori_image,strlen($prepath));}
		
		$linkId="javascript:escolhe('" . $ori_image ."' , '" . $t["titulo"] ."' )";
		$linkEditar='<a href="'. $linkId . '">';
		//$linkApagar='<a href='. $linkId . "&cmd=apagar";		
				
		?><table width="320" border="0" cellpadding="0" cellspacing="0" class="miniTable">
		  <tr><td>&nbsp;</td></tr>
		  <tr>
			<td width="100" height="100" align="center" valign="middle">
			<?=$linkEditar?><img src="<?=$thumb?>" border="0"></a></td>
			<td width="220"><?
					$this->desc("Titulo", $t["titulo"], $linkEditar);
					$this->desc("Palavras", $t["palavras"], $linkEditar);
					$this->desc("Dimensões", $largura."x".$altura, $linkEditar);
					$this->desc("Nome", $t["imagem"], $linkEditar);
					if (!$this->idCat)$this->desc("Categoria", $temp["categoria"], $linkEditar);
			?></td>
		  </tr>
		  
		</table>   
	<? }
}
 
$a=new imagemPopup; ?>
</form>
</body>
