/*
	MsgBox.js: Journal CSS Editor; Custom requester.
	Copyright (c) 2008, 2009 Kalle Räisänen.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

var MsgBox =
{
	_idify: function(str) { return str.replace(/[^a-z0-9\-]/gi, ''); }

	,show: function(mb) {
		var view_dim    = document.viewport.getDimensions();
		var view_scroll = document.viewport.getScrollOffsets();
		var mb_dim      = $(mb).getDimensions();
		var mbtop       = Math.round((view_dim.height / 2) - (mb_dim.height / 2) + view_scroll.top) + 'px';
		var mblft       = Math.round((view_dim.width / 2)  - (mb_dim.width  / 2) + view_scroll.left) + 'px';

		if(Prototype.Browser.IE)
			mbtop  = mblft = '50%';   // Kluge

		$(mb).setStyle({
			position:   'absolute',
			display:    'block',
			top:        mbtop,
			left:       mblft,
			overflow:   'hidden',
			zIndex:     '100'
		});

		$$(
			'#' + $(mb).id + ' input',
			'#' + $(mb).id + ' textarea',
			'#' + $(mb).id + ' select'
		).first().focus();
	}

	,hide: function(mb) {
		$$(
			'#' + $(mb).id + ' input',
			'#' + $(mb).id + ' textarea',
			'#' + $(mb).id + ' select'
		).first().blur();

		$(mb).hide();
	}

	,create: function(title, fields, callback, container, numbuttons) {
		var mbid     = 'mb-' + MsgBox._idify(title);
		var buttons  = [{v: 'OK', i: mbid + '-ok', t: 'submit'}];
		var mbdiv    = document.createElement('div');
		var mbhtml   = '';
		var subfunc  = '';
		var frmid    = mbid + '-form';
		var btnhtml  = '';
		var nbuttons = (typeof numbuttons == 'undefined' ? 2 : numbuttons);

		if(nbuttons == 2)
			buttons.push({v: 'Cancel', i: mbid + '-cancel', t: 'button'});

		mbdiv.id            = mbid;
		mbdiv.className     = 'msgbox';
		mbdiv.style.display = 'none';

		if(typeof container == 'undefined')
			container = $(document.getElementsByTagName('body')[0]);

		mbdiv.innerHTML = '<h3>' + title + '</h3>';
		if(typeof fields == 'string') {
			mbhtml += '<div id="' + mbid + '-fields">' + fields + '</div>';
		} else {
			for(var k in fields) {
				var fid = mbid + '-' + MsgBox._idify(k);
				mbhtml += '<p class="msgbox-field"><label> ' + k + ':'
			                +  (fields[k] == 'textarea'
				                 	? '<textarea id="' + fid + '"></textarea>'
				                 	: '<input type="' + fields[k] + '" id="' + fid +'" />')
				             +  '</label></p>';
			}
		}

		for(var i = buttons.length - 1; i >= 0; i--)
			btnhtml += '<input type="#{t}" value="#{v}" id="#{i}" /> '.interpolate(buttons[i]);

		mbdiv.innerHTML += '<form id="'+ frmid +'" method="post" action="">'
		                +  ' <div class="msgbox-fields">' + mbhtml  + '</div>'
		                +  ' <p class="msgbox-buttons">'  + btnhtml + '</p>'
		                +  '</form>';

		$(container).insert({top: mbdiv});

		subfunc = function() {
			var res = {};
			if(typeof fields == 'string') {
				var fdiv = '#' + mbid + '-fields';
				$$(fdiv + ' input', fdiv + ' textarea', fdiv + ' select').each(function(fld) {
					res[fld.id] = $F(fld);
				});
			} else {
				for(var k in fields) {
					var fid = mbid + '-' + MsgBox._idify(k);
					res[k] = $F(fid);
				}
			}

			if(callback(res))
				MsgBox.hide(mbid);
		};

		Event.observe(buttons[0].i, 'click',  subfunc);
		if(nbuttons == 2)
			Event.observe(buttons[1].i, 'click',  function() { MsgBox.hide(mbid) });
		Event.observe(frmid,      'submit', function(evt) { subfunc(); Event.stop(evt); });

		return mbid;
	}

} // MsgBox
