/**-------------------------------------------------*\
 * 													*
 * @author Wang Jianchun							*
\*--------------------------------------------------*/
var GlobalUpdater = Class.create();
GlobalUpdater.prototype = {
	container:		null,// param[0], container to be updated, required
	url:			"",// param[1], url, required
	param:			"",// param[2], parameter, optional
	
	ifSessionCheck:	true,
	sessionChkUri:	"sessionCheck",
	
	hintSpan:		null,
	ifShowHint:		true,
	hintInside:		false,// show hint span inside containder
	hintMessage:	"Loading...",
	
	msgInside:		false,// show messages inside container
	
	initialize: function() {
		var args = $A(arguments);
		
		this.setContainer(args[0]);
		this.setUrl(args[1]);
		this.setParam(args[2]);
	},
	/**
	 * Setters
	 */
	setContainer: function(container) {
		this.container = (container && $(container)) ? $(container) : null;
	},
	setUrl: function(url) {
		this.url = url ? url.trim() : "";
	},
	setParam: function(param) {
		this.param = param ? param.trim() : "";
	},
	setIfSessionCheck: function(ifSessionCheck) {
		this.ifSessionCheck = (ifSessionCheck === true);
	},
	setSessionCheckUri: function(sessionChkUri) {
		this.sessionChkUri = sessionChkUri;
	},
	setIfShowHint: function(ifShowHint) {
		this.ifShowHint = (ifShowHint === true);
	},
	setHintInside: function(hintInside) {
		this.hintInside = (hintInside === true);
	},
	setHintMessage: function(hintMessage) {
		this.hintMessage = hintMessage ? hintMessage : "Loading...";
	},
	setMsgInside: function(msgInside) {
		this.msgInside = (msgInside === true);
	},
	/**
	 * The interface for outer world to call
	 */
	update: function() {
		if(!this.container) {
			alert("Error:[GlobalUpdater.update]\n\tThe container to be updated does not exist!");
			return;
		}
		if(!this.url) {
			alert("Error:[GlobalUpdater.update]\n\tThe url must be specified!");
			return;
		}
		
		if(!window._lbUpdater) {
			window._lbUpdater = $H();
		}
		if(window._lbUpdater[this.container.id]) {
			window._lbUpdater[this.container.id].removeHint();
		}
		window._lbUpdater[this.container.id] = this;

		if(this.ifShowHint) {
			this.showHint();
		}
		if(this.ifSessionCheck) {
			new Ajax.Request(
				this.sessionChkUri,
				{
					method:		"get",
					onFailure:	this.sessionCheckFail.bind(this),// 404 500
					onSuccess:	this.sessionCheckSucc.bind(this),
					onComplete:	this.sessionCheckCompl.bind(this)
				});
		} else {
			this.doUpdate();
		}
	},
	sessionCheckFail: function() {
		var errMessage = "系统错误!<br>SessionCheckUri可能有错<br>请联系开发维护人员..^_^<br>";
		this.showError(errMessage, "[GlobalUpdater]错误信息");
	},
	sessionCheckSucc: function(xmlHttpReq) {
		var rtnJson = eval( "(" + xmlHttpReq.responseText.trim() + ")");
		if(rtnJson.error) {
			var errMessage = "系统超时!<br>请重新登录..<a href=\"mgrlogin.jsp\">登录</a>";
			this.showError(errMessage, "[GlobalUpdater]失败信息");
		} else {
			this.doUpdate();
		}
	},
	sessionCheckCompl: function(xmlHttpReq) {
		if(xmlHttpReq.responseText.trim() === "") {
			var errMessage = "网络错误!<br>SessionCheck时发生错误<br>可能是服务器停止或您的网络连接有问题..";
			this.showError(errMessage, "[GlobalUpdater]错误信息");
		}
	},
	doUpdate: function() {
		new Ajax.Updater(
			{success: this.container},
			this.uuUrlGen(),
			{
				method:		"get",
				parameters:	this.param,
				onFailure:	this.updateFail.bind(this),// 404 500
				onComplete:	this.updateCompl.bind(this),
				evalScripts:true
			}
		);
	},
	updateFail: function() {
		var errMessage = "系统错误!<br>URL相关的页面可能有错(内部错误或该URL不存在)<br>请联系开发维护人员..^_^<br>container: " + this.container + "<br>url: " +  this.url + "<br>param: " + this.param;
		this.showError(errMessage, "[GlobalUpdater]错误信息");
	},
	updateCompl: function() {
		this.removeHint();
	},
	/**
	 * Generate a unique url from this.url
	 * @private
	 */
	uuUrlGen: function() {
		var uuUrl;
		var xcharset = document.all ? "GB2312" : "UTF-8";
		
		if (this.url.indexOf("?") >= 0) {
			uuUrl = this.url + "&xcharset=" + xcharset + "&ran=" + Math.random();
		} else {
			uuUrl = this.url + "?xcharset=" + xcharset + "&ran=" + Math.random();
		}
	
		return uuUrl;
	},
	/**
	 * Show loading hint according to hintInside
	 * @private
	 */
	showHint: function() {
		this.hintSpan = document.createElement("span");
		this.hintSpan.innerHTML = this.hintMessage;
		
		if(this.hintInside) {
			this.hintSpan.className = "containerHint";
			var ltOffsets = Position.cumulativeOffset(this.container);// left-top offsets
			
			this.hintSpan.style.left = ltOffsets[0] + "px";
			this.hintSpan.style.top = ltOffsets[1] + "px";
			this.container.appendChild(this.hintSpan);
		} else {
			this.hintSpan.className = "universalHint";
			document.body.appendChild(this.hintSpan);
		}
	},
	/**
	 * Remove the loading hint span
	 */
	removeHint: function() {
		if(this.hintSpan && this.hintSpan.parentNode) {
			this.hintSpan.parentNode.removeChild(this.hintSpan);
			this.hintSpan = null;
		}
	},
	/**
	 * @private
	 * @param {Object} headText
	 * @param {Object} errMessage
	 */
	showError: function(errMessage, headText) {
		if(this.msgInside) {// Show message in con
			this.container.innerHTML = errMessage;
		} else {
			var dWin = new DragWin();
			dWin.setWidth(300);
			dWin.setHeaderInner("<span style=\"font: 12px STSong; color: red;\">" + headText + "</span>", "left");
			dWin.setCloseInner("关闭");
			dWin.setBodyInner("<span style=\"font: 12px STSong; color: gray;\">" + errMessage + "</span>");
			dWin.show("content");
		}
		
		this.removeHint();
	}
};