/* Author: yangzeming
 * Date: 2013-12-10
 * Desc: 遮罩层
    
 */
define("component/addplan",['component/scrollpanel','component/jsortable'],function (require, exports, module) {
	var sc = require('./scrollpanel');
	var so = require('./jsortable');
	function getBox(t, i) {
        var e = t.cloneNode(!0);
        document.body.appendChild(e);
        var s, a;
        return i ? (s = t.clientWidth, a = t.clientHeight) : (s = e.clientWidth, a = e.clientHeight), document.body.removeChild(e), e = null, {
            width: s,
            height: a
        }
    }
    var addplan = _.Class({
    	initial : function(options){
    		var defaultOp = {
    			con : "body",
    			data : {},
    			daytemplate : "",
    			plantemplate : ""
    				
    		};
    		this.options = $.extend(true,{},defaultOp,options);
    		this.$con = $(this.options.con);
    		this.currentDay = 1;
    		this.bt = baidu.template;
    		this.dayLineEng();
    		this.scrollPlan = {};
    		this.planEngine();
    		
    	},
    	// setPlan 编辑时候初始化右侧计划
    	setPlanByDay : function(data){
    		var self = this;
    		// 默认添加了第一天
    		for(var i=0,len=data.length;i<len;i++){
    			var ps = data[i]["publishScenics"];
    			// 如果不是第一天
    			if(i!=0){    				
    				self.addDayItem();
    			}
    			// 景点遍历生成相应格式的数据放在allpool中
    		    for(var j=0,l=ps.length;j<l;j++){
    		    	var item = ps[j];
    		    	var temp = {
    		    		xid : item.resourceId,
    		    		name : item.zwmc,
    		    		type : item.type,
    		    		travel_time : item.ylsj
    		    	};
    		    	self.addPlan(temp);
    		    	
    		    }
    			
    			
    		}
    		/*
    		var item = self.$con.find('[data-role="dayLineItem"]'),
				data = {day:(item.size()+1)},
				pd = {roleName:'plan'+(item.size()+1)};
			self.addDayLine(data);
			self.addPlanBody(pd);
			self.currentDay = (item.size()+1);
			self.showPlanBody();*/
    		
    	},
    	addDayItem : function(){
    		var self = this;
    		var item = self.$con.find('[data-role="dayLineItem"]'),
			data = {day:(item.size()+1)},
			pd = {roleName:'plan'+(item.size()+1)};
			self.addDayLine(data);
			self.addPlanBody(pd);
			self.currentDay = (item.size()+1);
			self.showPlanBody();
    	},
    	// 时间轴引擎
    	dayLineEng : function(){
    		// 添加时间按钮
    		var self = this;
    		this.$dayLineAdd = this.$con.find('[data-role="dayLineAdd"]');
    		this.$dayLineCon = this.$con.find('[data-role="dayLineCon"]');
    		this.$con.delegate('[data-role="dayLineAdd"]',"click",function(){	
    				var item = self.$con.find('[data-role="dayLineItem"]'),
    					data = {day:(item.size()+1)},
    					pd = {roleName:'plan'+(item.size()+1)};
    				self.addDayLine(data);
    				self.addPlanBody(pd);
    				self.currentDay = (item.size()+1);
    				self.showPlanBody();
    		});
    		this.$con.delegate('[data-role="dayLineItem"]',"click",function(){
    			var that = $(this),
    				day = that.data("target");
    			self.currentDay = day;
    			self.showPlanBody();
    			
    			that.data("select","yes");
    			self.$con.find('[data-role="dayLineItem"]').removeClass("on");
    			that.addClass("on");
    			setSize();
    			
    		});
    		this.$con.delegate('[data-role="dayLineItem"]',"mouseenter",function(){
    			var that = $(this),
    				day = that.data("target");
    			// 始终保留一天
    			if(self.$con.find('[data-role="dayLineItem"]').size()>1){
    				that.find('.m_close').show();
    			}
    			//if($(this).data("select")!="yes"){
    				that.addClass("on");
    			// }
    			
    		});
    		this.$con.delegate('[data-role="dayLineItem"]',"mouseleave",function(){
    			var that = $(this),
				day = that.data("target");
    			that.find('.m_close').hide();
    			if(that.data("select")!="yes"){
    				that.removeClass("on");
    			 }
    			
    		});
    		this.$con.delegate('.j_day_close',"click",function(event){
    			var that = $(this),
					day = that.parent().data("target");
    			self.deleteByDay(day);
    			event.stopPropagation();
    			
    		});
    		this.$con.delegate('[data-role="detail"]',"mouseenter",function(){
    			$(this).find('.m_close').show();
    			
    		});
    		this.$con.delegate('[data-role="detail"]',"mouseleave",function(){
    			$(this).find('.m_close').hide();
    			
    		});
    		this.$con.delegate('.j_plan_close',"click",function(){
    			$(this).parent().remove();
    			self.$planbody.find('[data-role^="plan"]').each(function(index,sel){
    				
    				var scrollPlan = $(sel).data("scrollPlan");
    				scrollPlan.setHeight($("#j_vleft").height()-$("#j_search").height()-90);
    				
    			});
    			
    		});
    		// 暂时用此方法
    		this.addDayLine();
    	},
    	renderDayLine : function(data){
    		var da = data || [{day : 1}];
    		
    		
    		
    	},
    	addDayLine : function(data){
    		var self = this,
    			
				da = data|| {day : 1},
				_html = $(this.bt(this.options.daytemplate,da));
    			self.$dayLineAdd.before(_html);
    		
    	},
    	showPlanBody : function(){
    		var self =this;
    		var cur = 'plan' + this.currentDay;
    		this.$planHead.html(this.currentDay);
    		this.$planbody.find('[data-role^="plan"]').hide();
    		this.$planbody.find('[data-role^="'+cur+'"]').show();
    		var that = self.$con.find('[data-target="'+this.currentDay+'"]');
    		self.$con.find('[data-role="dayLineItem"]').data("select","no").removeClass("on");
    		that.data("select","yes");
			
			that.addClass("on");
    		
    	},
    	// 按照天来删除计划
    	deleteByDay : function(day){
    		// 首先先删除掉原来的天 和component;
    		var $con = this.$con,
    			self = this;
    		$con.find('[data-target="'+day+'"]').remove();
    		$con.find('[data-role="plan'+day+'"]').remove();
    		//delete this.scrollPlan['plan'+day];
    		// 重新设置顺序
    		$con.find('[data-role="dayLineItem"]').each(function(index,sel){
    			var _html = 'Day'+(index+1)+'<i data-eventrole="closebtn" class="m_close j_day_close" style="display:none;right:15px;"></i>';
    			$(sel).attr("data-target",(index+1));
    			$(sel).data("target",(index+1));
    			$(sel).html(_html);
    			
    		});

    		this.$planbody.find('[data-role^="plan"]').each(function(index,sel){
    			
    			$(sel).attr("data-role",("plan"+(index+1)));
    			$(sel).data("role",("plan"+(index+1)));
    			
    		});
    		// 处理scrollPlan中的数据
    		
    		// 选中最后一天
    		self.currentDay = $con.find('[data-role="dayLineItem"]').size();
			self.showPlanBody();
    	},
    	// 添加某一天
    	planEngine : function(){
    		var self = this;
    		this.$planbody = this.$con.find('[data-role="planBody"]');
    		this.$planHead = this.$con.find('[data-role="planHead"]')
    		// 暂时用此方法
    		this.renderPlanBody();
    		
    	},
    	renderPlanBody : function(){
    		this.addPlanBody();
    		this.$planbody.find('[data-role="plan'+this.currentDay+'"]').show();
    		//this.$planbody.sortable();
    	},
    	addPlanBody : function(data){
    		var da = data || {roleName:'plan1'},
    		_html = $(this.bt(this.options.plantemplate,da));
    		this.$planbody.append(_html);
    		//this.scrollPlan[da.roleName] 
    		var _h = $("#j_vleft").height()-$("#j_search").height()-90;
    		var temp= new sc({
    				con: _html,
    				height: _h
    			});
    		_html.data("scrollPlan",temp);
    		temp.$content.sortable();
    		_html.hide();
    		
    		
    	},
    	
    	// 添加计划
    	addPlan : function(data){
    		this.zIndex = this.zIndex || 1;
    		var _html;
    		datapool[data.xid] = data;
    		if(data.flag==0){
    			 _html = $(this.bt('plan_detail_user',data));
    			 _html.data("userdata",data);
    			
    		}else{
    			data.flag = 1;
    			datapool[data.xid].flag = 1;
	    		 _html = $(this.bt(this.options.planDetail,data));
	    		var box = getBox(_html[0],false);
    		}
    		var self = this;
    		
    		//this.scrollPlan['plan'+this.currentDay].appendContent(_html);
    		this.$con.find('[data-role="plan'+this.currentDay+'"]').data("scrollPlan").appendContent(_html);
    		/*_html.draggable({ axis: "y",start: function(){
    			var top = $(this).position().top;
    			$(this).css({"zIndex" : self.zIndex++,"position":"absolute"});
    			$(this).data("")
    			$(this).before('<div data-role="detailCLone" style="background-color: #EEEEEE;margin-top: 5px;height:80px;position: relative;overflow:hidden;width:190px;"></div>');
    			
    			
    		}
    		,drag : function(){
    			var that = $(this);
    			
				var top = Math.round(that.position().top /85);
				var temp = self.$planbody.find('[data-role="detailCLone"]'),
					temp1 = self.$planbody.find('[data-role="detail"]').eq(top),
					tcl = temp.clone();
				temp.remove();
				temp1.after(tcl);
				
    		}
    		,stop:function(){
				var that = $(this);

				var top = Math.round(that.position().top /85);
				var temp = self.$planbody.find('[data-role="detailCLone"]');

				temp.replaceWith(that.css({"position":"relative","top":"auto"}));
					
				//var cl = that.clone().css("top","auto");
				//that.remove();
				//temp.after(cl);
				//temp.replaceWidth(that.css({"position":"relative","top":pos.top}));
				//self.$planbody.find('data-role="detailCLone"').replaceWidth(tclone);
				//that.css({left:(left*83+15)})
	
    		} });

    		/*_html.animate({
    			height:box.height
    			
    		},500,function(){
    			
    			self.scrollPlan['plan'+self.currentDay]._resizeHeight();
    			
    			self.scrollPlan['plan'+self.currentDay].scrollTopTo(20000); //滚动到最下面
    			
    		});
    		_html.height(0);
    		TweenLite.to(_html,.2,{"visibility":"visible",height:box.height,onUpdate:function(){
    			
    			//self.scrollPlan['plan'+self.currentDay]._resizeHeight();
    			
    			//self.scrollPlan['plan'+self.currentDay].scrollTopTo(20000); //滚动到最下面
    		}});*/
    		
    		
    	},
    	// 删除计划
    	deletePlan : function(){
    		
    		
    	},
    	// 获取计划数据
    	getPlanData : function(alldata){
    		var self = this,
    			plans = this.$planbody.find('[data-role^="plan"]'),
    			data = {};
    		plans.each(function(index,sel){
    			var day = index+1,
    				i = "plan"+day;
				var item = $(sel).data("scrollPlan"),
					con = item.$content;
				data[i] = [];
			   con.find('[data-role="detail"]').each(function(){
				   var xid = $(this).data("xid"),
				   		flag = $(this).data("flag");
				   if(flag==1){
	    			   var temp = alldata[xid];
	    			   data[i].push(temp);
				   }else{
					   var temp = $(this).data("userdata");
					   data[i].push(temp);
				   }
				   
			   });
    			
    			
    		});
    	/*	for(var i in plans){
    			var planday = i,
    				day = planday.replace("plan","");
    			var item = plans[i],
    				con = item.$content;
    			data[i] = [];
    		   con.find('[data-role="detail"]').each(function(){
    			   var xid = $(this).data("xid"),
    			   		flag = $(this).data("flag");
    			   if(flag==1){
	    			   var temp = alldata[xid];
	    			   data[i].push(temp);
    			   }else{
    				   var temp = $(this).data("userdata");
    				   data[i].push(temp);
    			   }
    			   
    		   });
    		   
    			
    		}*/
    		
    		return data;
    		
    		
    	}
    	
    	
    	
    });
   
    module.exports = addplan;
});