function ajusta() {
	var ie = document.all;
	var obj = $('conteudo_geral');
	if(ie) {
		var altura = document.documentElement.clientHeight;
		obj.style.height = altura+'px';
		//obj.style.width = '795px';
		if(document.documentElement.clientWidth <= 800) {
			obj.style.width = '100%';
		} else {
			obj.style.width = '805px';
		}
		//alert(obj.style.cssText);
	} else {
		obj.style.height = top.conteudo.innerHeight+'px';
		obj.style.width = '800px';
	}
	obj.style.overflow = 'auto';
}

function delegate( escope, fnc, args )
{
	return function()
	{
		fnc.apply( escope, args );
	}
}

function mais_noticias() {
	var w = window.open('noticias.busca.php', 'busca','width=680,height=460');
	w.focus();
}
function abreAlbumDaNoticia(id){
	if(window.opener.location.href == "http://www.veracruz.edu.br/noticias.busca.php"){
		window.opener.opener.location.href = "http://www.veracruz.edu.br/?frame=comunidade.album.php&abre_album="+id;
	}else{
		window.opener.location.href = "http://www.veracruz.edu.br/?frame=comunidade.album.php&abre_album="+id;
	}
	window.close();
}
function abreDocDaNoticia(id){
	if(window.opener.location.href == "http://www.veracruz.edu.br/noticias.busca.php"){
		window.opener.opener.location.href = 'http://www.veracruz.edu.br/?frame=secretaria_virtual.php&abre_item='+id;
	}else{	
		window.opener.location.href = 'http://www.veracruz.edu.br/?frame=secretaria_virtual.php&abre_item='+id;
	}
	window.close();
}
function show( id ) {
	var el = $('subitens_'+id);
	if(el) {
		el.style.display = el.style.display == '' ? 'none':'';
	}
}

function LerNoticia( id ) {
	var w=window.open('noticias.ler.php?id='+id,'noticias','width=650,height=450,scrollbars=yes,resizable=yes');
	w.focus();
}

function MenuEsquerda () {
	this.itens = [];
	this.id = '__menu_esquerda'+(Math.round(Math.random()*(new Date().getTime())));
	this.intervalo = null;
	this.tempo = 1;
	this.inicial = null;
	window[this.id] = this;
	
	this.idmenu = this.id + '__menu_flutuante';
	this.baseurl = 'paginas.php?id=';
	
	document.write('<div id="'+this.idmenu+'" onmouseover="'+this.id+'.clearHide()" onmouseout="'+this.id+'.startHide()" style="display:none; position:absolute" class="MenuFlutuante"></div>');
	this.objMenu = $(this.idmenu);
}

