<?php
// Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<script type="text/javascript">
//<![CDATA
Jsw.namespace('AdminPanel.Health');
AdminPanel.Health.Chart = {

	cache: $H(),

	paramOnClick: function(checkbox, paramId, elementId) {
        var checkbox = $(checkbox);
        if (checkbox.checked) {
            this.scrollOnLoad = elementId;
            this.show(paramId, elementId);
            this.updateScroll(elementId);
        } else {
            this.hide(elementId);
        }
    },

    show: function(paramId, elementId)
    {
        <?php if (!$this->targetId): ?>
    	Jsw.Cookie.set('health-parameter-' + elementId + '-chart', 'show');
        <?php endif; ?>

        var container = $('vdc-' + elementId);
        var period = $('sp-' + elementId).value;

        var img = this._getCachedImage(paramId, period);
        if (img) {
            container.update(img);
        } else {
            container.update('<div class="b-visual-data-loading"><span class="ajax-loading"><?php echo $this->lmsg('loading')?></span></div>');
        	img = new Element('img', {
                "src": Jsw.baseUrl + "/health/chart-image/?parameterId=" + encodeURIComponent(paramId) + '&period=' + $('sp-' + elementId).value + "&" + <?php echo $this->jsEscape(md5(time()));?>
            });
            this._setCachedImage(paramId, period, img);
            img.observe('load', function() {
                container.update(img);
                if (this.scrollOnLoad == elementId) {
                    this.updateScroll(elementId);
                }
            });
        }

        container.up('tr').removeClassName('t-collapsible-content-collapsed');
    },

    hide: function(elementId)
    {
        <?php if (!$this->targetId): ?>
        Jsw.Cookie.remove('health-parameter-' + elementId + '-chart');
        <?php endif; ?>

        var container = $('vdc-' + elementId);
        var tr = container.up('tr');
        tr.addClassName('t-collapsible-content-collapsed');
    },

    onChangePeriod: function(paramId, elementId)
    {
    	var img = $('vdc-' + elementId).down('img');
    	if (img) {
        	img.remove();
    	}
        <?php if (!$this->targetId): ?>
        Jsw.Cookie.set('health-parameter-' + elementId + '-period', $('sp-' + elementId).value);
        <?php endif; ?>
    	this.show(paramId, elementId);
    },

    updateScroll: function(elementId) {
        var elementControl = $('hp-' + elementId).up('tr');
        var elementChart = $('vdc-' + elementId).up('tr');
        var eTopOffset = Element.cumulativeOffset(elementControl).top;
        var eHeight = elementControl.getHeight() + elementChart.getHeight();

        this._updateScroll(eTopOffset, eHeight);
    },

    _getCachedImage: function(paramId, period) {
    	return this.cache.get(paramId) && this.cache.get(paramId).get(period)
            ? this.cache.get(paramId).get(period)
            : null;
    },

    _setCachedImage: function(paramId, period, img) {
        if (!this.cache.get(paramId)) {
        	this.cache.set(paramId, $H());
        }
        this.cache.get(paramId).set(period, img);
    },

    groupOnClick: function(groupId) {
        var groupElement = $('pg-' + groupId);
        var expanded = !groupElement.hasClassName('t-collapsible-collapsed');
        if (expanded) {
        	this.hideGroup(groupId);
        } else {
            this.showGroup(groupId);
            this.updateGroupScroll(groupId);
        }
    },

    updateGroupScroll: function(groupId) {
        var element = $('pg-' + groupId);
        var eTopOffset = Element.cumulativeOffset(element).top;
        var eHeight = element.getHeight();

        this._updateScroll(eTopOffset, eHeight);
    },

    _updateScroll: function(eTopOffset, eHeight) {
        var vTopScroll = document.viewport.getScrollOffsets().top;
        var vHeight = document.viewport.getHeight();

        if (eHeight > vHeight || eTopOffset < vTopScroll) {
            window.scrollTo(0, eTopOffset);
        } else
        if (eTopOffset + eHeight > vTopScroll + vHeight) {
            window.scrollTo(0, eTopOffset - (vHeight - eHeight));
        }
    },

    showGroup: function(groupId) {
        var groupElement = $('pg-' + groupId);
        <?php if (!$this->targetId): ?>
        Jsw.Cookie.set('health-group-' + groupId + '-expanded', 'true');
        <?php endif; ?>
        groupElement.removeClassName('t-collapsible-collapsed');
    },

    hideGroup: function(groupId) {
        var groupElement = $('pg-' + groupId);
        <?php if (!$this->targetId): ?>
        Jsw.Cookie.remove('health-group-' + groupId + '-expanded');
        <?php endif; ?>
        groupElement.addClassName('t-collapsible-collapsed');
    }
}
//]]>
</script>
<?php
foreach ($this->healthParametersList as $group) {
    echo $this->partial('health/_group.phtml', array(
        'group' => $group,
        'targetId' => $this->targetId,
    ));
}

echo $this->partial('partials/health-thresholds-hint.phtml');
