/*
  JScript library    - obj_tPopup.js
  Author             - Brendon Matthews

History
  Oct 2006		BM				Started.
*/

// ====================== tPopup Class ====================== //

function findPopup(id,framedoc) {
	if (!framedoc) framedoc = document;
	// Locate the popup element
	var popup;
	if (framedoc.all) popup = eval('framedoc.all.'+id);
	else popup = framedoc.getElementById(id);
	// Check for the show and hide functions
	if (popup && popup.show && popup.hide) return popup;
	else return null;
}

function createPopup(id,framedoc) {
	if (!framedoc) framedoc = document;
	// Create popup element
	var popup = framedoc.createElement('DIV');
	popup.id = id;
	popup.width = 0;
	popup.height = 0;
	popup.style.position = 'absolute';
	popup.style.border = 'none';
	popup.style.zIndex = 5;
	popup.style.visibility = 'hidden';
	popup.style.overflowX = 'hidden';
	popup.style.overflowY = 'hidden';
	framedoc.body.appendChild(popup);
	// Create SHIM (MSIE only)
	var browserName = navigator.appName; 
	var browserVer = parseInt(navigator.appVersion); 
	if (browserName == "Microsoft Internet Explorer" && browserVer >= 4 && browserVer <= 6) {
		var html = '<iframe id="'+id+'SHIM" src="/bgmbsp/controls/blank.html" scrolling="no" frameborder="0" style="position:absolute; top:0px; left:0px; visibility:hidden; z-index:4;"></iframe>';
		var xdiv = framedoc.createElement('DIV');
		xdiv.style.visibility = 'hidden';
		xdiv.innerHTML += html;
		framedoc.body.appendChild(xdiv);
		popup.m_shim = framedoc.all[id+'SHIM'];
	}
	// Add custom fields
	popup.maxHeight = 0;
	popup.hideInterval = 0;
	// Add event methods
	popup.show = tPopup_show;
	popup.hide = tPopup_hide;
	popup.onmouseover = tPopup_mouseover;
	popup.onmouseout = tPopup_mouseout;
	// Return the popup element
	return popup;
}

function tPopup_setVisibility(vis,element1,element2) {
	if (element1 && element2) {
		if (element1.style.visibility == vis && element2.style.visibility == vis) return;
	}
	else if (element1) {
		if (element1.style.visibility == vis) return;
	}
	else if (element2) {
		if (element2.style.visibility == vis) return;
	}
	else {
		return;
	}
	try {
		if (element1) element1.style.filter = "blendTrans(duration=0.25,overlap=1.0)";
		if (element2) element2.style.filter = "blendTrans(duration=0.25,overlap=1.0)";
		// Make sure the filter is not playing.
		//if (element.filters.blendTrans.status == 2) element.filters.blendTrans.stop();
		if (element1) element1.filters.blendTrans.apply();
		if (element2) element2.filters.blendTrans.apply();
		if (element1) element1.style.visibility = vis;
		if (element2) element2.style.visibility = vis;
		if (element1) element1.filters.blendTrans.play();
		if (element2) element2.filters.blendTrans.play();
		//if (element.filters && element.filters[0]) element.filters[0].apply();
		//element.style.visibility = 'hidden';
		//if (element.filters && element.filters[0]) element.filters[0].play();
	}
	catch (ex) {
		if (element1) element1.style.visibility = vis;
		if (element2) element2.style.visibility = vis;
	}
}

function tPopup_checkHasChanged(popup) {
	if (popup.m_oldHTML != popup.innerHTML) {
		popup.m_oldHTML = popup.innerHTML;
		return true;
	}
	return false;
}

function tPopup_show(offsetX,offsetY) {
	// Update the popup
	var c = tPopup_checkHasChanged(this);
	if (this.width > 0) {
		this.style.width = this.width;
	}
	else if (c) {
		this.style.width = 10;
		var bw = this.offsetWidth-this.clientWidth;
		this.style.width = this.scrollWidth+bw;
	}
	if (this.height > 0) {
		this.style.height = this.height;
	}
	else if (c) {
		this.style.height = 10;
		var bh = this.offsetHeight-this.clientHeight;
		var h = this.scrollHeight+bh;
		if (this.maxHeight > 0 && h > this.maxHeight) {
			this.style.overflowY = 'auto';
			this.style.height = this.maxHeight;
		}
		else {
			this.style.overflowY = 'hidden';
			this.style.height = h;
		}
	}
	this.scrollTop = 0;
	this.scrollLeft = 0;
	// Display the popup
	var offsetY = parseInt(offsetY);
	var offsetX = parseInt(offsetX);
	if (!isNaN(offsetY)) this.style.top = offsetY;
	if (!isNaN(offsetX)) this.style.left = offsetX;
	if (this.m_shim) {
		this.m_shim.style.backgroundColor = this.style.backgroundColor;
		this.m_shim.style.width = this.offsetWidth;
		this.m_shim.style.height = this.offsetHeight;
		this.m_shim.style.top = this.style.top;
		this.m_shim.style.left = this.style.left;
	}
	tPopup_setVisibility('visible',this.m_shim,this);
}

function tPopup_hide() {
	if (this.m_interval) window.clearTimeout(this.m_interval);
	tPopup_setVisibility('hidden',this,this.m_shim);
}

function tPopup_mouseout() {
	var millis = parseInt(this.hideInterval);
	if (isNaN(millis) || millis <= 0) return;
	if (this.m_interval) window.clearTimeout(this.m_interval);
	eval('document.POPUP_'+this.id+' = this');
	this.m_interval = window.setTimeout('document.POPUP_'+this.id+'.hide()',millis);
}

function tPopup_mouseover() {
	if (this.m_interval) window.clearTimeout(this.m_interval);
}
