
function lpu_open( id, options )
{
	var	url,title,x,y,width,height,resizable,draggable,scrollbars,modal,modalEffect,onClose,decorations;
	var	iframe,mask,content;

	// read options
	url=lpu_getOption(options,"url");
	title=lpu_getOption(options,"title","");
	x=lpu_getOption(options,"x","center");
	y=lpu_getOption(options,"y","center");
	width=lpu_toInt(lpu_getOption(options,"width",800));
	height=lpu_toInt(lpu_getOption(options,"height",490));
	resizable=lpu_getOption(options,"resizable",true);
	draggable=lpu_getOption(options,"draggable",true);
	scrollbars=lpu_getOption(options,"scrollbars",true);
	modal=lpu_getOption(options,"modal",false);
	modalEffect=lpu_getOption(options,"modalEffect",false);
	onClose=lpu_getOption(options,"onClose");
	decorations=lpu_getOption(options,"decorations",true);

	// close dialog with the same id if it already exists
	content=$("#"+id);
	lpu_closeDialog(content);

	// insert HTML fragment
	$("body").append(
		"<div id='"+id+"' class='lightPopUp' style='padding: 0px; margin: 0px;'>"+
		"<iframe id='"+id+"_frm' src='"+url+"' scrolling='"+(scrollbars ? "auto" : "no")+"' frameborder='0' marginwidth='0' marginheight='0' "+
		"style='"+(scrollbars ? "" : "overflow: hidden;")+" padding: 0px 0px 0px 0px; margin: "+(decorations ? "0px 6px 0px 6px" : "0px 0px 0px 0px")+";'"+
		"></iframe>"+
		"<div id='"+id+"_msk' style='position: absolute; z-index: 1000;'></div>"+
		"</div>"
		);

	// set iframe size
	iframe=$("#"+id+"_frm");
	iframe.width(decorations ? (width-12) : width-4);
	iframe.height(decorations ? (height-38) : height-4);
	if (!decorations) {
		iframe.css("border","solid 2px black");
		}

	// hide mask and set opacity
	mask=$("#"+id+"_msk");
	mask.hide();
	if ($.browser.mozilla) {
		mask.css("background-color","#000000");
		mask.css("-moz-opacity",".15");
		}
	else if ($.browser.msie) {
		mask.css("background-color","#000000");
		mask.css("filter","alpha(opacity=15)");
		}

	var	overlayStyle;

	if (modal && modalEffect) {
		if ($.browser.mozilla) {
			overlayStyle={ "background-color": "#000000", "-moz-opacity": ".40" };
			}
		else if ($.browser.msie) {
			overlayStyle={ "background-color": "#000000", "filter": "alpha(opacity=40)" };
			}
		else {
			overlayStyle={};
			}
		}
	else {
		overlayStyle={};
		}

	// create the dialog
	content=$("#"+id);
	content.dialog({
		title: title,
		width: width,
		height: height,
		position: [ x, y ],
		resizable: resizable,
		draggable: draggable,
		modal: modal,
		overlay: overlayStyle,
		dragStart: function() {
			lpu_setMaskSize(mask,iframe);
			mask.show();
			},
		dragStop: function() {
			mask.hide();
			},
		resizeStart: function() {
			lpu_setMaskSize(mask,iframe);
			mask.show();
			},
		resize: function() {
			lpu_setFrameSize(iframe,content);
			lpu_setMaskSize(mask,iframe);
			},
		resizeStop: function() {
			mask.hide();
			},
		close: function() {
			if (onClose!=undefined) {
				onClose();
				}

			lpu_closeDialog(content);
			}
		});

	// set an id to the dialog div in order to be able to reference it later...
	content=content.parent().parent();
	content.attr("id",id+"_dialog");

	// hack to reenable handle display...
	if (!resizable) {
		$("#"+id+"_dialog .ui-resizable-handle").each(function() {
			$(this).show();
			$(this).css("cursor","default");
			});
		}

	if (!draggable) {
		$("#"+id+"_dialog .ui-dialog-titlebar").each(function() {
			$(this).css("cursor","default");
			});
		}

	if (!decorations) {
		$("#"+id+"_dialog .ui-dialog-titlebar").each(function() {
			$(this).css("display","none");
			});
		$("#"+id+"_dialog .ui-resizable-handle").each(function() {
			$(this).css("display","none");
			});
		}

	iframe.focus();

	return content;
}

function lpu_getOption( options, name, defValue )
{
	return (options[name]==undefined ? defValue : options[name]);
}

function lpu_getContainer( id )
{
	return $("#"+id).parent().parent();
}

function lpu_setFrameSize( iframe, content )
{
	iframe.width(content.width()-12);
	iframe.height(content.height()-8);
}

function lpu_setMaskSize( mask, iframe )
{
	var	container;

	container=iframe.parent().parent().parent();

	mask.css("top",(iframe.offset().top-container.offset().top)+"px");
	mask.css("left",(iframe.offset().left-container.offset().left)+"px");
	mask.width(iframe.width());
	mask.height(iframe.height());
}

function lpu_close( id )
{
	lpu_closeDialog($("#"+id));
}

function lpu_closeDialog( content )
{
	if (typeof(content)=="string") {
		content=$('#'+content);
		}

	if (content.length>0) {
		content.dialog("destroy").remove();
		}
}

function lpu_changeDialogTitle( content, newTitle )
{
	if (typeof(content)=="string") {
		content=$('#'+content);
		}
	if (content.length>0) {
		content.data('title.dialog',newTitle); 
		}
}

function lpu_toInt( o )
{
	if (typeof(o)=="number") {
		return o;
		}
	return (typeof(o)=="string" ? parseInt(o) : NaN);
}


////////////////////////////////////////////////////////////////////////////////////////////////

function sl_slide( elem, x1, y1, x2, y2 )
{
	elem.slideX=x1;
	elem.slideY=y1;
	elem.slideDX=(x2-x1)/50;
	elem.slideDY=(y2-y1)/50;
	elem.slideCounter=50;

	sl_start(elem);
}

function sl_start( elem )
{
	elem.css("left",elem.slideX+"px");
	elem.css("top",elem.slideY+"px");
	elem.show();

	if (elem.slideIntervalID) {
		sl_stop(elem);
		}

	elem.slideIntervalID=setInterval(function() {
		sl_move(elem);
		},10);
}

function sl_move( elem )
{
	elem.slideX+=elem.slideDX;
	elem.slideY+=elem.slideDY;

	elem.css("left",elem.slideX+"px");
	elem.css("top",elem.slideY+"px");

	elem.slideCounter--;
	if (elem.slideCounter==0) {
		sl_stop(elem);
		}
}

function sl_stop( elem )
{
	if (elem.slideIntervalID) {
		clearInterval(elem.slideIntervalID);
		elem.slideIntervalID=null;
		}
}


