var DDSPEED = 10;
var DDTIMER = 15;

/* DENK EROM DAT DYNAMISCH HTML INLADEN BETEKEND DAT ER GEEN PHP CODE IN MAG VOORKOMEN! */
function rollout(itemid, targetobj) {
  makeRequest('rolloutdata.php', '?item='+itemid, targetobj);//ajax om de gegevens van dit item op te halen
};

// main function to handle the mouse events //
function ddMenu(id,d){
  var c = document.getElementById(id);
  clearInterval(c.timer);
  if(d == 1){
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
      c.style.padding = '10px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    c.style.padding = '0';
    setTimeout(function(){ddCollapse(c)},50);
  }
};

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var c = document.getElementById(id);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
};

// incrementally expand/contract the dropdown
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));      
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  var helperheight = c.offsetHeight;
	c.style.height = currh + (dist * d) + 'px';
	if ((c.offsetHeight >= helperheight && d == -1)) {
		c.innerHTML = "";		
		c.style.height = '0px';
	}
	
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if (d == -1 && (currh / c.maxh < 0.01)) {
	  c.style.opacity = 0;
	  c.style.filter = 'alpha(opacity=0)';
  }
  if((currh < 2 && d == -1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
};

/*AJAX SUPPORT*/
var http_request = false;
var xmldoc = null;
var targetobj = null;

function makeRequest(url, parameters, _targetobj) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	targetobj = _targetobj;
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
};
	 
function alertContents () {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {	
			var xmldoc = http_request.responseXML;	
			var newdiv = document.createElement("div");
			newdiv.innerHTML = "<a onclick=\"ddMenu('"+targetobj+"', -1);\"><img src=\"./images/btclose.jpg\" style=\"float: right; top: 0px;\" alt=\"close\"></a><br><strong>"+xmldoc.getElementsByTagName('title')[0].childNodes[0].nodeValue+"</strong><p>"+xmldoc.getElementsByTagName('content')[0].childNodes[0].nodeValue;
			var container = document.getElementById(targetobj);
			container.appendChild(newdiv);
			document.getElementById(targetobj).maxh = null; // reset de maxh waarde zodat het vak precies de inhoud gaat weergeven
			ddMenu(targetobj, 1);
		} 
		else {
			alert('There was a problem with the request.');
		}
	}
};
