

var ajax;

function $(id) {
	return document.getElementById(id);
}
function $$(tag) {
	return document.getElementsByTagName(tag);
}

function suportAjax() {
	ajax = null;
	try {
		ajax = new XMLHttpRequest();
	} catch (e) {
		try {
			ajax = new ActiveXObject("Msxml.XMLHTTP");
		} catch (e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Seu navegador não suporta AJAX!");
				return false;
			}
		}
	}
}

function criaBgBody() {
	var tagBody	= $$('body').item(0);
	var sizePage = getPageSize();
	var bgBody	= document.createElement('div');
	bgBody.setAttribute('id','bgBody');
	bgBody.style.height	= arrayPageSize[1] +"px";
	bgBody.style.width	= arrayPageSize[0] +"px";
	if(!$('bgBody')) {
		tagBody.insertBefore(bgBody, tagBody.firstChild);
	}
	criaBox();
}

function criaBox() {
	var bgBody	= $('bgBody');
	var sizePage = getPageSize();
	var box		= document.createElement('div');
	box.setAttribute('id', 'box');
	var boxTop	= arrayPageSize[1];
	var boxLeft	= arrayPageSize[0];
	/* Aqui é por 2 boxTop / 2  */
	box.style.top  = (boxTop / 10) +"px";
	box.style.left = (boxLeft / 10) +"px";
	if(!$('box')) {
		bgBody.insertBefore(box, bgBody.lastChild);
	}
}

function removeCamada() {
	var bgBody = $('bgBody');
	bgBody.parentNode.removeChild(bgBody);
}

function loading(acao) {
	if(acao == true) {
		$('box').innerHTML = '<img src="../images/loading.gif">';
	} else {
		$('box').innerHTML = '';
	}
}

function chamaPg(dir,acao) {
	suportAjax();
	criaBgBody();
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 1) {
			loading(true);
		}
		if(ajax.readyState == 4 || ajax.readyState == "complete") {
			if(ajax.status == 200) {
				loading(false);
				$('box').innerHTML = ajax.responseText;
			}
		}
	}
	var url = dir;
	url = url +"?action="+	acao;
	url = url +"&sid="+		Math.random();
	ajax.open("GET",url,true);
	ajax.send(null);
}

/* Funções de terceiros */
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 

}

