
var Mantix = {};

Mantix.Popup = Class.create({
	
	initialize: function(options) {
		this.options = options;
		this.blocker = $('popup_overlay');
		this.container = $('header');
		this.container.focus();
		
		this.blockPage();
		this.createDialog(this);
	},
	
	createEl: function(tag, className, style, content) {
		var el = document.createElement(tag);
		if (className) el.className = className;
		if (style) el.style.top = style;
		if (content) el.innerHTML = content;
		return el;
	},
	
	createBtn: function(name, value) {
		var btn = document.createElement('input');
		btn.type = 'button';
		btn.className = 'inputsubmit';
		btn.name = name;
		btn.value = value;
		return btn;
	},
	
	createDialog: function(popup) {
		this.dialog = this.createEl('div', 'popup_dialog', (document.documentElement.scrollTop+85)+'px');
		
		var pcontent = this.createEl('div', 'popup_content');
		var dcontent = this.createEl('div', 'dialog_content');
		var dbody = this.createEl('div', 'dialog_body', null, this.options.message);
		var dbtns = this.createEl('div', 'dialog_buttons');
		
		pcontent.appendChild(this.createEl('h2', 'dialog_title' +((this.options.type == 1)?' dialog_error':''), null, '<span>' +this.options.title +'</span>'));
		pcontent.appendChild(dcontent);
		dcontent.appendChild(dbody);
		dcontent.appendChild(dbtns);
		
		for (var button in this.options.buttons) {
			var btn = this.options.buttons[button];
			var create = this.createBtn(button, btn.value);
			switch (btn.type) {
				case 'cancel':
					create.className += ' inputcancel';
					break;
			}
			if (btn.cancel) create.onclick = function() { popup.close() };
			if (btn.action) {
				btn.action = this.evalAction(popup, btn.action);
				create.onclick = btn.action;
			}
			dbtns.appendChild(create);
		}
		
		this.dialog.appendChild(pcontent);
		this.dialog = this.container.appendChild(this.dialog);
		create.focus();
	},
	
	evalAction: function(popup, act) {
		return function() { eval(act); }
	},
	
	blockPage: function() { this.blocker.style.display = 'block'; },
	unblockPage: function() { this.blocker.style.display = ''; },
	
	close: function() {
		this.unblockPage();
		this.container.removeChild(this.dialog);
	}
})

Mantix.Prompt = Class.create({
	initialize: function(title, prompt, act) {
		return new Mantix.Popup({
			type: 0,
			title: title,
			message: prompt,
			buttons: {
				ok: {type: 'submit', value: 'Uredu', cancel: false, action: act },
				cancel: {type: 'cancel', value: 'Odustani', cancel: true}
			}
		})
	}
})

Mantix.Alert = Class.create({
	initialize: function(type, title, message) {
		return new Mantix.Popup({
			type: type,
			title: title,
			message: message,
			buttons: {ok: {type: 'submit', value: 'Uredu', cancel: true}}
		})
	}
})

Mantix.JSON = Class.create({
	
	initialize: function(jsonstring) {
		this.object = jsonstring.evalJSON();
		
		if (this.object.redirect != null) {
			if (this.object.redirect == '') document.location = document.location;//location.reload(true);
			else document.location = this.object.redirect;
		}
		if (this.object.message != null) new Mantix.Alert(1, 'Poruka od servera!', this.object.message);
		if (this.object.dialog != null) new Mantix.Popup(this.object.dialog);
		return this.object.data;
		
	}
	
})