$(document).ready(function() {
	function loadXML() {
		if (window.XMLHttpRequest) {
			// code for IE7+, Firefox, Chrome, Opera,
		// Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", "includes/banner.xml", false);
	xmlhttp.send();
	xmlDoc = xmlhttp.responseXML;
	return xmlDoc;
}

var xml = loadXML();
function slide() {
	this.imagens = $("#imagens");
	this.legendas = $("#legendas");
	this.botoes = $("#botoes");
	this.posicao = 0;
	
	this.criarImagens = function() {
		var imagens = "";
		var legendas = "";
		var botoes = "";
		var link = "";
		
		var xmlImagens = xml.getElementsByTagName("imagem");
		var xmlHref = xml.getElementsByTagName("href");
		var xmlLegendas = xml.getElementsByTagName("legenda");
		
		for (var i=0; i < xmlImagens.length; i++) {
			//cria imagens.
			imagens += "<li>";
			imagens += "<img src='" + xmlImagens[i].childNodes[0].nodeValue	+ "' />";
			imagens += "</li>";
			
			//cria legendas.
			legendas += "<li>";
			legendas += "<a";
			
			//trata erro que acontece ao tentar acessar o valor da tag xml
			//quando ela está vazia.
			try {
				link = xmlHref[i].childNodes[0].nodeValue;
			}catch(e) {
				link = "";
			}
			
			//verifica se é um link normal ou se deve chamar a funcão 'popup()'.
			if(xmlHref[i].getAttribute('tipo') == 0) {
				legendas += " target='_blank' href='";
				legendas += link;
				legendas += "'";
			}else if(xmlHref[i].getAttribute('tipo') == 1) {
				legendas += " href='javascript: popup(\"800\",\"530\",\"";
				legendas += link;
				legendas += "\")'";
			}
			legendas += ">";
			legendas += xmlLegendas[i].childNodes[0].nodeValue;
			legendas += "</a>";
			legendas += "</li>";
			
			//crai botoes.
			botoes += "<li";
			if(i == 0)
				botoes += " class='over'>";
			else
				botoes += ">";
			botoes += i + 1;
			botoes += "</li>";
		}
		
		this.imagens.html(imagens);
		this.legendas.html(legendas);
		this.botoes.html(botoes);
	}
	this.criarImagens();
	this.itens = parseInt($("#imagens li").length) - 1;
	
	this.muda = function() {
		if (this.posicao < this.itens) {
			this.posicao++;
		} else {
			this.posicao = 0;
		}

		this.rotativo(this.posicao);
		// alert(this.imagens.find("li:eq(" + this.posicao +
		// ")").html());
	}
	this.mudaLink = function(id) {
		this.posicao = id -1;
		this.rotativo(id - 1);
		this.int = setInterval(function() {
			slide.muda();
		}, 5000);
	}
	this.rotativo = function(id) {
		
		$("div#fundoBranco").stop();
		
		// muda slide
		var imagens = this.imagens;
		var legendas = this.legendas;
		$("div#fundoBranco").animate({
			opacity: 1
		}, 300, function(){
			$(".itens li").css('display', 'none');
			imagens.find("li:eq(" + id + ")").css('display', 'block');
			legendas.find("li:eq(" + id + ")").css('display', 'block');
		});
		
		$("div#fundoBranco").animate({
			opacity: 0
		}, 300);

		// marca o link ativo
		this.botoes.find("li").attr("class", "");
		this.botoes.find("li:eq(" + id + ")").attr('class', 'over');
	}
	
	this.int = setInterval(function() {
		slide.muda();
	}, 5000);
}

var slide = new slide();

$("#botoes li").click(function() {
	clearInterval(slide.int);
	slide.mudaLink(parseInt($(this).text()));
});

function get(id) {
	return document.getElementById(id);
}
});
