var DayParting = {
	init: function(con,selected){
		this.addEvent(con);
		//回填
		if (selected) {
			var arr = utils.String.hexToBinary(selected);
			for (var i=0; i < arr.length; i++) {
				for(var j = 0 ; j < arr[i].length;j++){
					var str = arr[i];
					if (arr[i][j] == '1') {
						$('tr:nth('+(i+1)+') span:nth('+(24-str.length+j)+')').addClass('selected');
					};
				}
			};
			DayParting.checkForAllSelected();
		};
		this.spans = $('tr:gt(0) span');
	},
	//将取所有选中的时段格式化为二进制字串
	getData: function(){
		return this.spans.map(function(i,span){
			return $(span).hasClass('selected')? '1':'0';
		}).toArray().join('');
	},
	addEvent: function(con) {
		//全天选择
		$('.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);
		}).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');
		});
		//单选某天
		$(con).find('td>span').click(function(){
			var method = $(this).hasClass('selected') ? 'removeClass' : 'addClass';
			$(this)[method]('selected');
			DayParting.checkForAllSelected(this);
		});
		//确定保存
		$('#confirm').click(function(){
			var selected = this.getData();
			var formated = utils.String.formatDayPartingSettings(selected);
			this.emit('change',[utils.String.binaryToHex(selected),formated]);
			parent.$(parent.document).trigger('setData');
		}.bind(this));

		new Drag($(document), {
			onComplete: function(){
				DayParting.checkForAllSelected();
			},
			onBeforeDrag: function(){
				// 记录自己当前状态，在拖拽过程中用来判断怎么切换选中状态
				this.spans.each(function(i,item){
					$(item).data('status',$(item).hasClass('selected') ? 1 : 0);
				});
			}.bind(this),
			onDragging:function(start, stop) {
				// 手动拖拽出来的矩形
				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 = $('tr').find('td:nth('+this.getAttribute('data-index')+') span:not(.selected)');
			if (eles.length === 0) {
				this.checked = 'checked';
			}else{
				this.checked = false;
			}
		});
		$('.select').each(function(){
			var eles = $(this).parents('tr').find('span:not(.selected)');
			if (eles.length === 0) {
				this.checked = 'checked';
			}else{
				this.checked = false;
			}
		});
		if ($('tr:gt(0) span:not(.selected)').length === 0) {
			$('#selectAll')[0].checked = 'checked';
		}else{
			$('#selectAll')[0].checked = false;
		}
	},
	release: function(){
		this.spans = null;
		delete this.spans;
		$('input').unbind('click').unbind('mouseover').unbind('mouseout');
	}
};
EventEmitter.call(DayParting);