/**
 * @fileOverview lvs负载均衡系统
 * 显示本地系统时间
 * @author 马贺
 */
if (!window.ceopen) {
	window.ceopen = {};
}
if (!window.ceopen.lvs) {
	window.ceopen.lvs = {};
}
if (!window.ceopen.lvs.eclock) {
	window.ceopen.lvs.eclock = {};
}
Date.prototype.format = function(format)  
{  
   var o = {  
     "M+" : this.getMonth()+1, //month  
     "d+" : this.getDate(),    //day  
     "h+" : this.getHours(),   //hour  
     "m+" : this.getMinutes(), //minute  
     "s+" : this.getSeconds(), //second  
     "q+" : Math.floor((this.getMonth()+3)/3), //quarter  
     "S" : this.getMilliseconds() //millisecond  
   }  
   if(/(y+)/.test(format)) format=format.replace(RegExp.$1,  
     (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
   for(var k in o)if(new RegExp("("+ k +")").test(format))  
     format = format.replace(RegExp.$1,  
       RegExp.$1.length==1 ? o[k] :   
         ("00"+ o[k]).substr((""+ o[k]).length));  
   return format;  
};

(function(package){
	var EClock = ceopen.object.createClass(null, {
		dom : null,
		dateFmt:null,
		date : null,
		timeout : null,
		current : null,
		
		isshut : true,
		timedifference : 0,
		init : function(arg0) {
			var _this = this;
			if(!arg0.dom) throw new Error("must designated parameter : dom");
			else this.dom = arg0.dom;
			if(arg0.date)_this.setDate(arg0.date);
			//_this.initBind();
			this.dateFmt=arg0.dateFmt;
		},
		setDate : function(date) {
			var _this = this;
			if(date instanceof Date) {
				this.date = date;
			} else if(typeof date == "string" || typeof date == "number") {
				var idate = new Date();
				idate.setTime(date);
				this.date = idate;
			}
			return _this;
		},
		start : function() {
			var _this = this;
			_this.shut();
			this.isshut = true;
			if(this.date == null) {
				_this.meizzTime(0);
			} else {
				var time1 = this.date.getTime() / 1000;
				var time2 = parseInt(new Date().getTime() / 1000);
				this.timedifference = time1 - time2; // 两端时间秒差
				_this.meizzTime(this.timedifference);
			}
		},
		meizzTime : function (n) {
			//console.log("test");
			var _this = this;
			var newDate = new Date();
			if(n!=0) newDate.setTime(newDate.getTime() + this.timedifference * 1000); // 得到一个新的时间
			this.current = newDate;
			//alert("ff:"+this.dateFmt+"|"+this.dom);
			if(this.isshut)
				//$(this.dom).html("ff:"+newDate.toLocaleString()+' 星期'+'日一二三四五六'.charAt(newDate.getDay()));
				$(this.dom).html(newDate.format(this.dateFmt));
			this.timeout = setTimeout(function(){
				_this.meizzTime(_this.timedifference);
			}, 1000);
		},
		shut : function() {
			if(this.timeout) clearTimeout(this.timeout);
		},
		shutHtml : function() {
			this.isshut = false;
		},
		restart : function(date) {
			var _this = this;
			if(typeof date != "undefined") {
				_this.setDate(date).start();
			} else _this.start();
		},
		getCurrent  : function() {
			if(this.current) 
				return this.current;
			else throw new Error("时间似乎还没有运行!");
		},
		getCurrentStr  : function() {
			if(this.current) 
				return this.current.toLocaleString()+' 星期'+'日一二三四五六'.charAt(this.current.getDay());
			else throw new Error("时间似乎还没有运行!");
		},
		getClient : function(){
			var date = new Date();
			var dateStr = date.toLocaleString()+' 星期'+'日一二三四五六'.charAt(this.current.getDay());
			return {"date":date,"dateStr":dateStr};
		},
		resume : function() {
			var _this = this;
			this.isshut = true;
			if(this.current) {
				_this.setDate(this.current).start();
			} else {
				_this.start();
			}
		}
	});
	
	jQuery.extend(package, {
		EClock : EClock
	});
})(ceopen.lvs.eclock);
