var DDSPEED = 10;
var DDTIMER = 15;

/* DENK EROM DAT DYNAMISCH HTML INLADEN BETEKEND DAT ER GEEN PHP CODE IN MAG VOORKOMEN! */
function rollout(itemid, obj) {
  makeRequest('rolloutdata.php', '?item='+itemid, obj);//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);
  c.style.visibility = 'visible';
  var setwidth = 500; //gewenste width
  clearInterval(c.timer);
  if(d == 1){
  	//c.style.visibility = "visible";
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.style.width = setwidth + 'px';
      c.maxh = c.offsetHeight;
     // alert( c.offsetHeight);
      c.style.height = '2px';
      c.style.width = '2px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    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 and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var currw = c.offsetWidth;
  var setwidth = 500; //gewenste width
  var dist;
  var incwidth;
  if(d == 1){
  	if (currw < setwidth) {
    	incwidth = (Math.round((setwidth - currw) / DDSPEED));
  	}else {  	
    	dist = (Math.round((c.maxh - currh) / DDSPEED));
  	}
  }else{
  	if (currh >= DDSPEED) {
	    dist = (Math.round(currh / DDSPEED));
  	}else {  	
  		if (currh > 2) { // laatste stukje omhoog trekken
		    dist = 2;
		  }
		  else { // horizontale lijn intrekken
	    	incwidth = (Math.round(currw / DDSPEED));
	    }
  	}
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }  
  if(incwidth <= 1 && d == 1) {
  	incwidth = 1;
  }
  if (dist != null) {
  	c.style.height = currh + (dist * d) + 'px';
  }
  if (incwidth != null) {
  	c.style.width = currw + (incwidth * d) + 'px';
  }
  if (currw == setwidth && d == 1) {
	  c.style.opacity = currh / c.maxh;
	  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
	}
	else {
		c.style.opacity = 100;
		c.style.filter = 100;
	}
  if(currh < 5 && d != 1 && currw < 5){
    clearInterval(c.timer);
    c.style.visibility = "hidden";
  }
  
  if (currh > (c.maxh - 2) && d == 1) {
    clearInterval(c.timer);
//    alert ("end 2");
  }
}

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

function makeRequest(url, parameters, obj) {
	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;
	}

	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;		
			document.getElementById('rolloutmenuleft').innerHTML = "<div class=\"toprollout\"><span class=\"closerollout\"><a onclick=\"ddMenu('rolloutmenuleft', -1);\"><img src=\"images/btclose.png\"></a></span></div><div class=\"contentrollout\">"+xmldoc.getElementsByTagName('content')[0].childNodes[0].nodeValue+"</div><div class=\"bottomrollout\"></div>";		
			//document.getElementById('rolloutmenuleft').innerHTML = xmldoc.getElementsByTagName('content')[0].childNodes[0].nodeValue;
			document.getElementById('rolloutmenuleft').maxh = null; // reset de maxh waarde zodat het vak precies de inhoud gaat weergeven
			ddMenu('rolloutmenuleft', 1);
		} 
		else {
			alert('There was a problem with the request.');
		}
	}
}

