// JavaScript Document

var xdoc = null;

function criaXML(xmlFile, cbFun){
	//alert('chegou 1');
	if( window.ActiveXObject && /Win/.test(navigator.userAgent) ){
		//alert('chegou 2');
		xdoc = new ActiveXObject("Microsoft.XMLDOM");
		
		//alert('chegou 3');

		xdoc.async = false;
		
		//alert('chegou 4');
		xdoc.load(xmlFile+"?r="+Math.floor(Math.random()*9999));

		//alert('chegou 5');
		
		cbFun();

		//alert('chegou 6');

		return true;
	}else if( document.implementation && document.implementation.createDocument ){
		xdoc = document.implementation.createDocument("", "", null);
		xdoc.load(xmlFile+"?r="+Math.floor(Math.random()*9999));
	
		xdoc.onload = function(){
		 cbFun();
		}
		return true;
	}else{
		return false;
	}
}

function constructMenu(){
	if(xdoc == null){
		throw("Erro na utilizacao do XMLDocument.");
	}else{
		var linkMenu = document.getElementById("Map");
		var totalNode = xdoc.getElementsByTagName("page");

		for(var i = 0; i<totalNode.length -2; i++){
			this["_title"+i] 		= totalNode[i].getAttribute("title");
			this["_link"+i] 		= totalNode[i].getAttribute("link");
			this["_description"+i] 	= totalNode[i].getAttribute("description");
		}
		
		for(var b = 0; b<linkMenu.getElementsByTagName("area").length; b++){
			with(linkMenu.getElementsByTagName("area")[b]){
				href 				= this["_link"+b];
				alt 				= this["_title"+b];
				title 				= this["_description"+b];
			}
		}
	}
}

function populateLeftMenu(){
	if(xdoc == null){
		throw("Erro na utilizacao do XMLDocument.");
	}else{
		var totalNodeMenuLeft 		= xdoc.getElementsByTagName("button");

		for(var i = 0; i<totalNodeMenuLeft.length; i++){
			this["_path"+i] 		= totalNodeMenuLeft[i].getAttribute("path");
			this["_link"+i] 		= totalNodeMenuLeft[i].getAttribute("link");
			this["_title"+i] 		= totalNodeMenuLeft[i].getAttribute("title");
			this["_description"+i] 	= totalNodeMenuLeft[i].childNodes[0].nodeValue;
		}
		
		
		with(document.getElementById("caixaCatalogo").getElementsByTagName("a")[0]){
			href 					= this["_link"+0];
			alt 					= this["_title"+0]
			title 					= this["_title"+0]
			innerHTML 				= this["_description"+0];
		}
		
		with(document.getElementById("caixaOutrosProdutos").getElementsByTagName("a")[0]){
			href 					= this["_link"+1];
			alt 					= this["_title"+1]
			title 					= this["_title"+0]
			innerHTML 				= this["_description"+1];
		}
		
		with(document.getElementById("caixaGaleria").getElementsByTagName("a")[0]){
			href 					= this["_link"+2];
			alt 					= this["_description"+2];
			title 					= this["_description"+2];
			innerHTML 				= this["_description"+2];
		}
		
		document.getElementById("caixaCatalogo").style.backgroundImage 			= "url("+this['_path'+0]+")";
		document.getElementById("caixaOutrosProdutos").style.backgroundImage 	= "url("+this['_path'+1]+")";
		document.getElementById("caixaGaleria").style.backgroundImage 			= "url("+this['_path'+2]+")";
		criaXML("xml/conteudo.xml", constructMenu);
	}
}
