<?php 
    $script = 'var createUrl = "'.$this->createUrl('create').'";';
    
    Yii::app()->clientScript->registerScript('testid', $script, CClientScript::POS_HEAD);
    

?>
<?php
Yii::app()->clientScript->registerScript('search', "
var createUrl = '".$this->createUrl('create')."';

$('.search-button').click(function(){
    $(this).parent().toggleClass('active');
    $('.filters').toggle();
    if(updateTimer){
       $('.autoRefresh').parent().removeClass('active');
       stopUpdatingGrid(updateTimer);
    }
    return false;
});
$('.search-form form').submit(function(){
    $.fn.yiiGridView.update('calendar-grid', {
        data: $(this).serialize()
    });
    return false;
});

var updateTimer;


function stopUpdatingGrid(){
    clearInterval(updateTimer);
    updateTimer = null;
}


function updateGrid(){
    $.fn.yiiGridView.update('calendar-grid');
}

$('#addNewCalendarButton').click(function(){
    $('#modalHeader').html('".__("CALENDAR_HEADER_CREATE")."');
    $.ajax({
      url: '".url("calendarGeneral/default/create")."',
    })
    .success(function(data) {
      $('.modal-body').html(data);
      $('#newCalendarModal').modal('show');
    });
});

// submit on enter key
$('#newCalendarModal').on('shown', function () {
    $('#newCalendarModal').find('input[type=text],textarea,select').filter(':visible:first').focus();
    $('.modal-backdrop').unbind('click');
    
    $('#newCalendarModal').on('keyup', this, function(e){
        if(e.keyCode == 13 && $(this).is(':visible')){
             $('#saveNewCalendarButton').click();
        }
    });
});


", null);
?>

<?php $this->renderPartial('ajaxCreate',array(
        'model'=>$model,
    ));
?>
<?php $this->widget('ext.PlNavbar', array(
        'brand'=>__('CALENDAR_NAVBAR_TITLE'),
        'htmlOptions'=>array(
            'data-translation-id'=>'CALENDAR_NAVBAR_TITLE',
        ),
        'items'=>array(
            array(
                'class'=>'bootstrap.widgets.TbMenu',
                'items'=>array(
                    //array('label'=>'Admin', 'url'=>'', 'active'=>true, 'icon'=>'list'),
                    '---',
                    array(
                        'label'=>__('CALENDAR_NAVBAR_NEW_CALENDAR'),
                        'url'=>'#',
                        'icon'=>'plus',

                        'linkOptions'=>array(
                            'id'=>'addNewCalendarButton',
                           // 'onclick'=>"$('#newCalendarModal').modal('show')",
                            'data-target'=>'#newCalendarModal',
                            'data-translation-id'=>'CALENDAR_NAVBAR_NEW_CALENDAR',
                        ),
                    ),
                    array(
                        'label'=>__('CALENDAR_NAVBAR_SEARCH'),
                        'url'=>'#',
                        'icon'=>'search',
                        'linkOptions'=>array(   'class'=>'search-button',
                            'data-translation-id'=>'CALENDAR_NAVBAR_SEARCH',
                        ),

                    ),
                ),
            ),
        ),
    )); ?>

<?php

$this->widget('bootstrap.widgets.TbGridView', array(
        'id'=>'calendar-grid',
        'dataProvider'=>$model->search(),
        'type'=>'striped bordered condensed',                  //change color from columns in 'white' and 'grey' responsively
        'itemsCssClass'=>'plTable',                           //change design of the table, removed search bar
        'filter'=>$model,
        //         'selectableRows' => 0,
        'columns'=>array(
        	array(
        			'name' => 'title',
        			'header' => $model->getAttributeLabel('title'),
        			'headerHtmlOptions'=>array('data-translation-id'=>'CALENDAR_MODEL_TITLE')
        	),
        		array(
        				'name' => 'color',
        				'header' => $model->getAttributeLabel('color'),
        				'headerHtmlOptions'=>array('data-translation-id'=>'CALENDAR_MODEL_COLOR')
        		),
            array(
                'class'=>'bootstrap.widgets.TbButtonColumn',
                'htmlOptions'=>array('class'=>'buttonColumnIcons'),
                'template'=>'{ajaxUpdate}{delete}',
                'buttons'=>array(
                    //'ajaxUpdate' => array(
                    //    'label'=>'ajax',
                    //    'url'=>'Yii::app()->createUrl("ajaxUpdate", array("id"=>$data->id))',
                    //),

                    'ajaxUpdate'=>array(
                        'label' => '<i class="icon-pencil"></i>',
                        'imageUrl'=> false,
                        'options'=>array(
                            'title'=>'Update',
                            //'data-id'=>'$data->id',
                        ),
                        'url'=>'Yii::app()->createUrl("calendarGeneral/default/update", array("id"=>"$data->id"))',
                        'click'=>'js:function(){
                                $.get(this.href, function(data){
                                $(".modal-body").html(data);
                                $("#modalHeader").html("'.__('CALENDAR_HEADER_UPDATE').'");
                                $("#newCalendarModal").modal("show");
                                return false;
                            });
                        return false;
                        }',
                    ),
                ),
            ),
        ),
    ));
?>