﻿/*
	InfoRAP
	Dependencies: Util, jQuery, jQuery.simpleModal [for aProps.mModal]
*/
Util.ensureNamespacePath("Util.PopUp");
/*=======================================================================*/

/*-------------------------------------------------------------*/

	Util.PopUp.cPopUpHide = function(e) {
		var e = Util.getEvent(e);
		var aAnc = Util.getEventEle(e);

		if (this.mIsVisible)
			this.hide();
		
		return false;
	}
	
/*-------------------------------------------------------------*/

	Util.PopUp.cPopUpShow = function(e) {
		var e = Util.getEvent(e);
		var aAnc = Util.getEventEle(e);
		
		if (!this.mIsVisible)
			this.show();
		else {
			if (this.mEnableShowToggle)
				this.hide();
		}
		
		return false;
	}
	
/*=======================================================================*/
/*
	Class: Util.PopUp.cPopUp
	* aPopUpEle: Base Popup div (typically will have class PopUp)
	* aEleForLoc: Typically an anchor for locating control
		- wired to show by default
*/
	Util.PopUp.cPopUp = function(aOwnerObj, aPopUpEle, aEleForLoc, aProps) {
		this.mOwnerObj = aOwnerObj;
		this.mPopUpEle = aPopUpEle;
		this.mEleForLoc = aEleForLoc;
		for (var aProp in aProps) {
			//mModal
			//mEscapeable
			//mEnableShowToggle
			this[aProp] = aProps[aProp];
		}
		
		this.mIsVisible = false;
		this.mWidth = 0;
		
		this.mToggleVisEffect = "slideToggle";
		this.mToggleVisSpeed = "fast";
		
		/*-----------------------*/
		
		if (this.mPopUpEle) {
			if (!this.mModal) {
				//done backwards to avoid IE error setting position: 'inherit'
				$(this.mPopUpEle).css({position: 'absolute', top: 0, left: 0});
			}

			this.mWidth = $(this.mPopUpEle).width();
			$(this.mPopUpEle).css("visibility", "inherit"); //??
			$(this.mPopUpEle).hide();
		}
		
		this.wireUpShowEle(this.mEleForLoc);
		this.wireUpHideEle($(this.mPopUpEle).find("table.Title a")[0]);
	}
	
/*-------------------------------------------------------------*/

	Util.PopUp.cPopUp.prototype.wireUpShowEle = function(aShowEle, aShowFunc) {
		if (aShowEle) {
			var aThis = this;
			if (aShowFunc)
				aShowEle.onclick = function(e) {return aShowFunc.call(aThis, e);};
			else {
				aShowEle.onclick = function(e) {return Util.PopUp.cPopUpShow.call(aThis, e);};
			}
		}
	}
	
/*-------------------------------------------------------------*/

	Util.PopUp.cPopUp.prototype.unWireShowEle = function(aShowEle) {
		if (aShowEle) {
			aShowEle.onclick = null;
		}
	}
	
/*-------------------------------------------------------------*/

	Util.PopUp.cPopUp.prototype.wireUpHideEle = function(aHideEle, aHideFunc) {
		if (aHideEle) {
			var aThis = this;
			if (aHideFunc)
				aHideEle.onclick = function(e) {return aHideFunc.call(aThis, e);};
			else
				aHideEle.onclick = function(e) {return Util.PopUp.cPopUpHide.call(aThis, e);};
		}
	}
	
/*-------------------------------------------------------------*/

	Util.PopUp.cPopUp.prototype.toggleVisibility = function(aCallback) {
		this.mIsVisible = !this.mIsVisible;
		eval("$(this.mPopUpEle)." + this.mToggleVisEffect + "(" +
			"'" + this.mToggleVisSpeed + "'" + 
			(aCallback ? ", aCallback" : "") +
			")");
	}
	
/*-------------------------------------------------------------*/

	Util.PopUp.cPopUp.prototype.position = function() {
		if (this.mEleForLoc) {
			var aLoc = $(this.mEleForLoc).offset();
			aLoc.top += $(this.mEleForLoc).height();
			$(this.mPopUpEle).css(aLoc); //aLoc is a map w/ left,top
			
			if ((aLoc.left+this.mWidth) > ($(window).width()-6)) {
				aLoc.left = (($(window).width()-6) - this.mWidth) + $(document).scrollLeft();
				$(this.mPopUpEle).css(aLoc);
			}
		}
	}
	
/*=======================================================================*/

	Util.PopUp.cPopUp.prototype.show = function(aCalledFromOwner, aCallback) {
		if (this.mOwnerObj.show && !aCalledFromOwner) {
			this.mOwnerObj.show.call(this.mOwnerObj, aCallback);
		}
		else {
			this.position();
			if (this.mOwnerObj.load)
				if (!this.mOwnerObj.load.call(this.mOwnerObj, this))
					return;
			
			if (this.mModal) {
				var aThis = this;
				$(this.mPopUpEle).modal({
					overlayCss: {backgroundColor: '#000000'},
					persist: true,
					appendTo: 'form',
					overlayClose: this.mEscapeable,
					escClose: this.mEscapeable,
					//jQuery.SimpleModal handles centering automatically
					position: !this.mEleForLoc ? {} : [this.mPopUpEle.style.top, this.mPopUpEle.style.left],
					onOpen: function(dialog) {
						dialog.overlay.fadeIn('fast', function () {
							dialog.container.show();
							aThis.toggleVisibility(aCallback);
						}); //fadeIn call
					}, //onOpen
					onClose: function(dialog) {
						aThis.toggleVisibility(function () {
							dialog.overlay.fadeOut('fast', null);
							$.modal.close();
						});
						//alert("??");
					} //onClose
				});
			}
			else
				this.toggleVisibility(aCallback);
		}
	}
	
/*-------------------------------------------------------------*/

	Util.PopUp.cPopUp.prototype.hide = function(aCalledFromOwner, aCallback) {
		if (this.mOwnerObj.hide && !aCalledFromOwner) {
			this.mOwnerObj.hide.call(this.mOwnerObj, this);
		}
		else {
			if (this.mOwnerObj.hideUnload) {
				if (!this.mOwnerObj.hideUnload.call(this.mOwnerObj, this))
					return;
			}
		
			if (this.mModal) {
				$.modal.close();
				/*this.toggleVisibility(function (dialog) {
					dialog.overlay.fadeOut('fast', aCallback);
					$.modal.close();
				}); //toggleVisibility*/
			}
			else
				this.toggleVisibility(aCallback);
		}
	}
	
/*=======================================================================*/

	Util.PopUp.cPopUp.prototype.title = function(aTitle) {
		var aTitleTD = $(this.mPopUpEle).find("Table.Title td.id_PopUpFrame_Title")[0]
		
		if (!aTitle) {
			return aTitleTD.innerHTML;
		}
		else {
			aTitleTD.innerHTML = aTitle;
		}
	}
	
/*=======================================================================*/

