//the currently visible popup (if any)
var _tppCurrent = false;

//the modal mask - shared by all instances
var _tppMask = false;

//register the onclick handlers
_tppAddEvent(document, "click", _tppDocumentOnClick);
_tppAddEvent(window, "resize", _tppSetMaskSize);

//useful event registration function
function _tppAddEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

function _tppDocumentOnClick(event) {
	//use document level onclick so we know when to hide non-modals	
	//defer hiding logic to current popup
	if (_tppCurrent)
		return _tppCurrent.handleEvent(event);
}

//sizes/resizes the modal mask if the window size changes
function _tppSetMaskSize() {

	//only bother if there's a popup visible and modal
	if (!_tppCurrent || !_tppCurrent.isModal())
		return;

	if (window.innerHeight!=window.undefined) 
		fullHeight = window.innerHeight;
	else if (document.compatMode=='CSS1Compat')
		fullHeight = document.documentElement.clientHeight;
	else if (document.body)
		fullHeight = document.body.clientHeight;

	if (window.innerWidth!=window.undefined) 
		fullWidth = window.innerWidth;
	else if (document.compatMode=='CSS1Compat')
		fullWidth = document.documentElement.clientWidth;
	else if (document.body)
		fullWidth = document.body.clientWidth;
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > document.body.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = document.body.scrollHeight
	}
	
	_tppMask.style.height = popHeight + "px";
	_tppMask.style.width = document.body.scrollWidth + "px";
	//alert('_tppMask height: ' + _tppMask.style.height + ' _tppMask width: ' + _tppMask.style.width );
}

//public functions

//close the currently active popup (if any)
function closeCurrentPanelPopup() {
	if (_tppCurrent)
		_tppCurrent.hide();
}

//close a specific popup (if shown)
function closePanelPopup(panelComponentId) {
	if (_tppCurrent)
		_tppCurrent.hide();
}

//define the tpp object and methods
function TrinidadPanelPopup(containerId, triggerId, modal, centered, hideOnClick) {
	//define object properties
	this._modal = modal;
	this._centered = centered;
	this._hideOnClick = hideOnClick;
	this._containerId = containerId;
	this._triggerId = triggerId;

	//initialise the mask if modal and not already done
	if (this._modal && !_tppMask) {
		//create mask for modal popups
		mask = document.createElement('div');
		mask.id = '_trPopupMaskId';
		mask.style.cssText = "position: absolute; z-index: 200;top: 0px;left: 0px;width: 100%;height: 100%;opacity: .0;filter: alpha(opacity=0);background-color: #333333;cursor: not-allowed;display:none;";
		mask.innerHTML = "&nbsp;<!--[if lte IE 6.5]><iframe style='display:none;/*sorry for IE5*/ display/**/:block;/*sorry for IE5*/ position:absolute;/*must have*/ top:0;/*must have*/ left:0;/*must have*/ z-index:-1;/*must have*/ width:3000px;/*must have for any big value*/ height:3000px;/*must have for any big value*/cursor: not-allowed;'></iframe><![endif]-->";
		//add mask to body
		document.body.appendChild(mask);
		_tppMask = mask;
	}
}

//show the popup
TrinidadPanelPopup.prototype.show = function(event)
{
	//ignore if same same instance
	if (this == _tppCurrent)
		return;
	
	//if already another instance showing, then hide it	
	if (_tppCurrent)
		_tppCurrent.hide(event);
	
	//position the popup
	var left = 0;
	var top = 0;
	if (this._centered) {
		if (_agent.isIE) {
			left = document.body.scrollLeft + ((document.body.clientWidth - this.getContainer().clientWidth) / 2);
			top = document.body.scrollTop + ((document.body.clientHeight - this.getContainer().clientHeight) / 2);
		} else {			
			left = window.pageXOffset + ((window.innerWidth - this.getContainer().clientWidth) / 2);			
			top = window.pageYOffset + ((window.innerHeight - this.getContainer().clientHeight) / 2);
		}
	} else {
		//position relative to event
	
		if (_agent.isIE) {
			//ensure we keep popup within current page width
			if (window.event.clientX + this.getContainer().clientWidth > document.body.scrollWidth)
				left = document.body.scrollLeft + (document.body.scrollWidth - this.getContainer().clientWidth);
			else
				left = window.event.clientX;

			//ensure we keep popup within current page height
			if (window.event.clientY + this.getContainer().clientHeight > document.body.scrollHeight)
				top = document.body.scrollTop + (document.body.scrollHeight - this.getContainer().clientHeight);
			else
				top = window.event.clientY;
		} else {
			//ensure we keep popup within current page width
			if (event.clientX + this.getContainer().clientWidth > document.body.scrollWidth)
				left = window.pageXOffset + (document.body.scrollWidth - this.getContainer().clientWidth);
			else
				left = event.clientX;

			//ensure we keep popup within current page height
			if (event.clientY + this.getContainer().clientHeight > document.body.scrollHeight)
				top = window.pageYOffset + (document.body.scrollHeight - this.getContainer().clientHeight);
			else
				top = event.clientY;
		}
	}
	
		//get the popUpDiv
		popUpDiv = document.getElementById('_trPopupDivId');
		if(popUpDiv == null){			
			popUpDiv = document.createElement('div');
			popUpDiv.id = '_trPopupId';
			document.body.appendChild(popUpDiv);
		}
		
	
	//alert('container='+ this.getContainer().id+'left='+left+', top='+top);	
	this.getContainer().style.left = left + "px";
	this.getContainer().style.top =  top + "px";
	
	if (this.isModal()) {
		_tppSetMaskSize();
		_tppMask.style.display = "block";
	}

	this.getContainer().style.visibility = "visible";
	
	
	_tppCurrent = this;
	popUpDiv.appendChild(this.getContainer());
		
}

//hide the popup
TrinidadPanelPopup.prototype.hide = function()
{
	_tppCurrent = false;
	if (_tppMask)
		_tppMask.style.display = "none";
	this.getContainer().style.visibility = "hidden";
	//move popup back to top left so it won't affect scroll size if window resized
	this.getContainer().style.left = "0px";
	this.getContainer().style.top = "0px";
}

//tests the event and hides the popup if appropriate
TrinidadPanelPopup.prototype.handleEvent = function(event)
{
	if (this.isModal())
		return;

	var currElement = false;
	if (_agent.isIE) {
		currElement = event.srcElement;
	} else {
		currElement = event.target;
	}
	//loop through element stack where event occurred
	while (currElement) {
		//if clicked on trigger or popup	
		if (currElement == this.getContainer() || 
			currElement == this.getTrigger()) {
			break;
		}
		currElement = currElement.parentNode;
	}
	if (!currElement) {
		//if click was on something other than the popupContainer
		this.hide();
	} else if (currElement == this.getContainer() && this._hideOnClick) {
		this.hide();
	}
}

TrinidadPanelPopup.prototype.getContainer = function()
{
	return document.getElementById(this._containerId);
}

TrinidadPanelPopup.prototype.getTrigger = function()
{
	return document.getElementById(this._triggerId);
}

TrinidadPanelPopup.prototype.isModal = function()
{
	return this._modal;
}

