$(document).ready(function() {
  replaceText();
  // fix png's for ie6
  setTimeout("fixPngs();", 1);
});

function replaceText() {

  //replace page title
  $('h1').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="fonts/image.php?type=1&text='+text+'" />');
  });

  $('#contentlefttext h2').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="fonts/image.php?type=1&text='+text+'" />');
  });

  //replace referentie header
  $('#header_right_referenties h2').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="fonts/image.php?type=5&text='+text+'" />');
  });

  //replace footer h2
  $('#footer h2').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="fonts/image.php?type=2&text='+text+'" />');
  });

  //replace footer h2
  $('#menu span').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="fonts/image.php?type=3&text='+text+'" />');
  });

  $('.subnav_header h2').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="fonts/image.php?type=4&text='+text+'" />');
  });

}

function fixPngs() {
  $.ifixpng('assets/pngfix.gif');
  $('img[src$=.png], #tools').ifixpng();
}


/*
  getColor
  gets hexvalues of color
*/
function getColor(color) {
  var pos = color.indexOf(')');
  if(pos > 0) {
    var rgb = color.substr(4,pos-4);
    color = '';
    rgb = rgb.split(', ');
    for (var i in rgb) {
      dec = parseInt(rgb[i]);
      color = color+''+toHex(dec);
    }
  }
  var pos = color.indexOf('#');
  if (pos > -1) {
    var color = color.substr(1);
  }

  return color;
}


/*
  toHex
  converts decimals 0 <> 255 to hex
*/
function toHex(dec) {
  var hexCharacters = "0123456789ABCDEF"
  if (dec < 0)
  return "00"
  if (dec > 255)
  return "FF"
  var i = Math.floor(dec / 16)
  var j = dec % 16
  return hexCharacters.charAt(i) + hexCharacters.charAt(j)
}

function setActiveTab(id) {
  var tabElement = document.getElementById(id);
  tabElement.className = "active";
}


function setContentHeight() {

  //alert('aangeroepen');

  var divhL = document.getElementById('content_left').offsetHeight;
  var divhR = document.getElementById('content_right').offsetHeight;

  //alert('heigth' + divhL);

  if(divhL > 615 || divhR > 615) {
    if(divhL > divhR) {
      divhL = divhL + 40;
      var contentElement = document.getElementById('content');
      contentElement.style.height = divhL +"px";
    }else{
      divhR = divhR +  40;
      var contentElement = document.getElementById('content');
      contentElement.style.height = divhR +"px";
    }
  }
}

function switchImage(id, img) {
  var img_big = document.getElementById('header_left_referenties');
  img_big.innerHTML = "<img height=\"232\" width=\"309\" alt=\"\" src=\"images/referenties/groot/" + img + "\"/>";
}

function setActiveSubnav(id) {
  if(id > 0) {
    var activeSubNav = document.getElementById("child_nav_" + id);
    if(activeSubNav != null)
    {
      activeSubNav.style.visibility = "visible";
      activeSubNav.style.display = "block";
    }
  }
}

function collapseSubNav(id, element) {
  if(id > 0)
  {
    listElement = document.getElementById('subnav');
    var child=listElement.firstChild;
    while (child!=null)
    {
      // only process li elements (and not text elements)
      if (child.nodeType==1)
      {
        // build a list of child ol and ul elements and hide them
        var list=new Array();
        var grandchild=child.firstChild;
        while (grandchild!=null)
        {
          if (grandchild.tagName=='OL' || grandchild.tagName=='UL')
          {
            grandchild.style.display='none';
            list.push(grandchild);
          }
          grandchild=grandchild.nextSibling;
        }
      }
      child=child.nextSibling;
    }

    var activeSubNav = document.getElementById("child_nav_" + id);
    var parrentSubNav = document.getElementById("parrent_" + element);
    //activeSubNav.style.visibility = "visible";
    if(activeSubNav.style.display == "none") {
      activeSubNav.style.display = "block";
    }else {
      activeSubNav.style.display = "none";
    }
  }
}
