function dialog_PopupDialog()
{
	this.left = 0;
	this.top = 0;
	this.width = 0;
	this.height = 0;
	this.title = null;

	this.hostObj = null;
	this.divObj = null;
	this.frameObj = null;
	this.titleObj = null;

	this.onclose = null;

    this.init = function()
    {
        var str = "<div id=\"dialog_dialogLayer\" style=\"position: absolute; z-index: 9999; width: 500px; height: 300px; display: none; border: 1px solid #83cec5; background: #f5f5f5\" align=\"center\">";
        str += "<div id=\"dialog_title\" class=\"sub_caption\" style=\"text-align: center\"></div>";
        str += "<iframe id=\"dialog_dialogFrame\" frameborder=\"0\"></iframe></div>";
        
        $(str).appendTo($(document.body));

	    this.divObj = document.getElementById("dialog_dialogLayer");
	    this.frameObj = document.getElementById("dialog_dialogFrame");
	    this.titleObj = document.getElementById("dialog_title");
    }
    
	this.show = function()
	{
	    if (this.title == "" || this.title == null)
	    {
	        this.titleObj.innerText = this.frameObj.document.title;
	    }
	    else
	    {
	        this.titleObj.innerText = this.title;
	    }
	    
		var e = dialog_popupDialog.hostObj;

		// set dialog size
		var o = this.divObj.style;
		o.width = this.width;
		o.height = this.height;
		
		this.frameObj.style.width = this.width - 10;
		this.frameObj.style.height = this.height - 25;

		// get e position and size
		var t = e.offsetTop,  h = e.clientHeight, l = e.offsetLeft, p = e.type;
		while (e = e.offsetParent){t += e.offsetTop; l += e.offsetLeft;}

		this.frameObj.document.body.focus();

		// calc dialog position
		var dw = document.body.clientWidth;
		var dl = document.body.scrollLeft;
		var dt = document.body.scrollTop;

		if (document.body.clientHeight + dt - t - h >= this.height)
		{
			o.top = t + h;
		}
		else
		{
			o.top  = (t - dt < this.height) ? (t + h) : (t - this.height);
		}

		if (dw + dl - l >= this.width)
		{
			o.left = l;
		}
		else
		{
			o.left = (dw >= this.width) ? (dw - this.width + dl) : dl;
		}

        $(document).bind("click", dialog_hide);
        $(this.divObj).fadeIn(500);
	}
	
	this.hide = function()
	{
	    $(document).unbind("click", dialog_hide);
        $(this.divObj).fadeOut(500);
	}
}

var dialog_popupDialog = new dialog_PopupDialog();

function dialog_returnValue()
{
	dialog_popupDialog.hide();

	if (arguments.length == 0)
	{
		return;
	}

    if (dialog_popupDialog.onclose != null)
    {
	    var cmd = "dialog_popupDialog.onclose(";
	    var len = cmd.length;
	    for (var i = 0; i < arguments.length; ++i)
	    {
		    cmd += "\"" + arguments[i] + "\", ";
	    }

	    cmd = cmd.substring(0, cmd.length - 2) + ");";
	    eval(cmd);
	}
}

function dialog_showDialog()
{
    var url = "about:blank";

	if (arguments.length > 0)
	{
	    url = arguments[0];
	}
	else
	{
		return;
	}

	dialog_popupDialog.onclose = (arguments.length > 1) ? arguments[1] : null;
	dialog_popupDialog.title = (arguments.length > 2) ? arguments[2] : null;
	dialog_popupDialog.width = (arguments.length > 3) ? arguments[3] : 500;
	dialog_popupDialog.height = (arguments.length > 4) ? arguments[4] : 300;
	dialog_popupDialog.hostObj = window.event.srcElement;

	if (url.indexOf('?') > -1)
    {
        url += "&width=" + (dialog_popupDialog.width - 10) + "&height=" + (dialog_popupDialog.height - 25) + "&tmp=" + new Date();
    }
    else
    {
        url += "?width=" + (dialog_popupDialog.width - 10) + "&height=" + (dialog_popupDialog.height - 25) + "&tmp=" + new Date();
    }
	dialog_popupDialog.frameObj.src = url;
	
	dialog_popupDialog.show();
}

function dialog_hide()
{
    if(dialog_popupDialog.hostObj != window.event.srcElement)
    {
        dialog_popupDialog.hide();
    }
}

$(document).ready(function(){
    dialog_popupDialog.init();
});

