// ------------------------------------------------ AJAX Q&A search --------------------------------------------------------------
toggle_visibility = function(elmid) {
  var subm = $(elmid);
  if (subm.style.display=='none' || subm.style.display=='') {
      subm.style.display='block';                            
  } else {
      subm.style.display='none'; 
  }
};


var qaold = '';

doliveqasearch = function () {
  res = $('qaresults');
  qaqry = $('qaquery').value;
  if (qaqry != qaold) {
    if (qaqry == '') {
      res.innerHTML = '';
      res.style.display = 'none';
    } else
      intraxxion.submitform('DesignerTemplates/qaresults.html', $('qaform'), displayqaresults);
  }
  qaold = qaqry;
  setTimeout('doliveqasearch()', 700) ;
};
initqasearch = function () {
  doliveqasearch();
};
displayqaresults = function (data) {
  $('qaresults').style.display = 'block';
  $('qaresults').innerHTML = data.responseText;
};


fetch_questions = function (subject_id ) {
  tt = $(subject_id).previousSibling;
  if ((tt.className != 'subjecttitle') && (tt.className != 'selected')) { 
    tt = tt.previousSibling;
  }
  
  if ($(subject_id).style.display == 'none') {
    $('subject').value = subject_id;  
    $('target').value = $('mytarget').value;
    tt.className = 'selected';    
    intraxxion.submitform(location.href+'/fetch_questions', $('subjform'), displaysubjectresults);
  } 
  if ($(subject_id).style.display == 'block') {
    $(subject_id).style.display = 'none';
    tt.className = 'subjecttitle';    
  }
};

displaysubjectresults = function (data) {
  resultset = JSON.parse(data.responseText);
  var newDiv = DIV({'id':resultset.id,'class':'subjectcontent'}) 
  for (x = 0; x < resultset.rows.length;x++) {
    hdr = DIV({'id':resultset.rows[x].id,'class':'answertitle'}, resultset.rows[x].pagetitle)
    connect(hdr,'onclick', this, 
      function(e) {
        toggle_visibility('CN_'+e.target().id);
        
      }
    );
    newDiv.appendChild(hdr);
    cnt = DIV({'id':'CN_'+resultset.rows[x].id,'class':'answercontent'});
    cnt.innerHTML = '<span class="answer">Antwoord:</span>'+resultset.rows[x].content;
    newDiv.appendChild(cnt);
  }
  swapDOM(resultset.id, newDiv);
  $(resultset.id).style.display = 'block';
};
// ------------------------------------------------ Page initialization --------------------------------------------------
connect(window, 'documentReady',
  function(evt) {  
    try {
      connect($('directnaar'), 'onclick',
        function() {                                          
          if ($('directnaarmenu').style.display == 'none' || $('directnaarmenu').style.display == '') {
            appear($('directnaarmenu'));
          } else {
            blindUp($('directnaarmenu'));
          }
        }
      );
    } catch(e) {
      ;//log('init direct naar: '+ e.message);
    }
    
    try {  
      if ($('qaquery')!=null) {
        initqasearch();
      }
    } catch(e) {
      ;//log('init V&A Query: '+ e.message);
    }

    // Patch voor waarschijnlijk een bug in FF
    if (WxUtils.browser.mozilla) {
      if ($$('.formfield select').length>0)
        setStyle($('right'),{'position':'absolute','right':'0'});
    }
    try {
      highlightWord($('querystring').value,'#e5d9bf', $('searchresult'));
    } catch (e) {}
    
    // op home moet directnaar geopend zijn
    if (/riagg-home/.test(location.href)) {
      appear($('directnaarmenu'));
    }
    if (location.pathname == '/') {
        appear($('directnaarmenu'));        
    }
  }
);

// ------------------------------------------------ Print pagina ---------------------------------------------------
function print_pagina() {       
    window.print();  
}

// ------------------------------------------------ Pagina Batch ----------------------------------------------------
get_page = function(art_no)
{
  my_form = document.getElementById('myfrm');
  document.getElementById('start').value=parseInt(art_no);
  my_form.submit();
}

// ------------------------------------------------ Hilite words ----------------------------------------------------
function highlightWord(word, color, element) {
  var i;
  for (i = 0; i < element.childNodes.length; i++)  {
      childNode = element.childNodes[i];
      if (childNode.nodeType == 3 /*childNode.TEXT_NODE*/)
      {
          var j;
          while ((j = childNode.data.toLowerCase().indexOf(word.toLowerCase())) != -1)
          {
              continuingText = childNode.data.substr(j + word.length, childNode.data.length);
              originalWordStuff = childNode.data.substr(j, word.length);
              childNode.data = childNode.data.substr(0, j);

              newNode = document.createElement("font");
              newNode.style.background = color;
              newNode.appendChild(document.createTextNode(originalWordStuff));
              continuingNode = document.createTextNode(continuingText);

              // insert continuingNode first, and then insertBefore it newNode.
              if (i == (element.childNodes.length - 1))
                  element.appendChild(continuingNode);
              else
                  element.insertBefore(continuingNode, element.childNodes[i+1]);
              element.insertBefore(newNode, continuingNode);

              i += 2;

              childNode = continuingNode;
          }
      }
      else if (typeof childNode.htmlFor != "undefined")
      {
          // script element, ignore.
      }
      else if (childNode.getAttribute && childNode.getAttribute("color") == color)
      {
          // already coloured, probably.
      }
      else if (childNode.childNodes.length)
      {
          highlightWord(word, color, childNode);
      }
  }
}