var httpRequestObj;
var imagensRollOverRefGenerica;
var imagensRollOverObj;
var imagensRollOverProdutosObj;
var objetosVisibleObj;

function CacheImagens() {
	cacheArr = new Array(
					"../imagens/destaque.selecao.seta-v.gif",
					"../imagens/seta-fechada.gif",
					"../imagens/seta-aberta.gif",
					"../imagens/seta-aberta-hover.gif",
					"../imagens/seta-fechada-hover.gif",
					"../imagens/ponto-vermelho.gif",
					"../imagens/produtos-icone-foto-hover.gif",
					"../imagens/produtos-icone-medidas-h.gif",
					"../imagens/seta-produtos-voltar-h.gif"
					);
	imageCache = new Array();
	
	for (a = 0; a < cacheArr.length; a++) {
		imageCache[a] = new Image();
		imageCache[a].src = cacheArr[a];
	}
}

function setOpacity(obj, nivel) {
	cssObj = obj.style;
	cssObj.opacity = (nivel / 100); 
	cssObj.MozOpacity = (nivel / 100); 
	cssObj.KhtmlOpacity = (nivel / 100); 
	cssObj.filter = "alpha(opacity=" + nivel + ")";
};


function Janela(e, w, h, p) {
	try {
		param = "";
		if (p != null)
			param = ", " + p;

		centrox = (screen.width / 2) - (w / 2);
		centroy = (screen.height / 2) - (h / 2);

		if (navigator.appName.indexOf("Opera") != -1) {
			test = (screen.height - (screen.height * 0.25));
			centroy = (test / 2) - (h / 2);
		}

		window.open(e.href, '', "left=" + centrox + ", top=" + centroy + ", width=" + w + ", height=" + h + "" + param);

		if (window.event)
			event.returnValue = false;
	}
	catch(e) {
		if (window.event)
			event.returnValue = true;
		
	}
	return false;
}
function Janela2(e, w, h, p) {
	try {
		param = "";
		if (p != null)
			param = ", " + p;

		centrox = (screen.width / 2) - (w / 2);
		centroy = (screen.height / 2) - (h / 2);

		if (navigator.appName.indexOf("Opera") != -1) {
			test = (screen.height - (screen.height * 0.25));
			centroy = (test / 2) - (h / 2);
		}

		window.open(e, '', "left=" + centrox + ", top=" + centroy + ", width=" + w + ", height=" + h + "" + param);

		if (window.event)
			event.returnValue = false;
	}
	catch(e) {
		if (window.event)
			event.returnValue = true;
		
	}
	return false;
}

function ValidarFormulario(Form) {
	Campos = Form.getElementsByTagName("input");
	for (a = 0; a < Campos.length; a++) {
		if (Campos[a].type == "text" && Campos[a].getAttribute("obrigatorio") != null && Campos[a].value.length == 0) {
			alert("Campo de preenchimento obrigatório.");
			Campos[a].focus();
			return false;
		}
	}
	
	Campos = Form.getElementsByTagName("select");
	for (a = 0; a < Campos.length; a++) {
		if (Campos[a].getAttribute("obrigatorio") != null && Campos[a].options[Campos[a].selectedIndex].value.length == "") {
			alert("Campo de preenchimento obrigatório.");
			Campos[a].focus();
			return false;
		}
	}
	
	Campos = Form.getElementsByTagName("textarea");
	for (a = 0; a < Campos.length; a++) {
		if (Campos[a].getAttribute("obrigatorio") != null && Campos[a].value.length == 0) {
			alert("Campo de preenchimento obrigatório.");
			Campos[a].focus();
			return false;
		}
	}
	return true;
}


function ajaxRequest(url, metodo) {

	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open((metodo) ? "POST" : "GET", url, true);
	http_request.send(null);

	
}

function alertContents() {

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {

			var xmlDoc = http_request.responseXML;

			alert(xmlDoc.getElementsByTagName("Item").length);


		} else {
			alert('There was a problem with the request.');
		}
	}

}

function httpRequestClass() {

	this.req = null;
	this.XMLHttpFactories = [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];

	this.sendRequest = function(url, callback, postData) {
		this.req = this.createXMLHTTPObject();
		if (!this.req)
			return;
		var method = (postData) ? "POST" : "GET";
		this.req.open(method,url,true);
		this.req.setRequestHeader('User-Agent','XMLHTTP/1.0');
		if (postData)
			this.req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		this.req.onreadystatechange = function () {
			if (httpRequestObj.req.readyState != 4)
				return;
			if (httpRequestObj.req.status != 200 && httpRequestObj.status != 304) {
				return;
			}
			callback(httpRequestObj.req);
		}
		if (this.req.readyState == 4)
			return;
		this.req.send(postData);
	}
	this.createXMLHTTPObject = function() {
		var xmlhttp = false;
		for (var i=0;i< this.XMLHttpFactories.length;i++) {
			try {
				xmlhttp = this.XMLHttpFactories[i]();
			}
			catch (e) {
				continue;
			}
		}
		return xmlhttp;
	}
}


