function PopupWindow(url, params)
{
	var me = this;
	
	var pageSize = PopupWindow.getPageSize();

	var className = params.className? params.className: "window_modal";
	var width = params.width? params.width: Math.round(pageSize.windowWidth / 3);
	var height = params.height? params.height: Math.round(pageSize.windowHeight / 2);
	var top = params.top? params.top: Math.round((pageSize.windowHeight - height) / 2);
	top += pageSize.yScroll - 30;
	var left = params.left? params.left: Math.round((pageSize.windowWidth - width) / 2);
	var title = params.title? params.title: "&nbsp;";

	var id = "window_modal"; //"popup" + Math.random();
	var contentid = id + "content";
	var titleid = id + "title";
	
	PopupWindow.windows[id] = this;
	
	var div = $(id);
	if(!div)
	{
		/*var images = PopupWindow.config.images[className];
		if(images)
		{
			for(var i = 0; i < images.length; i++)
			{
				var src = images[i];
				document.createElement("img").src = src;
			}
		}*/
	
		var div = document.createElement("div");
		div.id = id;
		div.className = className;
		div.style.zIndex = 40;
		var html = '';
		
/*		var table = document.createElement("table");
		table.className = "table_window";
		table.cellPadding = 0;
		table.cellSpacing = 0;
		var row = table.insertRow(-1)
		var cell = row.insertCell(-1);
		cell.className = "topleft";
		var cell = row.insertCell(-1);
		cell.className = "top";
		var divTitle = document.createElement("div");
		divTitle.id = titleid;
		divTitle.className = "window_title";
		cell.appendChild(divTitle);
		var cell = row.insertCell(-1);
		cell.className = "topright";

		div.appendChild(table);*/

		html += '<div id="' + id + 'btnClose' + '" class="window_close"></div>';
		
		html += '<div id="' + id + 'divTopImage' + '" class="top_image"></div>';
		html += '<table id="' + id + 'tblTitle' + '" class="table_window" cellpadding="0" cellspacing="0">';
		html += '<tr><td class="topleft"></td><td class="top"><div id="' + titleid + '" class="window_title">&nbsp;</div></td><td class="topright"></td></tr>';
		html += '</table>';
		html += '<table class="table_window" cellpadding="0" cellspacing="0">';
		/*html += '<tr><td class="left"></td><td class="center" id="' + contentid + '"></td><td class="right"></td></tr>';*/
		html += '<tr><td class="left"></td><td class="center"><div id="' + contentid + '" class="window_content">&nbsp;</div></td><td class="right"></td></tr>';
		html += '</table>';
		html += '<table class="table_window" cellpadding="0" cellspacing="0">';
		html += '<tr><td class="bottomleft"></td><td class="bottom"><div id="" class="window_statusbar">&nbsp;</div></td><td class="bottomright"></td></tr>';
		html += '</table>';
		
		div.innerHTML = html;
		
		/*var tds = div.getElementsByTagName("td");
		for(var i = 0; i < tds.length; i++)
		{
			var td = tds[i];
			alert(getStyle(td, "background-image"));
			if(td.style.backgroundImage != "")
				document.createElement("img").src = td.style.backgroundImage;
		}*/
		
		document.getElementsByTagName("body")[0].appendChild(div);
	}

	div.style.left = left + "px";
	div.style.top = top + "px";
	div.style.width = (width + 14) + "px";
	div.style.height = (height + 30) + "px";
	
	var content = $(contentid);
	content.innerHTML = "<div style='padding: 20px;'>loading...</div>"
	content.style.width = width + "px";
	content.style.height = height + "px";
	
	this.content = content;
	
	$(id + 'btnClose').onclick = function(){me.close()};

	$(titleid).innerHTML = title;

	var callback = Function.createCallback(MoveObject.mouseDown, id);
	$addHandler($(id + 'tblTitle'), "mousedown", callback);
	$addHandler($(id + 'divTopImage'), "mousedown", callback);
	
	PopupWindow.disableScreen();
	div.style.display = "";

	this.id = id;
	this.contentid = contentid;

	this.bindData = params.bindData;
	this.okId = params.okId;
	this.cancelId = params.cancelId;
	this.onClose = params.onClose;

	if(url) this.setAjaxContent(url, {onComplete: function(s){me.fillWindow(s)}});
	
	return this;
}

PopupWindow.windows = {};

