/* Author: yangzeming
 * Date: 2013-12-10
 * Desc: 简单的日历组件
 */
define("component/hortime",function (require, exports, module) { 
    
	var bc = require("./basecalendar"),
		dr = require("./jdraggable");
	var hortime = bc.extend({
		initial : function(options){
			var defaultOp = {
				con : "body",
				config:{},
				startDate : new Date(),
				dur : 10,
				timescale : "day",
				timedir : "forward",
				currentDate : new Date()
				
			};
			this.options = $.extend(true,{},defaultOp,options);
			this._eventStack = {};
			this.config = $.extend(true,{},this.config,this.options.config);
			this.date = this.options.startDate;
			this.$con = $(this.options.con);
			this.currentDate = this.datePart(this.options.currentDate);
			this.renderView(this.currentDate);
			this.bindEvent();
		
		},
		// 渲染时间轴模板
		renderView : function(){
			var st = this.datePart(this.date||new Date()),
				td = this.datePart(new Date()),
				body = this.$con.find('[data-role="time_body"]'),
				dur = this.options.dur,
				_html = "";
			var dts,dw="";
			if(this.options.timescale=="day"){
				dts = this.dateToStr("%m.%d");
				
			}else if(this.options.timescale=="month"){
				 dts = this.dateToStr("%m");
			}else if(this.options.timescale=="year"){
			     dts = this.dateToStr("%Y");
				 dw = "年"
			}
			this.startDate = new Date(st.valueOf());
			//顺序还是逆序
			if(this.options.timedir=="forward"){
				for(var i=1;i<=dur;i++){
					
					var str = dts(st);
					if(st.valueOf()==td.valueOf()&&this.options.timescale=="day") str = "今天";
					_html +='<li data-role="dateItem" >'+str+dw+'</li>';
					st = this.add(st,-1,this.options.timescale);
				}
				this.endDate = new Date(this.add(st,1,this.options.timescale).valueOf());
			}else{
				for(var i=1;i<=dur;i++){
					
					var str = dts(st);
					if(st.valueOf()==td.valueOf()&&this.options.timescale=="day") str = "今天";
					_html +='<li data-role="dateItem" >'+str+dw+'</li>';
					st = this.add(st,1,this.options.timescale);
				}
				this.endDate = new Date(this.add(st,1,this.options.timescale).valueOf());
			
			}
			body.html(_html);
			this.playBtn = this.$con.find('[data-role="play"]');
			
		},
		getDateByPos : function(posi){
			var dragger = this.$con.find('[data-role="dragger"]'),
				self = this,
				pos = posi||dragger.position().left,
				dur =  Math.round((pos-15)/83);
			if(this.options.timedir=="forward"){
					return this.add(this.startDate,-dur,this.options.timescale);

			}else{

				return this.add(this.startDate,dur,this.options.timescale);
			}
		
		},
		getPosByDate : function(date){
			//console.log(date.valueOf()-this.startDate.valueOf());
			var sd = 1;
			if(this.options.timescale=="day"){
				sd = Math.abs((date.valueOf()-this.startDate.valueOf()))/(3600*24*1000);
				
			}else if(this.options.timescale=="month"){
				sd = Math.abs((date.valueOf()-this.startDate.valueOf()))/(3600*24*1000);
			}else if(this.options.timescale=="year"){
				var year1 = date.getFullYear(),
					year2 = this.startDate.getFullYear();
			    sd = Math.abs(year1 - year2);
			}
			return sd*83+15;

		},
		stopPlay : function(){
			var self = this;
			self.playtween&&self.playtween.stop();
			self.playtween = null;
			this.playBtn.data("play","pause");
            $(this).removeClass("time_stop").addClass("time_play");
		
		},
        getPointPL:function(s,e){
		    var pl = Math.abs(s-e);
		    return pl*0.03;
	
	    },
		// 添加操作事件
		bindEvent : function(){
			var prevBtn = this.$con.find('[data-role="prev"]'),
				nextBtn = this.$con.find('[data-role="next"]'),
				
				dragger = this.$con.find('[data-role="dragger"]'),
				self = this,
				duration = 40;
			
			this.startPos = 15;// dragger.position().left;
			dragger.css({left:this.getPosByDate(this.currentDate)});
			this.currentPos = dragger.position().left;
			this.endPos = this.$con.find('[data-role="time_body"]').width()-83+15;
			prevBtn.click(function(){
				self.stopPlay();
				if(self.currentDate < self.startDate){
					
					self.currentDate = self.add(self.currentDate,1,self.options.timescale);
					var that = dragger;
					self.trigger("daychange",self.currentDate);
					that.css({left:self.getPosByDate(self.currentDate)})
				}
			
			});
			this.$con.find('[data-role="time_body"]').delegate('[data-role="dateItem"]',"click",function(){
				
				var left = $(this).position().left;
				dragger.css({left:left});
				self.stopPlay();
				var date = self.getDateByPos(left);
				if(date.valueOf()!=self.currentDate.valueOf()){
						//console.log(self.currentDate);
						self.trigger("daychange",date);
						self.currentDate = date;
					
				}
				
				
			
			});
			nextBtn.click(function(){
				self.stopPlay();
				if(self.currentDate > self.endDate){
					
					self.currentDate = self.add(self.currentDate,-1,self.options.timescale);
				
					var that = dragger;
					
					self.trigger("daychange",self.currentDate);
					
					that.css({left:self.getPosByDate(self.currentDate)})
				}
			});
			function getTween(){
				var tween = new TimelineMax({ease:Power0.easeIn,onUpdate  :function(){
					var date = self.getDateByPos();
					
					if(date.valueOf()!=self.currentDate.valueOf()){
						
						self.trigger("daychange",date);
						self.currentDate = date;
					
					}
				
				},onComplete: function(){
					var le = dragger.position().left;
					var that = dragger;
					var left = Math.round((le - 15)/83);
					that.css({left:(left*83+15)});
                    le = dragger.position().left;
					if(le==self.startPos){
						tween.to(dragger,self.getPointPL(le,self.endPos),{left:self.endPos});
					}else{
						tween.to(dragger,self.getPointPL(le,self.startPos),{left:self.startPos});
					}
				
				}});
				return tween;
			
			};
			this.playtween = getTween();
			this.playBtn.click(function(){
				var le = dragger.position().left;
					data = $(this).data("play");
				if(data&&data=="play"&&self.playtween!=null){
					self.playtween.paused(true);
					
					var that = dragger;
					var left = Math.round((le - 15)/83);
					that.css({left:(left*83+15)})
					$(this).data("play","pause");
                    $(this).removeClass("time_stop").addClass("time_play");
				}else{
					if(data=="pause"&&self.playtween!=null){
						self.playtween.play();
						$(this).data("play","play");
                        $(this).removeClass("time_play").addClass("time_stop");
					}else{

						if(self.playtween!=null)
							self.playtween.to(dragger,self.getPointPL(le,self.endPos),{left:self.endPos});
						else{
							self.playtween = getTween();
							self.playtween.to(dragger,self.getPointPL(le,self.endPos),{left:self.endPos});
						}
						$(this).data("play","play");
                        $(this).removeClass("time_play").addClass("time_stop");
					}
					
				
				}
				
			
			});
			dragger.draggable({start:function(){
				self.stopPlay();
			
			}, containment: "parent",axis: "x",stop:function(){
				//console.log($(this).position());
				var that = $(this);
				var left = Math.round((that.position().left - 15)/83);
				that.css({left:(left*83+15)});
				var date = self.getDateByPos();
				if(date.valueOf()!=self.currentDate.valueOf()){
						//console.log(self.currentDate);
						self.trigger("daychange",date);
						self.currentDate = date;
					
				}
				//console.log(self.currentDate);
			} });
		
		}
		// 时间跨度
	});
	 module.exports = hortime;
});