//popup manager
//

function objDump(obj, debugAreaId){
	if (!debugAreaId) debugAreaId = "debug";
	if (!$(debugAreaId)) return false;
	
	$(debugAreaId).innerHTML = '';
	for (var i in obj){
		$(debugAreaId).innerHTML += i + "=" + obj[i] + "<br/ >";
	}
}

function errlog(text, debugAreaId){
	if (!debugAreaId) debugAreaId = "debug";
	$(debugAreaId).innerHTML += text;
}


function getEventTarget(e){
	if(!e){
		e=window.event;
	}
	if(e&&!e.target){
		e.target=e.srcElement;
	}
	return e.target;
}




function popupMng(delayTime){
	this.popups = new Array();
	this.timer = null;
	this.currentPopup = null;
	this.delayTime = delayTime;
	
	Event.observe(document, 'mousedown', this.killPopup.bindAsEventListener(this), false);
}

popupMng.prototype.registerPopup = function(trgr, pp){
	var popup = $(pp);
	if (!popup) return false;
	
	var trigger = $(trgr);
	if (!trigger) return false;

	Event.observe(trigger, 'click', this.showPopup.bindAsEventListener(this), false);
	Event.observe(popup, 'mousedown', this.mouseDown.bindAsEventListener(this), false);
	
//	Event.observe(trigger, 'mouseover', this.keepPopup.bindAsEventListener(this), false);
//	Event.observe(trigger, 'mouseout', this.hidePopup.bindAsEventListener(this), false);
	
//	Event.observe(popup, 'mouseover', this.keepPopup.bindAsEventListener(this), false);
//	Event.observe(popup, 'mouseout', this.hidePopup.bindAsEventListener(this), false);
	
	this.popups[trgr] = pp;
}

popupMng.prototype.mouseDown = function(e){
	//do NOT call preventDefault()
	e.cancelBubble = true;
}

popupMng.prototype.showPopup = function(e){
//	$("debug").value += "[showPopup begin] ";
	
	//イベントオブジェクトはshowPopupをコールしたtriggerがtargetとなっている
	var trgr = getEventTarget(e).id;
	//triggerに関連付けられているpopupのID
	var pp = this.popups[trgr];
	
	//ポップアップの表示
	var popup = $(pp);
	if (popup){
		if (pp != this.currentPopup){
			//違うPopupが出ているならkill
			if (this.currentPopup){
				this.killPopup();
			}
			
			//triggerの下に、右揃えで表示する
			var trgrLeft = Position.cumulativeOffset($(trgr))[0];
			var trgrTop = Position.cumulativeOffset($(trgr))[1];
			var trgrWidth = Element.getDimensions(trgr).width;
			var trgrHeight = Element.getDimensions(trgr).height;
			var ppWidth = Element.getDimensions(pp).width;
			var ppLeft = trgrLeft + trgrWidth - ppWidth;
			popup.style.left = (ppLeft > 4 ? ppLeft : 4) + "px";
			popup.style.top = (trgrTop + trgrHeight) + "px";
			
			
			
			//表示
			popup.style.display = "block";
			
			//070830
			//ポップアップがウィンドウ下限を超えるようならtrgrの上側に出す
			var wndHeight =  window.innerHeight
			|| document.documentElement && document.documentElement.clientHeight 
			|| document.body && document.body.clientHeight 
			|| -1;
			var ppHeight = Element.getDimensions(pp).height;
			if (trgrTop + trgrHeight + ppHeight > wndHeight){
				popup.style.display = "none";
				popup.style.top = (trgrTop - ppHeight) + "px";
				popup.style.display = "block";
			}
			
			
			//現在のポップアップを更新
			this.currentPopup = pp;
			
		//	$("debug").value += "[newPopup] ";
		}
		else{
			this.keepPopup();
		//	$("debug").value += "[keepPopup] ";
		}
	}
}

//killPopup用のタイマーをリセットして表示継続
popupMng.prototype.keepPopup = function(e){
	window.clearTimeout(this.timer);
	
//	$("debug").value += "keepPopup ";
}

//ポップアップをdelayTime後に消す
popupMng.prototype.hidePopup = function(e){
	this.killPopup(this.delayTime);

//	$("debug").value += "hidePopup ";
}

//ポップアップ消去タイマーの開始
popupMng.prototype.killPopup = function(delayTime){
	var popup = $(this.currentPopup);
	var ppmng = this;
	if (popup){
		window.clearTimeout(this.timer);
		if (delayTime > 0){
			this.timer = window.setTimeout(function(){
				popup.style.display = "none";
				ppmng.clearCurrentPopup();
			//	$("debug").value += "killPopupTimer ";
			}, delayTime);
		}
		else{
			popup.style.display = "none";
			ppmng.clearCurrentPopup();
		//	$("debug").value += "killPopupSoon ";
		}
	}
}

popupMng.prototype.clearCurrentPopup = function(){
	this.currentPopup = null;
}
