// debug
var lines = new Array();
function lastLines(array, num) {
  var text = "";
  var i = array.length - num - 1;
  i = (i > 0) ? i : 0;
  for (; i < array.length; i++) {
    text += i + ": " + array[i] + "<br/>";  
  }
  return text;
}
function dbg(msg) {
  lines.push(msg);
  var e = document.getElementById("debug");
  e.innerHTML = lastLines(lines,10);
}



/* browser detection */
var detect = navigator.userAgent.toLowerCase();
var OS,browser,total,thestring;
var version = 0;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}



function applyHover(element) {
  for (var i = 0; i < element.childNodes.length; i++) {
    node = element.childNodes[i];
    if (node.nodeName == "TD") {
      node.className += " out";
      node.onmouseover = function() {
        this.className = this.className.replace(" out", " over");
      }
      node.onmouseout = function() {
        this.className = this.className.replace(" over", " out");
      }
    }
    applyHover(node);
  }
}

var aBtnOn = '<a href="javascript:swapMusic()"><img src="imagenes/audioOff.gif"/> Audio on</a>';
var aBtnOff = '<a href="javascript:swapMusic()"><img src="imagenes/audioOff.gif"/> Audio off</a>';

function swapMusic() {
  var audio = document.getElementById("audioButton");
  if (audio) {
    if (parent.musica.src == "") {
      parent.musica.src = parent.musica_src;
      audio.innerHTML = aBtnOff;
    }
    else {
      parent.musica.src = "";
      audio.innerHTML = aBtnOn;
    }
  }
}

function applyAudio() {
  if (parent.musica) {
    var audio = document.getElementById("audioButton");
    if (audio) {
      if (parent.musica.src == "") {
        audio.innerHTML = aBtnOn;
      }
      else {
        audio.innerHTML = aBtnOff;
      }
    }
  }
}


/* estilos */
function styles() {
  if (browser == "Internet Explorer") {
    applyHover(document.getElementById("menuPrincipal"));
  }
  if (browser == "Netscape Navigator") {
    cols2();
  }
  applyAudio();

}

/* expande .texto cuando dentro existe un .cols2 */
function cols2() {
  var divs = document.getElementsByTagName("div");
  for (var i = 0; i < divs.length; i++) {
    if (divs[i].className.indexOf("cols2") != -1) {
      var clear = document.createElement("br");
      clear.className = "clear";
      divs[i].parentNode.insertBefore(clear, divs[i].siblingNode);
    }
  }
}
