<!--
/**
 * popup.js
 * @fileoverview 右下角弹出框功能
 * @author Lynk Li
 */

/**
 * @class Popup
 * 右下角弹出框的显示，隐藏，摧毁，管理
 * @constructor
 * @author Lynk Li
 * @param int index 弹出提示框索引
 * @param String str 弹出提示框内容 
 */
function Popup(index, str) {
	/**
	 * 弹出提示框索引
	 */
	this.index = index;
	/**
	 * 弹出提示框对象
	 */
	this.obj = null;
	/**
	 * 弹出提示框顶距
	 */
	this.popTop = 0;
	/**
	 * 上升、停留、下降总间隔数
	 */
	this.popEnd = _setting.popupHeight * 2 / _setting.popupIntervalHeight + _setting.popupStay / _setting.popupInterval;//上升、停留、下降总间隔数
	/**
	 * 上升、停留总间隔数
	 */
	this.stayEnd = _setting.popupHeight / _setting.popupIntervalHeight + _setting.popupStay / _setting.popupInterval;//上升、停留总间隔数
	/**
	 * 弹出提示框内容
	 */
	this.str = str;
	this.init = Popup_init;
	this.distory = Popup_distory;
	this.show = Popup_show;
	this.popup = Popup_popup;
	this.getLeft = Popup_getLeft;
	this.getTop = Popup_getTop;
}

/**
 * 初始化弹出提示框
 */
function Popup_init() {
	var data = {
		popupWidth 	: _setting.popupWidth,
		popupHeight	: _setting.popupHeight,
		imagesPath	: _setting.imagesPath,
		str			: this.str,
		index		: this.index
		};
	var winstr = TrimPath.processDOMTemplate("popup_tpl", data);
	this.obj = document.createElement("div");
	this.obj.style.display = "none";
	this.obj.style.overflow = "hidden";
	document.body.insertBefore(this.obj, null);
	this.obj.innerHTML = winstr;
}

/**
 * 删除摧毁弹出提示框
 */
function Popup_distory() {
	this.obj.style.display = "none";
	this.obj.innerHTML = "";
	document.body.removeChild(this.obj);
	//delete(this.obj);
}

/**
 * 显示弹出提示框
 * @param int x 顶距
 * @param int y 左边距
 * @param int w 宽度
 * @param int h 高度
 */
function Popup_show(x, y, w, h) {
	this.obj.style.display = "";
	this.obj.style.position = "absolute";
	this.obj.style.left = x;
	this.obj.style.top = y;
	this.obj.style.width = w;
	this.obj.style.height = h;
}

/**
 * 计算获取弹出提示框的左边距
 * @return int 左边距
 */
function Popup_getLeft() {
	var wc = 9999;
	var ch = document.documentElement.clientHeight;
	var cw = document.documentElement.clientWidth;
	if (ch - 5 >= _setting.popupHeight) wc = Math.floor((ch - 5) / _setting.popupHeight);
	var left = 0;
	var t = 0;
	t = Math.floor(this.index / wc) + 1;
	left = cw - _setting.popupWidth * t - 5;
	return left;
}

/**
 * 计算获取弹出提示框的顶距
 * @return int 顶距
 */
function Popup_getTop(type) {
	var wc = 9999;
	var start = this.index;
	var ch = document.documentElement.clientHeight;
	var cw = document.documentElement.clientWidth;
	if (ch - 5 >= _setting.popupHeight) {
		wc = Math.floor((ch - 5) / _setting.popupHeight);
		start = this.index % wc;
	}
	var top = 0;
	if (type == 0) {//上升
		top = ch - start * _setting.popupHeight - 5 - this.popTop * _setting.popupIntervalHeight;
	} else {//下降
		top = ch - (start + 1) * _setting.popupHeight - 5 + (this.popTop - this.stayEnd) * _setting.popupIntervalHeight;
	}
	return top;
}

/**
 * 处理弹出提示框显示/摧毁
 */
function Popup_popup() {
	if (this.popTop > this.popEnd) {
		this.distory();
		return;
	} else if (this.popTop >= this.stayEnd && this.popTop < this.popEnd) {//下降渐变
		this.show(this.getLeft(), this.getTop(1), _setting.popupWidth, (this.popEnd - this.popTop) * _setting.popupIntervalHeight);
	} else if (this.popTop <= _setting.popupHeight / _setting.popupIntervalHeight) {//上升渐变
		this.show(this.getLeft(), this.getTop(0), _setting.popupWidth, this.popTop * _setting.popupIntervalHeight);
	}
	this.popTop++;
}

/**
 * 弹出提示框定时管理器
 */
function PopUpTimer() {
	/**
	 * 弹出提示框存储数组，其每个元素都是一个{@link Popup}对象
	 */
	this.arr = new Array();
	this.run = PopUpTimer_run;
	this.getIndex = PopUpTimer_getIndex;
	/**
	 * 定时显示刷新Timer
	 */
	this.timer = null;
}

/**
 * 弹出提示框Timer运行函数
 */
function PopUpTimer_run() {
	for (var i = 0; i < this.arr.length; i++) {
		var popObj = this.arr[i];
		if (popObj != null) {
			if (popObj.popTop > popObj.popEnd) {
				popObj.distory();
				this.arr[i] = null;
				delete(popObj);
			} else {
				popObj.popup();
			}
		}
	}
}

/**
 * 获取新的弹出提示框的索引
 */
function PopUpTimer_getIndex() {
	for (var i = 0; i < 10; i++) {
		if (this.arr.length <= i || this.arr[i] == null) return i;
	}
	return -1;
}

var _popUpTimer = new PopUpTimer();
_popUpTimer.timer = setInterval("_popUpTimer.run()", _setting.popupInterval);
//-->