

Tauri.UI.MsgBox = Class.create(Tauri.UI.OverlayBox, {
	
	id          : 0,
	zIndex      : 10000,
	onClose     : null,
	onHide      : null,
	onShow      : null,
	onClick     : null,
	message     : '',
	buttons     : 0,
	box         : null,
	textBox     : null,
	buttonsBox  : null,
	hidden      : true,
	showTimeout : 0,
	hideTimeout : 0,
	closeTimeout: 0,
	boxClass    : 'message-box',
	modal       : true,
	
	initialize : function ($super, message, opts) {
		$super(opts);
		this.message = message;
		
		Tauri.UI.Msgs.register(this);
		this.createBox(!this.showTimeout);
		if (this.showTimeout) {
			var t = this;
			setTimeout(function () {
				t.setContent();
				t.setTimeouts();
			}, Math.round(this.showTimeout * 1000));
		} else {
			this.setTimeouts();
		}
	},
	
	setTimeouts : function () {
		if (this.hideTimeout) {
			setTimeout(this.hide.bind(this), Math.round(this.hideTimeout * 1000));
		} else if (this.closeTimeout) {
			setTimeout(this.close.bind(this), Math.round(this.closeTimeout * 1000));
		}
	},
	
	update : function (opts) {
		this.copyOpts(opts);
		this.setContent();
	},
	
	createBox : function ($super, showMessage) {
		$super();
		if (showMessage) {
			this.setContent();
		}
	},
	
	setContent : function () {
		var ul, style, btn;
		if (!this.message) {
			this.message = '';
		}
		this.textBox.update(this.message);
		if (this.buttons) {
			if (this.buttons & Tauri.UI.MsgBox.BTN_CLOSE) {
				this.addButton(Tauri.Lang.get('close'), this.close.bind(this, Tauri.UI.MsgBox.BTN_CLOSE));
			}
			if (this.buttons & Tauri.UI.MsgBox.BTN_NO) {
				this.addButton(Tauri.Lang.get('no'), this.close.bind(this, Tauri.UI.MsgBox.BTN_NO));
			}
			if (this.buttons & Tauri.UI.MsgBox.BTN_CANCEL) {
				this.addButton(Tauri.Lang.get('cancel'), this.close.bind(this, Tauri.UI.MsgBox.BTN_CANCEL));
			}
			if (this.buttons & Tauri.UI.MsgBox.BTN_YES) {
				this.addButton(Tauri.Lang.get('yes'), this.close.bind(this, Tauri.UI.MsgBox.BTN_YES));
			}
			if (this.buttons & Tauri.UI.MsgBox.BTN_OK) {
				this.addButton(Tauri.Lang.get('ok'), this.close.bind(this, Tauri.UI.MsgBox.BTN_OK));
			}
			if (this.buttons & Tauri.UI.MsgBox.BTN_ACCEPT) {
				this.addButton(Tauri.Lang.get('accept'), this.close.bind(this, Tauri.UI.MsgBox.BTN_ACCEPT));
			}
			this.showButtons();
		}
		if (this.hidden) {
			this.show();
		}
		this.box.setStyle({width: 'auto'});
		this.setPosition();
	},
	
	setPosition : function (max) {
		if (!max) {
			max = 400;
		}
		var w = this.box.getDimensions().width + 20;
		if (w > max) {
			w = max;
		}
		this.box.setStyle({
			marginLeft: Math.round(((w / 2) * -1) + 102) + 'px',
			top:        (document.viewport.getScrollOffsets().top + 200) + 'px',
			width:      w + 'px'
		});
	}
});

Tauri.UI.MsgBox.BTN_CLOSE  = 1;
Tauri.UI.MsgBox.BTN_YES    = 2;
Tauri.UI.MsgBox.BTN_NO     = 4;
Tauri.UI.MsgBox.BTN_CANCEL = 8;
Tauri.UI.MsgBox.BTN_ACCEPT = 16;
Tauri.UI.MsgBox.BTN_OK     = 32;

Tauri.UI.Msgs = new (Class.create({
	
	id         : 0,
	boxes      : null,
	closingAll : false,
	
	initialize : function () {
		this.boxes = [];
	},
	
	register : function (box) {
		var maxZ = box.zIndex;
		this.boxes.each(function (item) {
			if (item.zIndex > maxZ) {
				maxZ = item.zIndex + 2;
			}
		});
		box.zIndex  = maxZ;
		this.id    += 1;
		box.id      = this.id;
		this.boxes.push(box);
		
	},
	
	unregister : function (box) {
		if (this.closingAll) {
			return;
		}
		var tmp = [];
		this.boxes.each(function (item) {
			if (item.id !== box.id) {
				tmp.push(item);
			}
		});
		this.boxes = tmp;
	},
	
	closeAll : function () {
		this.closingAll = true;
		this.boxes.each(function (box) {
			box.close();
		});
		this.boxes = [];
		this.closingAll = false;
	},
	
	mergeOpts : function (defs, opts) {
		var merged = {};
		$H(defs).each(function (pair) {
			merged[pair.key] = pair.value;
		});
		$H(opts).each(function (pair) {
			merged[pair.key] = pair.value;
		});
		return merged;
	},
	
	makeContent : function (message, divClass) {
		if (Object.isString(message)) {
			message = message.split(/\r?\n/g);
		}
		if (Object.isArray(message)) {
			var div = new Element('div');
			if (divClass) {
				div.addClassName(divClass);
			}
			message.each(function (line) {
				if (Object.isString(line)) {
					line = new Element('p').update(line);
				}
				div.insert(line);
			});
			message = div;
		}
		return message;
	},
	
	openBox : function (message, options, buttons, divClass) {
		message = this.makeContent(message, divClass);
		var opts    = this.mergeOpts({buttons: buttons}, options);
		return (new Tauri.UI.MsgBox(message, opts));
	},
	
	info : function (message, options) {
		var buttons = Tauri.UI.MsgBox.BTN_OK;
		return this.openBox(message, options, buttons);
	},
	
	error : function (message, options) {
		var buttons = Tauri.UI.MsgBox.BTN_OK;
		return this.openBox(message, options, buttons, 'error');
	},
	
	question : function (message, options) {
		var buttons = Tauri.UI.MsgBox.BTN_YES | Tauri.UI.MsgBox.BTN_NO;
		return this.openBox(message, options, buttons);
	},
	
	progress : function (message, options) {
    	if (Object.isUndefined(message)) {
        	message = Tauri.Lang.get('Please wait..');
        }
		return this.openBox([message], options, 0, 'progress');
	}
	
}))();






