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

	var className = params.className? params.className: "bsweb_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 = "bsweb_window_modal"; //"popup" + Math.random();
	var contentid = id + "content";
	var titleid = id + "title";
	
	bsweb.PopupWindow.windows[id] = this;
	
	var div = bsweb.$(id);
	if(!div)
	{
		/*var images = bsweb.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">';
		if(params.iscloselink){
			html += '<a class="Close_Lnk" style="cursor: pointer;">CLOSE</a>';
		}
		html += '</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"><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 = bsweb.$(contentid);
	content.innerHTML = "<div style='padding: 20px;'>loading...</div>"
	if(!params.disablecontentsize){
		content.style.width = width + "px";
		content.style.height = height + "px";
	}
	
	this.content = content;
	
	bsweb.$(id + 'btnClose').onclick = function(){me.close()};

	bsweb.$(titleid).innerHTML = title;

	var callback = Function.createCallback(bsweb.MoveObject.mouseDown, id);
	$addHandler(bsweb.$(id + 'tblTitle'), "mousedown", callback);
	$addHandler(bsweb.$(id + 'divTopImage'), "mousedown", callback);
	
	bsweb.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;
}

bsweb.PopupWindow.windows = {};

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

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

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

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

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

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

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

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

bsweb.PopupWindow.options = {};

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

	var pageSize = bsweb.PopupWindow.getPageSize();
	
	var left = bsweb.PopupWindow.options.disableLeft? bsweb.PopupWindow.options.disableLeft: 0;
	var top = bsweb.PopupWindow.options.disableTop? bsweb.PopupWindow.options.disableTop: 0;
	var right = bsweb.PopupWindow.options.disableRight? pageSize.pageWidth - bsweb.PopupWindow.options.disableRight: pageSize.pageWidth;
	var bottom = bsweb.PopupWindow.options.disableBottom? pageSize.pageHeight - bsweb.PopupWindow.options.disableBottom: pageSize.pageHeight;
	right -= left;
	bottom -= top;
	
	var div = bsweb.$("bsweb_overlay_modal");
	if(!div)
	{
		div = document.createElement("div");
		div.id = "bsweb_overlay_modal";
		div.className = "bsweb_overlay_modal";
		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";
	
	bsweb.$("bsweb_overlay_modal").style.display = "";
	
	bsweb.PopupWindow.hideSelect();
	
	bsweb.PopupWindow.screenDisabled = true;
}

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

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

bsweb.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";
		}		
	}
}

bsweb.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;
			}
		}
	}
}

bsweb.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};
}

bsweb.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; };
	//bsweb.PopupWindow.isAlert = true;
	var showId		= param_default("showId",	null);
	var cancelId	= param_default("cancelId",	null);
	var hidden		= param_default("hidden",	false);

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

	Sys.Application.add_load(function(){
		if(!bsweb.$(id)) return;
	
	if(hidden)
		bsweb.$(id).style.display = "none";
		
	if(hideUntil && bsweb.$(id).style.display != "none")
	{
		bsweb.$(id).style.display = "none";
		bsweb.$(id).setAttribute("hideUntil", "1");
	}
	document.getElementsByTagName("form")[0].appendChild(bsweb.$(id));
	
											bsweb.PopupWindow.setBounds(id);
											var callback = Function.createCallback(bsweb.MoveObject.mouseDown, id);
											$addHandler(bsweb.$(id + "title"), "mousedown", callback);
											$addHandler(bsweb.$(id + "btnClose"), "click", function(){bsweb.PopupWindow.hide(id)});
											if(showId && bsweb.$(showId))
												$addHandler(bsweb.$(showId), "click", function(){bsweb.PopupWindow.visibility(id)});
											if(cancelId && bsweb.$(cancelId))
											{
												bsweb.$(cancelId).onclick = bsweb.$(id + "btnClose").onclick;
												$addHandler(bsweb.$(cancelId), "click", function(){bsweb.PopupWindow.hide(id)});
											}
											
											window.onscroll = function(){bsweb.PopupWindow.setBounds(id);};
											//alert("test");
										}
							);
}

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

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

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

bsweb.PopupWindow.setBounds = function(id)
{
	var el = bsweb.$(id);
	if(el.getAttribute("hideUntil") == "1")
	{
		bsweb.$(id).style.display = "";
		bsweb.$(id).removeAttribute("hideUntil");
	}
	var pageSize = bsweb.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)) + window.pageYOffset + "px";
	el.style.left = Math.round((pageSize.windowWidth - el.offsetWidth) / 2) + document.body.scrollLeft + "px";
	el.style.top = Math.round((pageSize.windowHeight - el.offsetHeight) / 2) + document.body.scrollTop + "px";
	//el.style.top = "300px";
	
	var tables = el.getElementsByTagName('table');
	if (tables.length) {
		var t = tables[0];
		t.style.height = "1px";
		t.style.height = el.style.height;
		t.style.width = el.style.width;
		t.tBodies[0].rows[1].cells[1].style.height = (el.offsetHeight - t.tBodies[0].rows[0].cells[1].offsetHeight - t.tBodies[0].rows[2].cells[1].offsetHeight) + 'px';
	}

}

//bsweb.PopupWindow.isAlert = false;
bsweb.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; };
	//bsweb.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 bsweb.PopupWindow('', {width: width, height: height, className: 'bsweb_window_modal', title: strTitle, onClose: function(){if(params && params.onClose) params.onClose()}});
	var contentid = wind.contentid;
	var html = '<table class="bsweb_alert_message"><tr><td>' + message + '</td></tr><tr><td align="center">';
	html += '<div style="text-align: center;"><div style="text-align: left; width: 80px; margin: auto;"><a class="bsweb_button" onclick="bsweb.PopupWindow.close(\'' + wind.id + '\'); bsweb.PopupWindow.isAlert = false;"><table class="bsweb_button_tbl" cellpadding="0" cellspacing="0" onclick="parentNode.onClick;"><tr>';
	html += '<td valign="top" nowrap="nowrap" class="bsweb_button_left"></td><td valign="top" nowrap="nowrap" class="bsweb_button_center">Close</td>';
	html += '<td valign="top" nowrap="nowrap" class="bsweb_button_right"></td></tr></table></a></div></div>';
	html += '</td></tr><table>';
	bsweb.$(contentid).innerHTML = html;
}

bsweb.PopupWindow.onLoad = function()
{

}
 
Sys.Application.add_load(bsweb.PopupWindow.onLoad);

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