// JavaScript Document


var $expandMaxHeight = 0;
var $expandInterval;
var GROW = 1;
var SHRINK = -1;
function smoothChange(object, factor) {
    var k = Math.abs((1 - object.offsetHeight/$expandMaxHeight) - object.offsetHeight/$expandMaxHeight);
    
    if (k<0.4) {
        var growth = 4*factor; //в середине элемента скорость прироста высоты быстрее
    } else {
        var growth = 4*factor; // чем в начале и конце элемента
    }
    
    if (
        ( (factor == GROW) && ((object.offsetHeight + growth) > $expandMaxHeight) ) ||
        ( (factor == SHRINK) && ((object.offsetHeight + growth) < 0) )
        )
    {
        growth = factor>0 ? ($expandMaxHeight - object.offsetHeight) : 0;
        clearInterval($expandInterval);
        $expandInterval = null;
    }
    
    object.style.height = object.offsetHeight + growth + 'px';
    if (factor == SHRINK && !$expandInterval) {
        object.style.height = $expandMaxHeight + 'px';
        object.style.display = 'none';
    }
}
function smoothShowVertical(object) {
    if ($expandInterval)
        return false;
        
    object.style.height = '0px';
    $expandInterval = setInterval(function(object) { return function () { smoothChange(object, GROW) } }(object), 20);
}
function smoothHideVertical(object) {    
    if ($expandInterval)
        return false;
    $expandInterval = setInterval(function(object) { return function () { smoothChange(object, SHRINK) } }(object), 20);
}
var CURRENT_TARGET;
function show(aID) {
    if ($expandInterval)
        return false;
    var target = document.getElementById(aID).style;
    var value = target.display == 'block' ? 'none' : 'block';
        
    if (CURRENT_TARGET && CURRENT_TARGET != target) {
        CURRENT_TARGET.display = 'none';
    };
    
    if (target.display == 'none') {
        CURRENT_TARGET = target;
        target.display = 'block';
    
        //---
        object = document.getElementById(aID);
        $expandMaxHeight = object.offsetHeight;
        object.style.height = '0px';
        smoothShowVertical(document.getElementById(aID));
        //---
    } else {
        $expandMaxHeight = object.offsetHeight;
        smoothHideVertical(document.getElementById(aID));
        //target.display = 'none';
    }
    
};
