

/* PopUp1
location can be an x,y,rel coordinate array
where x and y are either a pixel coordinate or "center"
where rel is either "absolute" "relative" or "mouse"
examples:
pop_up(event,"my_popup",[200,300])
pop_up(event,"my_popup",["center",300])
pop_up(event,"my_popup",[-400,-200,"mouse"])
pop_up(event,"my_popup",[20,20,"negoffset_mouse"]) --> positions the box's lower right hand corner 20px up and left from the mouse
*/

function pop_up(e,pop_up_id,location,opt_bg,opt_anim,opt_closehandler,other_properties)
	{
	if (!e) var e = window.event;
	var props = typeof(other_properties) == "object" ? other_properties : {};
	if (props.parent && $.browser.msie && $.browser.version < 7) return;
	var top = props.parent ? $(parent.document) : $(document);
	var p = top.find("#"+pop_up_id);
	if (!p.length && props.parent)
		{
		p = $('<div></div>').attr('id',pop_up_id);
		top.find('body').append(p);
		}
	if (props.parent) top.find(props.iframe_selector).css('z-index','1000');
	if (opt_closehandler) p[0].pop_up_1_closehandler = opt_closehandler;
	p.css({"visibility":"hidden","display":"block"}); // have to display the element to grab its width + height
	if (location)
		{
		var x = 0;
		var y = 0;
		var rel;
		switch(location[2])
			{
			case "absolute" : rel = "absolute";break;
			case "relative" : rel = "relative";break;
			case "mouse" : rel = "absolute";break;
			case "offset_mouse" : rel = "absolute";break;
			default : rel = "absolute";
			}
		p.css("position",rel);
		var ctr = getCenterOfScreen();
		var whe = getFullWidthHeightOf(p);
		if (!isNaN(location[0])) x = location[0];
		else if (location[0] == "center" || location[0] == "middle") x = ctr.x-whe.width/2;
		if (!isNaN(location[1])) y = location[1];
		else if (location[1] == "center" || location[1] == "middle") y = ctr.y-whe.height/2;
		if (location[2] == "mouse")
			{
			var mxy = getMouseXY(e);
			if (!isNaN(location[0])) x += mxy.width;
			if (!isNaN(location[1])) y += mxy.height;
			}
		else if (location[2] == "negoffset_mouse")
			{
			var mxy = getMouseXY(e);
			if (!isNaN(location[0])) x = mxy.x - whe.width - x;
			if (!isNaN(location[1])) y = mxy.y - whe.height - y;
			}
		x = Math.round(x);
		y = Math.round(y);
		if (y < 0) y = 0;
		var scroll_height = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
		if (location[1] == "center" && y < scroll_height) y = scroll_height;
		p.css({"left":x+"px","top":y+"px"});
		}
	if (typeof(opt_bg) == "object")
		{
		var color = opt_bg[0] || "#000000";
		var alpha = (opt_bg[1] || opt_bg[1] === 0 || opt_bg[1] === "0") ? opt_bg[1]*1 : 60;
		var bg = $("<div></div>");
		bg.css({"position":"absolute","right":"0px","top":"0px"});
		var b_wh = getWidthHeightOfDocument(top);
		bg.css({"width":b_wh.width+"px","height":b_wh.height+"px","background-color":color,"z-index":"999"});
		bg.css({"filter":"alpha(opacity="+alpha+")","-moz-opacity":alpha/100,"-khtml-opacity":alpha/100,"opacity":alpha/100});
		p.css("z-index","1000");
		top.find('body').prepend(bg);
		p[0].bgref = bg;
		}
	var closers = p.find(".close_overlay");
	closers.bind("click",function()
		{
		hide_pop_up(this.poverlay.attr("id"));
		return false;
		});
	for (var i=0;i<closers.length;i++) closers[i].poverlay = p;
	if (typeof(opt_anim) == 'number')
		{
		p.css({"display":"none","visibility":"visible"}).fadeIn(opt_anim);
		}
	else if (opt_anim == "fade_in")
		{
		p.css({"display":"none","visibility":"visible"}).fadeIn(500);
		}
	else if (opt_anim == "slide_down")
		{
		var target_y = (p.css("top").replace(/[^0-9]/g,"") || "0")*1;
		var fh = fullHeight(p,true);
		p.css({"top":(-1*fh)+"px","visibility":"visible","opacity":"0"}).animate({opacity:1,top:target_y+"px"},500);
		}
	else
		{
		p.css("visibility","visible");
		}
	return false;
	}

function hide_pop_up(pop_up_id,other_properties)
	{
	var props = typeof(other_properties) == "object" ? other_properties : {};
	var p = props.parent ? $(parent.document).find("#"+pop_up_id) : $("#"+pop_up_id);
	p.css("display","none");
	if (p[0].bgref) p[0].bgref.remove();
	if (p[0].pop_up_1_closehandler) p[0].pop_up_1_closehandler();
	return false;
	}

function getFullWidthHeightOf(elm)
	{
	var h = fullHeight(elm,true);
	var w = fullWidth(elm,true);
	return {width:w,height:h};
	}

function fullHeight(of,exclude_margin)
	{
	var add_these_up = ["padding-top","padding-bottom","border-top-width","border-bottom-width"];
	if (!exclude_margin) add_these_up = add_these_up.concat(["margin-top","margin-bottom"]);
	var nnums = new RegExp("[^0-9]","g");
	var h = 0;
	for (var i=0;i<add_these_up.length;i++) h += (of.css(add_these_up[i]) || "0").replace(nnums,"")*1;
	return h+of.height();
	}

function fullWidth(of,exclude_margin)
	{
	var add_these_up = ["padding-left","padding-right","border-left-width","border-right-width"];
	if (!exclude_margin) add_these_up = add_these_up.concat(["margin-left","margin-right"]);
	var nnums = new RegExp("[^0-9]","g");
	var w = 0;
	for (var i=0;i<add_these_up.length;i++) w += (of.css(add_these_up[i]) || "0").replace(nnums,"")*1;
	return w+of.width();
	}

function getWidthHeightOfWindow()
	{
	var w = $(window);
	return {width:w.width(),height:w.height()};
	}

function getWidthHeightOfDocument(force_document)
	{
	var d = force_document ? force_document : $(document);
	return {width:d.width(),height:d.height()};
	}

function getCenterOfScreen()
	{
	var scrollHeight = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	var w_wh = getWidthHeightOfWindow();
	var x = w_wh.width/2;
	var y = w_wh.height/2+scrollHeight;
	return {x:x,y:y};
	}

function getMouseXY(e)
	{
	var x = 0;
	var y = 0;
	if (e.pageX && e.pageY)
		{
		x = e.pageX;
		y = e.pageY;
		}
	else if (e.clientX && e.clientY)
		{
		x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	return {x:x,y:y};
	}