// JavaScript Document
var browser = null;
var g_id = null;
var g_img = null;
var g_title = null;
var g_id_anterior = null;
var destaque = null;
var to_slowmotion = null;
var g_indice = 0;

function createRequest()
{
  var request = null;
  try
  {
    request = new XMLHttpRequest();
	  browser = "other";
  }
  catch (trymicrosoft)
  {
    try
	{
      request = new ActiveXObject("Msxml2.XMLHTTP");
	  browser = "ie";
    }
	catch (othermicrosoft)
	{
      try
	  {
        request = new ActiveXObject("Microsoft.XMLHTTP");
	  browser = "ie";
      }
	  catch (failed)
	  {
        request = null;
      }
    }
  }

  if (request == null) {
    alert("Erro ao criar o objeto Ajax!");
  } else {
    return request;
  }
}

var requestDestaque = createRequest();
var requestConteudo = createRequest();

function replaceText(el, text)
{
  if (el != null)
  {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

function clearText(el)
{
  if (el != null)
  {
    if (el.childNodes)
	{
      for (var i = 0; i < el.childNodes.length; i++)
	  {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

function getText(el)
{
  var text = "";
  if (el != null)
  {
    if (el.childNodes)
	{
      for (var i = 0; i < el.childNodes.length; i++)
	  {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null)
		{
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}

function iniciaImagemDestaque(id) {
	g_id_anterior = id;
}

function mudaDestaque(id, img, title)
{
	g_id = id;
	g_img = img;
	g_title = title;
	document.getElementById("img_prod_grande").src = '../imagens/px_transp.gif';
	var url = img;
	requestDestaque.open("GET", url, true);
	requestDestaque.onreadystatechange = updateDestaque;
	requestDestaque.send(null);
}

function updateDestaque()
{
	if (requestDestaque.readyState == 4)
	{
		if (requestDestaque.status == 200)
		{
			//Muda a listagem das imagens, faz a imagem selecionada desaparecer da listagem
			/*document.getElementById('imagem_'+g_id_anterior).style.display = "block";
			document.getElementById('imagem_'+g_id).style.display = "none";*/
			document.getElementById('imagem_'+g_id_anterior).className = "";
			document.getElementById('imagem_'+g_id).className = "imagem_destaque";
			g_id_anterior = g_id;
			//Muda imagem
			document.getElementById("img_prod_grande").src = g_img;
			document.getElementById("img_prod_grande").title = g_title;
			//Faz efeito de fade
			initImage();
		}
		else
			alert("Erro! Status de request: " + requestDestaque.status);
	}
}


function iniciaSlowMotion(total) {
	// chama função que troca imagem
	mudaDestaque(g_imagens_id[g_indice], g_imagens_img[g_indice], g_imagens_title[g_indice]);
	// atualiza índice
	g_indice++;
	// se for a última imagem a próxima deverá ser a primeira
	if (total == g_indice) g_indice = 0;
	
	to_slowmotion = window.setTimeout("iniciaSlowMotion(" + total +")", 5000);
}

function stopSlowMotion() {
	if (to_slowmotion) { clearTimeout(to_slowmotion); }
}

function image_next(total) {
	stopSlowMotion();
	
	// atualiza índice
	g_indice++;
	// se for a última imagem a próxima deverá ser a primeira
	if (total == g_indice) g_indice = 0;

	// chama função que troca imagem
	mudaDestaque(g_imagens_id[g_indice], g_imagens_img[g_indice], g_imagens_title[g_indice]);
}

function image_previous(total) {
	stopSlowMotion();
	
	// atualiza índice
	g_indice--;
	// se for a última imagem a próxima deverá ser a primeira
	if (g_indice < 0) g_indice = total - 1;

	// chama função que troca imagem
	mudaDestaque(g_imagens_id[g_indice], g_imagens_img[g_indice], g_imagens_title[g_indice]);
}


// somente números
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
// somente números - fim

// função para abrir pop-up Publicidade
function popup_publicidade(endereco, nome)
{
	var larguraTela = screen.width-10;
	var alturaTela = screen.height-87;
	window.open(endereco,nome,'width='+larguraTela+',height='+alturaTela+',top=0, left=0,toolbar=no,location=center,status=yes,menubar=no,scrollbars=yes,resizable=yes')
}

// função para abrir pop-up News
function popup_news(endereco, nome)
{
	var larguraTela = screen.width-10;
	var alturaTela = screen.height-87;
	window.open(endereco,nome,'width='+larguraTela+',height='+alturaTela+',top=0, left=0,toolbar=no,location=center,status=yes,menubar=no,scrollbars=yes,resizable=yes')
}