// include Utils.js

function DialogBox() {}

// Transforms a div into a dialog box, and adds functions: show(), hide().
DialogBox.create = function(id) {
	box = document.getElementById(id);
	box.style.position = "absolute";
	box.style.backgroundColor = "#FFFFFF"
	
	if (document.getElementById('dialog_overlay') != null) {
		box.overlay = document.getElementById('dialog_overlay');
	} else {
		box.overlay = document.createElement('div');
		box.overlay.id = 'dialog_overlay';
		box.overlay.style.position = 'absolute';
		box.overlay.style.top = 0;
		box.overlay.style.left = 0;
		box.overlay.style.zIndex = 90;
		box.overlay.style.width = '100%';
		box.overlay.style.height = '100%';
		box.overlay.style.backgroundColor = '#000000';
		box.overlay.style.display = 'none';
		document.body.insertBefore(box.overlay, document.body.childNodes[0]);
	}
	
	box.show = DialogBox.show;
	box.showContextMenu = DialogBox.showContextMenu;
	box.hide = DialogBox.hide;
}

DialogBox.show = function() {
	this.style.display = "inline";

	document.body.style.overflow = "hidden";
	this.overlay.style.top = Scroll.top() + "px";

	this.overlay.style.opacity = 0.3;
	this.overlay.style.filter = 'alpha(opacity=30)';
	this.overlay.style.display = "inline";
	this.overlay.style.zIndex = 90;
	this.style.display = "inline";
	//new Effect.Appear(this.id, {duration:0.5, from:0, to:1.0});
	
	box.style.left = (Window.width()/2 - Element.width(this)/2) + "px";
	box.style.top = (Scroll.top() + Window.height()/2 - Element.height(this)/2) + "px";
	box.style.zIndex = 91;
}

DialogBox.showContextMenu = function() {
	this.style.display = "inline";
	
	this.overlay.style.top = '0px';
	this.overlay.style.height = Page.height() + 'px';
	
	this.overlay.style.opacity = 0.0;
	this.overlay.style.filter = 'alpha(opacity=0)';
	this.overlay.style.display = "inline";
	this.overlay.style.zIndex = 90;
	this.style.display = "inline";
	
	box.style.left = Mouse.x + "px";
	box.style.top = Mouse.y + "px";
	box.style.zIndex = 91;
	
	this.overlay.onclick = function() {
		box.hide();
	};
}

DialogBox.appear = function() {
	this.style.display = "inline";
	
	document.body.style.overflow = "hidden";
	this.overlay.style.top = Scroll.top() + "px";
	
	this.overlay.style.opacity = 0.3;
	this.overlay.style.filter = 'alpha(opacity=30)';
	this.overlay.style.display = "inline";
	//this.style.display = "inline";
	new Effect.Appear(this.id, {duration:0.5, from:0, to:1.0});
	
	//box.style.left = (Window.width()/2 - Element.width(this)/2) + "px";
	//box.style.top = (Scroll.top() + Window.height()/2 - Element.height(this)/2) + "px";
	//box.style.zIndex = 91;
	this.style.left = (Window.width()/2 - Element.width(this)/2) + "px";
	this.style.top = (Scroll.top() + Window.height()/2 - Element.height(this)/2) + "px";
	this.style.zIndex = 91;
}

DialogBox.showInvisible = function() {
	this.style.opacity = "0";
	this.style.display = "inline";
	
	document.body.style.overflow = "hidden";
	this.overlay.style.top = Scroll.top() + "px";
	
	this.overlay.style.opacity = 0.3;
	this.overlay.style.display = "inline";
	this.style.display = "inline";
	
	box.style.left = (Window.width()/2 - Element.width(this)/2) + "px";
	box.style.top = (Scroll.top() + Window.height()/2 - Element.height(this)/2) + "px";
	box.style.zIndex = 91;
}

DialogBox.appear = function() {
	new Effect.Appear(this, 
       {duration:0.3, from:0, to:1.0});
}

DialogBox.hide = function() {
    document.body.style.overflow = "visible";
	this.overlay.style.display = "none";
	this.style.display = "none";
}