MenuEsquerda.prototype = {
	addItem:function(id, pai, titulo, restrito) {
		this.itens.push({id:id, pai:pai, titulo:titulo, restrito:restrito});
	},
	
	MenuFlutuante:function(id, obj) {
		/*
		var p = getPosition(obj);
		var t = getSize(obj);
		var cl = '';
		
		var itens = '';
		var filhos = this.getFilhos(id);
		if(filhos.length > 0) {
			itens += '<ul style="margin: 0px;padding:0px;">';
			for(var i=0; i<filhos.length; i++) {
				if(filhos[i].id == this.inicial) {
					cl = ' style="font-weight:bold" ';
				} else {
					cl = '';
				}
				itens += '<li style="list-style-type:none;line-height:15px; padding-left: 15px;">';
				if(filhos[i].restrito == 1) {
					itens += ' <img src="imagens/cadeado.gif" border="0" alt="cadeado" align="absmiddle" />';
				}
				itens += '<a href="javascript:'+this.id+'.GetURL('+filhos[i].id+')" '+cl+' class="Nivel3">'+filhos[i].titulo+'</a>';
				itens += '<br /></li>';
			}
			itens += '</ul>';
			this.objMenu.innerHTML = itens;
			var sy = $('conteudo_geral').scrollTop;
			setPosition(this.objMenu, p.x + t.width, p.y - sy);
			setVisible(this.objMenu, true);
		}
		*/
	},
	SaibaMais:function(id){
		var itens = '';
		var filhos = this.getFilhos(id);
		var pai = this.getPai(id).pai;
		var irmaos = this.getFilhos(pai);
		var paizao = this.getPaiMestre(id);
		
		//alert("id:"+id+" pai.id:"+pai+" paizao:"+paizao+" irmaos:"+irmaos.length+" filhos:"+ filhos.length);
		
		//if(pai == this.inicial) {
			//alert("pai:"+pai);
		if(filhos.length > 0) {
			itens = '<span style="font-family: Verdana, Arial, Helvetica, sans-serif;font-weight: bold;font-size:12px;">Veja também</span><br /><br />';
			for(var i=0; i<filhos.length; i++) {
				if(filhos[i].restrito == 1) {
					itens += ' <img src="imagens/cadeado.gif" border="0" alt="cadeado" align="absmiddle" />';
				}
				itens += '<a href="javascript:'+this.id+'.GetURL('+filhos[i].id+')">'+filhos[i].titulo+'</a>';
				itens += '<br />';
			}
		}else if(irmaos.length > 0 && pai != paizao){
			itens = '<span style="font-family: Verdana, Arial, Helvetica, sans-serif;font-weight: bold;font-size:12px;">Veja também</span><br /><br />';
			for(var i=0; i<irmaos.length; i++) {
				if(irmaos[i].id != id){
					if(irmaos[i].restrito == 1) {
						itens += ' <img src="imagens/cadeado.gif" border="0" alt="cadeado" align="absmiddle" />';
					}
					itens += '<a href="javascript:'+this.id+'.GetURL('+irmaos[i].id+')">'+irmaos[i].titulo+'</a>';
					itens += '<br />';
				}
			}
		}
		//}
		return itens;
	},
	startHide:function() {
		clearTimeout(this.interval);
		this.interval = setTimeout(this.id+'.hideAll()', this.tempo * 1000);
	},
	
	hideAll:function() {
		clearTimeout(this.interval);
		setVisible(this.objMenu, false);
	},
	
	clearHide:function() {
		clearTimeout(this.interval);
	},
	
	setBold:function( id ) {
		var al = $('linkMenu'+id);
		if(al) {
			al.style.cssText = 'font-weight:bold; color:#000000';
		}
		var it = this.getPai( id );
		if(it != null) {
			this.setBold( it.pai );
		}
	},
	
	MontaSubmenu:function(pai) {
		var paizao = this.getPaiMestre( pai );
		var filhos = this.getFilhos( paizao );
		
		if(filhos.length > 0) {
			var objMestre = $('menu'+paizao);
			if(objMestre) {
				var subitem = '<ul id="submenu'+paizao+'" class="subitens">';
				
				for(var i=0; i<filhos.length; i++) {
					subitem += '<li onmouseover="'+this.id+'.hideAll(); '+this.id+'.MenuFlutuante('+filhos[i].id+', this)" onmouseout="'+this.id+'.startHide()">';
					if(filhos[i].restrito == 1) {
						subitem += ' <img src="imagens/cadeado.gif" border="0" alt="cadeado" align="absmiddle" />';
					}
					subitem += '<a href="javascript:'+this.id+'.GetURL('+filhos[i].id+')" class="Nivel2" id="linkMenu'+filhos[i].id+'">'+filhos[i].titulo+'</a>';
					subitem += '<br /></li>';
				}
				subitem += '</ul>';
				
				objMestre.innerHTML += ( subitem );
			}
		}
		this.inicial = pai;
		this.setBold( pai );
	},
	
	getFilhos:function( id ) {
		var filhos = new Array();
		for(var i=0; i<this.itens.length; i++) {
			if(this.itens[i].pai == id) {
				filhos.push( this.itens[i] );
			}
		}
		return filhos;
	},
	
	getPai:function( id ) {
		for(var i=0; i<this.itens.length; i++) {
			if(id == this.itens[i].id) {
				return this.itens[i];
			}
		}
		return null;
	},
	getPaiMestre:function( id ) {
		var it = null;
		while(1) {
			it = this.getPai( id );
			if(it == null) {
				return id;
			} else {
				id = it.pai;
			}
		}
	},
	
	getItem:function(id) {
		for(var i=0; i<this.itens.length; i++) {
			if(this.itens[i].id == id) {
				return this.itens[i];
			}
		}
		return null;
	},
	
	GetURL:function(id) {
		var it = this.getItem(id);
		if(it.restrito == 1 && LOGADO != 1) {
			LoginDinamico.onLoginSuccess = delegate( this, this.LoginSucess, [id] );
			LoginDinamico.show();
			return
		}
		var url = this.baseurl+id;
		location.href=url;
	},
	
	LoginSucess : function( id )
	{
		LOGADO = 1;
		this.GetURL( id );
	}
}

