/*
MATHML
*/
var isIE = document.createElementNS==null;
if (document.getElementById==null) 
  alert("This webpage requires a recent browser such as\\nMozilla/Netscape 7+ or Internet Explorer 6+MathPlayer")

function isMathMLavailable() {
  if (navigator.appName.slice(0,8)=="Netscape") 
    if (navigator.appVersion.slice(0,1)>="5") return true;
    else return false;
  else if (navigator.appName.slice(0,9)=="Microsoft")
    try {
        var ActiveX = new ActiveXObject("MathPlayer.Factory.1");
        return true;
    } catch (e) {
        return false;
    }
  else return false;
}

var MathML = {
  root: 'math',
  ns: 'http://www.w3.org/1998/Math/MathML'
}

function createElementMathML(t) {
  if (isIE) return document.createElement("m:"+t);
  else return document.createElementNS(MathML.ns,t);
}

// clone a DOM subtree
function treeMath( source, dest) {
  for (var i=0; i<source.childNodes.length; i++) {
    var oldchild = source.childNodes[i];
    if (oldchild.nodeType == 1) { // element
        var newchild = createElementMathML(oldchild.nodeName.toLowerCase());
        treeMath( oldchild, newchild);
        dest.appendChild(newchild);
    } else if (oldchild.nodeType == 3) { // text
      	var newchild = document.createTextNode(oldchild.nodeValue);
      	dest.appendChild(newchild);
    }
  }
}

function loadMath(txt) {
  try {
  	 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
 	 xmlDoc.async="false";
 	 xmlDoc.loadXML(txt);
  }
  catch(e) {
  	alert(e.message);
  	return;
  }
  return xmlDoc;
}

  /**
    * Función de utilidad para depurado de objetos.
    */
    function debugObject(oValue)
    {
        temp="";
        obj= eval(oValue);
        for (x in obj)
            temp += x + ": " + obj[x] + "; ";
        alert (temp);
        temp= "";
    }

function generic() {
  if (isMathMLavailable())  {
  	var roots = document.getElementsByTagName(MathML.root);
	var re = new RegExp(MathML.root.toUpperCase());
  	for (var j=0; j<roots.length; j++) {
  	  var source = roots[j];
	  if (isIE) {
		var sourceAux="";
		var node=source.nextSibling;
		var padre=node.parentNode;
		while(!node.nodeName.match(re)) {
			if (node.nodeType==3) 
				sourceAux+=node.nodeValue;
			else 
				sourceAux+=node.outerHTML;
			
			nodeaux=node.nextSibling;
  	    	padre.removeChild(node);
			node=nodeaux;
		}
		sourceAux=loadMath(sourceAux);
	  }
	  else sourceAux=source;
      var dest = document.createDocumentFragment();
  	  treeMath(sourceAux, dest);
  	  var node = createElementMathML("mstyle");
  	  node.setAttribute("displaystyle","true");
	  node.setAttribute("mathsize","12");
	  node.appendChild(dest);
  	  dest = createElementMathML(MathML.root);
	  dest.appendChild(node);
  	  source.parentNode.insertBefore(dest, source);
  	  source.parentNode.removeChild(source);
  	}
  }
  else 
       alert("To view the MathML notation use Internet Explorer 6 +\nMathPlayer (free from http://www.dessci.com/en/products/mathplayer/download.htm)\n\ or Firefox/Mozilla/Netscape");
};
//setup onload function
if(typeof window.addEventListener != 'undefined') //.. gecko, safari, konqueror and standard
  window.addEventListener('load', generic, false);
else if(typeof document.addEventListener != 'undefined') //.. opera 7
  document.addEventListener('load', generic, false);
else if(typeof window.attachEvent != 'undefined') //.. win/ie
  window.attachEvent('onload', generic);
else {
  //.. mac/ie5 and anything else that gets this far
  //if there's an existing onload function
  if(typeof window.onload == 'function')
  {
    //store it
    var existing = onload;
    window.onload = function() {
      //call existing onload function
      existing();
      generic();
    };
  }
  else //setup onload function
    window.onload = generic;
}
if (isIE) { // avoid adding MathPlayer info explicitly to each webpage
  document.write("<object id=\"mathplayer\"\ classid=\"clsid:32F66A20-7614-11D4-BD11-00104BD3F987\"></object>");
  document.write("<?import namespace=\"m\" implementation=\"#mathplayer\"?>");
}

