var ELEMENT_NODE = 1;
function loadXMLs()
{
	var url = window.location.href;
	var urlparts = url.split('/');
  urlparts.pop();
  urlparts.push("xmls", "staff.xml");
  var host = urlparts.join('/');
  
	if (document.implementation && document.implementation.createDocument) {
    xmlDoc = document.implementation.createDocument("", "", null);
    xmlDoc.onload = writeXMLs;
    xmlDoc.load(host);
  }
  else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) writeXMLs()
	  }
		xmlDoc.load(host);
 	}
}

function addStaff(nome, desc, foto) {
	var d = document.getElementById('staffDIV');
  var dc = document.getElementById("staffDIVChild");
  d.removeChild(dc);

 	var newdiv = document.createElement('div');
	newdiv.setAttribute("id","staffDIVChild");
	var code = "";
	code += "<center>";
	code += "<img src='staff/" + foto + "' style='border:1px solid #2F7ABB;'><br>";
	code += "<img src='images/spacer.gif' height='13px'><br><img src='images/1hline1.gif'><br><img src='images/spacer.gif' height='13px'><br>";
	code += "<font class='white12'>" + nome + "</font><br>";
	code += "<img src='images/spacer.gif' height='13px'><br><img src='images/1hline1.gif'><br><img src='images/spacer.gif' height='13px'><br>";
	code += desc;
	code += "</center>";
	newdiv.innerHTML = code;
	d.appendChild(newdiv);
}

function removeStaff() {
  var d = document.getElementById('staffDIV');
  var dc = document.getElementById("staffDIVChild");
  d.removeChild(dc);
 	var newdiv = document.createElement('div');
  newdiv.setAttribute("id","staffDIVChild");
	var code = "";
	code += "<center>";
	code += "<img src='images/unknown-person.gif' style='border:1px solid #2F7ABB;'><br>";
	code += "<img src='images/spacer.gif' height='13px'><br><img src='images/1hline1.gif'><br><img src='images/spacer.gif' height='13px'><br>";
	code += "<span class='yellow'>Selecione um membro ao lado &raquo;</span><br>";
	code += "<img src='images/spacer.gif' height='13px'><br><img src='images/1hline1.gif'>";
	code += "</center>";
	newdiv.innerHTML = code;
	d.appendChild(newdiv);
}

function writeXMLs() {
	writeStaff();
}

function writeDiv(elem, nome, desc, foto, fotothumb, staffCount, lastStaff) {
  //   onMouseOver="javascript:addStaff(0);" onMouseOut="javascript:removeStaff();">

	img = document.createElement('img');
	img.setAttribute('src', 'staff/' + fotothumb);
	img.setAttribute('id', 'staff' + staffCount);
  if ((staffCount % 5) == 0) {
  	img.style.cssText = 'border:1px solid #2F7ABB;width:45px;height:34px;';
  } else {
	  img.style.cssText = 'border:1px solid #2F7ABB;margin-left:20px;width:45px;height:34px;';
  };
	elem.appendChild(img);
	var a = document.getElementById('staff' + staffCount);
	a.onmouseover = new Function('addStaff("' + nome + '","' + desc + '","' + foto + '");'); 
  a.onmouseout = new Function('removeStaff();');
  
	if ( (lastStaff) || ((staffCount % 5) == 4) ) {
		elem.appendChild(document.createElement('BR'));
		imglink = document.createElement('img');
		imglink.setAttribute('src', 'images/spacer.gif');
		imglink.height = 20;
		elem.appendChild(imglink);
		elem.appendChild(document.createElement('BR'));    		
	};
};

function writeStaff() {
  var staff = xmlDoc.getElementsByTagName('staff');
  element = document.getElementById("staff");
  var staffCount = 0; var lastStaff = false;
  for (i=0; i < staff.length; i++) {
	  var nome = ""; var desc = "";
	  var foto = ""; fotothumb = "";
    for (j=0; j < staff[i].childNodes.length; j++) {
	    if (staff[i].childNodes[j].nodeType != ELEMENT_NODE) continue;
	    if (staff[i].childNodes[j].nodeName == "nome") nome = staff[i].childNodes[j].firstChild.nodeValue;
	    if (staff[i].childNodes[j].nodeName == "desc") {
		    if (desc == "") {
			    desc += staff[i].childNodes[j].firstChild.nodeValue;
		    } else {
			    desc += "<br>" + staff[i].childNodes[j].firstChild.nodeValue;
		    };
	    };
	    if (staff[i].childNodes[j].nodeName == "foto") {
		    foto = staff[i].childNodes[j].firstChild.nodeValue;
		   	preload_image_object = new Image();
       	preload_image_object.src = 'staff/' + foto;
	    }
	    if (staff[i].childNodes[j].nodeName == "foto-thumb") fotothumb = staff[i].childNodes[j].firstChild.nodeValue;
    };
    if ((staff.length - 1) == staffCount) lastStaff = true;
    writeDiv(element, nome, desc, foto, fotothumb, staffCount, lastStaff);
    staffCount += 1;
  };
};

loadXMLs();

