
var userPatternForDateFormat;
var pMName;
var DTPUserDatePattern;
var showMeridian;
var startDate;

function systemDateTimeProfileJSHelper(conf)
{
    
    
    
    userPatternForDateFormat = conf.userPatternForDateFormat;
    pMName = conf.pMName;
    DTPUserDatePattern = conf.DTPUserDatePattern;
    showMeridian = conf.showMeridian;
    startDate = new Date();
    
    this.initDateTimePicker  = function(timePickerDiv) { initDateTimePicker(timePickerDiv);};
    this.initTimePicker = function(timepickerInput){ initTimePicker(timepickerInput);};
    this.newWeekRuleCallback = function(inputElement,index) { newWeekRuleCallback(inputElement,index);};
 
    
    initDateRuleDynamicGrid();
    initWeekRuleDynamicGrid();
    $('.hideRow').remove();

    $(".dateTimePicker").each(function(index,element) {
        initDateTimePicker(element);
    });

    $('.timepickerInput').each(function(i,element){
        initTimePicker(element);
    });
    
}

function initDateRuleDynamicGrid() {
    DateRuleGridJS = new plDynamicGrid(
        '#date-time-date-grid',
        "hideRow",
        '.addDateRule',
        "new-item-animated",
        function(inputElement) {
            initDateTimePicker($(inputElement).parent());
        }
    );
}

function initWeekRuleDynamicGrid() {

    WeekRuleGridJS = new plDynamicGrid(
        '#date-time-week-grid',
        "hideRow",
        '.addWeekRule',
        "new-item-animated",
        function (inputElement,index) {    
            newWeekRuleCallback(inputElement,index);
        }
    );
}

function initDateTimePicker(timePickerDiv) {
        
        $(timePickerDiv).datetimepicker(
        {
            startDate:startDate,
            date: startDate,
            format: DTPUserDatePattern,
            showMeridian:showMeridian,
            autoclose:true,
            forceParse:false

        }).on('show', function(e) {
            
            $('.datetimepicker').css({'z-index':'2000'});
            if ($(timePickerDiv).find('input').attr('class')== 'span2 date_from') {
                var date_to = $(this).parent().next().find('input').val();
                if (date_to) {
                    var newEndDate = getDate(date_to, userPatternForDateFormat, pMName);
                    $(timePickerDiv).datetimepicker('setEndDate',newEndDate);
                } 
            }

            if ($(timePickerDiv).find('input').attr('class')== 'span2 date_to') {
                var date_from = $(timePickerDiv).parent().prev().find('input').val();
                if (date_from) {
                    var newStartDate = getDate(date_from, userPatternForDateFormat, pMName);
                    $(timePickerDiv).datetimepicker('setStartDate',newStartDate);
                }
            }
        });
        
        $(timePickerDiv).find("input:visible, select:visible").each(function(i,e) {
            contextHelper.addlisteningElement("#"+e.id, $(e).data("custom-contexthelp-key"));
        });
    }

function getDate(date) {
    var pattern = userPatternForDateFormat;
    var seperator = pattern.replace('MM','');
    seperator = seperator.replace('yyyy','');
    seperator = seperator.replace('dd','');
    seperator = seperator[0];
    var dateTime = date.split(" ");
    var date = dateTime[0];
    date = date.split(seperator);
    var time = dateTime[1];
    time = time.split(":");
    if (dateTime[2] && dateTime[2] == pMName) {
        time[0] = time[0]%23+12;
    }

    var parts = pattern.split(seperator);
    var y = 0;
    var m = 0;
    var d = 0;

    for (var i=0;i<=2;i++) {
        if (parts[i] == 'yyyy') {
            y=i;
        } else if(parts[i] == 'MM') {
            m=i;
        } else {
            d=i;
        }
    }

    var year = date[y];// year
    var month = date[m]-1; // month -1
    var day = date[d];  //day
    var hours =time[0];  //houer
    var minutes = time[1]; // minute

    var newDate = new Date(year, month, day, hours, minutes);
    return newDate;
};
    
function initTimePicker(timepickerInput) {

    $(timepickerInput).timepicker({
        minuteStep: 1,
        showSeconds: false,
        showMeridian: showMeridian,
        defaultTime: false,
        template: "dropdown"
    }).on('show.timepicker',function(){
        $(this).val('00:00');
    });
}

function newWeekRuleCallback(inputElement,index)
{
    if ($(inputElement).hasClass("timepickerInput")) {
        initTimePicker($(inputElement), showMeridian);
    }

    if ($(inputElement).hasClass('hiddenSort')) {
        inputElement.value =index;
    } else if(inputElement.type == 'checkbox') {

        inputElement.checked = '1';
        inputElement.value = '1';
    } else {
        inputElement.value='';
    }

    if(inputElement.id !== "") {
        contextHelper.addlisteningElement("#"+inputElement.id, $(inputElement).data("custom-contexthelp-key"));
    }
    
    //remove class after animation to fix z-index problem.
    setTimeout(function(){
        $('.new-item-animated').removeClass("new-item-animated");
    }, 300); 
}
    
function resetSelectableDateRange(el) {        
        
    if (el.hasClass("date_to")) {
        $(el).parent().prev().find('#datetimepicker').datetimepicker('setEndDate');
    } else {
        $(el).parent().next().find('#datetimepicker').datetimepicker('setStartDate', new Date());
    }
}

function toggleTimePicker(e) {
    $(e).timepicker().on('show.timepicker', function(el) {
        console.log(el);
    });
    $(e).timepicker('showWidget');
}

function clearParentInput(e){
    $(e).parent().parent().find('input').val('');
}