function checa(o, m) {
	if(o.value.length >= m) {
		o.value = o.value.substr(0, m);
	}
}

function min_max(o, f, mi, ma) {
	var s = o.value;
	if(s.length < mi) {
		alert(f+' deve ter ao menos '+mi+' caracteres e está com '+s.length);
		o.focus();
		return false;
	}
	if(s.length > ma) {
		alert(f+' deve ter no máximo '+ma+' caracteres e está com '+s.length);
		o.focus();
		return false;
	}
	return true;
}


/**
 * mostra a tela de login dinamicamente
 */
var LoginDinamico = {
	mp:{},
	obj:null,
	altura:210,
	divname:'tela_login',
	postLoginAction:'',
	
	show:function() {
		var d = this.setObj();
		if(d) {
			var tx = this.mp.x;
			var ty = this.mp.y;
			var s = {};
			if(document.all) {
				s.height = document.body.offsetHeight;
			} else {
				s = getSize(document.body);
			}

			while((ty + this.altura) > s.height) {
				ty--;
			}
			
			setPosition(d, tx, ty);
			setVisible(d,true);
		} else {
			alert('NO DIV CALLED "'+this.divname+'"');
			return false;
		}
	},
	
	hide:function() {
		var r = this.setObj();
		if(r) {
			setVisible(r,false);
		}
	},
	
	setMousePosition:function(e) {
		LoginDinamico.mp = getMousePosition(e);
	},
	
	setObj:function() {
		if(this.obj == null) {
			this.obj = $(this.divname);
		}
		return this.obj;
	},
	
	doLogin:function() {
		var ponte = new Ajax;
		ponte.addParameter('txtLogin', $('email').value);
		ponte.addParameter('txtSenha', $('senha').value);
		
		var listener = {
			type:'load',
			execute:function( o ) {
				if(o.responseText == 1) {
					LOGADO = 1;
					LoginDinamico.onLoginSuccess();
				} else {
					LOGADO = '';
					LoginDinamico.onLoginFail();
				}
			}
		}
		
		ponte.addListener( listener );
		ponte.send('login.dinamico.php');
	},
	
	onLoginSuccess:function() {
		
	},
	
	onLoginFail:function() {
		alert('Falha no login:\nE-mail ou senha incorreta');
	}
}

function checaUnidade() {
	var p = location.href.split('?');
	if(p[1]) {
		var args = p[1].split('&');
		for(var i=0; i<args.length; i++) {
			var v = args[i].split('=');
			if(v[0] == 'unidade') {
				// top.topFrame.updateBarra( v[1] );
				return;
			}
		}
	}
}

function ChecaAcesso( id , restrito )
{
	if(LOGADO != 1 && restrito == 1)
	{
		var result = function(id)
		{
			location.href = 'paginas.php?id=' + id ;
		}
		
		LoginDinamico.onLoginSuccess = delegate( null, result, [id] );
		LoginDinamico.show();
	} else {
		location.href = 'paginas.php?id=' + id ;
	}
}

addEvent(document,'mousedown',LoginDinamico.setMousePosition);
addEvent(window,'load', checaUnidade);