function imagensRollOverClass() {

	this.ImagemObj = null;
	this.Imagens = new Array();
	this.ImagensCache = new Array();

	this.Links = new Array();


	this.ImagensSrc = null;
	this.ImagensSrc_Prev = null;
	this.ImagensSrc_Atual = null;
	this.Opacidade = 0;
	this.OverOpacityLevel = 0;
	this.Timer = null;
	this.TimerSlideShow = null;
	this.SlideShowEnabled = false;
	this.SlideShowEnabled__ = false;
	this.SlideShowIndex = 0;
	this.SlideShowTimeSpan = 0;

	this.IndexSelecionado = 0;

	this.Init = function() {

		// preload / cache

		if (this.Imagens.length == 0)
			return false;

		for (var a = 0; a < this.Imagens.length; a++) {
			this.ImagensCache[a] = new Image();
			this.ImagensCache[a].src = this.Imagens[a];
		}

		this.ImagensSrc = this.ImagensCache[this.IndexSelecionado].src;
		this.ImagensSrc_Atual = this.ImagensSrc;

		if (this.SlideShowEnabled == true) {
			this.SlideShowEnabled__ = true;
			this.SlideShow();
		}

		this.Acao();
		

	}
	this.Add = function(Img) {
		this.Imagens[this.Imagens.length++] = Img;
	}

	this.RollOver = function(Index, RefObj) {
		imagensRollOverRefGenerica = RefObj;

		if (RefObj.ImagensCache[Index] != null && RefObj.ImagensCache[Index].src != null) {
		
			if (RefObj.ImagensSrc != RefObj.ImagensCache[Index].src) {
				setOpacity(RefObj.ImagemObj, 80);
			}

			RefObj.ImagensSrc = RefObj.ImagensCache[Index].src;
			RefObj.SildeShowEnabled__ = false;
			clearInterval(RefObj.TimerSlideShow);
			RefObj.TimerSlideShow = null;

		}
	}

	this.RollOut = function(RefObj) {
		imagensRollOverRefGenerica = RefObj;
		
		if (RefObj.ImagensSrc != this.ImagensSrc_Atual) {
			setOpacity(RefObj.ImagemObj, 80);
			RefObj.ImagensSrc = this.ImagensSrc_Atual;
		}

		if (RefObj.SlideShowEnabled) {
			RefObj.SlideShowEnabled__ = true;
			RefObj.SlideShow();
		}
	}

	this.Acao = function() {
		var Objeto = this;
		this.Timer = setTimeout(function() {Objeto.Acao()}, 80);

		this.Opacidade = parseInt((this.ImagemObj.style.opacity * 100) + (this.OverOpacityLevel - (this.ImagemObj.style.opacity * 100)) / 6);
		setOpacity(this.ImagemObj, this.Opacidade);
		
		if (this.ImagensSrc != null && this.ImagensSrc != this.ImagensSrc_Prev) {
			this.OverOpacityLevel = 100;
			this.ImagemObj.src = this.ImagensSrc;
			setOpacity(this.ImagemObj, 0);
		}
		this.ImagensSrc_Prev = this.ImagensSrc;
	}
	this.SlideShow = function() {
		var Objeto = this;
		
		if (this.TimerSlideShow == null) {
			this.TimerSlideShow = setInterval(function() {Objeto.SlideShow()}, 60);
		}
		

		this.Opacidade = parseInt((this.ImagemObj.style.opacity * 100) + (this.OverOpacityLevel - (this.ImagemObj.style.opacity * 100)) / 6);
		setOpacity(this.ImagemObj, this.Opacidade);
		
		if (this.SlideShowTimeSpan > 120 && this.SlideShowEnabled__) {
			this.OverOpacityLevel = 100;
			this.ImagemObj.src = this.SlideShowNextPic();
			setOpacity(this.ImagemObj, 0);
			this.SlideShowTimeSpan = 0;
		}
		this.SlideShowTimeSpan++;
	}
	this.SlideShowNextPic = function() {
		try {
			if ((this.SlideShowIndex+1) < this.ImagensCache.length) {
				this.SlideShowIndex++;
			}
			else {
				this.SlideShowIndex = 0;
			}
			return this.ImagensCache[this.SlideShowIndex].src;
		}
		catch (e) {
			return null;
		}
	}

}


function objetosVisibleClass() {
	this.Itens = new Array();
	this.Add = function(Botao, Container) {
		Botao.className = "";
		Container.style.display = "none";
		this.Itens[this.Itens.length++] = new Array(Botao, Container);
	}
	this.Click = function(Index) {
		for (var a = 0; a < objetosVisibleObj.Itens.length; a++) {
			objetosVisibleObj.Itens[a][0].className = "none";
			objetosVisibleObj.Itens[a][1].style.display = "none";
		}
		objetosVisibleObj.Itens[Index][0].className = "Selecionado";
		objetosVisibleObj.Itens[Index][1].style.display = "block";
	}
	this.VisHid = function(Obj, Classe, ClasseDefault) {
		if (ClasseDefault == null) {
			Obj.className = (Obj.className == "") ? Classe : "";
		}
		if (ClasseDefault != null) {
			Obj.className = (Obj.className == ClasseDefault) ? Classe : ClasseDefault;
		}
	}
}

function ProdutosListagemLateralInit() {

	var UL = document.getElementById("ULProdutosMenuLateral");

	var ULFilhos = UL.getElementsByTagName("UL");

	for (var a = 0; a < ULFilhos.length; a++) {
		//if (ULFilhos[a].getAttribute("class") != null) {
			ULFilhos[a].style.display = "none";
		//}
	}
}

// calls



function OnLoad() {

	if (document.getElementById("ULProdutosMenuLateral") != null) {
		ProdutosListagemLateralInit();
	}
	

}

CacheImagens();