/**
 * expandables v0.1
 * displays a defined part of a container's contents and offers links to toggle 
 * the view (expand or collapse the container height)<br>
 * example code:<br>
 * <code>
 * <p class="expandable" style="min-height: 10.5em;"></p><br>
 * <div><br>
 *    <a style="display:none;" class="expandlink" href="#" onclick="expand(this);return false;">Expand</a><br>
 *    <a class="collapselink" href="#" onclick="collapse(this);return false;">Collapse</a><br>
 * </div><br>
 * </code>
 * where <code>min-height</code> defines the collapsed height of the container 
 * (not including the links below), the expanded height fits to the contents
 * (css: height:auto)
 */

/**
 * initializes an expandable
 */
function initExpandable(xpd) {
    xpd = $(xpd);
    xpd.css('overflow','hidden');
    container = xpd.parent();
    var origHeight = xpd.height();
    xpd.height(xpd.css('min-height')); // collapse
    if(xpd.height() < origHeight) { // originally higher than collapsed?
        container.find(".expandlink").show(); // yes: leave collapsed, show expand link
    } else {
        xpd.height('auto'); // no: auto-size
        xpd.css('min-height',0); // reset min-height
    }
}
/**
 * initializes all expandables
 */
function initExpandables() {
    $('.expandable').each(function(i) {
        initExpandable(this);
      }
    );
}
/**
 * sets related expandable to auto and swaps link
 */
function expand(sender) {
    var xpd = [];
    var container = $(sender);
    while(xpd.length == null || xpd.length == 0) {
        container = container.parent();
        xpd = container.find('.expandable');
    }
    xpd.css('min-height',xpd.height());
    xpd.height('auto');
    xpd.parent().parent().css('background-color','#fffffe'); /* IE8 force repaint */
    container.find('.expandlink').hide();
    container.find('.collapselink').show();
}
/**
 * restores related expandable to min-height value and swaps link
 */
function collapse(sender) {
    var xpd = [];
    var container = $(sender);
    while(xpd.length == null || xpd.length == 0) {
        container = container.parent();
        xpd = container.find('.expandable');
    }
    xpd.height(xpd.css('min-height'));
    xpd.parent().parent().css('background-color','#ffffff'); /* IE8 force repaint */
    container.find('.expandlink').show();
    container.find('.collapselink').hide();
}

/**
 * init all expandables on the page when dom is ready
 */
/*$(function() { // document ready
    initExpandables();
});*/