/**
 * 检索条件
 */
KISSY.add("qr/listview",function(S, mvc,Template,Collection) {
	function ListView(config){
		var tmpl = Template(S.one('#'+config.template).html());
		ListView.superclass.constructor.apply(this, arguments);
		this.set('el',S.one('#'+config.container));
		var self = this,el = self.get('el');
		var conllection = self.get("conllection");
		conllection.on("*Change", function(e) {
	      if(e.target != self) {
	    	  el.html(tmpl.render({list:conllection.toJSON()}));
	    	  self.selects = [];
	      }
	    });
		
	}
	S.extend(ListView, mvc.View,{
		_selects:[],
		getSelection:function(){
			return this._selects;
		},
		rowDbClick:function(e){
			var foo = this.get('onRowDbClick');
			if(foo != undefined)
			foo(e);
		},
		cancelAll:function(){
			this._selects = [];
			this.get('el').all('.active').removeClass("active");
			this.get('el').all('input').removeAttr('checked');
		},
		selectRow:function(rowIndex){
			var obj = this.get('conllection').at(rowIndex);
			var el = this.get('el');
			var row = el.all('.row').item(rowIndex);
			row.addClass("active");
			row.one('input').attr('checked','checked');
			this._selects = [];
			this._selects.push(obj);
		},
		_check: function(e){
			var self = this;
			e.stopPropagation();
			var target = $(e.currentTarget);
			var row = target.parent().parent();
			var index = row.index()-1;
			var obj = self.get('conllection').at(index);
			var checkbox = row.find('input[type=checkbox]');
			var isChecked = checkbox.attr('checked');
			
			if(isChecked == "checked"){
				row.removeClass("active");
				checkbox.removeAttr('checked');
				var newArr = [];
				S.each(self._selects,function(o){
					if(o.get('id') != obj.get('id')){
						newArr.push(o);
					}
				});
				self._selects = newArr;
			}
			else{
				row.addClass("active");
				checkbox.attr('checked','checked');
				self._selects.push(obj);
			}
			
			var foo = this.get('onRowCheck');
			if(foo != undefined)
			foo(obj);
		},
		_rowClick:function(e){
			var self = this;
			var target = $(e.currentTarget);
			var index = target.index()-1;
			var obj = self.get('conllection').at(index);
			var checkbox = target.find('input[type=checkbox]');
			var isChecked = checkbox.attr('checked');
			if(isChecked == "checked"){
				target.removeClass("active");
				checkbox.removeAttr('checked');
				var newArr = [];
				S.each(self._selects,function(o){
					if(o.get('id') != obj.get('id')){
						newArr.push(o);
					}
				});
				self._selects = newArr;
			}
			else{
				self.cancelAll();
				target.addClass("active");
				checkbox.attr('checked','checked');
				self._selects.push(obj);
			}
			var foo = self.get('onRowClick');
			if(foo != undefined)
			foo(obj);
		},
		_rowMouseover:function(e){
			var self = this;
			var foo = self.get('onRowMouseover');
			if(foo != undefined)
			foo(e);
		},
		_rowMouseout:function(e){
			var self = this;
			var foo = self.get('onRowMouseout');
			if(foo != undefined)
			foo(e);
		},
		getCheckStr:function(){
			var checksArray = '';
			for(i = 0 ; i < this._selects.length ; i++){
				checksArray += this._selects[i].get('id');
				if(i < this._selects.length -1) checksArray += ',';
			}
			return checksArray;
		}
	},{
	      ATTRS:{
	    	  el:{
	    		  value:'.listview'
	    	  },
	          events:{
	              value:{
	            	  ".row":{
	            		  click:"_rowClick",
	            		  dblclick:"rowDbClick",
	            		  mouseover:"_rowMouseover",
	            		  mouseout:"_rowMouseout"
	            	  },
	    	  		  ".checkbox":{
	    	  			  click:"_check"
	    	  		  }
	              }
	          }
	      }});
	return ListView;
}, {attach:false,requires:["mvc","template","./collection"]});
