// ***** Copyright 2003 Frogtrade Limited. All rights reserved *****
//
// Purpose: 
//
// Notes: 
//

function popup_controller()
{
	this.popups=new Array();
	this.iframe_objects=new Array();
	this.orig_z_index=998;
	this.z_index=this.orig_z_index;

	this.add_popup=popup_controller_add_popup;
	this.show_popup=popup_controller_show_popup;
	this.position_popup=popup_controller_position_popup;
	this.get_empty_iframe_object=popup_controller_get_empty_iframe_object;
	this.hide_popup=popup_controller_hide_popup;
	this.hide_all_popups=popup_controller_hide_all_popups;
	this.spawn_popup_iframe=popup_controller_spawn_popup_iframe;
	this.hide_child_popup=popup_controller_hide_child_popup;
	this.reset=popup_controller_reset;
}

function popup_controller_add_popup(popup_object, overwrite)
{
//	if (!this.popups[popup_object.object_ref] || overwrite)
//	{

// the components have to be overwritten because of page refresh, or comonents have to be unregistered on unload
		this.popups[popup_object.object_ref]=popup_object;
		popup_object.set_visibility(false);
		popup_object.popup_controller=this;
		return true;
//	}
//	else
//	{
// error: trying to insert a popup that has already been registered
//		return false;
//	}
}

function popup_controller_reset()
{
	var i_obj = this.get_empty_iframe_object();
	var iframe_elem=document.getElementsByName(i_obj.iframe_ref)[0];
	iframe_elem.style.width='auto';
	  // ^ reset iframe width so can use measure elements again to assess width
}

function popup_controller_show_popup(popups_parent, popup_ref, x1, y1, x2, y2, display_how)
{
	this.popups_parent=popups_parent;
	this.hide_child_popup(popup_ref);

	if (this.popups[popup_ref])
	{
		var i_obj=this.get_empty_iframe_object();
		var iframe_win=eval(i_obj.iframe_ref);
		var iframe_elem=document.getElementsByName(i_obj.iframe_ref)[0];

		var style_str='';
		var style_doc=this.popups[popup_ref].creation_window.document;
		var styles=style_doc.getElementsByTagName('STYLE');
		for (i=0; i<styles.length; i++)
		{
			style_str+='<STYLE TYPE="text/css">'+styles[i].innerHTML+'</STYLE>';
		}

		iframe_win.document.write(
		"<html><head>" + style_str +
		// if the width of the border is changed make sure that this is taken into account in show_popup
		"</head><body style='border:none;margin:0; oncontextmenu='return false;' onselectstart='return false;'>" +
		this.popups[popup_ref].get_html() +
		"</body></html>");
		iframe_win.document.close();
		iframe_win.get_broker=get_broker;
		iframe_win.get_absolute_position=get_absolute_position;

		iframe_elem.style.display="block";
		iframe_elem.style.zIndex=this.z_index++;

		this.popups[popup_ref].set_html_window(iframe_win);
		this.popups[popup_ref].set_visibility(true);
		var dims=this.popups[popup_ref].get_dimensions();
		if (dims)
		{
			if (!dims.width && !dims.height)// mac bugfix to get size after it has been displayed
			{
				document.current_popup_elem=this.popups[popup_ref];
				document.current_popup_iframe=iframe_elem;
				window.setTimeout('ppc_size_iframe();', 50);
			}
			i_obj.popup_visible=popup_ref;
			iframe_elem.style.width=dims.width;
			iframe_elem.style.height=dims.height - 1;

			this.position_popup(iframe_elem, x1, y1, x2, y2, display_how);
		}
		else
		{
// ERROR: could not get dimentions of popup
		iframe_elem.style.display="none";
		this.popups[popup_ref].set_visibility(false);
		}
	}
}

function ppc_size_iframe()
{
	var dims=document.current_popup_elem.get_dimensions();
	var iframe_elem=document.current_popup_iframe;
	iframe_elem.style.width=dims.width + 4;
	iframe_elem.style.height=dims.height + 4;
}

