function showDiv(DivId, bShow)
{
	document.getElementById(DivId).style.display = 'block';
}

function hideDiv(DivId)
{
	document.getElementById(DivId).style.display = 'none';
}	

function changeImage(imgId, imgSrc)
{
	if(imgId != "")
	{
		document.getElementById(imgId).src = imgSrc;
	}
}



var aImage = new Array();
var CounterDoneLoad = 0;

function imageCheck(){
    
     for(var i = 0; i < aItems.length; i++){
         var BoxId = 'coverup' + i;
         objImage = new Image();  
        objImage.onload = function(){setTimeout(function(){popp();}, 500);}  
        objImage.src = aItems[i]['photo'];
        aImage.push(objImage);
        document.getElementById('dragcontent').innerHTML += "<div id='" + BoxId + "' class=\"bol\"  style='visibility:hidden;position:absolute;'><div id='top'></div><div id='contentBol'>"+ aItems[i]["name"] +" <a href=' " + aItems[i]["link"] + "' target='" + aItems[i]["target"] + "'><img src='"+ aItems[i]['photo'] +"' /></a> "+ aItems[i]["text"] +" </div></div>";
     }
}
  
function popp(){
    CounterDoneLoad++;
    if(CounterDoneLoad < aImage.length) {
        return;
    }

	var counter = 0;
    var elementHTML = "";
    var overFlowMax = 0;
         
    
	for (i=0; i<aItems.length; i++) {
	    var BoxId = 'coverup' + i;
        var bHeight = false;
        var bWidth = false;        
        
        if(subboxwidth < 0){
            subboxwidth = $('#coverup'+i).innerWidth();
            if(subboxwidth >= boxwidth){
                if(aItems.length == 1){
                	subboxwidth = Math.round((boxwidth / 2)-20);
                } else {
                	subboxwidth = Math.round((boxwidth / aItems.length)-20);	
                }
            }
            bWidth = true;     
        }
        if(subboxheight <= 0){
            subboxheight = $('#coverup'+i).innerHeight();
           if(subboxheight > boxheight){
           		if(aItems.length == 1){
                	subboxheight = Math.round((boxheight / 2)-20);
               } else {
               		subboxheight = Math.round((boxheight / aItems.length)-20);	
              }
            } 
            bHeight = true;     
        }
        
		counter++;
		var rndom_left = Math.round(Math.random() * boxwidth);
		var rndom_top = Math.round(Math.random() * boxheight);
		
        if(!ValidateBox(rndom_left, rndom_top)) {
            if(overFlowMax >= 50){
                overFlowMax = 0;   
            } else {
                i = i - 1;
                overFlowMax++;    
            }
		} else {
			/*if ((rndom_left + subboxwidth) > boxwidth) {
				i = i - 1;	
			} else if ((rndom_top + subboxheight) > boxheight) {
				i = i - 1;	
			} else {*/
            if(true) {
				aLeftPos[i] = rndom_left;
                aTopPos[i] = rndom_top;
                
                aBoxWidth[i] = (subboxwidth - 150);   
				aBoxHeight[i] = (subboxwidth - 150);

                
                
				if(aItems[i]["photo"] == "")
				{
					aItems[i]["photo"] = "http://repos.imanager.nl/transparent.gif";
				}
			
				 elementHTML += "<div id='" + BoxId + "' class=\"bol\"  style='visibility:visible;position:absolute;left:"+rndom_left+"px; top:"+rndom_top+"px; width: "+subboxwidth+"px; height: "+subboxheight+"px; display: block; background-color: "+ aItems[i]["bgcolor"] +"; color: "+ aItems[i]["textcolor"] +"'><div id='top' style='background-color: "+ aItems[i]["bgcolortop"] + "; width: " + parseInt(subboxwidth)  + "px;'></div><div id='contentBol' style='width: " +  parseInt(subboxwidth-20) + "px;'>"+ aItems[i]["name"] +" <a href=' " + aItems[i]["link"] + "' target='" + aItems[i]["target"] + "'><img src='"+ aItems[i]['photo'] +"' /></a> "+ aItems[i]["text"] +" </div></div>";
							
			}	
		} 			
        if(bWidth){
            subboxwidth = -1;    
        }
        if(bHeight){
            subboxheight = -1;    
        }
	}
    
    
    document.getElementById('dragcontent').innerHTML = elementHTML + "<div id=\"debug_float\" style=\"display:none;background-color:#FF8080;position:absolute;opacity:60;\">&nbsp;</div>";

    $(function() {
        $(".bol").draggable();
    });	
	return counter;
}

function debug2(value, width, height, left, top)
{
    document.getElementById('debug').innerHTML += "<li onmouseover=\"document.getElementById('debug_float').style.display = 'block';document.getElementById('debug_float').style.left = '"+left+"px';document.getElementById('debug_float').style.top = '"+top+"px';document.getElementById('debug_float').style.width= '"+width+"px';document.getElementById('debug_float').style.height = '"+height+"px';\" onmouseout=\"document.getElementById('debug_float').style.display = 'none';\">" + value + "</li>";
}

function debug(value)
{
    document.getElementById('debug').innerHTML += "<li>" + value + "</li>";
}

var CCCCC = 0;

function ValidateBox(bleft, btop)
{
	for(var i = 0; i < aLeftPos.length; i++) {
        CCCCC++;
        //debug2(CCCCC + " - " + i + ": LEFT" + aLeftPos[i] + " TOP" + aTopPos[i] + " WIDTH" + aBoxWidth[i] + " HEIGHT" + aBoxHeight[i], aBoxWidth[i], aBoxHeight[i], aLeftPos[i], aTopPos[i]);
        //debug2(CCCCC + " - " + i + ": LEFT" + bleft + " TOP" + btop + " WIDTH" + subboxwidth + " HEIGHT" + subboxheight, subboxwidth , subboxheight, bleft, btop);
        //alert("subboxwidth: " + aBoxWidth[i] + " subboxheight: " + aBoxHeight[i]);
     //   if((((bleft >= aLeftPos[i]) && (bleft <= (aLeftPos[i] + aBoxWidth[i]))) || (((bleft + subboxwidth) >= aLeftPos[i]) && ((bleft + subboxwidth) <= (aLeftPos[i] + aBoxWidth[i]))))) {
		if((((bleft >= aLeftPos[i]) && (bleft <= (aLeftPos[i] + aBoxWidth[i]))) || (((bleft + subboxwidth) >= aLeftPos[i]) && ((bleft + subboxwidth) <= (aLeftPos[i] + aBoxWidth[i])))) || ((bleft < aLeftPos[i]) && ((bleft + subboxwidth) > (aLeftPos[i] + aBoxWidth[i])))) {
            //debug(CCCCC + " - LEFT NOK");
          //  if((((btop >= aTopPos[i]) && (btop <= (aTopPos[i] + aBoxHeight[i]))) || (((btop + subboxheight) >= aTopPos[i]) && ((btop + subboxheight) <= (aTopPos[i] + aBoxHeight[i]))))) {
			if((((btop >= aTopPos[i]) && (btop <= (aTopPos[i] + aBoxHeight[i]))) || (((btop + subboxheight) >= aTopPos[i]) && ((btop + subboxheight) <= (aTopPos[i] + aBoxHeight[i])))) || ((btop < aTopPos[i]) && ((btop + subboxheight) > (aTopPos[i] + aBoxHeight[i])))) {
              //  debug(CCCCC + " - TOP NOK");  
				return false; 
			} else {
              //  debug(CCCCC + " - TOP OK");   
            }  
		} else {
    //       debug(CCCCC + " - LEFT OK");  
        }
        
	}
	return true;
}


