/**
 * can be used to equalize height of div elements, based on attribute class 
 *
 *  
 * usage:
 * - add css classes to the divs that need an equal height
 *   two css classes needs to be added: equalizeHeight groupKey
 *   group key can be anything you like  
 * - add x.js script in the head section from the (x)html document
 * - add this script in the head section from the (x)html document
 * 
 * example xhtml:
 <div id="column1" class="equalizeHeight columnGroup1">
...
</div>

<div id="column2" class="equalizeHeight columnGroup1">
...
</div>
 *
 * Distributed by GNU LGPL  
 * Copyright 2006 Masterlands IT Solutions www.masterlands.nl
 */  
 
/**
 * constructor, only used to allow static methods on this Class
 */ 
function EqualizeHeight() {
}

/**
 * context, by default document; can be another dom element, used 
 * when only part of the screen is refreshed 
 */ 
EqualizeHeight.context = document;

/**
 * a key value map
 * key: name of the group
 * value: Object of type EqualizeGroup,
 *        @see #EqualizeGroup   
 */ 
EqualizeHeight.groupMap = new Array();

/**
 * initialize event context, this can be used if only a part of the page
 * is reloaded with an integrated ajax framework (currently not public available) 
 */ 
EqualizeHeight.init = function(anEvent) {
    if (anEvent!=null) {
       EqualizeHeight.context = anEvent.getElement();
    }
    EqualizeHeight.initGroups();
    EqualizeHeight.equalizeGroups();
    
}

// add it to listener, support standalone usage for this script
if (typeof(PageRequest)!='undefined') {
    PageRequest.addOnPageStructureChangeEvent(EqualizeHeight.init);
} else {
    onload = 
        function() {
            EqualizeHeight.init();
        }
}


/**
 * init all groups, analyses document with dom, and collect divs
 * to be processed 
 */ 
EqualizeHeight.initGroups = function() {
    var myDivElements  = this.context.getElementsByTagName("div");
    var myDivElement;
    for(var i=0;i<myDivElements.length;i++) {
        myDivElement = myDivElements[i];
        if (EqualizeHeight.shouldEqualize(myDivElement)) {
            EqualizeHeight.initEqualizeElement(myDivElement);
        }
    }
}

/**
 * equalize the heights from all groups
 */ 
EqualizeHeight.equalizeGroups = function() {
    var myGroup;
    for(var myGroupName in this.groupMap) {
        myGroup = this.groupMap[myGroupName];
        EqualizeHeight.equalizeGroup(myGroup);        
    }
}

/**
 * equalize a single group
 */ 
EqualizeHeight.equalizeGroup = function(aGroup) {
    var myHeight = aGroup.getMaxHeight();
    for(var i in aGroup.getDomElements()) {
        var myElement = aGroup.getDomElements()[i];
        xHeight(myElement, myHeight);
        xShow(myElement);
    }
}

/**
 * initializes a dom Element, add it to the map
 * for processing it later  
 */ 
EqualizeHeight.initEqualizeElement = function(anElement) {
    var myClasses = anElement.className.split(" ");
    var i=0;
    
    // find position of equalizeHeight element
    for(var i=0;i<myClasses.length;i++) {
        if (myClasses[i]==="equalizeHeight") {
            break;
        }
    }
    
    var myGroup = myClasses[i+1];
    
    var myEqualizeGroup;    
    // add it to existing EqualizeGroup object, or construct one
    if (!this.groupMap[myGroup]) {
        myEqualizeGroup = new EqualizeGroup(myGroup);
        this.groupMap[myGroup] = myEqualizeGroup;        
    } else {
        myEqualizeGroup = this.groupMap[myGroup];
    }
    myEqualizeGroup.addDomElement(anElement);
}

/**
 * returns true if heights for this element should be equalized with a group
 */ 
EqualizeHeight.shouldEqualize = function(anElement) {
    return anElement.className.match("equalizeHeight");
    
}

/**
 * Class, holds data for a group 
 */ 
function EqualizeGroup(aGroup) {
    
    // (private) name of the group
    this.group=aGroup;
    
    // (private) dom elements in this group
    this.domElements=new Array();
    
}
/**
 * add another element to this group
 */ 
EqualizeGroup.prototype.addDomElement = function(aDomElement) {
    this.domElements[this.domElements.length]=aDomElement;
}

/**
 * get all dom elements from this group
 */ 
EqualizeGroup.prototype.getDomElements = function() {
    return this.domElements;
}

// Returns the height from the highest element
EqualizeGroup.prototype.getMaxHeight = function() {
    var myElement;
    var myRetHeight=0;
    
    for(var i in this.domElements) {
        myElement = this.domElements[i];
        myRetHeight = Math.max(myRetHeight, xHeight(myElement));
    }
    
    return myRetHeight;
}

/**
 * returns the name of this group
 */ 
EqualizeGroup.prototype.getGroup = function() {
    return this.group;
}