PopupWindow.prototype.close = function()
{
	$(this.id).style.display = "none";
	PopupWindow.enableScreen();
	if(this.onClose)
		this.onClose()
}

PopupWindow.close = function(id)
{
	var wnd = PopupWindow.windows[id];
	wnd.close();
}

PopupWindow.prototype.setAjaxContent = function(url, params)
{
	var onComplete = params.onComplete;
	
    new Ajax.Request(url, {onSuccess: onComplete, method: 'get'});
}

PopupWindow.prototype.fillWindow = function(sender)
{
	var me = this;

	var contentid = this.contentid;
	
	var reply = sender.get_responseData();

    $(contentid).innerHTML = reply;
	
	executeScript(contentid);

	if(this.okId)
	{
		if(typeof $(this.okId).onclick == "function")
		{
			var tf = function()
			{
				var onclickF = $(me.okId).onclick;
				var tempF = function()
								{
									if(onclickF())
										me.close();
								}
				$(me.okId).onclick = tempF;
			}();
		}
		else
		{
			$(this.okId).onclick = function(){me.close()};
		}
		
	}

	if(this.cancelId)
	{
		if(typeof $(this.cancelId).onclick == "function")
		{
			var tf = function()
			{
				var onclickF = $(me.cancelId).onclick;
				var tempF = function()
								{
										onclickF()
										me.close();
								}
				$(me.cancelId).onclick = tempF;
			}();
		}
		else
		{
			$(this.cancelId).onclick = function(){me.close()};
		}
	}
	
	if(this.bindData) this.bindData(sender, this);
}

PopupWindow.options = {};

PopupWindow.disableScreen = function()
{
	if(PopupWindow.screenDisabled)
		return;

	var pageSize = PopupWindow.getPageSize();
	
	var left = PopupWindow.options.disableLeft? PopupWindow.options.disableLeft: 0;
	var top = PopupWindow.options.disableTop? PopupWindow.options.disableTop: 0;
	var right = PopupWindow.options.disableRight? pageSize.pageWidth - PopupWindow.options.disableRight: pageSize.pageWidth;
	var bottom = PopupWindow.options.disableBottom? pageSize.pageHeight - PopupWindow.options.disableBottom: pageSize.pageHeight;
	right -= left;
	bottom -= top;
		
	var div = $("overlay_modal");
	if(!div)
	{
		div = document.createElement("div");
		div.id = "overlay_modal";
		div.className = "overlay_realtymind";
		div.style.position = "absolute";
		div.style.zIndex = 39;
		document.getElementsByTagName("body")[0].appendChild(div);
	}

	div.style.left = left + "px";
	div.style.top = top + "px";
	div.style.height = bottom + "px";
	div.style.width = right + "px";
	
	$("overlay_modal").style.display = "";
	
	PopupWindow.hideSelect();
	
	PopupWindow.screenDisabled = true;
}

PopupWindow.enableScreen = function()
{
	if(!PopupWindow.screenDisabled)
		return;

	if($("overlay_modal"))
		$("overlay_modal").style.display = "none";
	PopupWindow.showSelect();
	
	PopupWindow.screenDisabled = false;
}

PopupWindow.hideSelect = function()
{
	if(isIE)
	{
		var els = document.getElementsByTagName("select");
		for(var i = 0; i < els.length; i++)
		{
			var el = els[i];
			el.oldVisibility = el.style.visibility;
			el.style.visibility = "hidden";
		}
	}
}

PopupWindow.showSelect = function()
{
	if(isIE)
	{
		var els = document.getElementsByTagName("select");
		for(var i = 0; i < els.length; i++)
		{
			var el = els[i];
			if(el.oldVisibility != null)
			{
				el.style.visibility = el.oldVisibility;
				el.oldVisibility = null;
			}
		}
	}
}

PopupWindow.getPageSize = function()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	var pageHeight, pageWidth;

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	yScroll = document.body.scrollTop || document.documentElement.scrollTop;
	
	return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight, xScroll: xScroll, yScroll: yScroll};
}

