// -*- ecmascript -*-

if (typeof(DiscNW) == 'undefined') {
    DiscNW = {};
}
if (typeof(DiscNW.Popup) == 'undefined') {
    DiscNW.Popup = {};
    DiscNW.Popup.NAME = "DiscNW.Popup";
}

DiscNW.Popup.ModalDialog = function (id) {
    this.id = id || '-discnw-js-popup-div';

    var bind = function (self, method) {
	return function () { method.apply(self, arguments); };
    }
    this.alert = bind(this, this.alert);
    this.hide = bind(this, this.hide);

    // Set up an OK button.
    this._ok_button = document.createElement('button');
    this._ok_button.innerHTML = 'OK';
    this._ok_button.onclick = this.hide;
};
DiscNW.Popup.ModalDialog.prototype = {
    alert: function (bodyHTML, title, event, extraClass) {

	this.get_head_div().innerHTML = title || 'Notice';
	this.get_body_div().innerHTML = bodyHTML;
	this._set_buttons(this._ok_button);

	var div_class = 'js-popup';
	if (extraClass) div_class = div_class + ' ' + extraClass;
	this.get_popup_div().className = div_class;

	try {
	    this._show(event);
	}
	catch (err) {
	    try { this.hide(); } catch (e) {}
	    throw err;
	}
    },

    _show: function (event) {
	var cover = this.get_cover_div();
	var popup = this.get_popup_div();
	var de = document.documentElement;


	cover.style.height = de.scrollHeight + "px";
	cover.style.width = de.scrollWidth + "px";
	cover.style.display = 'block';


	var s = popup.style;
	s.visibility = 'hidden';
	s.width = s.height = 'auto';
	s.display = 'block';

	var min = Math.min;
	var max = Math.max;
	var clamp = function (x, minval, maxval) {
	    return max(minval, min(maxval, x));
	};
	var position = function (xc, width, x0, xw) {
	    // Compute position of left edge of box of given WIDTH.
	    // Try to center box on xc, but in any case keep it within
	    // [x0, x0 + xw].
	    if (typeof xc == 'undefined') {
		// kluge for when/if we don't have an event
		xc = x0 + Math.round(xw / 2);
	    }
	    var pos = Math.round(xc - width / 2);
	    var xmin = max(x0, xc - width);
	    var xmax = min(x0 + xw - width, xc);
	    // If there's extra space, try to leave a margin.
	    var extra = Math.floor((xw - width) / 2);
	    if (extra > 0) {
		extra = min(extra, 10);
		pos = clamp(pos, xmin + extra, xmax - extra);
	    }
	    return clamp(pos, xmin, xmax);
	};


	// Fixup for IE
	event = event || window.event;
	if (event && typeof event.pageX == 'undefined') {
	    event.pageX = event.x;
	    event.pageY = event.y;
	}

	var width = clamp(popup.offsetWidth,
			  400, min(700, de.offsetWidth - 50));
	s.width = width + 'px';
	s.left = position(event.pageX, width,
			  de.scrollLeft, de.offsetWidth) + 'px';
	s.top = position(event.pageY, popup.offsetHeight,
			 de.scrollTop, de.offsetHeight) + 'px';
	s.visibility = 'visible';
    },

    hide: function () {
	this.get_popup_div().style.display = 'none';
	this.get_cover_div().style.display = 'none';
    },

    _create_dom: function () {
	var cover = document.createElement('div');
	cover.className = 'js-popup-cover';
	cover.style.display = 'none';
	// IE hackage
	cover.style.filter = 'alpha(opacity=50)';
	//cover.style.backgroundColor = 'transparent';

	var div = document.createElement('div');
	div.id = this.id;
	div.className = 'js-popup';
	div.style.display = 'none';

	var heading = document.createElement('h1');
	div.appendChild(heading);

	var body = document.createElement('div');
	body.className = 'js-popup-body';
	div.appendChild(body);

	var buttons = document.createElement('div');
	buttons.className = 'js-popup-buttons';
	div.appendChild(buttons);

	var docbody = document.getElementsByTagName('body')[0];
	docbody.appendChild(cover);
	docbody.appendChild(div);

	return div;
    },

    _set_buttons: function () {
	var buttons = this.get_button_div();

	while (buttons.firstChild) {
	    buttons.removeChild(buttons.firstChild);
	}

	for (var i = 0; i < arguments.length; i++) {
	    buttons.appendChild(arguments[i]);
	}
    },

    get_popup_div: function () {
	if (! this._popup_div) {
	    this._popup_div = document.getElementById(this.id);
	}
	if (!this._popup_div) {
	    this._popup_div = this._create_dom();
	}
	return this._popup_div
    },


    get_head_div: function () {
	return this.get_popup_div().firstChild;
    },
    get_body_div: function () {
	return this.get_popup_div().childNodes[1];
    },
    get_cover_div: function () {
	return this.get_popup_div().previousSibling;
    },
    get_button_div: function () {
	return this.get_popup_div().lastChild;
    }
};

(function () {
    var self = DiscNW.Popup;

    var dialog = new self.ModalDialog();

    self.alert = dialog.alert;
})();
