// maxlength-script: quirksmode.org
function discussSetMaxLength() {
  var x = document.getElementsByTagName('textarea');
  var counter = document.createElement('div');
  counter.className = 'counter';
  for (var i=0;i<x.length;i++) {
    if (x[i].getAttribute('maxlength')) {
      var counterClone = counter.cloneNode(true);
      counterClone.relatedElement = x[i];
      counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
      x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
      x[i].relatedElement = counterClone.getElementsByTagName('span')[0];
      
      x[i].onkeyup = x[i].onchange = discussCheckMaxLength;
      x[i].onkeyup();
    }
  }
}

function discussCheckMaxLength() {
  var maxLength = this.getAttribute('maxlength');
  var currentLength = this.value.length;
  if (currentLength > maxLength)
    this.relatedElement.className = 'toomuch';
  else
    this.relatedElement.className = '';
  this.relatedElement.firstChild.nodeValue = currentLength;
  // not innerHTML
}

// xmlhttprequest-code: www.quirksmode.org
function sendRequest(url,callback,postData) {
  var req = createXMLHTTPObject();
  if (!req) return;
  var method = (postData) ? "POST" : "GET";
  req.open(method,url,true);
  req.setRequestHeader('User-Agent','XMLHTTP/1.0');
  if (postData)
    req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  req.onreadystatechange = function () {
    if (req.readyState != 4) return;
    if (req.status != 200 && req.status != 304) {
//      alert('HTTP error ' + req.status);
      return;
    }
    callback(req);
  }
  if (req.readyState == 4) return;
  req.send(postData);
}

var XMLHttpFactories = [
  function () {return new XMLHttpRequest()},
  function () {return new ActiveXObject("Msxml2.XMLHTTP")},
  function () {return new ActiveXObject("Msxml3.XMLHTTP")},
  function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
  var xmlhttp = false;
  for (var i=0;i<XMLHttpFactories.length;i++) {
    try {
      xmlhttp = XMLHttpFactories[i]();
    }
    catch (e) {
      continue;
    }
    break;
  }
  return xmlhttp;
}

/* Smooth scrolling
   Changes links that link to other parts of this page to scroll
   smoothly to those links rather than jump to them directly, which
   can be a little disorienting.
   
   sil, http://www.kryogenix.org/
   
   v1.0 2003-11-11
   v1.1 2005-06-16 wrap it up in an object
*/

var ss = {
  smoothScroll: function(destinationLink) {
    var anchor = destinationLink.name;
    
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL =
setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 25;

function handleRequest(req) {
  var discuss = document.getElementById('discuss-container');
  discuss.innerHTML = req.responseText;
  setTimeout('discussSetMaxLength();scrollToDiscussVerstuurd();',500);
}

function scrollToDiscussVerstuurd() {
  var discussVerstuurd = document.getElementById('discuss_bericht_verstuurd_t_id');
  if (discussVerstuurd) {
    ss.smoothScroll(discussVerstuurd);
  }
}

function discussPostToggleVisibilityAction(a,msg_id) {
  sendRequest(a.href,handleRequest,'post_action=1&toggle_visibility='+msg_id);
  discussAppendIndicator(a);
}
function discussPostDeleteAction(a,msg_id) {
  if (!confirm('Het bericht wordt verwijderd.')) return false;
  sendRequest(a.href,handleRequest,'post_action=1&delete='+msg_id);
  discussAppendIndicator(a);
}
function discussAppendIndicator(a) {
  var img = document.createElement('img');
  img.src = '/assets/indicator_arrows_circle.gif';
  a.parentNode.insertBefore(img, a.nextSibling);
}

if (document.createElement && document.getElementsByTagName && createXMLHTTPObject()) {
  var discussContainer = document.getElementById("discuss-container");
  if (discussContainer && discussContainer.getAttribute('objid')) {
    sendRequest('/discuss/'+discussContainer.getAttribute('objid'),handleRequest);
  }
}


