/**
 * @namespace com.thesis.control.TimeRemainBar
 * @author 钟军锐 August.R@263.net
 */

/** @id TimeRemainBar */
function TimeRemainBar(sEndDate, sParentID){
	this.sParentID = sParentID? sParentID : "Global";
	this.dEnd = new Date(sEndDate.replace(/-/g,'/'));
	this.RemainMilliseconds = 0;
	this.RemainMinutes = 0;
	this.Frame = C$("Div");
	this.Frame.className = "pbTimeOut Background";
	this.Body = C$("Div");
	this.Body.className = "pbTimeOut Remain";
	this.Frame.appendChild(this.Body);
	
	if(typeof TimeRemainBar._initialized == "undefined"){
		TimeRemainBar._initialized = true;
		
		TimeRemainBar._IntervalProcess = setInterval(function(){
			uEvent.Listener.notify("TimeRemainBar","refresh");
		},30000);
		
		/** @id release */
		TimeRemainBar.release = function(sParentID){
			uEvent.Listener.notify("TimeRemainBar", (sParentID? sParentID:"Global") + "_Dispose");
			uEvent.Listener.remove("TimeRemainBar",(sParentID? sParentID:"Global") + "_Dispose");
		};
		
		/** @id refresh */
		TimeRemainBar.prototype.refresh = function(){
			this.RemainMilliseconds = (this.dEnd - new Date());
			this.RemainMinutes = Math.floor(this.RemainMilliseconds/1000/60);
			if(this.RemainMinutes<=0){
				this.Body.style.width = "0px";
				this.Frame.title = "已超时" + (-this.RemainMinutes) + "分钟";
			}
			else{
				this.Body.style.width = (this.RemainMinutes > 180? 180:this.RemainMinutes)/3 + "px";
				this.Frame.title = "付款时限为：" + this.RemainMinutes + "分钟";
			}
		};
		
		/** @id dispose */
		TimeRemainBar.prototype.dispose = function(){
			this.RemainMilliseconds = null;
			this.RemainMinutes = null;
			this.Body = null;
			this.Frame = null;
			this.sParentID = null;
			uEvent.Listener.remove("TimeRemainBar","refresh",this._refresh);
			delete(this._refresh);
		};
		
		/** @id initialize */
		TimeRemainBar.prototype.initialize = function(Root){
			this.refresh();
			this._refresh = function(){Root.refresh();};
			uEvent.Listener.add("TimeRemainBar", "refresh", Root._refresh);
			uEvent.Listener.add("TimeRemainBar",Root.sParentID + "_Dispose", function(){
				Root.dispose();
			});
		};
	}
	
	this.initialize(this);
}

