//-----------------------------------------------------------------
// DECLARAÇÃO VARIAVEIS
//-----------------------------------------------------------------
	var BASE_URL = "http://"+window.location.hostname+"/shop/"; // URL ABSOLUTA DO SITE
	var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
	var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
	xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE
	
//-----------------------------------------------------------------
// FUNÇÃO AJAX REQUEST
//-----------------------------------------------------------------

function xhrRequest(type) {
	if (!type) {
		type = 'html';
	}

	// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
	// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
	// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
	var xhrsend = xi.length;
	
	// GO THROUGH AVAILABLE xi VALUES
	for (var i=0; i<xi.length; i++) {

		// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
		if (xi[i] == 1) {
			xi[i] = 0;
			xhrsend = i;			
			break;
		}
	}

	// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
	xi[xhrsend] = 0;

	// SET UP THE REQUEST
	if (window.ActiveXObject) {
		try {
			xhr[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhr[xhrsend] = new XMLHttpRequest();
		if (xhr[xhrsend].overrideMimeType) {
			xhr[xhrsend].overrideMimeType('text/' + type);
		}
	}
	return (xhrsend);
}

//-------------------------------------------------------------------
// FUNÇÃO GLOBAL, PARA DIMINUIR O GETELEMENTBYID, COMO NO MYSQLQUERY
//-------------------------------------------------------------------

function getById(elementoID) { 
	return document.getElementById(elementoID)
}

//----------------------------------------------------------------
// FUNÇÃO PEGAR COORDENADAS MOUSE X,Y
//----------------------------------------------------------------

	var IE = document.all ? true:false; // Variavel detecta navegador	
	var coodX = 0
	var coodY = 0
	
if (!IE){
	document.addEventListener('mousemove', getMouseXY, true);
} else {
	// Set-up to use getMouseXY function onMouseMove
	document.onmousemove = getMouseXY;
}
 
// Main function to retrieve mouse x-y pos.s
 
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    coodX = event.clientX + document.documentElement.scrollLeft;
    coodY = event.clientY + document.documentElement.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    coodX = e.pageX;
    coodY = e.pageY;
  }
  // catch possible negative values in NS4
  if (coodX < 0){coodX = 0}
  if (coodY < 0){coodY = 0}
  return true;
}

//----------------------------------------------------------------
// FUNÇÃO NAVEGAÇÃO GERAL COM HISTÓRICO - SEM ELEMENTOS ID´S
//----------------------------------------------------------------

function requisicao(crt)
{
	var xhri = xhrRequest('html');
	var elemento = getById("carrega-conteudo");
	
	xhr[xhri].onreadystatechange = function()
	{
		if (xhr[xhri].readyState == 1) {
			elemento.innerHTML =  "<div class=\"carregando\"><img src=\"http://buscagratis.com/shop/img/carregando.gif\" /> Aguarde, carregando dados...</div>";
		}
		if (xhr[xhri].readyState == 4){
			if(xhr[xhri].status == 200) {
				var texto = xhr[xhri].responseText;
				texto = texto.replace(/([^\x01-\x7E])/g,function(word){return'&#'+word.charCodeAt(0)+ ';';});
				elemento.innerHTML = texto;
				xi[xhri] = 1;
				xhr[xhri] = null;
				// Analytics Monitoramento para AJAX
				pageTracker._trackPageview(crt);
			} else{
				setTimeout("requisicao('" + crt + "')", 1000);
			}
		}
	};
	xhr[xhri].open("GET", BASE_URL+"ajax/conteudo_nav.php?act="+crt, true);
	xhr[xhri].setRequestHeader("encoding", "ISO-8859-1");
	xhr[xhri].setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    xhr[xhri].setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    xhr[xhri].setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
    xhr[xhri].setRequestHeader("Pragma", "no-cache");
	xhr[xhri].send(null);
}

//-----------------------------------------------------------------------------------------
// FUNÇÃO ENVIAR FORMULARIOS - PASSANDO ID, ARQUIVO E TEMPO
//-----------------------------------------------------------------------------------------

