// JavaScript Document
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

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);
}

function atualiza_dados(arquivo, elementoID, tempoR) {
        
var xhri = xhrRequest('html');
xhr[xhri].open('GET', arquivo, 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].onreadystatechange = function() {
        
if (xhr[xhri].readyState == 1) {
        document.getElementById(elementoID).innerHTML = "<div id='carregando'><img src='http://jconnection.com.br/siteJC/imgs/carreg_ped.gif' /></div>";
        }
if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
        document.getElementById(elementoID).innerHTML = xhr[xhri].responseText;
        xi[xhri] = 1;
        xhr[xhri] = null;
        }
};
xhr[xhri].send(null);
setTimeout("atualiza_dados('" + arquivo + "','" + elementoID + "','" + tempoR + "')", tempoR);
}

function addEvent( obj, type, fn ) {
        if (obj.addEventListener) {
                obj.addEventListener( type, fn, false );
        }
        else if (obj.attachEvent) {
                obj["e"+type+fn] = fn;
                obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
                obj.attachEvent( "on"+type, obj[type+fn] );
        }
        else {
                obj["on"+type] = obj["e"+type+fn];
        }
}

function paraInitPage(){
	loadSlideshow();
	$("#form_pedido").css("display", "none");
	$("#mensagem").css("display", "none");
	$("#erro").css("display", "none");
	$("#men_player_pedido").click(pedido);
	$("#ped_enviar").click(add_pedido);
	$("#ped_nome").blur(function(){if($(this).attr("value") != ""){ $(this).addClass("verde"); } else { $(this).addClass("vermelha"); }});
	$("#ped_recado").blur(function(){if(($(this).attr("value") != "") && ($(this).attr("value").length <= 2 ) ){ $(this).addClass("verde"); } else { $(this).addClass("vermelha"); }});
}

function loadSlideshow(){
	$(".slideshow").load("./slides/imagens.html", function(){	$(".slideshow").cycle({ fx: 'fade', pause: true });});
}

function verificar(){
	if($("#ped_nome").attr("value") == ""){
		$("#ped_nome").addClass("vermelho");
		return false;
	}

	if($("#ped_recado").attr("value") == ""){
		$("#ped_recado").addClass("vermelho");
		return false;
	}
	
	return true;
}

function pedido(){
	$("#texto").css("display", "none");
	$("#fotodj").css("display", "none");
	$("#men_player").css("display", "none");
	$("#carregando").css("display", "block");
	setTimeout(pedido2, 2000);
}

function pedido2(){
	$("#carregando").css("display", "none");
	$("#form_pedido").css("display", "block");
}

function add_pedido(){
	if(verificar()){
		$.ajax({
			beforeSend: carregando,
			success: sucesso,
			error: mostrarErro,
			url: "http://www.jconnection.com.br/newPanel/request/addPedido.php?nome=" + escape($("#ped_nome").attr("value")) + "&pedido=" + escape($("#ped_recado").attr("value")),
		});
	}
}

function carregando(ajax){
	$("#form_pedido").css("display", "none");
	$("#carregando").css("display", "block");
}

function mostrarErro(ajax, statusText, error){
		$("#carregando").css("display", "none");
		$("#form_pedido").css("display", "none");
		$("#erro").css("display", "block");
		setTimeout(showDjbox, 10000);
}

function sucesso(data, statusText, ajax){
	if(ajax.responseText == "OK"){
		window.setTimeout(sucesso2, 2000);
	} else {
		if(ajax.responseText == "menor"){
			window.setTimeout(sucesso3, 2000);

		} else {
			window.setTimeout(sucesso4, 2000);
		}
	}
}

function sucesso2(){
	$("#carregando").css("display", "none");
	$("#form_pedido").css("display", "none");
	$("#mensagem").css("display", "block");
	setTimeout(showDjbox, 10000);
}

function sucesso3(){
	$("#carregando").css("display", "none");
	$("#form_pedido").css("display", "none");
	$("#errou").css("display", "block");
	setTimeout(pedido, 5000);
}

function sucesso4(){
	$("#carregando").css("display", "none");
	$("#form_pedido").css("display", "none");
	$("#mensagem").css("display", "block");
	setTimeout(showDjbox, 10000);
}

function showDjbox(){
	$("#carregando").css("display", "block");
	$("#mensagem").css("display", "none");
	$("#erro").css("display", "none");
	window.setTimeout(showDjbox2, 2000);
}

function showDjbox2(){
	$("#carregando").css("display", "none");
	$("#texto").css("display", "block");
	$("#fotodj").css("display", "block");
	$("#men_player").css("display", "block");
}