﻿function MaxBox(BoxID, minItem, pHeight, itemTagName, itemClassName) {
    if ($id(BoxID).style.height.replace('px', '') == '') { $id(BoxID).style.height = $id(BoxID).clientHeight + 'px';}
	var MinHeight = BoxItemHeight(BoxID, minItem, pHeight, itemTagName, itemClassName);
	if (parseInt($id(BoxID).clientHeight) <= 0)
	setBoxHeight(BoxID, MinHeight, '+');
	else
    setBoxHeight(BoxID, $id(BoxID).scrollHeight, '+');
}
function MinBox(BoxID, minItem, pHeight, itemTagName, itemClassName) {
    if ($id(BoxID).style.height.replace('px', '') == '') $id(BoxID).style.height = $id(BoxID).clientHeight + 'px';
	var MinHeight = BoxItemHeight(BoxID, minItem, pHeight, itemTagName, itemClassName);  
    if (MinHeight >= parseInt($id(BoxID).style.height)) setBoxHeight(BoxID, 0, '-');
    else setBoxHeight(BoxID, MinHeight, '-');   
}
function SetHeightofBox(BoxID, minItem, pHeight, itemTagName, itemClassName)
{
	var MinHeight = BoxItemHeight(BoxID, minItem, pHeight, itemTagName, itemClassName);
	$id(BoxID).style.height = MinHeight;
}

function BoxItemHeight(BoxID, minItem, pHeight, itemTagName, itemClassName)
{
	var MinHeight = 0;
	var Items = $tagClass(BoxID, itemTagName, itemClassName);
	if (Items.length > 0)
	{
		if (Items.length < minItem) minItem = Items.length;
		for (var i=0; i < minItem ; i++)
		{
			if (Items[i].clientHeight != null)
			MinHeight += parseInt(Items[i].clientHeight) + (2 * pHeight);
		}
	}
	return MinHeight;
}
function setBoxHeight(BoxID, endHeight, state) {

    switch (state) {
        case '+': setBoxHeightAdd(BoxID, endHeight);
            break;
        case '-': setBoxHeightMin(BoxID, endHeight);
            break;
        case '=':
            break;
    }
    function setBoxHeightAdd(BoxID, endHeight) {
        var Index = 0;
        var beginHeight = parseInt($id(BoxID).style.height);
        var heightAnimIDPlus = setInterval(animHeightPlus, 1);
        function animHeightPlus() {
            if (parseInt($id(BoxID).style.height) <= endHeight) {
                if (parseInt($id(BoxID).style.height) + Index <= endHeight) {
                    $id(BoxID).style.height = parseInt($id(BoxID).style.height) + Index + 'px';
                    Index++;
                }
                else {
                    $id(BoxID).style.height = endHeight + 'px';
                    clearInterval(heightAnimIDPlus);
                }
            }
            else clearInterval(heightAnimIDPlus);
        }
    }

    function setBoxHeightMin(BoxID, endHeight) {
        var Index = 0;
        var beginHeight = parseInt($id(BoxID).style.height);
        var heightAnimIDMinus = setInterval(animHeightMinus, 1);
        function animHeightMinus() {
            if (parseInt($id(BoxID).style.height) > endHeight) {
                if (parseInt($id(BoxID).style.height) - Index >= endHeight) {
                    $id(BoxID).style.height = (parseInt($id(BoxID).style.height) - Index) + 'px';
                    Index++;
                }
                else {
                    $id(BoxID).style.height = endHeight + 'px';
                    clearInterval(heightAnimIDMinus);
                }
            }
            else clearInterval(heightAnimIDMinus);
        }
    }
}