// DHTML popup experiment.
// should fall back on normal popup


function PopUp() {}

PopUp.data = null;
PopUp._callback = null;

// elements
PopUp._container = null;
PopUp._title = null;
PopUp._d = null;
PopUp._x = null;
PopUp._iframe = null;

PopUp._src = null;

PopUp._deattachable = true;

PopUp.isLoaded = false;
PopUp.isShown = false;

PopUp.displayElement = false;

PopUp.init = function()
{
	if(arguments[0])
	{
	 	PopUp.displayElement = arguments[0];
	}
	 
	if(!PopUp.isLoaded)
	{
		document.write('<div id="cc_popup_c" style="position: absolute; display:none; z-index: 1200;">');
		document.write('<div class="popup-con">');
		
		document.write('<table cellpadding="0" cellspacing="0" width="100%" border="0">');
		document.write('<tr>');
		document.write('<td width="100%" id="cc_popup_t" style="padding-top: 3px;" class="popup-outset"></td>');
		document.write('<td valign="bottom" id="cc_popup_d" class="popup-outset"><img src="http://i.boomtown.net/gfx/16x16/attach.gif" height="16" width="16"></td>');
		document.write('<td valign="bottom" id="cc_popup_x" class="popup-outset"><img src="http://i.boomtown.net/gfx/16x16/close.gif" height="16" width="16"></td>');
		document.write('</tr>');
		document.write('<tr>');
		document.write('<td colspan="3" style="padding: 2px;" class="popup-outset">');
		document.write('<iframe class="popup-iframe" id="cc_popup_i" style="width: 100%;" frameborder="0"></iframe>');
		document.write('</td>');
		document.write('</table>');
		
		document.write('</div>');
		document.write('</div>');
		
		PopUp._container = document.getElementById("cc_popup_c");

		PopUp._title = document.getElementById("cc_popup_t");
		PopUp._d = document.getElementById("cc_popup_d");
		PopUp._x = document.getElementById("cc_popup_x");
		PopUp._iframe = document.getElementById("cc_popup_i");
		
		PopUp._x.onclick = function()
		{
			PopUp.hide();
		}
		
		PopUp._d.onclick = function()
		{
			if(PopUp._deattachable)
			{
				var posx = window.screenLeft + PopUp._container.offsetLeft;
				var posy = window.screenTop +  PopUp._container.offsetTop;
				
				var win = window.open(PopUp._src,"","height=" + (PopUp._iframe.offsetHeight+8) + ",width=" + (PopUp._container.offsetWidth-8) + "status=no,toolbar=no,menubar=no,location=no,scrollbars=yes" + ",left=" + posx + ",top=" + posy);
				PopUp.hide();
			}
		}
		PopUp.isLoaded = true;
	}
}

PopUp.callback = function()
{
	if(PopUp._callback)
	{
		eval(PopUp._callback + "()");
	}
}

PopUp.html = function()
{
	
	
}

PopUp.add = function(elmid,titletext,src,callback,deattach)
{
	
	pwidth = (arguments[5] > 0 ? arguments[5] : 100);
	pheight = (arguments[6] > 0 ? arguments[6] : 100);
	
	var elm = document.getElementById(elmid);
	if(elm)
	{
		PopUp._assignClick(elm, titletext, src, callback, deattach, pwidth, pheight);
	}
}

PopUp._assignClick = function(elm, titletext, src, callback, deattach, pwidth, pheight)
{
	elm._src = src;
	elm._titletext = titletext;
	elm._pwidth = pwidth;
	elm._pheight = pheight;
	elm._callback = callback;
	elm._deattach = deattach;
	
	_click = function()
	{
		PopUp.show(elm, elm._titletext, elm._src, elm._callback, elm._deattach, elm._pwidth, elm._pheight);
	}
	Dom.addEvent(elm,"click","_click");
}

// PopUp.show(elm, src, width, height)
PopUp.show = function(elm, titletext, src, callback, deattach, pwidth, pheight)
{
	if(PopUp.isLoaded)
	{
		var elmLeft = (elm.offsetLeft-pwidth)+elm.offsetWidth;
		var elmTop = elm.offsetTop+elm.offsetHeight;
			
		
		PopUp._container.style.width = pwidth;
		PopUp._container.style.height = pheight;
	
		if(PopUp.displayElement)
		{
			var disElm = document.getElementById(PopUp.displayElement);
			var disLeft = Dom.getOffsetX(disElm);			
			
			
			if((elmLeft + pwidth) > (disLeft + disElm.offsetWidth))
			{
				elmLeft -= (pwidth - elm.offsetWidth - 2);
				elmTop = elmTop + elm.offsetHeight + 4;
			}			
			
		}
		else
		{
			// default display mode must be here
			elmTop = elmTop + elm.offsetHeight -4;
			
		}	
		
		PopUp._container.style.left = elmLeft;
		PopUp._container.style.top = elmTop;
		
		
		PopUp._container.style.display = "block";
		
		PopUp._title.innerHTML = titletext;
		PopUp._iframe.src = "";
		PopUp._iframe.style.height = pheight - PopUp._title.offsetHeight;
		PopUp._iframe.src = src;
		
		if(!deattach)
		{
			PopUp._d.innerHTML = "&nbsp;";
			PopUp._deattachable = false;
		}
		
		PopUp._callback = callback;
				
		PopUp.isShown = true;
	}
}
	PopUp.hide = function hide()
	{
		if(PopUp.isShown)
		{
			PopUp._container.style.display = "none";
		}
	}
	