
var dialogUrl = function(url) {
//	alert(url)
	new DialogView(url)
}

if(Base === undefined) {
	cmd = '<script type="text/javascript" src="scripts/Base.js"></script>';
	document.writeln(cmd);
}

var DialogView = Base.extend({
	url: "",
	callback : null,
	domNode: null,
	width: 700,
	constructor: function(sUrl, fCallBack, params, postParams) {
		this.url = sUrl;
		this.callback = fCallBack;
		this.params = params;
		nScrollTop = parseInt(document.body.scrollTop,10);
		vertical = document.body.clientWidth || window.innerWidth;
		dialogLeft = vertical/2 - this.width/2;
		this.domNode = Builder.node("div", {id: "_dialogCurtain"});
		// get viewable width and height
		height = Element.getHeight(document.body);
		
		Element.setStyle(this.domNode, 
			{ 	width: "100%", height: height + "px", 
				position: "absolute", top: nScrollTop + "px", 
				left: "0px", backgroundColor: "#acacac",
				margin: "0 auto", padding: "0", zIndex: "99", 
				filter: "alpha(opacity=50)", opacity:".5",
				display: "" });
		loader = Builder.node("div", {id: "_dialogLoader" });
		Element.setStyle(loader,
			{	zIndex: "102", backgroundColor: "#fff", top: (nScrollTop+50)+"px",
				position: "absolute", width: this.width + "px", 
				left: dialogLeft + "px", color: "#ff0000",
				display: ""
			});
		loader.innerHTML = "Please wait, loading Screen...";
		this.dialogContainer = Builder.node("div", { id: "_dialogContainer" });
		Element.setStyle(this.dialogContainer,
			{	zIndex: "100", backgroundColor: "#fcfcfc", top: (nScrollTop+50)+"px",
				position: "absolute", width: this.width + "px", color: "#ff0000",
				left: dialogLeft + "px",
				display: "none"
			});
		this.domNode.oDialog = this;
		this.dialogContainer.oDialog = this;


		document.body.appendChild(this.domNode);
		document.body.appendChild(this.dialogContainer);
		document.body.appendChild(loader);
		
		
//		eff = new Effect.Opacity(this.domNode, { from: 0, to: 0.5, duration: 1 });			
		Event.observe(window,"scroll",this.curtainScroll);
		this.pparams = $H(postParams || {});
		this.fetchUrl(sUrl);
	},
	curtainScroll: function(evt) {
		curtain = $("_dialogCurtain");
		container = $("_dialogContainer");
		scrollTop = document.body.scrollTop;
		Element.setStyle(curtain, { top: scrollTop});
	},
	fetchUrl: function(url) {
		aj = new Ajax.Updater(this.dialogContainer, url, {
			method: "post",
			parameters: this.pparams.toQueryString(),
			onSuccess: function(t) {
				t.responseText.evalScripts();
//				Element.show($("_dialogContainer"));
				Element.remove($("_dialogLoader"));
				eff = new Effect.Appear("_dialogContainer", {
					afterFinish: function(eff) {
						handle = $("__dialogForm");
						Event.observe(handle, "submit", $("_dialogCurtain").oDialog.hide);
					}});
			},
			onFailure: function(t) {
				alert("Failed: " + t.status + "\r\n" );
				$("_dialogCurtain").oDialog.remove();
			}
		
		});
	},
	remove: function() {
		aObjs = ["_dialogCurtain", "_dialogContainer", "_dialogLoader"];
		oSelf = $("_dialogCurtain").oDialog;
		try {
			Event.stopObserving(window,"scroll",oSelf.curtainScroll);
			Event.stopObserving($("__dialogForm"),"submit", oSelf.hide);
		} catch(e) { alert("wierd error"); }
		aObjs.each(function(item) {
			try { Element.remove(item); } catch(e) { }
		});
		return false;
	},
	show: function() {
	
	}, 
	hide: function(e) {
		curtain = $("_dialogCurtain");
		oDialog = curtain.oDialog;
		try {
			oDialog.callback(oDialog.params);
		} catch(e) { }
		var eff = new Effect.DropOut($("_dialogContainer"),{
			afterFinish: function() {
				oDialog.remove();
				return false;
			}
		});
		Event.stop(e);
		return false;
	}
});

