<?php

$this->widget->init('topbar', array(
    'back'    => N2Html::tag('a', array(
            'class' => 'n2-ss-back-dashboard n2-h4 n2-uc',
            'href'  => $this->appType->router->createUrl(array(
                "sliders/index"
            ))
        ), n2_('Dashboard')) . N2Html::tag('a', array(
            'id'    => 'n2-ss-preview',
            'href'  => $this->appType->router->createUrl(array(
                "preview/index",
                array('sliderid' => $slider['id']) + N2Form::tokenizeUrl()
            )),
            'class' => 'n2-h4 n2-uc'
        ), n2_('Preview')),
    "actions" => array(
        N2Html::tag('a', array(
            'href'    => $this->appType->router->createUrl(array(
                "sliders/index"
            )),
            'onclick' => 'return nextend.cancel(this.href);',
            'class'   => 'n2-button n2-button-normal n2-button-l n2-radius-s n2-button-red n2-h4 n2-b n2-uc',
        ), n2_('Cancel')),
        N2Html::tag('a', array(
            'href'    => '#',
            'class'   => 'n2-button n2-button-normal n2-button-l n2-radius-s n2-button-green n2-h4 n2-b n2-uc',
            'onclick' => 'return NextendForm.submit("#smartslider-form");'
        ), n2_('Save'))
    )
));

$this->widget->init('heading', array(
    'title'   => $slider['title'],
    'actions' => $_class->getGroupButtons($slider)
));
?>

<div class="n2-ss-sliders-outer-container">
    <?php
    $this->renderInline("slidermanager", array(
        'groupID' => $slider['id']
    ));
    ?>
</div>
<div class="n2-clear"></div>
<form id="smartslider-form" class="n2-ss-slider-edit" action="" method="post">
    <?php
    $_class->renderGroupForm($slider);
    ?>
    <input name="save" value="1" type="hidden"/>
</form>
    <script type="text/javascript">
    nextend.ready(
        function ($) {

            var form = $('#smartslider-form'),
                formAction = form.attr('action'),
                headings = $('.n2-top-bar-menu .n2-tab-heading'),
                newWindow = <?php echo intval(N2SmartSliderSettings::get('preview-new-window', 0)); ?>;

            if (!newWindow) {
                var modal = new NextendSimpleModal('<iframe name="n2-tab-preview" src="about:blank" style="width: 100%;height:100%;"></iframe>', {
                    class: 'n2-ss-preview-modal'
                });
                modal.modal.on('ModalHide', function () {
                    modal.modal.find('iframe').attr('src', 'about:blank');
                    $(window).trigger('SSPreviewHide');
                });
            }

            var isPreview = false;

            form.on('submit', function (e) {
                if (!isPreview) {
                    e.preventDefault();
                    nextend.askToSave = false;
                    NextendAjaxHelper.ajax({
                        type: 'POST',
                        url: NextendAjaxHelper.makeAjaxUrl(window.location.href),
                        data: $('#smartslider-form').serialize(),
                        dataType: 'json'
                    }).done(function () {
                        $('.n2-heading-bar .n2-heading').html($('#slidertitle').val());
                        $('dt.n2-ss-slide2-list > a > span').eq(0).html($('#slidertitle').val());
                        nextend.askToSave = true;
                        n2('#smartslider-form').trigger('saved');
                    });
                }
            });

            $('#n2-ss-preview').on('click', function (e) {
                isPreview = true;
                e.preventDefault();
                if (!newWindow) {
                    modal.show();
                } else {
                    NextendNewFullWindow('', 'n2-tab-preview');
                }
                form.attr({
                    action: '<?php echo $this->appType->router->createUrl(array(
                        "preview/index",
                        N2Form::tokenizeUrl() + array('sliderid' => $slider['id'])
                    ))?>',
                    target: 'n2-tab-preview'
                }).submit().attr({
                    action: formAction,
                    target: null
                });
                isPreview = false;
            });
        }
    );
</script>
<?php N2SS3::showBeacon('Slider settings'); ?>