function popup_controller_position_popup(popup, x1, y1, x2, y2, display_how)
{
	var w=popup.style.pixelWidth;
	var h=popup.style.pixelHeight;
	var x, y;
	var top_win = get_top_window(self);
	var cW=top_win.document.body.clientWidth;
	var cH=top_win.document.body.clientHeight;
	
	display_how=display_how.toUpperCase().split("_");

	var min_top=y1-h;
	var min_disp_up=y2-h;
	var max_disp_down=y1+h;
	var max_bottom=y2+h;
	var min_left=x1-w;
	var min_disp_left=x2-w;
	var max_disp_right=x1+w;
	var max_right=x2+w;

	if (display_how[0]=="T")
	{
		//y=(min_top<0? y2 : min_top);
		y = y2;
	}
	else if (display_how[0]=="B")
	{
		//y=(max_bottom>cH && min_top>0? min_top : y2);
		y = y2;
	}
	else if (display_how[0]=="L")
	{
		//x=(min_left<0? x2 : min_left);
		x = x2;
	}
	else if (display_how[0]=="R")
	{
		//x=(max_right>cW && min_left>0? min_left : x2);
		x = x2;
	}
	else
	{
// If display how is wrong display error and use default of bottom
		display_how[0]="B";
		//y=(max_bottom>cH && min_top>0? min_top : y2);
		y = y2;
	}

	if (display_how[0]=="T" || display_how[0]=="B")
	{
		if (display_how[1]=="R")
		{
			//x=(max_disp_right>cW && min_disp_left>0? min_disp_left : x1);
			x = x1;
		}
		else if (display_how[1]=="L")
		{
			//x=(min_disp_left<0? x1 : min_disp_left);
			x = x1;
		}
		else
		{
	// If display how is wrong display error and use default of right
			display_how[1]="R";
			//x=(max_disp_right>cW && min_disp_left>0? min_disp_left : x1);
			x = x1;
		}
	}
	else if (display_how[0]=="L" || display_how[0]=="R")
	{
		if (display_how[1]=="D")
		{
			//y=(max_disp_down>cH && min_disp_up>0? min_disp_up : y1);
			y = y1;
		}
		else if (display_how[1]=="U")
		{
			//y=(min_disp_up<0? y1 : min_disp_up);
			y = y1;
		}
		else
		{
	// If display how is wrong display error and use default of down
			display_how[1]="D";
			//y=(max_disp_down>cH && min_disp_up>0? min_disp_up : y1);
			y = y1;
		}	
	}

	container_x=0;
	container_y=0;
	if (window.parent!=window)
	{
		try
		{
			elem=window.parent.document.getElementsByName(window.name)[0];
			if ((!elem || elem.length) && window.frameElement)
			{	// IE 5.5 only
				elem=window.frameElement;
			}
		}
		catch(e)
		{
			var no_parent_access=1;
			elem=false;
		}

		if (elem && !no_parent_access)
		{
			co_ords=get_absolute_position(elem);
			container_x=co_ords.x;
			container_y=co_ords.y;
		}
	}

	// take into account the scroll of the window and the position of a containing frame
	// in the parent window if there is one
	popup.style.left=x + document.body.scrollLeft - container_x;

	if(navigator.userAgent.indexOf("Firefox")!=-1){
	popup.style.top=y + document.body.scrollTop - container_y - 4;
	}
	else{
	popup.style.top=y + document.body.scrollTop - container_y;
	}
}

function popup_controller_get_empty_iframe_object()
{
	for (var i=0; i<this.iframe_objects.length; i++)
	{
		if (!this.iframe_objects[i].popup_visible)
		{
			return this.iframe_objects[i];
		}
	}
	var i_obj=new Object();
	this.spawn_popup_iframe("popup_iframe_" + this.iframe_objects.length);
	i_obj.iframe_ref="popup_iframe_" + this.iframe_objects.length;
	i_obj.popup_visible=false;
	this.iframe_objects[this.iframe_objects.length]=i_obj;
	return i_obj;
}

function popup_controller_hide_popup(popup_ref)
{
	if (this.popups_parent)
	{
		this.popups_parent.hide_popup();
	}
}

function popup_controller_hide_child_popup(popup_ref)
{
	if (this.popups[popup_ref])
	{
		var popups_showing=false;
		this.popups[popup_ref].set_visibility(false);
		for (var i=0; i<this.iframe_objects.length; i++)
		{
			if (this.iframe_objects[i].popup_visible==popup_ref)
			{
				var iframe_elem=document.getElementsByName(this.iframe_objects[i].iframe_ref)[0];
				iframe_elem.style.display="none";
				this.iframe_objects[i].popup_visible=false;
			}
		popups_showing=(this.iframe_objects[i].popup_visible || popups_showing);
		}
		if (!popups_showing)
		{
			this.z_index=this.orig_z_index;
		}
	}
}

function popup_controller_hide_all_popups()
{
	var popup_ref, iframe_elem;

	for (var i=0; i<this.iframe_objects.length; i++)
	{
		if (popup_ref=this.iframe_objects[i].popup_visible)
		{
			var iframe_elem=document.getElementsByName(this.iframe_objects[i].iframe_ref)[0];
			iframe_elem.style.display="none";
			this.popups[popup_ref].set_visibility(false);
			this.iframe_objects[i].popup_visible=false;
		}
	}
	this.z_index=this.orig_z_index;
}

function popup_controller_spawn_popup_iframe(popup_iframe_ref)
{ 
	try{
		document.body.insertAdjacentHTML("BeforeEnd", "<iframe src='/sysicons/spacer.gif' name='" + popup_iframe_ref + "' " + "style='position:absolute; z-index:998; display:none; width:200px; height:200px' frameborder='no' scrolling='no' noresize='noresize'></iframe>");
	}
	catch(e)
	{ 
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id', 'appendediframe');
		document.body.appendChild(newDiv);
		document.getElementById("appendediframe").innerHTML="<iframe src='/sysicons/spacer.gif' name='" + popup_iframe_ref + "' " + "style='position:absolute; z-index:998; display:none; width:200px; height:200px' frameborder='no' scrolling='no' noresize='noresize'></iframe>";
	}
	
	var tmp_iframe=eval(popup_iframe_ref);

	tmp_iframe.document.write(
	"<html><head>" +
	// if the width of the border is changed make sure that this is taken into account in show_popup
	"</head><body style='border:2px outset grey; margin:0;' oncontextmenu='return false;' onselectstart='return false;'></body></html>");

	tmp_iframe.document.close();
	tmp_iframe.get_broker=get_broker;
	tmp_iframe.get_absolute_position=get_absolute_position;
}
