//Verifico se ajax concluido
function ajaxDone(acao){
	var intervaloo = window.setInterval(liroliro, 100);
	function liroliro(){
		if(ajaxStatus == 200){
			clearInterval(intervaloo);
			if(typeof(acao) == "function")
				acao.call();
		}
	}
}

//Verifico se adicionou/editou
function add_bt(){
	showLoader('block');
	var intervalo = window.setInterval(lerolero, 100);
	function lerolero() {
		var show = frames['aFr'].aadd;
		var blAjax = frames['aFr'].blAjax;
		var toid = frames['aFr'].toid;
		var tourl = frames['aFr'].tourl;
		var tourltime = frames['aFr'].tourltime;
		var status = frames['aFr'].status;
		var message = frames['aFr'].message;
		if(typeof message != 'undefined'){
			clearInterval(intervalo);
			showLoader('none');
			
			showNotif(status, message);
			if(typeof tourl != 'undefined' && tourl != ''){
				if(typeof blAjax != 'undefined' && blAjax != ''){
					ajax(tourl, toid, 'new');
				} else{
					window.setTimeout(function(){
						document.location = tourl;
					}, tourltime);
				}
			}
		}
	}
}

//Mostro e escondo loader (EDITAR O ID DO IFRAME SE NECESSARIO)
function showLoader(status){
	if(status == 'block'){
		var posScroll = getScrollTop();
		var height = getHeight();
		var height2 = getHeight2('divSite');
		if(height > height2) height2 = height;
		var margemTop = posScroll + Math.ceil(height/2) - 50;
		if(margemTop < 0) margemTop = 0;
		if(margemTop > 0)document.getElementById('allLoader').style.marginTop = margemTop+"px";
		document.getElementById('bckTransparent').style.height = height2+"px";
	} document.getElementById('divLoader').style.display = status;
}

function showNotif(cName, msg){
	if(top.document.getElementById('divNotification')){
		top.document.getElementById('divNotification').style.display = 'none';
		top.$("#divNotification").fadeIn(1000);
		top.document.getElementById('divNotification').className = 'notificacao '+cName;
		top.document.getElementById('divNotification').innerHTML = msg;
		
		if(cName == 'success'){
			window.setTimeout(function(){
				top.$("#divNotification").fadeOut(1000);
			}, 5000);
		}
	}
}

//Position fixed quando a tela desce
function fixedTop(id){
	posElem = getPosicaoElemento(id).top;
	window.onscroll = function(){
		var posScroll = getScrollTop();
		if(posScroll >= posElem){
			document.getElementById(id).style.position = 'fixed';
			document.getElementById(id).style.top = '0';
			document.getElementById(id).style.zIndex = '2';
		} else{
			document.getElementById(id).style.position = '';
			document.getElementById(id).style.top = '';
			document.getElementById(id).style.zIndex = '';
		}
	}
}

function getPosicaoElemento(elemID){
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	while(offsetTrail){
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if(navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined"){
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	
	return {left:offsetLeft, top:offsetTop};
}

function getWidth(){
	return window.innerWidth ? window.innerWidth : 
	    document.documentElement ? document.documentElement.clientWidth : 
	    document.body ? document.body.clientWidth : 
	    window.screen.width;
}

function getHeight(){
	return window.innerHeight ? window.innerHeight : /* Para non-IE */
	     document.documentElement ? document.documentElement.clientHeight : /* IE 6+ */
	     document.body ? document.body.clientHeight : /* IE 4 Compativel */
	     window.screen.height; /* Outros (Nao eh browser window size, mas screen size) */
}

function getScrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function getScrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function getHeight2(id){
	return document.getElementById(id).offsetHeight;
}

function getWidth2(id){
	return document.getElementById(id).offsetWidth;
}

//Foco da Pesquisa
function foco(t, id){
	var strPesquisa = document.getElementById(id).value;
	if(strPesquisa == t){
		document.getElementById(id).value = '';
		document.getElementById(id).style.color = '#333333';
	} else if(strPesquisa == ''){
		document.getElementById(id).value = t;
		document.getElementById(id).style.color = '#888888';
	}
}

//Mostro e escondo
function mostra(id){
	if(document.getElementById(id).style.display == 'block')
		document.getElementById(id).style.display = 'none';
	else
		document.getElementById(id).style.display = 'block';
}
