﻿// (c) Copyright ADGroup.
// All other rights reserved.

function Expander(control, time, orientationVertical, text) {
    /// <summary>
    /// This class used for expand or collapse any object / Этот класс используется для раскрытия или закрытия любого объекта
    /// </summary>
    /// <param name="control" type="Object" mayBeNull="false">
    /// Expandable or collapsable object / Раскрывающийся или закрывающийся объект
    /// </param>
    /// <param name="time" type="Number" mayBeNull="true">
    /// Expandable or collapsable time in seconds (1 second by default) / Время в секундах, за которое необходимо раскрыть или закрыть объект (по умолчанию 1 секунда)
    /// </param>
    /// <param name="orientationVertical" type="Boolean" mayBeNull="true">
    /// Orientation for Expand or collapse (vertical by default) / Ориентация для раскрытия или закрытия объекта (по умолчанию вертикальная)
    /// </param>
    /// <param name="text" type="String" mayBeNull="true">
    /// Text for innerHTML of object, if you need change them / Текст для innerHTML объекта, если его необходимо сменить
    /// </param>
    
    if (!control)
        throw "Control is null / Не указан объект";

    // object / объект
    this._control = control;
    // orientation / ориентация
    this._orientationVertical = orientationVertical == false ? orientationVertical : true;

    // current width or height (by orientation) of object / текущая ширина или высота (по ориентации) объекта
    if (this._control.style.display == "none") {
        if ((this._orientationVertical && this._control.style.pixelHeight) || (!this._orientationVertical && this._control.style.pixelWidth))
            this._controlSize = this._orientationVertical ? this._control.style.pixelHeight : this._control.style.pixelWidth;
        else {
            var size = this._orientationVertical ? this._control.style.height : this._control.style.width;
            if (size.length > 2 && size.substring(size.length - 2, size.length).toLowerCase() == "px") {
                size = size.substring(0, size.length - 2);
            }
            this._controlSize = parseInt(size);
        }
    } else {
        this._controlSize = this._orientationVertical ? this._control.clientHeight : this._control.clientWidth;
    }

    // innerHTML text / innerHTML текст
    this._text = text ? text : "";

    // time / время
    if (!time) time = 1;

    // calculate _sizeInterval and _timeInterval by time / рассчитать _sizeInterval и _timeInterval по времени
    this._sizeInterval = 1;
    this._timeInterval = 20;
    while (this._timeInterval = time * 1000 / ((this._controlSize / this._sizeInterval) * 2) < 20) {
        this._sizeInterval++;
    }
    
    // default action - collapse object then expand / действие по умолчанию - закрыть и раскрыть объект
    this._action = "collapseexpand";
}

Expander.prototype = {

    _process: function(expand) {
        /// <summary>
        /// Еxpand or collapse object / Раскрыть или закрыть объект
        /// </summary>
        /// <param name="expand" type="Boolean" mayBeNull="true">
        /// Action - Expand or collapse / Действие - раскрыть или закрыть
        /// </param>
    
        var newSize;
        var self = this;
        if (expand) {
            // expand / раскрыть
            if (this._control.style.display == "none") {
                if (this._orientationVertical)
                    this._control.style.height = "0px";
                else
                    this._control.style.width = "0px";
                this._control.style.display = "";
            }
            newSize = (this._orientationVertical ? this._control.clientHeight : this._control.clientWidth) + this._sizeInterval;
            if (newSize > this._controlSize)
                newSize = this._controlSize;
            if (this._orientationVertical)
                this._control.style.height = newSize + "px";
            else
                this._control.style.width = newSize + "px";
            if ((this._orientationVertical && this._control.clientHeight < this._controlSize) || (!this._orientationVertical && this._control.clientWidth < this._controlSize))
                setTimeout(function() { self._process(true); }, this._timeInterval);
        } else {
            // collapse and if you need, change innerHTML / закрыть и если нужно, изменить innerHTML
            newSize = (this._orientationVertical ? this._control.clientHeight : this._control.clientWidth) - this._sizeInterval;
            if (newSize < 0)
                newSize = 0;
            if (this._orientationVertical)
                this._control.style.height = newSize + "px";
            else
                this._control.style.width = newSize + "px";
            if ((this._orientationVertical && this._control.clientHeight > 0) || (!this._orientationVertical && this._control.clientWidth > 0))
                setTimeout(function() { self._process(false); }, this._timeInterval);
            else {
                if (this._text != "")
                    this._control.innerHTML = this._text;
                if (this._action == "collapseexpand")
                    setTimeout(function() { self._process(true); }, this._timeInterval);
                else {
                    this._control.style.display = "none";
                    if (this._orientationVertical)
                        this._control.style.height = this._controlSize + "px";
                    else
                        this._control.style.width = this._controlSize + "px";
                }
            }
        }
    },

    CollapseAndExpand: function() {
        /// <summary>
        /// Collapse and expand object / Закрыть и раскрыть объект
        /// </summary>
        
        this._action = "collapseexpand";
        this._process(false);
    },

    Collapse: function() {
        /// <summary>
        /// Collapse object / Закрыть объект
        /// </summary>
    
        this._action = "collapse";
        this._process(false);
    },

    Expand: function() {
        /// <summary>
        /// Expand object / Раскрыть объект
        /// </summary>
    
        this._action = "expand";
        this._process(true);
    }

}