<?php

$pattern = str_replace(array('-d','/d','d.'),array('','',''),phpUserDatePattern());
$html='';
        $html.='<div
                class=" input-append input-prepend date dateTimePicker navbar-search"
                id="datetimepicker" 
            >';        
	  $html.='<span class="add-on"><i class="icon-arrow-left" onclick="prevMonth()"></i></span>';
	  $html.= CHtml::textField('StatisticDay',date($pattern), array(
            
            'size'=>60,
            'maxlength'=>256,
            'readonly'=>'readonly',
            'disabled' => 'disabled',
            'class'=>'span1'
        ));

        $html.='<span class="add-on"><i class="icon-th"></i></span>';
        $html.='<span class="add-on"><i class="icon-arrow-right" onclick="nextMonth()"></i></span>';
        $html.='</div>';

      echo $html;
	
      $dataUrl =  url('analyticsDailyReport/default/createReport',
	array(
	  'id'=>isset($_GET['id']) ? $_GET['id'] : $model ? $model->id :'',
	  'targetType'=>isset($_GET['targetType']) ? $_GET['targetType'] : $model ? $this->getStatisticTypeByClassName(get_class($model)) :'',
	)
      );
?>
<script>
var pattern = '<?php echo DTPUserDatePattern() ?>';
pattern = pattern.replace('-dd','');
pattern = pattern.replace('/dd','');
pattern = pattern.replace('dd.','');
// endDate = parseDate($('#StatisticDay').val());
var date = new Date();
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

var dailyOptions = {
    startView:4,
    minView:3,
    todayBtn:true,
    pickerPosition:'bottom-left',
    showMeridian:false,
    endDate: lastDay,//new Date(new Date(endDate).getTime() + (31*24 * 60 * 60 )),
    date: null,
    format: pattern,
    autoclose:true,
    forceParse:false
}

$('.dateTimePicker').datetimepicker(dailyOptions)

$('#StatisticDay').on('change',function(ev) {  
      changeDate();
      updateDiagram();
});
function nextMonth() {

    var date = parseDate($('#StatisticDay').val());
    $('.dateTimePicker').datetimepicker('setDate',new Date(new Date(date).getTime() + (32*24 * 60 * 60 * 1000)));
    $('#StatisticDay').trigger('change');
    
}

function prevMonth() {

    var date = parseDate($('#StatisticDay').val());
    $('.dateTimePicker').datetimepicker('setDate',new Date(new Date(date).getTime() - (24 * 60 * 60 * 1000)));
    $('#StatisticDay').trigger('change');
    
}

function parseDate(date) {

    var pattern = '<?php echo phpUserDatePattern() ?>';
    var delimiter = pattern.substr(1,1);
    var dateParts = date.split(delimiter)
    if (delimiter !== '-') {
        date = dateParts[1]+'-'+dateParts[0]+'-01';
        return date;
    }
    date = dateParts[0]+'-'+dateParts[1]+'-01';
    return date;

}
</script>