function UNI_infoboxInArray(infoboxarray, searchInfoboxId)	{
	for(var i =0;i < infoboxarray.length; i++){
		var infobox = infoboxarray[i];
		if(infobox.style.display == "block"){
			if(infobox.id == searchInfoboxId)	{
				return true;
			}
		}
	}
	return false;
}

function UNI_getOpenInfoboxs()	{
	
	//document.getElementById('CMS_debug_content').innerHTML += "<br />-----------------------------------";
	
	var infoboxarray = getElementsByAttribute(document.getElementById('index'),"DIV","u:infobox","true");
	var _infoboxarray = new Array();
	for(var i =0;i < infoboxarray.length; i++){
		var infobox = infoboxarray[i];
		if(infobox.style.display == "block")	{
			_infoboxarray.push(infobox);
			//document.getElementById('CMS_debug_content').innerHTML += "<br />+ "+infobox.id;
		}
	}
	
	//document.getElementById('CMS_debug_content').innerHTML += "<br />-----------------------------------";
	return _infoboxarray;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////.
function UNI_deactivateAllInfoBox(infobox_idnr, type){
	var infoboxarray = UNI_getOpenInfoboxs();
	
	var infobox = document.getElementById(infobox_idnr);
	var zIndex = 800;
	var zIndex_MAX = 800;
	var zIndex_staffel = 10;
	
	if(type == "show")	{
		
		// Eerst controleren of de infobox al zichtbaar is
		if(UNI_infoboxInArray(infoboxarray,infobox_idnr))	{

			//document.getElementById('CMS_debug_content').innerHTML += "<br />"+infobox.id+" STAAT AL OPEN!";
			
			// Sequence van huidige infobox bepalen
			var infoboxSEQ = parseInt(infobox.getAttribute("u:infobox_seq"));
			
			// Nu de array loopen om de infobox die een hoger sequence nummer hadden allemaal 1 te verlagen
			for(var i =0;i < infoboxarray.length; i++){
				var __infobox = infoboxarray[i];
				var __infoboxSEQ = parseInt(__infobox.getAttribute("u:infobox_seq"));
				
				if(__infobox.id == infobox_idnr)	{
					//document.getElementById('CMS_debug_content').innerHTML += "<br />"+infobox.id+" BOVENAAN GEZET! = "+(infoboxarray.length-1);
					__infobox.setAttribute("u:infobox_seq",(infoboxarray.length-1));
				} else if(__infoboxSEQ > infoboxSEQ)	{
					//document.getElementById('CMS_debug_content').innerHTML += "<br />"+__infobox.id+" VERLAAGD!! = "+(__infoboxSEQ-1);
					// Als het nieuwe seq < infoboxSEQ dan hoeft er niets aan de volgorde gedaan te worden
					__infobox.setAttribute("u:infobox_seq",(__infoboxSEQ-1));
				}
			}
		} else {
			infobox.setAttribute("u:infobox_seq",infoboxarray.length);
		}
		
		
	} else {
			
		// Sequence van huidige infobox bepalen
		var infoboxSEQ = parseInt(infobox.getAttribute("u:infobox_seq"));
			
		for(var i =0;i < infoboxarray.length; i++)	{	
			var __infobox = infoboxarray[i];
			var __infoboxSEQ = parseInt(__infobox.getAttribute("u:infobox_seq"));
			
			// Alle infoboxs die een hogere infoboxSEQ hadden 1 verlagen
			if(__infobox.id == infobox_idnr)	{
				//document.getElementById('CMS_debug_content').innerHTML += "<br />"+infobox.id+" VERWIJDERED!";
				infoboxarray.splice(i,1);
			} else if(__infoboxSEQ > infoboxSEQ)	{
				//document.getElementById('CMS_debug_content').innerHTML += "<br />"+__infobox.id+" VERLAAGD!! = "+(__infoboxSEQ-1);
				// Als het nieuwe seq < infoboxSEQ dan hoeft er niets aan de volgorde gedaan te worden
				__infobox.setAttribute("u:infobox_seq",(__infoboxSEQ-1));
			}
		}
		
	}
	
	// Nu de array al laatste lopen om zodoende alle zIndexen te setten
	for(var i =0;i < infoboxarray.length; i++)	{
		
		var _infobox = infoboxarray[i];
		var _infoboxSEQ = parseInt(_infobox.getAttribute("u:infobox_seq"));
		
		// bereken zIndex
		var _zIndex = (zIndex_staffel * _infoboxSEQ) + zIndex;
		_infobox.style.zIndex = _zIndex;
		//document.getElementById('CMS_debug_content').innerHTML += "<br />"+_infobox.id+"="+_zIndex+"::"+_infoboxSEQ;
		
		// Kijken hoe hoog deze zIndex ligt
		if(_zIndex > zIndex_MAX)	{
			zIndex_MAX = _zIndex;
		}
		
	}
	
	if(!UNI_infoboxInArray(infoboxarray,infobox_idnr) && type == "show")	{
		// Nu alleen nog maar de huidige infobox die getoond moet worden op de juiste zIndex zetten
		var infoboxZIndex = (zIndex_staffel * infobox.getAttribute("u:infobox_seq")) + zIndex;
		infobox.style.zIndex = infoboxZIndex;
		//document.getElementById('CMS_debug_content').innerHTML += "<br />"+infobox.id+"="+infoboxZIndex+"::"+infobox.getAttribute("u:infobox_seq");
		
		// Kijken hoe hoog deze zIndex ligt
		if(infoboxZIndex > zIndex_MAX)	{
			zIndex_MAX = infoboxZIndex;
		}
	}
	
	if(infoboxarray.length > 0 || type == "show")	{
		// controleren welke z-index deze div getoond moet worden!
		UNI_setAttrib('deactivateAll','zIndex',zIndex_MAX-5);
		UNI_setAttrib('deactivateAll','display','block');
	} else if(infoboxarray.length == 0)	{
		UNI_setAttrib('deactivateAll','display','none');
	}
	
	//document.getElementById('CMS_debug_content').innerHTML += "<hr />";
}

function UNI_maxInfobox(infobox)	{
	infobox_object = document.getElementById(infobox);
	infobox_object_content = document.getElementById(infobox+"_content");
	if(infobox_object.attributes['u:max'].nodeValue == "false")	{
		
		// Oude infobox grootte opslaan
		infobox_object.setAttribute('u:width',infobox_object.style.width);
	    infobox_object.setAttribute('u:height',infobox_object.style.height);
	    infobox_object.setAttribute('u:top',infobox_object.style.top);
	    infobox_object.setAttribute('u:left',infobox_object.style.left);
	    infobox_object.setAttribute('u:max','true');
	    
		var max = UNI_showInfobox_MAX();
		infobox_object.style.width = (max['width']-13)+"px";
	    infobox_object.style.height = (max['height']-10)+"px";
	    infobox_object_content.style.width = (max['width']-30)+"px";
	    infobox_object_content.style.height = (max['height']-120)+"px";
	    infobox_object.style.position = "absolute";
	    infobox_object.style.left = "3px";
	    infobox_object.style.top = "3px";
	    infobox_object.style.bottom = "3px";
	    infobox_object.style.right = "3px";
	    
	    // Controleren of er een HTML editor actief is
	    var htmlItems = getElementsByAttribute(infobox_object_content,"textarea","u:html","true");
		var htmlItemsSize = htmlItems.length;
		
		for(var i =0;i < htmlItemsSize; i++)	{
			var spanItems = htmlItems[i].parentNode.getElementsByTagName('SPAN');
			for(var s =0;s < spanItems.length; s++)	{
				// SPAN pakken van parent
				var spanItem = spanItems[s];
				
				// TABLE width instellen
				spanItem.firstChild.style.width = (max['width']-100)+"px";
				spanItem.firstChild.style.height = (max['height']-250)+"px";
				
				// IFRAME Pakken
				var iFrames = spanItem.getElementsByTagName('IFRAME');
				var iFrame = iFrames[0];

				iFrame.style.width = (max['width']-100)+"px";
				iFrame.style.height = (max['height']-250)+"px";
			}
		}
	    
	} else {
		infobox_object.setAttribute('u:max','false');
		infobox_object.style.width = infobox_object.attributes['u:width'].nodeValue;
	    infobox_object.style.height = infobox_object.attributes['u:height'].nodeValue;
	    infobox_object.style.top = infobox_object.attributes['u:top'].nodeValue;
	    infobox_object.style.left = infobox_object.attributes['u:left'].nodeValue;
	    infobox_object_content.style.width = infobox_object.attributes['u:width'].nodeValue;
	    infobox_object_content.style.height = infobox_object.attributes['u:height'].nodeValue;
	}
}

function UNI_showInfobox_MAX()	{
	// create the checkpoint element
    var cp = document.createElement("div");
    cp.style.position = "absolute";
    cp.style.width = "0px";
    cp.style.height = "0px";
    cp.style.right = "0px";
    cp.style.bottom = "0px";

    // we can only read it's position after we
    // insert it into the document
    document.body.appendChild(cp);

    // here we get the actual client size
    var current_width = cp.offsetLeft;
    var current_height = cp.offsetTop;
    
    // we can safely delete the checkpoint now
    document.body.removeChild(cp);

    var max = new Array();
    max['width'] = current_width;
    max['height'] = current_height;
    // for the curious, return to see how much
    // did IE miss it
  
    return max;
}

function UNI_showInfobox_dynamicContent(content, reserved_height)	{
	try {
		var infobox_found = false
		var content_object = document.getElementById(content);
		var infobox_object = content_object.parentNode;
		while(!infobox_found)	{
			try	{
				if(infobox_object.attributes['u:infobox'].nodeValue == "true") 	{
					infobox_found = true;
				}
			} catch(err)	{
				infobox_object = infobox_object.parentNode;
			}
		}
		
		if(infobox_found)	{
			// Height van infobox pakken
			var height = infobox_object.style.height.substring(0,infobox_object.style.height.length-2);
			content_object.style.height = (height-reserved_height)+"px";
		}
	} catch(err){}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_showInfobox(object)	{
	// Ervoor zorgen dat alleen het huidige infoboxtje gebruikt kan worden.
	UNI_deactivateAllInfoBox(object.attributes['u:infobox'].nodeValue, "show");
	
	UNI_setAttrib(object.attributes['u:infobox'].nodeValue,'display','block');
	// Proberen de width te zetten.
	try{
		UNI_setAttrib(object.attributes['u:infobox'].nodeValue,'width',object.attributes['u:infobox_width'].nodeValue+"px");
		UNI_setAttrib(object.attributes['u:infobox'].nodeValue+'_content','width',object.attributes['u:infobox_width'].nodeValue+"px");
	} catch(err){
	}
	// proberen de height te zetten.
	try{
		UNI_setAttrib(object.attributes['u:infobox'].nodeValue,'height',object.attributes['u:infobox_height'].nodeValue+"px");
		UNI_setAttrib(object.attributes['u:infobox'].nodeValue+'_content','height',object.attributes['u:infobox_height'].nodeValue+"px");
	}catch(err){
	}	
	
	// Infobox centeren
	UNI_center(object.attributes['u:infobox'].nodeValue,"");
	
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_showInfobox_manual(infobox, width, height)	{
	var obj = document.createElement('div');
	obj.setAttribute("u:infobox", infobox);
	obj.setAttribute("u:infobox_width", width);
	obj.setAttribute("u:infobox_height", height);
	UNI_showInfobox(obj);
}

function UNI_closeInfoboxExtra(object){
	UNI_closeInfobox(object);
	document.getElementById("CMS_infobox_extra_content").innerHTML = "";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_closeInfobox(object){
	var max_close = 5;
	var normal_close = 0;
	
	for(var i=1; i <= max_close; i++){
		try{
			if(UNI_checkInfoboxCondition(object)){
//				document.getElementById(object.attributes['u:infobox'].nodeValue + "_content").innerHTML = "";
				// Als parent als target infobox is meegegeven moet deze nu eerst worden opgezocht
				if(object.attributes['u:infobox_'+i].nodeValue == "parent"){
					var triggerObject = object;
					for(var teller=0; teller < 30; teller++){
						var parent = triggerObject.parentNode;
						
						try {
							if(parent.attributes['u:infobox'].nodeValue == "true"){
								object.setAttribute('u:infobox_'+i,parent.id);
								teller = 30;
							}
						} catch(err){
						}
						triggerObject = parent;
					}
				}
				
				var infobox = document.getElementById(object.attributes['u:infobox_'+i].nodeValue);
				infobox.style.display = 'none';
				document.getElementById(object.attributes['u:infobox_'+i].nodeValue+"_content").innerHTML = '<div>&#160;</div>';
				
				// Ervoor zorgen dat alleen het huidige infoboxtje gebruikt kan worden.
				UNI_deactivateAllInfoBox(object.attributes['u:infobox_'+i].nodeValue, "hide");
			}
			
		}catch(e){
			normal_close++;
//			alert ("CLOSE MODEL kan niet!!!"+i);
		}
	}
	
	if (normal_close == max_close){
		// Content van div verwijderen voor het rustiger laden van de content.
		var close = true;
		var eval_string = "";
		try {
			eval_string = object.attributes['u:infobox_condition'].nodeValue;
		} catch(err)	{
	
		}
	
		if(eval_string != "")	{
			close = eval(eval_string);
		}
	
		if(close)	{
			//document.getElementById(object.attributes['u:infobox'].nodeValue + "_content").innerHTML = "";
			// Als parent als target infobox is meegegeven moet deze nu eerst worden opgezocht
			if(object.attributes['u:infobox'].nodeValue == "parent")	{
				var triggerObject = object;
				for(var teller=0; teller < 30; teller++)	{
					var parent = triggerObject.parentNode;
					
					try {
						if(parent.attributes['u:infobox'].nodeValue == "true")	{
							object.setAttribute('u:infobox',parent.id);
							teller = 30;
						}
					} catch(err)	{
					}
					triggerObject = parent;
				}
			}
			
			
			var infobox = document.getElementById(object.attributes['u:infobox_'].nodeValue);
			infobox.style.display = 'none';
			
			// Ervoor zorgen dat alleen het huidige infoboxtje gebruikt kan worden.
			UNI_deactivateAllInfoBox(object.attributes['u:infobox'].nodeValue, "hide");
		}
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_closeInfobox_manual(infobox, infoboxcondition){
	var obj = document.createElement('div');
	obj.setAttribute("u:infobox", infobox);
	obj.setAttribute("u:infobox_condition", infoboxcondition);
	UNI_showInfobox(obj);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_checkInfoboxCondition(obj){
	var max_close = 5;
	var normal_close = 0;
	var close = true;
	var eval_string = "";
	
	for(var i=1; i <= max_close; i++){
		eval_string = "";
		
		try{
			eval_string = obj.attributes['u:infobox_condition_'+i].nodeValue;
		}catch(err){
			normal_close++;
		}
	
		if(eval_string != ""){
			close = eval(eval_string);
			break;
		}
	}
	if (normal_close == max_close){
		try{
			eval_string = obj.attributes['u:infobox_condition'].nodeValue;
		}catch(err){
		}
	
		if(eval_string != ""){
			close = eval(eval_string);
		}
	}
	
	return close;
}
