/*
  By Hangring
  #2008.11.03#
  ---
*/
if (typeof Frame == 'undefined')
	throw new Error('Frame is not defined');

Frame.Popup = function () {};

Frame.Popup.prototype = {
	// HTMLElement
	Container : null,
	
	// 关联浮动层列表
	CurrentPopups : [],
	// 浮动层级别索引
	CurrentIndex : 0,
	
	Create : function (p_el) {
		var c = this.Container = oNode.CreateNode('div');
		CSS.AddClass(c, 'frame', 'frame-popup');
		oNode.IsNode(p_el) &&
		oNode.AddNode(c, p_el);
		
		return c;
	},
	
	/*
	 * el实现以下属性及方法
	 *   attribute:
	 *     Visible : 标识当前浮动层显示与否
	 *   function:
	 *     Show : 执行显示
	 *     Hide : 执行隐藏，默认执行{el.style.visibility = 'visible'}使之隐藏
	 *     Close : 浮动窗关闭
	 */
	Add : function (el /* : HTMLElement */, level /* : Number */, modal /* : Boolean */) {
		Debug.Show(['====== Popup.Add()添加级别{ ', level, ' }之前; 已存在的浮动层数 : ', this.CurrentPopups.length, ' ======'].join(''));
		// 级别1，则清除所有浮动层
		if (level == 1 && this.CurrentPopups.length > 0
			&& (this.CurrentPopups.length != 1 || this.CurrentPopups[0] != el)) {
			this.RemoveAllFromLevel();
		}
		if (level > 1) {
			level = this.CurrentPopups[level - 1] == el ? level + 1 : level;
			this.RemoveAllFromLevel(level);
			Debug.Show(['====== Popup Remove { ' + level + ' } ======'].join(''));
		}
		
		// 添加浮动层到当前显示列表中
		if (level != this.CurrentIndex) {
			oNode.AddNode(el, this.Container);
			this.CurrentPopups.push(el);
			this.CurrentIndex = level;
			Debug.Show(['====== Popup Added { ' + level + ' } ======'].join(''));
		}
		Debug.Show(['====== Popup.Add()添加级别{ ', level, ' }之后; 总存在的浮动层数 : ', this.CurrentPopups.length, ' ======'].join(''));
		
		// 显示
		if (typeof el.Show == 'function')
			el.Show();
		else 
			el.style.visibility = 'visible';
		el.Visible = true;
	},
	
	// 某个级别（包括该级别）之后的所有级别
	RemoveAllFromLevel : function (level /* : int */) {
		level = level || 1;
		var startIndex = level - 1;
		for (var i = (startIndex || 0), len = this.CurrentPopups.length; i < len; i++) {
			if (typeof this.CurrentPopups[i].Hide == 'function')
				this.CurrentPopups[i].Hide();
			else 
				this.CurrentPopups[i].style.visibility = 'hidden';
			this.CurrentPopups[i].Visible = false;
			
			typeof this.CurrentPopups[i].Close == 'function' && 
			this.CurrentPopups[i].Close();
		}
		this.CurrentIndex = startIndex;
		this.CurrentPopups = startIndex > 0 ? this.CurrentPopups.slice(0, startIndex) : [];
		Debug.Show(['====== 删除索引{ ', level, ' }之后，所有浮动层数 : ', this.CurrentPopups.length, '，当前索引：', startIndex, ' ======'].join(''));
	},
	
	RemovePopupFromContainer : function (el) {
		oNode.RemoveNode(el, this.Container);
	}
};