/**
 * 试卷view
 */
KISSY.add("qr/paperview",function(S, mvc,Template,Collection) {
	function PaperView(config){
		var tmpl = Template(S.one('#'+config.template).html());
		PaperView.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("afterModelsChange", function(e) {
	      if(e.target != self) {
	    	  el.html(tmpl.render({list:conllection.toJSON()}));
	      }
	    });
		
	}
	S.extend(PaperView, mvc.View,{
		_enterbox:function(e){
			var target = $(e.currentTarget);
			var index = target.index();
			var cnt = target.parent().children().length;
			target.addClass('hover');
			if(index == 1){
				target.find('.toolbar-up').hide();
			}
			if(index == (cnt-1)){
				target.find('.toolbar-down').hide();
			}
		},
		_leavebox:function(e){
			var target = $(e.currentTarget);
			target.removeClass('hover');
		},
		_remove:function(e){
			var self = this;
			var target = $(e.currentTarget).parent().parent();
			
			var foo = self.get('onRemove');
			if(foo != undefined)
			foo(target.get(0));
		},
		_moveUp:function(e){
			var self = this;
			var target = $(e.currentTarget).parent().parent();
			var prevDom = target.prev();
			if(prevDom != null){
				var newDom = target.clone();
				var index = prevDom.index();
				newDom.insertBefore(prevDom);
				target.remove();
				if(index == 1){
					newDom.find('.toolbar-up').hide();
				}
				else{
					newDom.find('.toolbar-down').show();
				}
				prevDom.find('.toolbar-up').show();
			}
			
			var foo = self.get('onMoveUp');
			if(foo != undefined)
			foo(target.get(0),index);
		},
		_moveDown:function(e){
			var self = this;
			var target = $(e.currentTarget).parent().parent();
			var nextDom = target.next();
			if(nextDom != null){
				var newDom = target.clone();
				var index = nextDom.index();
				var cnt = target.parent().children().length;
				newDom.insertAfter(nextDom);
				target.remove();
				if(index == (cnt-1)){
					newDom.find('.toolbar-down').hide();
				}
				else{
					newDom.find('.toolbar-up').show();
				}
				nextDom.find('.toolbar-down').show();
			}
			
			var foo = self.get('onMoveDown');
			if(foo != undefined)
			foo(target.get(0),index-2);
		},
		_bigClean:function(e){
			var self = this;
			var target = $(e.currentTarget).parent();
			var str = target.attr('id').split('_')[1];
			var typeId = str.substring(1,str.length);
			var foo = self.get('onBigClean');
			if(foo != undefined)
			foo(typeId);
		},
		_bigEdit:function(e){
			var self = this;
			var target = $(e.currentTarget).parent();
			var obj = this.get('conllection').at(target.parent().index());
			
			var foo = self.get('onBigEdit');
			if(foo != undefined)
			foo(target.parent().index(),obj);
		},
		_addQuestionToBig:function(e){
			var self = this;
			var target = $(e.currentTarget).parent();
			var str = target.attr('id').split('_')[1];
			var typeId = str.substring(1,str.length);
			var foo = self.get('onAddQuestionToBig');
			if(foo != undefined)
			foo(typeId);
		}
	},{
	      ATTRS:{
	    	  el:{
	    		  value:'.paper-view'
	    	  },
	          events:{
	              value:{
	            	  ".question-box":{
	            		  mouseenter:"_enterbox",
	            		  mouseleave:"_leavebox"
	            	  },
	    	  		  ".toolbar-up":{
	    	  			  click:"_moveUp"
	    	  		  },
	    	  		  ".toolbar-down":{
	    	  			  click:"_moveDown"
	    	  		  },
	    	  		  ".toolbar-remove":{
	    	  			  click:"_remove"
	    	  		  },
	    	  		  '.big-clean':{
	    	  			  click:'_bigClean'
	    	  		  },
	    	  		  '.big-edit':{
	    	  			  click:'_bigEdit'
	    	  		  },
	    	  		  '.big-add':{
	    	  			  click:'_addQuestionToBig'
	    	  		  }
	              }
	          }
	      }});
	return PaperView;
}, {attach:false,requires:["mvc","template","./collection"]});