function MsgBox(id, opt) {
	opt_onComplete = opt != null ? opt.onComplete : null;
	opt_onFailure = opt != null ? opt.onFailure : null;
	opt_div = opt != null ? document.getElementById(opt.div) : null;
	
	if (opt_div) {
		this.div = opt_div;
		this.div.style.position = 'absolute';
		this.div.style.zIndex = 91;
	} else {
		this.div = document.createElement('div');
		this.div.id = id + '_div';
		this.div.style.position = 'absolute';
		this.div.style.zIndex = 91;
		this.div.style.display = 'none';
		document.body.insertBefore(this.div, document.body.childNodes[0]);
	}
	
	this.overlay = document.createElement('div');
	this.overlay.id = id + '_overlay';
	this.overlay.style.position = 'absolute';
	this.overlay.style.zIndex = 90;
	this.overlay.style.width = '100%';
	this.overlay.style.height = '100%';
	this.overlay.style.backgroundColor = '#000000';
	this.overlay.style.display = 'none';
	document.body.insertBefore(this.overlay, document.body.childNodes[0]);
	
	// Fields, user should not bother with them.
	var showOnComplete = false;
	
	// User's methods.
	this.Show = MsgBox.Show;
	this.Hide = MsgBox.Hide;
	this.Load = MsgBox.Load;
	this.Submit = MsgBox.Submit;
	this.LoadThenShow = MsgBox.LoadThenShow;
	this.IsLoaded = MsgBox.IsLoaded;
	
	// Events, can be set manually or through 'opt' parameter.
	var onComplete = opt_onComplete;
	var onFailure = opt_onFailure;
}
MsgBox.overflowStack = 0;

MsgBox.Show = function() {
	document.body.style.overflow = "hidden";
	this.overlay.style.top = Scroll.top() + "px";
	
	this.overlay.style.opacity = 0.3;
	this.overlay.style.filter = 'alpha(opacity=30)';
	this.overlay.style.display = "inline";
	//this.overlay.style.zIndex = 90;
	
	this.div.style.opacity = 1.0;
	this.div.style.filter = 'alpha(opacity=100)';
	this.div.style.display = "inline";
	this.div.style.left = (Window.width() / 2 - Element.width(this.div) / 2) + "px";
	this.div.style.top = (Scroll.top() + Window.height() / 2 - Element.height(this.div) / 2) + "px";
	//this.div.style.zIndex = 91;
}

MsgBox.Hide = function() {
	document.body.style.overflow = "visible";
	this.overlay.style.display = "none";
	this.style.display = "none";
}

MsgBox.Load = function(url, opt) {
	opt_method = ((opt != null && opt.method == 'get') ? 'get' : 'post');
	this.request = null;
	
	if (window.XMLHttpRequest)
		this.request = new XMLHttpRequest();
	else if (window.ActiveXObject)
		this.request = new ActiveXObject("Microsoft.XMLHTTP");
	else
		this.request = null;
	
	if (this.request) {
		this.request.parent = this;
		this.request.onreadystatechange = MsgBox.OnStateChanged;
		this.request.open(opt_method, url, true);
		this.request.send(null);
		return true;
	} else
		return false;
}

MsgBox.Submit = function(url, opt) {

}

MsgBox.LoadThenShow = function(url, opt) {
	this.showOnComplete = true;
	this.Load(url, opt);
}

MsgBox.IsLoaded = function() {
	
}

// This is assigned to the request object, don't try to call it from MsgBox.
MsgBox.OnStateChanged = function() {
	if (this.readyState == 4) {
		try {
			this.parent.div.innerHTML = this.responseText;
			//this.parent.div.innerHTML = "<html><head></head><body><table><tr><td>0</td></tr><tr><td>0,1</td><td>1,1</td></tr></table></body></html>";
			//this.parent.div.innerHTML = 'ciao';
			//alert(this.responseText);
		} catch(ex) {
			alert('Errore: ' + ex.description);	
		}
		//alert(this.responseText);
		if (this.parent.onComplete != null)
			this.parent.onComplete();
		if (this.parent.showOnComplete == true) {
			this.parent.Show();
			this.parent.showOnComplete = false;
		}
	}
}