function requisicao_post(crt,elementoID,formID)
{
	var xhri = xhrRequest('html');
	var elementosFormulario = getById(formID).elements;
    var qtdElementos = elementosFormulario.length;
    var queryString = "";
    var elemento;
	
	//Cria uma funcao interna para concatenar os elementos do form
    this.ConcatenaElemento = function(nome,valor) {
		if (queryString.length>0) {
			queryString += "&";
		}
		queryString += encodeURIComponent(nome) + "=" + encodeURIComponent(valor);
	};
	
	//Loop para percorrer todos os elementos
	for (var i=0; i<qtdElementos; i++) {
		//Pega o elemento
        elemento = elementosFormulario[i];
		if (!elemento.disabled) {
			//Trabalha com o elemento caso ele nao esteja desabilitado
            switch(elemento.type) {
				//Realiza a acao dependendo do tipo de elemento
                case 'text': case 'password': case 'hidden': case 'textarea':
                    this.ConcatenaElemento(elemento.name,elemento.value);
                    break;
                case 'select-one':
                    if (elemento.selectedIndex>=0) {
                        this.ConcatenaElemento(elemento.name,elemento.options[elemento.selectedIndex].value);
                    }
                    break;
                case 'select-multiple':
                    for (var j=0; j<elemento.options.length; j++) {
                        if (elemento.options[j].selected) {
                            this.ConcatenaElemento(elemento.name,elemento.options[j].value);
                        }
                    }
                    break;
                case 'checkbox': case 'radio':
                    if (elemento.checked) {
                        this.ConcatenaElemento(elemento.name,elemento.value);
                    }
                    break;
            }
        }
    }
    
    xhr[xhri].onreadystatechange = function()
	{
	
		if (xhr[xhri].readyState==1) {
			getById(elementoID).innerHTML =  "<div class=\"carregando\"><img src=\"http://buscagratis.com/shop/img/carregando.gif\" /> Aguarde, enviando dados...</div>";
		}
		if (xhr[xhri].readyState == 4){
			if (xhr[xhri].status == 200){
				getById(elementoID).innerHTML = xhr[xhri].responseText;
				xi[xhri] = 1;
				xhr[xhri] = null;
			} else {
				setTimeout("requisicao_post('" + crt + "','" + elementoID + "','" + formID + "')", 1000);
			}
		}
	};
	xhr[xhri].open("POST", BASE_URL+"ajax/conteudo_form.php?crt="+crt, true);
	xhr[xhri].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr[xhri].send(queryString);

}

//-----------------------------------------------------------------------------------------
// FUNÇÃO PARA CARREGAR PERFIL
//-----------------------------------------------------------------------------------------

function redireciona(crt,elementoID){
	
	var xhri = xhrRequest('html');
	var elemento = getById(elementoID);
	
	xhr[xhri].onreadystatechange = function(){
		if (xhr[xhri].readyState == 1) {
			elemento.innerHTML = "<img src=\"http://buscagratis.com/shop/img/carregando.gif\" />";
		}
		if (xhr[xhri].readyState == 4){
			if (xhr[xhri].status == 200) {
				var texto = xhr[xhri].responseText;
				texto = texto.replace(/([^\x01-\x7E])/g,function(word){return'&#'+word.charCodeAt(0)+ ';';});
				elemento.innerHTML = texto;
				xi[xhri] = 1;
				xhr[xhri] = null;
			} else {
				setTimeout("perfil('" + crt + "','" + elementoID + "')", 2000);
			}
		}
	};
		
	elemento.style.display = 'block';
	elemento.style.left = (coodX - 10)+"px";
	elemento.style.top = (coodY + 10)+"px";

	xhr[xhri].open('GET', BASE_URL+"ajax/redireciona.php?crt="+crt, true);
	xhr[xhri].setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xhr[xhri].setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xhr[xhri].setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xhr[xhri].setRequestHeader("Pragma", "no-cache");
	xhr[xhri].send(null);
}

//--------------------------------------------------------------------------------------
// FUNÇÃO FECHAR FECHAR POP-UPS
//--------------------------------------------------------------------------------------
// Função que fecha o pop-up ao clicar no link fechar
function fechar(elementoID){
	getById(elementoID).style.display = 'none';
	getById(elementoID).innerHTML = "";
}
// Aqui abrimos a janela
function abrir(elementoID){
	var elemento = getById(elementoID);
	if (elemento.style.display != 'block'){
		elemento.style.display = 'block';
		elemento.style.left = (coodX -10) + "px";
		elemento.style.top = (coodY + 10)  + "px";
	}else {
		elemento.style.display = 'none'
	}
}
