$(function(){

	//jQuery.maqAlert('message', 'Mensagem de teste');
	
	switch(controller){
		case "usuario":
			Usuario.init();
		break;
		case "congresso":
			Congresso.init();
		break;
		
	}
	
	Login.init();
	
});



var Usuario = {
	init: function(){
		$.each($("#annuity tr td a"), function(){
			$(this).click(function(){
				$.maqAlert('url', $(this).attr("href"));
				return false;
			})
		});		
	}
}


var Congresso = {
	
	init: function(){
		
		$.each($("#article tr td a.pagar"), function(){
			$(this).click(function(){
				$.maqAlert('url', $(this).attr("href"));
				return false;
			})
		});	
		
		// $("a[class=pagar-atigo]").click(function(){
		// 	$.maqAlert('url', $(this).attr("href"));
		// 	return false;
		// })
		
		var selectedCategory = 0;
		$("select[name=category]").change(function () {
			$("div.group").hide();
			selectedCategory = $(this).children('option:selected').val();
			if(selectedCategory!=""){
				$("#group-" + selectedCategory).show();	
			}
		});
		
		$("#articleForm input[type=file]").maqFileUpload({
			swfBgColor: "#eeeeee"
		});
		
		$('#articleForm').submit(function() {
			if ($('#articleForm div.ar_idCategory select').val() == "") {
				alert("Por favor, selecione a categoria do artigo.");
				return false;
			}
			if ($('#articleForm div.ar_idCategory select').val() != "") {
				var boolValid = false;
				$('#group-' + selectedCategory + ' label input:radio').each(function() {
					if ( $(this).is(':checked') ) {
						boolValid = true;
					}
				});
				
				if (!boolValid) {
					alert("Por favor, selecione a categoria do artigo.");
					return false;
				}
			}
			var selectedType = 0;
			$('div.ar_idType' + ' label input:radio').each(function() {
				if ( $(this).is(':checked') ) {
					selectedType = $(this).val();
				}
			});
			if (selectedType == 0) {
				alert("Por favor, selecione o tipo do artigo.");
					return false;
			}
			if ($('#articleForm #ar_ptTitle').val() == "") {
				alert("Por favor, preencha o título do artigo em português.");
				return false;	
			}
			if ($('#articleForm #ar_ptDescription').val() == "") {
				alert("Por favor, preencha a descrição do artigo em português.");
				return false;
			}
			return true;
		});
		
	}
	
}






