//### Dialog ###
function OpenDialog() {
	$("#modalDialog").overlay().load();
}

function CloseDialog() {
	$("#modalDialog").overlay().close();
}

function LoadInDialog(href, dict) {
	$('#modalContent').html('Carregando...');
	if(dict === undefined){
		dict = {};
	}
	$('#modalContent').load(href, dict);
}

//### Acesso requerido ###
function VerificaSessao(href){
	$.get('sessao_check.php', {'href': href}, function(data) {	
		switch (data){
			case 'ok':
				document.location = href;
				break;
			case 'login':
				LoadInDialog('sessao_login.php?href='+href);
				OpenDialog();
				break;
			default:
				alert('Erro: ' + data);
		}
	});
}

//### Download ###
function Download(href, arquivo_id){
	$.get('download_check.php', {'href': href}, function(data) {	
		switch (data){
			case 'ok':
				document.location = 'download_get.php?href='+href;
				break;
			case 'not_enough':
				LoadInDialog('sessao_falta_permissao.php?arquivo_id='+arquivo_id);
				OpenDialog();
				break;
			case 'login':
				LoadInDialog('sessao_login.php?download='+href);
				OpenDialog();
				break;
			default:
				alert('Erro: ' + data);
		}
	});
}

//### Código ###
$(document).ready(function(){
	$("#modalDialog").overlay({
		mask: {color: '#ebecff', loadSpeed: 200, opacity: 0.7},
		target: '#modalDialog',
		top: 80
	});

	$(".loginEntrar").click(function(event){
		event.preventDefault();
		var href = event.target;
		LoadInDialog(href.toString());
		OpenDialog();
	});
	
	$(".download").click(function(event){
		event.preventDefault();
		var href = event.target.toString();
		var parts = href.split('=');
		var arquivo_id = parts[1];
		Download(href, arquivo_id);
	});

    $(".acessoRequerido").click(function(event){
		event.preventDefault();
		var href = event.target.toString();
		VerificaSessao(href);
		
	});

});


