var prefix_id = "popLayer";
var PARSE_MSEC = 2;
var MIN_MSEC = 30;
var DISP_DISTANCE = 24;
var BORDER = 4;
var LAYER_W = 480;
var FORCUS_COLOR = "#ffffff";
var BLUR_COLOR = "#e8ffe8";

var popupManager = new PopUpManager();
var targetThread = "";
var targetUrl = "";

function initResPop(threadNo, url) {
	targetThread = threadNo;
	targetUrl = url;
	popupManager.counterFlg = true;
	setPopUpCounter();
}

function ResPopUp(id, fromResponseNo, toResponseNo, x, y) {
	this.id = id;
	this.fromResponseNo = fromResponseNo;
	this.toResponseNo = toResponseNo;
	this.closeTimer = MIN_MSEC + popupManager.obj.length * PARSE_MSEC;
	this.x = x;
	this.y = y;
	this.w = 0;
	this.h = 0;
	this.overFlg = true;
	this.zIndex = (1000 + popupManager.obj.length + 1);
}

ResPopUp.prototype.addLayer = function() {
	var obj = $("#" + this.id);

	obj.fadeIn(120);
};

ResPopUp.prototype.removeLayer = function() {
	var obj = $("#" + this.id);

	obj.fadeOut(200, function() {
		obj.remove();
	});
};

function PopUpManager() {
	this.obj = new Array();
	this.x = 0;
	this.y = 0;
	this.count = 0;
	this.openId = undefined;
	this.over = undefined;
};

PopUpManager.prototype.length = function() {
	return this.obj.length;
};

PopUpManager.prototype.indexOfByTR = function(fromResponseNo, toResponseNo) {
	var ret = -1;

	for (var i = 0 ; i < this.obj.length ; i++) {
		if (this.obj[i].fromResponseNo == fromResponseNo && this.obj[i].toResponseNo == toResponseNo) {
			ret = i;
			break;
		}
	}

	return ret;
}

PopUpManager.prototype.indexOfByID = function(id) {
	var ret = -1;

	for (var i = 0 ; i < this.obj.length ; i++) {
		if (this.obj[i].id == id) {
			ret = i;
			break;
		}
	}

	return ret;
}

PopUpManager.prototype.add = function(fromResponseNo, toResponseNo, count, flg) {
	var idx = this.indexOfByTR(fromResponseNo, toResponseNo);

	if (idx == -1) {
		loadResponse(this, fromResponseNo, toResponseNo, count, flg);
	}
	else {
		// レイヤーを最上位に持ってくる
		this.over = this.obj[idx].id;
		this.openId = this.obj[idx].id;

		// レイヤー階層表示
		this.zIndexSort();
	}
};

function loadResponse(popupObject, fromResponseNo, toResponseNo, count, flg) {
	var oldX = popupManager.x;
	var oldY = popupManager.y;

	var successfulFunc = function(rsp) {
		var id = prefix_id + (new Date().getTime());
		var x, y;

		// 取得したレスの文字数が0文字の場合は非表示
		if(rsp.length == 0) {
			return;
		}

		// 読込前からカーソルが一定距離動いている場合は非表示
		if (Math.sqrt(Math.pow(popupManager.x - oldX, 2) + Math.pow(popupManager.y - oldY, 2)) > DISP_DISTANCE) {
			return;
		}

		// 座標取得
		if (popupObject.obj.length > 0 && !flg) {
			// 第２階層以降
			var i = popupObject.obj.length;
			x = popupObject.obj[i - 1].x + 32;
			y = popupObject.obj[i - 1].y + 20;

			// クリッピング処理
			if (x + LAYER_W > $(window).width() - 24) {
				x = $(window).width() - LAYER_W - 24;
			}
			if (x < 24) {
				x = 24;
			}
		}
		else {
			// 第１階層
			var p = $("#resPop_" + fromResponseNo + "_" + toResponseNo + "_" + count);
			x = p.offset().left + p.width() + 16;
			y = p.offset().top - 16;
		}

		// 新規レイヤー作成
		var elem = document.createElement("div");
		elem.id = id;
		elem.style.position = "absolute";
		elem.style.padding = "2px 4px";
		elem.style.width = LAYER_W + "px";
		elem.style.backgroundColor = FORCUS_COLOR;
		elem.style.border = BORDER + "px solid #81bc0b";
		elem.style.fontSize = "88%";
		elem.style.lineHeight = "14px";
		elem.style.left = x + "px";
		elem.style.top = y + "px";
		elem.style.display = "none";
		elem.style.textAlign = "left";
		elem.innerHTML = rsp;

		$("body").append(elem);

		// レイヤー表示
		var data = new ResPopUp(id, fromResponseNo, toResponseNo, x, y);
		var newIdx = popupObject.obj.length;
		popupObject.obj[newIdx] = data;
		data.addLayer();
		popupObject.obj[newIdx].closeTimer = MIN_MSEC + newIdx * PARSE_MSEC;
		popupObject.obj[newIdx].w = $("#" + data.id).width();
		popupObject.obj[newIdx].h = $("#" + data.id).height();

		// 作成したレイヤーを最上位に持ってくる
		popupObject.over = popupObject.obj[newIdx].id;
		popupObject.openId = popupObject.obj[newIdx].id;

		// レイヤー階層表示
		popupObject.zIndexSort();
	};

	var failedFunc = function(xml, status, e) {
	};

	$.ajax({
		type : 'GET',
		url  : targetUrl,
		data : {
			TH : targetThread,
			RS : toResponseNo
		},
		success : successfulFunc,
		error : failedFunc
	});

	return false;
};