/*

$(function(){

	// Regras de validacao
	jQuery.validator.addMethod("title", function(value, element) { 
	  return (value.length > 5);
	}, "Por favor, insira um titulo valido.");
	jQuery.validator.addMethod("unique", function(value, element) { 
		$.get(
			'/services/checkunique?' + $(element).attr('name') + '=' + value,
			function(element) {
	  			return true;			
			}
		);
	}, "Campo unico");
	
	
	// CONTENTETREE e PRESENTERTREE
	if(pageId == "contenttree" || pageId == "presentertree"){
		$("ul#tree").maqTree({
			onUpdate: function(obj){
				Tree.applyButtonsAction(obj.find("a:not([class=view])"));
			}
		});
		Tree.applyButtonsAction($("ul#tree li a:not([class=view])"));

	// USER
	}else if(pageId == "user"){
		User.applyDataFormActions();
		User.applyPasswordFormActions();
		$("table").maqTableDataEditor({
			updateService: "/admcon/user/update",
			deleteService: "/admcon/user/delete"
		});
	}
	


});


/*
 * Funcoes User
 
var User = {
	// Aplica acoes do formulario
	applyDataFormActions: function(){
		$("#dataForm").maqForm({
			onValid: function(obj){
				return true;
			},
			onSuccess: function(obj, data){
				$("#user_form").html(data);
				User.applyDataFormActions();
			}
		});		
	},
	// Aplica acoes do formulario
	applyPasswordFormActions: function(){
		$("#passwordForm").maqForm({
			onValid: function(obj){
				return true;
			},
			onSuccess: function(obj, data){
				$("#password_form").html(data);
				User.applyPasswordFormActions();
			}
		});		
	}		
}


/*
 * Controle do navegador  de conteudo

var Tree = {
	// Aplica acao nos botes de edicao
	applyButtonsAction: function(elm){
		elm.maqSimpleAjaxLink({
			onInit: function(obj){
				Modal.open(
					"",
					"Carregando...",
					function(){
						if(obj.parent().parent().parent().hasClass("opened")){
							obj.parent().parent().parent().find("a[class=view]").click();	
						}else{
							window.location.reload(true);
						}
					}
				);
			},
			onSuccess: function(obj, data){
				$("html, body").animate({scrollTop:0});				
				Modal.html(
					(obj.attr("class") == "edit" ? "Atualiza&ccedil;&atilde;o de registro" : "Inclus&atilde;o de registro"),
					data
				);
				Tree.applyFormActions()
			},
			onError: function(obj){}
		});		
	},
	// Aplica acoes do formulario
	applyFormActions: function(){
		$("#dataForm input[type=file]").maqFileUpload({
			swfBgColor: "#ffffff"
		});	
		$("#dataForm input[name=co_uri]").maqURIGenerator({source:$("input[name=co_title]")});		
		$("form button[class~=remove]").click(function(){
			if(confirm("Deseja mesmo remover este registro?")){
				var link = "/admcon/" + ($(this).hasClass("content") ? "content" : "presenter") + "/delete"; 
				var form = $(this).parent().parent();
				$(this).parent().parent().children("input[name=actionType]").attr("value", "delete");
				$.ajax({
						url: link,
						data: { id:form.children("input[name=id]").attr("value") },
						type: "POST",
						dataType: 'json',
						success: function(data){
							$.modal.close();
						},
						error: function(data){
							
						}							
					});				
			}
		})		
		$("#schedule").maqLoadHtmlContent({
			onSuccess: function(elm, data){
				elm.html(data);
				$("#schedule table").maqTableSortEditor({
					service:"/admcon/contentschedule/order/",
					orderField: "cosc_order",
					parentIdField: "cosc_idContent"
				});
				$("#schedule table").maqTableDataEditor({
					updateService : "/admcon/contentschedule/update/",
					deleteService : "/admcon/contentschedule/delete/"
				});
			}
		});
		$("#plan").maqLoadHtmlContent({
			onSuccess: function(elm, data){
				elm.html(data);
				$("#plan table").maqTableSortEditor({
					service:"/admcon/contentplan/order/",
					orderField: "copl_order",
					parentIdField: "copl_idContent"
				});
				$("#plan table").maqTableDataEditor({
					updateService : "/admcon/contentplan/update/",
					deleteService : "/admcon/contentplan/delete/"
				});				
			}
		});	
		$("#area").maqLoadHtmlContent({
			onSuccess: function(elm, data){
				elm.html(data);
				$("#area table").maqTableDataEditor({
					updateService : "/admcon/contentarea/update/",
					deleteService : "/admcon/contentarea/delete/"
				});				
			}
		});	
		
		$("#dataForm").validate();
		$("#dataForm").maqForm({
			onValid: function(obj){
				return obj.valid();
			},
			onSuccess: function(obj, data){
				$("html, body").animate({scrollTop:0});
				Modal.html(Modal.getTitle(), data);
				Tree.applyFormActions();
			}
		});			
	}		
}


/*
 * Controle da janela modal

var Modal = {
	// Abre a janela
	open: function(title, data, closeFunction){
		closeFunction = (closeFunction == undefined ? function(){ alert('close'); } : closeFunction);
		$('#modal').modal({
			closeClass: "modal-close",
			onClose: function(){
				$.modal.close();
				closeFunction();
			}
		});
		Modal.html(title, data);
	},
	// Atualiza o cointeudo da janela
	html: function(title, data){
		$("#modal div.header div").html(title);
		$("#modal div.content").html(data);
	},
	// Retorna o titulo atual da janela
	getTitle: function(){
		return $("#modal div.header div").html();
	}
}

*/



/* Login */

var Login = {
	
	init: function(){
		$("#header #login-box ul li form input.email").focus(function() {
			if ($(this).val() == "E-mail") {
				$(this).val("");
			}
		});
		$("#header #login-box ul li form input.email").focusout(function() {
			if ($(this).val() == "") {
				$(this).val("E-mail");
			}
		});
		$("#header #login-box ul li form #fakepassword").focus(function() {
			$('#fakepassword').hide();
		    $('#password').show();
		    $('#password').focus();
		});
		
		$("#header #login-box ul li form #password").blur(function() {
			if ($('#password').attr('value') == '') {
		        $('#password').hide();
		        $('#fakepassword').show();
		    }
		});
	}
	
}

