/*
' **************************************************
' © VWA Software Development 2008
' Release: 1.01.0000
' Changed: Okt 22, 2008
' Author : H. Salomons
' **************************************************
*/

var mY                  = 1;   // De Onthouden hoogte van scherm          
var bDivSetLoaded       = false;
// Contstanten voor resize behaviour ennumration
var cFixed              = 0;
var cVariable           = 1;
var cGrow               = 2;
var bUpdate             = false;
var bPendingSetHeight   = false;
var setHeightTimer      = null;
var iDivsetHeight       = 0; // Hoogte van divset
var iDivSetWidth        = 0; // Breedte van divset
var iViewPortHeight     = 0; // Hoogte van scherm..
var iIframeCount        = 0; // Aantal pogingen dat de hoogte van een iframe wordt bepaald
var iDivsetY            = 0;

// Onload handler
function DivSetLoad(){
    window.onresize = setHeight;
    setHeight(true);
}


function DivCollection(){   
    this.items = new Object();   
    
    // this.height bepaald of de DivsetDiv (content) Verkleind moet worden
    // afhankelijk van boven/ondermarge
    // 0 = Variabel
    // >0 = vast
    // <0 = Volledige breedte/hoogte
    this.height = '0'; 
}


// Bepaald de juiste client hoogte van kader.
DivCollection.prototype.GetClientHeight = function(sID){
    var hDiv = $get(sID);        
    var iHeight2 = hDiv.offsetHeight;        
    var iHeight = hDiv.getElementsByTagName("DIV")[0].offsetHeight;        
    return (iHeight2 > iHeight) ? iHeight2 : iHeight;     
}

DivCollection.prototype.Init = function(){
   bDivSetLoaded = true;  
}

function setHeight() {
         
    var oDiv                = null;   // Object met div gegevens
    var div                 = null;   // Werkelijke div uit DOM  
    var bounds              = null; // Object met x/y/width/height van html element
    var bVarDivFound        = false;
  
    var iDivsetX            = 0;
        
     // Hoogte van scherm (Viewport) bepalen  
    GetScreenSize()// iWindowWidth , iWindowHeight bepalen
          
    // Bepalen of scherm wordt vergroot of verkleind
    bDecrease = (iWindowHeight < mY);    
    mY = iWindowHeight;    
     
    if(typeof(divs) != "undefined" &&  divs){
        // Y positie van divset in document,
        // ( indien naar beneden is gescrolt, -y )
        
        iDivsetY = Sys.UI.DomElement.getBounds($get('DivSet')).y;       
        iDivsetWidth = divs.width;
        
       
        iDivsetHeight = SetDiv(divs.items);
        iDivsetHeight -= iDivsetY;
      
        // Controleren op (minimale) vaste hoogte  
        if(divs.height > 0 && iDivsetHeight > divs.height){ 
           iDivsetHeight = divs.height;             
        }
            
        if (iDivsetWidth > divs.width){ 
            $get("tdMainMiddle").style.width = iDivsetWidth + 'px';
            $get("DivSet").style.width = iDivsetWidth + 'px';
        }
        
        if (divs.height == 0)
            $get('DivSetWrapper').style.overflow = 'auto';
        else
            $get("tdMainMiddle").style.height = ((iDivsetHeight > iWindowHeight || divs.height>0) ? iDivsetHeight : iWindowHeight -10) + 'px';
        
        $get("DivSet").style.height = iDivsetHeight + 'px';                
        
        if (!bPendingSetHeight)
            setHeightTimer = setTimeout(setHeight2, 20);
             
        bPendingSetHeight = true;        
    }           
}

function SetDiv(aDivs){
    var oDiv            = null; // Object met div gegevens
    var div             = null; // Werkelijke div
    var iMaxYChildren   = 0;
    var iMaxY         = 0;
    
    for(var i=0; i < aDivs.length; i++){                
        oDiv = aDivs[i];     
        div = document.getElementById(oDiv.Id);
        
        // Eerst de kinderen zetten + hoogste y positie opvragen.
        if (oDiv.Children.length > 0){            
           iMaxYChildren = SetDiv(oDiv.Children);     
        }
        
        // Dimensie van scroll div opzoeken.        
        var bounds = Sys.UI.DomElement.getBounds(div.getElementsByTagName('DIV')[0]);
                                         
        switch(oDiv.ResizeWidthBehaviour){
            case cVariable:
                bVarDivFound = true;
                if(bDecrease)
                    div.style.width = '2px';   
                break;
            default:
                div.style.width = oDiv.Width;     
        }
        
        switch(oDiv.ResizeHeightBehaviour){
            case cVariable:
                bVarDivFound = true;
                if(bDecrease)
                    div.style.height = '2px';
                bounds.height = 2; 
                break;
            case cGrow:   
               // Controleren of er een IFrame geladen is.
               var oUpdPnl =  $get(oDiv.Id + 'U');
               if (oUpdPnl) {
                   var aIframe = oUpdPnl.getElementsByTagName('IFRAME');               
                   if (aIframe.length > 0){
                       var oDoc = aIframe[0].contentWindow.document;
                       
                       if(oDoc.readyState && (oDoc.readyState != 'complete' || iIframeCount >100)) {
                           bPendingSetHeight = true;
                           setTimeout(setHeight, 300);
                           iIframeCount += 1;          
                       } else {  
                           iIframeCount = 0;
                           bPendingSetHeight = false;                 
                           bounds.height = oDoc.body.scrollHeight;  
                           $get(oDiv.Id + 'U').style.height =  bounds.height + 'px';                                                   
                       }                 
                   }
               }
                // Controleren of hoogte bijgesteld moet worden naar kinderen.
                if (((bounds.height + bounds.y) < iMaxYChildren)){
                      bounds.height = bounds.height + (iMaxYChildren - (bounds.height + bounds.y));                         
                }
                // Controleren op Minimale hoogte
               if(bounds.height <= oDiv.Height){                       
                    bounds.height = toNumber(oDiv.Height);                  
               }   
               div.style.height = bounds.height + 'px';
               var img = $get(oDiv.Id + '_imgwrp'); 
               if(img)
                    img.style.height = div.style.height;                    
               break;                                 
            default:
                div.style.height = oDiv.Height;
                bounds.height = toNumber(oDiv.Height);            
        }
          
        // Hoogste Y coord. van dit kader (scherm) behouden.           
        if((bounds.y + bounds.height) > iMaxY)
            iMaxY = (bounds.y + bounds.height);       
       
        oDiv.ClientHeight = bounds.height;       
    }   
    return iMaxY;
}

// Variabele divs nog resizen.
function setHeight2(){
        setDiv2(divs.items, iDivsetHeight);       
        bPendingSetHeight = false;      
}

function setDiv2(aDivs, iParentHeight){
     // alle divs in collectie resizen
    for(var i=0; i<aDivs.length; i++){
        oDiv = aDivs[i];        
        div = document.getElementById(oDiv.Id);
             
         switch(oDiv.ResizeWidthBehaviour){
            case cVariable:
                if(iDivSetWidth > oDiv.MarginWidth){
                  div.style.width = iDivSetWidth - oDiv.MarginWidth + 'px';
                }
                break;
        }
        
        switch(oDiv.ResizeHeightBehaviour){
            case cVariable:
                if(iParentHeight > oDiv.MarginHeight){                        
                    div.style.height = iParentHeight - oDiv.MarginHeight + 'px';
                }
                break;                        
        }
        if (oDiv.Children.length>0){            
           iMaxYChildren = setDiv2(oDiv.Children, oDiv.ClientHeight);     
        }        
    }
}