/**
 * Created by kogting on 14-6-17.
 */
(function($){
    $.fn.schedule = function(options){
        var defaults = {
            formatedStr : "",
            actionTarget : "",
            popupTitle : "时段定向设定",
            partial : "",
            callback :""
        };
        var Opts = $.extend(defaults,options);
        return this.each(function(){
            //初始化并显示插件
            var title = Opts.popupTitle;
            var target = Opts.actionTarget;
            var callback = Opts.callback;
            var partial = Opts.partial;
            var formated =Opts.formatedStr;
            if(formated!=""){
        		formated =JSON.parse(formated);
        		var isAll = formated.Monday.value.length==24&&formated.Tuesday.value.length==24&&formated.Wednesday.value.length==24&&formated.Thursday.value.length==24&&formated.Friday.value.length==24&&formated.Saturday.value.length==24&&formated.Sunday.value.length==24
                if(isAll){
                	schedule_display("all",partial);
                }else{
                	schedule_display(formated,partial);
                }
            }else{
            	schedule_display(formated,partial);
            };
            
//            回写测试
//            var formated = {
//                "Monday" : {
//                    "display" : [[1,5]],
//                    "value" : [1,2,3,4,5]
//                },
//                "Tuesday" : {
//                    "display" : [[1,5]],
//                    "value" : [1,2,3,4,5]
//                },
//                "Wednesday" : {
//                    "display" : [[1,5]],
//                    "value" : [1,2,3,4,5]
//                },
//                "Thursday" : {
//                    "display" : [[1,5]],
//                    "value" : [1,2,3,4,5]
//                },
//                "Friday" : {
//                    "display" : [[1,5]],
//                    "value" : [1,2,3,4,5]
//                },
//                "Saturday" : {
//                    "display" : [[1,5]],
//                    "value" : [1,2,3,4,5]
//                },
//                "Sunday" : {
//                    "display" : [[1,5]],
//                    "value" : [1,2,3,4,5]
//                }
//            }
            
            var schedulePopup = $("#schedule_popup")[0];
            if (!schedulePopup) {
                schedulePopup = new newPopup({
                    width:'900px',
                    height:'378px',
                    title:title
                });
                DayParting.init(formated);
            };
            target.on('click',function(){
            	schedulePopup.show();
            });

            //确定保存
            $('#confirm').click(function(){
                var selected = DayParting.getData();
                if(selected.replace(/0/g,'') == ''){
                    var noPick = "";
                    schedule_display(noPick,partial);
                    callback(noPick);
                    schedulePopup.hide();
                }else if(selected.replace(/1/g,'')==''){
                    var pickAll = "all";
                    var display = utils.String.formatDayPartingSettings(selected,false);
                    var value = utils.String.formatDayPartingSettings(selected,true);
                    getSelect(display,value);
                    schedule_display(pickAll,partial);
                    schedulePopup.hide();
                    mask.hide();
                }else{
                    var display = utils.String.formatDayPartingSettings(selected,false);
                    var value = utils.String.formatDayPartingSettings(selected,true);
                    getSelect(display,value);
                    schedule_display(getSelect(display,value),partial);
                    schedulePopup.hide();
                    mask.hide();
                }
            });

            var getSelect =  function(data1,data2){
                var display = data1;
                var value = data2;
                var dataJson = {
                    'Monday' : {
                        'display' : data1[0],
                        'value' : data2[0]
                    },
                    'Tuesday' : {
                        'display' : data1[1],
                        'value' : data2[1]
                    },
                    'Wednesday' : {
                        'display' : data1[2],
                        'value' : data2[2]
                    },
                    'Thursday' : {
                        'display' : data1[3],
                        'value' : data2[3]
                    },
                    'Friday' : {
                        'display' : data1[4],
                        'value' : data2[4]
                    },
                    'Saturday' : {
                        'display' : data1[5],
                        'value' : data2[5]
                    },
                    'Sunday' : {
                        'display' : data1[6],
                        'value' : data2[6]
                    }
                };
                dataStr =JSON.stringify(dataJson);
                callback(dataStr);
                return dataJson;
            }
        });
    }
})(jQuery);