/*******************************************************************************
 * This file is part of Educatio.
 *
 *     Educatio is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Educatio is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Educatio.  If not, see <http://www.gnu.org/licenses/>.
 *     
 *     Authors:
 *           Filipe Marinho de Brito - filipe.marinho.brito@gmail.com
 *           Rodrigo de Souza Ataides - rodrigoataides@gmail.com
 *******************************************************************************/
$(document).ready(function() {

	//propriedades do datatable
	var id = "listarEdital";
	var url = "Edital";
	var sInfoComplemento = "Edital";

	//sql
	var table = 	"edital.edital";
	var where = " where 1=? and 2=? ";
	var argumento = "1|2";
	var tipoArgumento = "Integer|Integer";
	var order = " order by id desc ";

	//cria Edital buttonset
	$("#listarEdital_btnNovo").button({icons: {primary: 'ui-icon-plusthick'}, text: true});
	$("#listarEdital_btnFiltrosAvancados").button({icons: {primary: 'ui-icon-circle-zoomout'}, text: true});
	$("#listarEdital_btnRecarregar").button({icons: {primary: 'ui-icon-arrowreturnthick-1-w'}, text: true});
	$("#listarEdital_btnPDF").button({icons: {primary: 'ui-icon-pdf'}, text: true});
	$("#listarEdital_btnPDF").click(function(){
		openNewPagePost(url+"/bServerSide/pdf", montarParametrosServidor(id, colunas, table, where, argumento, tipoArgumento, order, "${encoded}"));
	});
	$("#listarEdital_btnXLS").button({icons: {primary: 'ui-icon-xls'}, text: true});
	$("#listarEdital_btnXLS").click(function(){
		openNewPagePost(url+"/bServerSide/xls", montarParametrosServidor(id, colunas, table, where, argumento, tipoArgumento, order, "${encoded}"));
	});


	var aoColumns = [
	                 {"sTitle": "-", "mDataProp": "htmlControl","bSortable": false, "sWidth": "20px", "bVisible": true, "sDefaultContent":"","sType": "html", 
	                	 "fnRender": function ( oObj ) {
	                		 if(  $("#Edital_listar_popUp").attr('innerObject') === 'true' ){
	                			 return "<button type=\"button\" id=\"checkBtn_"+oObj.aData.id+"\" class=\"checkBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"selecionarPopUpListarEdital(event)\" role=\"button\" aria-disabled=\"false\" title=\"Selecionar\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-check\"></span><span class=\"ui-button-text\">Selecionar</span></button>";
	                		 }else{
						 			var buttons = "<a id=\"printBtn_"+oObj.aData.id+"\" class=\"printBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" href=\""+url+"/relatorio/"+oObj.aData.id+"?formato=pdf\" target=\"_blank\" title=\"Relatório\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-print\"></span><span class=\"ui-button-text\">Rel</span></a>";
						 			buttons+= "<button type=\"button\" id=\"magnifierGlassDataGridBtn_"+oObj.aData.id+"\"  class=\"magnifierGlassDataGridBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\"   onclick=\"listarResultadoEvento(event,'Resultado de Recrutamento & Seleção: "+oObj.aData.nome+"', 'Edital/listarResultado/"+oObj.aData.id+"',"+($(window).width()-16)+", "+($(window).height()-60)+");\" title=\"Executar pesquisa\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-search\"></span><span class=\"ui-button-text\">Mag</span></button>";
						 			if(oObj.aData.modificar){
						 				buttons+= "<button type=\"button\" id=\"editarBtn_"+oObj.aData.id+"\" class=\"editarBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"editar(event,'"+url+"','"+id+"',870)\" title=\"Editar\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-pencil\"></span><span class=\"ui-button-text\">Edi</span></button>";
						 			}
						 			if(oObj.aData.compartilhar){
						 				buttons+="<button type=\"button\" id=\"shareBtn_"+oObj.aData.id+"\" class=\"shareBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"compartilhar(event,'"+url+"','"+id+"')\" title=\"Compartilhar\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-extlink\"></span><span class=\"ui-button-text\">Acl</span></button>";
						 			}
						 			if(oObj.aData.excluir){
						 				buttons+="<button type=\"button\" id=\"deletarBtn_"+oObj.aData.id+"\" class=\"deletarBtn"+id+" ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only\" onclick=\"deletarDoBanco(event,'"+url+"','"+id+"')\" title=\"Deletar\" role=\"button\" aria-disabled=\"false\" style=\"width: 20px; height: 20px;\"><span class=\"ui-button-icon-primary ui-icon ui-icon-close\"></span><span class=\"ui-button-text\">Del</span></button>";
						 			}
						 			return buttons;
	                		 }
	                	 }
	                 },
	                 {"sTitle": "Id", "mDataProp": "id", "bVisible": false, "sType": "numeric" },         	 
	                 {"sTitle": "Nome", "mDataProp": "nome", "bVisible": true, "sDefaultContent":"", "sWidth": "45px"},
	                 ];

	var colunas = 	JSON.stringify(aoColumns, function(key, val) {if (typeof val === 'function') { return val + '';} return val;});
	listServerSide(id, colunas, sInfoComplemento, url, table, where, argumento, tipoArgumento, order, "${encoded}");

});

function selecionarPopUpListarEdital(event){
	var objetoSelecionado = selecionarObjeto(event, "listarEdital");
	buscar('listarEdital', 'Edital/'+objetoSelecionado.id, afterBuscaPopUpListarEdital);
}

function afterBuscaPopUpListarEdital(data){
	$("#listarEdital_filtrosAvancados").append('<input type="hidden" id="listarEdital_selecionado" />');
	$("#listarEdital_selecionado").val($.toJSON(data));
	try{$("#Edital_listar_popUp").dialog('close');
	$("#Edital_listar_popUp").dialog('destroy');
	$("#Edital_listar_popUp").remove();} catch(e){}
}




function listarResultadoEvento(event,titulo,url, x, y){
	var position = { "my": "center left", "at": "center left",  collision: "none" } ;
	var positionStr = JSON.stringify(position, function(key, val) {if (typeof val === 'function') { return val + '';} return val;});
	   loadPageToPopUp(titulo, url,x,y,true,
			   function(){},
			   function(){}, undefined, positionStr,undefined);
}

