<?php
// Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
?>
<div id="pageHint"></div>
<div id="subscriptionsGlobalOperations"></div>

<div id="billingOverview"><?php echo $this->billingOverviewContent; ?></div>
<div id="billingSubscriptions"><?php echo $this->billingSubscriptionsContent; ?></div>

<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function() {
	Jsw.namespace('Smb.Account.Index');
	Smb.Account.Index.onRemove = function(event) {
        Jsw.getComponent('subscriptions-list').execGroupOperation({
            url: '/account/delete/?redirect=/account/',
            subtype: 'delete',
            mouseEvent: event,
            locale: {
                confirmOnGroupOperation: <?php echo $this->subscriptionsList->jsLmsg('confirmOnDelete'); ?>
            },

            isAjax: <?php echo $this->isHomonymSearchEnabled ? 'true' : 'false' ?>,
            requestUrl: '/account/search-homonym-data/',
            loadingTitle: <?php echo $this->jsLmsg('components.homonyms.loading'); ?>
        });
    }

    new Jsw.Hint({
        renderTo: 'pageHint',
        hint: <?php echo $this->jsLmsg( $this->billingEnabled ? 'billingOverviewHint' : 'accountOverviewHint'); ?>
    });

    new Jsw.SmallTools({
        renderTo: 'subscriptionsGlobalOperations',
        operations: <?php echo $this->globalOperations; ?>
    });

    <?php if (!$this->billingEnabled): ?>

    var listOperations = (function() {
        var listOperations = [];
        return listOperations;
    }());

    var listColumns = (function() {
        var listColumns = [];

        listColumns.push({
            header: <?php echo $this->subscriptionsList->jsLmsg('subscription');?>,
            sortable: true,
            dataIndex: 'domain',
            renderer: function(item) {
                var subscriptionName = '';

                if (item.isMyResources) {
                    subscriptionName += '<a href="' + Jsw.baseUrl + '/account/my-resources">'
                        + item.domain + '</a>';
                } else {
                    subscriptionName += '<a href="' + Jsw.baseUrl + '/account/show/id/' + item.id + '">'
                        + item.domain + '</a>';
                    var hintId = 'hint-item-' + item.id;
                    subscriptionName += ' <span class="hint hint-info" id="' + hintId + '"><span>(?)</span></span>';
                }

                return subscriptionName;
            }
        });

        listColumns.push({
            header: <?php echo $this->subscriptionsList->jsLmsg('planName');?>,
            sortable: true,
            dataIndex: 'planName',
            renderer: function(item) {
                return ('' == item.planName)
                    ? '&mdash;'
                    : item.planName.escapeHTML();
            }
        });

        listColumns.push({
            header: <?php echo $this->subscriptionsList->jsLmsg('setupDate');?>,
            cls: 'text-right',
            headerCls: 't-r',
            sortable: true,
            dataIndex: 'setupDate',
            renderer: function(item) {
                return ('' == item.setupDate)
                    ? '&mdash;'
                    : item.setupDate;
            }
        });

        listColumns.push({
            header: '',
            cls: 'text-right',
            renderer: function(item) {
                if (item.isMyResources) {
                    return '';
                }

                var link =
                    '<span>'
                        + '<span class="tooltipData">'
                            + <?php echo $this->subscriptionsList->jsLmsg('switchToLinkHint'); ?>
                        + '</span>'
                        + '<a href="' + Jsw.baseUrl + '/account/switch/all/false/id/' + item.id + '" class="s-btn sb-switch">'
                            + '<i><i><i><span>'
                                + <?php echo $this->subscriptionsList->jsLmsg('switchToLink'); ?>
                            + '</span></i></i></i>'
                        + '</a>'
                    '</span>';

                return link
            }
        });

        return listColumns;
    }());

    new Jsw.Panel({
        cls: 'list-box',
        renderTo: 'billingSubscriptions',
        items: [{
            componentType: 'Jsw.List',
            id: 'subscriptions-list',
            data: <?php echo Zend_Json::encode($this->subscriptionsList->fetchData()); ?>,
            dataUrl: '/account/list-data',
            columns: listColumns,
            operations: listOperations,
            onRedraw: function() {
                $$('span.hint.hint-info').each(function(hintElement) {
                    new Jsw.AjaxTooltip.Instance({
                        title: <?php echo $this->jsLmsg('subscriptionsHintTitle'); ?>,
                        waitMsg: <?php echo $this->jsLmsg('subscriptionsHintWaitMessage'); ?>,
                        url: '/account/stats-hint/id/' + hintElement.id.replace('hint-item-', ''),
                        target: hintElement.id
                    });
                });
            },
            isDisabledItem: function(item) {
                return (item.id == 0);
            }
        }]
    });
    <?php endif; ?>

});
//]]>
</script>
