var DayParting = {
	init: function(selected){
		this.addEvent();
		//回填
		if (selected) {
            for(var i=0;i<7;i++){
                switch(i){
                    case 0:
                        var value = selected.Monday.value;
                        var spans = $('.popup tr:nth('+(i+1)+') span');
                        for(var j=0;j<value.length;j++){
                            spans.eq(value[j]).addClass('selected');
                        }
                        break;
                    case 1:
                        var value = selected.Tuesday.value;
                        var spans = $('.popup tr:nth('+(i+1)+') span');
                        for(var j=0;j<value.length;j++){
                            spans.eq(value[j]).addClass('selected');
                        }
                        break;
                    case 2:
                        var value = selected.Wednesday.value;
                        var spans = $('.popup tr:nth('+(i+1)+') span');
                        for(var j=0;j<value.length;j++){
                            spans.eq(value[j]).addClass('selected');
                        }
                        break;
                    case 3:
                        var value = selected.Thursday.value;
                        var spans = $('.popup tr:nth('+(i+1)+') span');
                        for(var j=0;j<value.length;j++){
                            spans.eq(value[j]).addClass('selected');
                        }
                        break;
                    case 4:
                        var value = selected.Friday.value;
                        var spans = $('.popup tr:nth('+(i+1)+') span');
                        for(var j=0;j<value.length;j++){
                            spans.eq(value[j]).addClass('selected');
                        }
                        break;
                    case 5:
                        var value = selected.Saturday.value;
                        var spans = $('.popup tr:nth('+(i+1)+') span');
                        for(var j=0;j<value.length;j++){
                            spans.eq(value[j]).addClass('selected');
                        }
                        break;
                    case 6:
                        var value = selected.Sunday.value;
                        var spans = $('.popup tr:nth('+(i+1)+') span');
                        for(var j=0;j<value.length;j++){
                            spans.eq(value[j]).addClass('selected');
                        }
                        break;
                }
            }

			DayParting.checkForAllSelected();
		};
        var spans = $('.popup tr:nth('+(i+1)+') span');
	},
	//将取所有选中的时段格式化为二进制字串
	getData: function(){
        this.spans = $('.popup tr:gt(0) span');
		return this.spans.map(function(i,span){
			return $(span).hasClass('selected')? '1':'0';
		}).toArray().join('');
	},
	addEvent: function() {
		//全天选择
		$('.select').change(function(){
			var method = this.checked ? 'addClass' : 'removeClass';
			$(this).parents('tr').find('span')[method]('selected');
		}).mouseover(function(){
			$(this).parents('tr').find('td:gt(1)').addClass('hover');
		}).mouseout(function(){
			$(this).parents('tr').find('td:gt(1)').removeClass('hover');
		});
		//全选按钮
		$('#selectAll').change(function(){
			var c = this.checked,method = c ? 'addClass' : 'removeClass';
			$('tr:gt(0) span')[method]('selected');
			$('input:checkbox').attr('checked',c);
			DayParting.checkForAllSelected();
		}).mouseover(function(){
			$('tr').find('td:gt(1)').addClass('hover');
		}).mouseout(function(){
			$('tr').find('td:gt(1)').removeClass('hover');
		});
		//时段00:00-5:00,6:00-11:00,12:00-17:00,18:00-23:00 mouseover与mouseout
		$('#selectBeforeDawn,#selectMorning,#selectAfternoon,#selectNight').mouseover(function(){
			$('tr').find('td:nth('+this.getAttribute('data-index')+')').addClass('hover');
		}).mouseout(function(){
			$('tr').find('td:nth('+this.getAttribute('data-index')+')').removeClass('hover');
		}).change(function(){
			var c = this.checked;
			var eles = $('tr').find('td:nth('+this.getAttribute('data-index')+') span');
			c ? eles.addClass('selected') : eles.removeClass('selected');
		});
		//单选某天
        $('td>span').click(function(){
			var method = $(this).hasClass('selected') ? 'removeClass' : 'addClass';
			$(this)[method]('selected');
			DayParting.checkForAllSelected(this);
		});

		new Drag($('.popup'), {
			onComplete: function(){
				DayParting.checkForAllSelected();
			},
			onBeforeDrag: function(){
                this.spans = $('.popup tr:gt(0) span');
				// 记录自己当前状态，在拖拽过程中用来判断怎么切换选中状态
				this.spans.each(function(i,item){
					$(item).data('status',$(item).hasClass('selected') ? 1 : 0);
				});
			}.bind(this),
			onDragging:function(start, stop) {
                this.spans = $('.popup tr:gt(0) span');
				// 手动拖拽出来的矩形
				var r1 = new Rectangle(start.x, start.y ,stop.x, stop.y);
				this.spans.each(function(index, span) {
					var self = $(span),
					o = self.offset(),
					// SPAN的BOX
					r2 = new Rectangle(
						o.left,						// x1
						o.top,						// y1
						o.left+self.outerWidth(),	// x2
						o.top+self.outerHeight()	// y2
					);
					// 根据是否被框选到 来切换选中状态
					if (r2.cross(r1) == (self.data('status') == '0')) {
						self.addClass('selected');
					}else{
						self.removeClass('selected');
					}
				}.bind(this));
			}.bind(this)
		});
		$(window).unload(this.release.bind(this));
	},
	checkForAllSelected: function(){
		$('#selectBeforeDawn,#selectMorning,#selectAfternoon,#selectNight').each(function(){
			// var c = this.checked;
			var eles = $('.popup tr').find('td:nth('+this.getAttribute('data-index')+') span:not(.selected)');
			if (eles.length === 0) {
				this.checked=true;
			}else{
				this.checked=false;
			}
		});
		$('.select').each(function(){
			var eles = $(this).parents('tr').find('span:not(.selected)');
			if (eles.length === 0) {
				this.checked=true;
			}else{
				this.checked=false;
			}
		});
		if ($('tr:gt(0) span:not(.selected)').length === 0) {
			$('#selectAll')[0].checked=true;
		}else{
			$('#selectAll')[0].checked=false;
		}
	},
	release: function(){
		this.spans = null;
		delete this.spans;
		$('input').unbind('click').unbind('mouseover').unbind('mouseout');
	}
};
EventEmitter.call(DayParting);