PopupWindow.startupInit = function(id, params, hideUntil)
{
	params = params? params: {};
	function param_default(pname, def) { if (params && typeof params[pname] != "undefined") return params[pname]; else return def; };
	//PopupWindow.isAlert = true;
	var showId		= param_default("showId",	null);
	var cancelId	= param_default("cancelId",	null);
	var hidden		= param_default("hidden",	false);

	if(!$(id)) return;
	
	if(hidden)
		$(id).style.display = "none";
		
	if(hideUntil && $(id).style.display != "none")
	{
		$(id).style.display = "none";
		$(id).setAttribute("hideUntil", "1");
	}
	document.getElementsByTagName("form")[0].appendChild($(id));
	/*while(!$(id).childNodes[0].getAttribute || $(id).childNodes[0].getAttribute("popupcontentdiv") != "1")
	{
		$(id + "content").appendChild($(id).childNodes[0]);
	}*/
	/*for(var i = 0; i < $(id).childNodes.length; i++)
	{
		if($(id).childNodes[i].getAttribute && $(id).childNodes[i].getAttribute("popupcontentdiv") == "1")
			alert(0);
	}
	
	var content = $(id).getElementsByTagName('div')[0];
	$(id + "content").appendChild(content);
	content.style.display = "";*/

	Sys.Application.add_load(function(){
											PopupWindow.setBounds(id);
											var callback = Function.createCallback(MoveObject.mouseDown, id);
											$addHandler($(id + "title"), "mousedown", callback);
											$addHandler($(id + "btnClose"), "click", function(){PopupWindow.hide(id)});
											if(showId && $(showId))
												$addHandler($(showId), "click", function(){PopupWindow.visibility(id)});
											if(cancelId && $(cancelId))
											{
												$(cancelId).onclick = $(id + "btnClose").onclick;
												$addHandler($(cancelId), "click", function(){PopupWindow.hide(id)});
											}
										}
							);
}

PopupWindow.show = function(id)
{
	$(id).style.display = "";
	PopupWindow.setBounds(id);
}

PopupWindow.hide = function(id)
{
	$(id).style.display = "none";
}

PopupWindow.visibility = function(id)
{
	if($(id).style.display != "none")
		PopupWindow.hide(id);
	else
		PopupWindow.show(id);
}

PopupWindow.setBounds = function(id)
{
	var el = $(id);
	if(el.getAttribute("hideUntil") == "1")
	{
		$(id).style.display = "";
		$(id).removeAttribute("hideUntil");
	}
	var pageSize = PopupWindow.getPageSize();
	if(!el.style.width) el.style.width = (Math.round(pageSize.windowWidth / 2)) + "px";
	if(!el.style.height) el.style.height = (Math.round(pageSize.windowHeight / 2)) + "px";
	el.style.left = Math.round((pageSize.windowWidth - el.offsetWidth) / 2) + "px";
	el.style.top = Math.round((pageSize.windowHeight - el.offsetHeight) / 2) + "px";
}

//PopupWindow.isAlert = false;
PopupWindow.alert = function(message, params)
{
	message = message.replace(/\n/g, "</br>")
	params = params? params: {};
	function param_default(pname, def) { if (params && typeof params[pname] != "undefined") return params[pname]; else return def; };
	//PopupWindow.isAlert = true;
	var width = param_default("width",     300);
	var height = param_default("height",   100);
	var strTitle = 'Message';
	if (params.title) strTitle = params.title;
	var wind = new PopupWindow('', {width: width, height: height, className: 'window_modal', title: strTitle, onClose: function(){if(params && params.onClose) params.onClose()}});
	var contentid = wind.contentid;
	var html = '<table class="alert_message"><tr><td width="100%" height="100%">' + message + '</td></tr><tr><td align="center">';
	html += '<div style="text-align: center;"><div style="text-align: left; width: 80px; margin: auto;"><a class="button" onclick="PopupWindow.close(\'' + wind.id + '\'); PopupWindow.isAlert = false;"><table class="button_tbl" cellpadding="0" cellspacing="0" onclick="parentNode.onClick;"><tr>';
	html += '<td valign="top" nowrap="nowrap" class="button_left"></td><td valign="top" nowrap="nowrap" class="button_center">Close</td>';
	html += '<td valign="top" nowrap="nowrap" class="button_right"></td></tr></table></a></div></div>';
	html += '</td></tr><table>';
	$(contentid).innerHTML = html;
}

PopupWindow.onLoad = function()
{
}
 
Sys.Application.add_load(PopupWindow.onLoad);

/*PopupWindow.config = {};
PopupWindow.config.images = {};
PopupWindow.config.images["window_modal"] = ["/media/popup/realtymind/left-top.gif", "/media/popup/realtymind/top-middle.gif", "/media/popup/realtymind/right-top.gif"];
*/