PopUpManager.prototype.remove = function(fromResponseNo, toResponseNo) {
	var idx = this.indexOfByTR(fromResponseNo, toResponseNo);

	if (idx > -1) {
		this.obj[idx].removeLayer();
		var z = this.obj[idx].zIndex;

		var wrk = this.obj.slice(0, idx);
		if (idx < this.length() - 1) {
			wrk = wrk.concat(this.obj.slice(idx + 1));
		}
		this.obj = wrk;

		for (var i = 0 ; i < this.obj.length ; i++) {
			if (this.obj[i].zIndex > z) {
				this.obj[i].zIndex--;
			}
		}
	}
};

PopUpManager.prototype.zIndexSort = function() {
	if (this.over == undefined) {
		return;
	}

	var idx = this.indexOfByID(this.over);
	var z = this.obj[idx].zIndex;

	for (var i = 0 ; i < this.obj.length ; i++) {
		if (i == idx) {
			this.obj[i].zIndex = (1000 + this.obj.length);
		}
		else if (this.obj[i].zIndex > z) {
			this.obj[i].zIndex--;
		}

		var obj = document.getElementById(this.obj[i].id);
		obj.style.zIndex = this.obj[i].zIndex;
	}
};

PopUpManager.prototype.check = function() {
	var z = 0;

	this.over = (this.openId != undefined) ? this.over : undefined;

	for (var i = 0 ; i < this.obj.length ; i++) {
		if ((this.obj[i].x < this.x) &&
			(this.obj[i].x + this.obj[i].w + (BORDER * 2) + 4 > this.x) &&
			(this.obj[i].y < this.y) &&
			(this.obj[i].y + this.obj[i].h + (BORDER * 2) + 8 > this.y)) {

			if (z < this.obj[i].zIndex) {
				this.over = this.obj[i].id;
				z = this.obj[i].zIndex;
			}
		}
	}

	// 自分より上位のレイヤーは削除カウントスタート
	var idx = this.indexOfByID(this.over);
	for (var i = 0 ; i < this.obj.length ; i++) {
		if (i <= idx || this.over == this.obj[i].id || this.openId == this.obj[i].id) {
			this.obj[i].closeTimer = MIN_MSEC + (this.obj.length - i) * PARSE_MSEC;
		}
		else {
			this.obj[i].closeTimer = this.obj[i].closeTimer - 1;
		}

		if (this.obj[i].closeTimer <= 0) {
			this.remove(this.obj[i].fromResponseNo, this.obj[i].toResponseNo);
		}
		else {
			var obj = $("#" + this.obj[i].id);
			if (this.openId != undefined) {
				obj.css("background-color", (this.obj[i].id == this.openId) ? FORCUS_COLOR : BLUR_COLOR);
			}
			else {
				obj.css("background-color", (this.obj[i].id == this.over) ? FORCUS_COLOR : BLUR_COLOR);
			}
		}
	}
};

function setPopUpCounter() {
	popupManager.count++;

	popupManager.check();

	if (popupManager.counterFlg) {
		// 常にチェック
		setTimeout("setPopUpCounter()", 10);
	}
}



$(document).mousemove(function(e) {
	popupManager.x = e.pageX;
	popupManager.y = e.pageY;
});

function resOpen(fromResponseNo, toResponseNo, count, flg) {
	var firstFlg = true;

	if (flg == null || flg == undefined) {
		firstFlg = false;
	}
	else {
		firstFlg = flg;
	}

	if (targetThread != "") {
		popupManager.add(fromResponseNo, toResponseNo, count, firstFlg);
	}
}

function resClose() {
	popupManager.openId = undefined;
}