var PPrompt = {
    alert : function(msg, options) {
	var options = this._extend(this.getOptions(), options || {});
	if (options.doOverlay)
	    this.addOverlay();

	var pwindow = this.getWindow(options.height, options.width).inject(document.body);
	var pmsg = new Element('div', {
	    'class' :'pmsg'
	}).inject(pwindow);
	var pmsg_html = new Element('div', {
	    'html' :msg
	}).inject(pmsg);
	var pbuttons = new Element('div', {
	    'id' :'pbuttons'
	}).inject(pwindow);

	// OK
	var pbuttonOk = new Element('input', {
	    'class' :options.classOk,
	    'type' :'button',
	    'value' :options.labelOk
	}).inject(pbuttons);
	pbuttonOk.addEvent('click', options.onOk);
	pbuttonOk.focus();
    },

    confirm : function(msg, options) {
	var options = this._extend(this.getOptions(), options || {});
	if (options.doOverlay)
	    this.addOverlay();

	var pwindow = this.getWindow(options.height, options.width).inject(document.body);

	var pmsg = new Element('div', {
	    'class' :'pmsg',
	    'html' :msg
	}).inject(pwindow);
	var pbuttons = new Element('div', {
	    'id' :'pbuttons'
	}).inject(pwindow);

	// OK
	var pbuttonOk = new Element('input', {
	    'class' :options.classOk,
	    'type' :'button',
	    'value' :options.labelOk
	}).inject(pbuttons);
	pbuttonOk.addEvent('click', options.onOk);
	pbuttonOk.focus();

	// CANCEL
	var pbuttonCancel = new Element('input', {
	    'class' :options.classCancel,
	    'type' :'button',
	    'value' :options.labelCancel
	}).inject(pbuttons);
	pbuttonCancel.addEvent('click', options.onCancel);
    },

    prompt : function(msg, options) {
	var opt = this.getOptions();
	opt.height = 100;
	var options = this._extend(opt, options || {});

	if (options.doOverlay)
	    this.addOverlay();

	var pwindow = this.getWindow(options.height, options.width).inject(document.body);

	var pmsg = new Element('div', {
	    'class' :'pmsg',
	    'html' :msg
	}).inject(pwindow);
	var pbuttons = new Element('div', {
	    'id' :'pbuttons'
	}).inject(pwindow);

	var pinput = new Element('input', {
	    'id' :'pinput',
	    'style' :'width:260px;',
	    'type' :'text'
	}).inject(pwindow);
	pinput.focus();

	// OK
	var pbuttonOk = new Element('input', {
	    'class' :options.classOk,
	    'type' :'button',
	    'value' :options.labelOk
	}).inject(pbuttons);
	pbuttonOk.addEvent('click', function() {
	    options.onOk(pinput.value);
	});

	// CANCEL
	var pbuttonCancel = new Element('input', {
	    'class' :options.classCancel,
	    'type' :'button',
	    'value' :options.labelCancel
	}).inject(pbuttons);
	pbuttonCancel.addEvent('click', options.onCancel);
    },

    addOverlay : function() {
	var pageSize = this._getPageSize();
	var poverlay = new Element(
		'div',
		{
		    'id' :'poverlay',
		    'style' :'top:0px; left:0px; position:absolute; background: #000;height: ' + (pageSize.pageHeight) + 'px; width: 100%'
		}).inject(document.body);
	this._setOpacity(poverlay, 0.5);
    },

    getWindow : function(height, width) {
	document.body.style.padding = '0';
	var pwindow = new Element('div', {
	    'id' :'pwindow',
	    'style' :'position: fixed'
	});
	return pwindow;
    },

    close : function() {
	if ($chk($('poverlay')))
	    $('poverlay').destroy();
	if ($chk($('pwindow')))
	    $('pwindow').destroy();
    },

    getOptions : function() {
	return {
	    'height' :70,
	    'width' :300,
	    'labelOk' :'OK',
	    'classOk' :'pbutton_ok',
	    'onOk' :PPrompt.close,
	    'labelCancel' :'Cancel',
	    'classCancel' :'pbutton_cancel',
	    'onCancel' :PPrompt.close,
	    'doOverlay' :true
	};
    },

    _extend : function(destination, source) {
	for ( var property in source) {
	    destination[property] = source[property];
	}
	return destination;
    },

    _realOffset : function(element) {
	var valueT = 0, valueL = 0;
	do {
	    valueT += element.scrollTop || 0;
	    valueL += element.scrollLeft || 0;
	    element = element.parentNode;
	} while (element);
	return [ valueL, valueT ];
    },

    _setOpacity : function(element, value) {
	if (typeof element == 'string')
	    element = $(element);
	if (value == 1) {
	    element.style.opacity = (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/
		    .test(navigator.userAgent)) ? 0.999999 : 1.0;
	    if (/MSIE/.test(navigator.userAgent) && !window.opera)
		element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi, '');
	} else {
	    if (value < 0.00001)
		value = 0;
	    element.style.opacity = value;
	    if (/MSIE/.test(navigator.userAgent) && !window.opera)
		element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi, '') + 'alpha(opacity=' + value
			* 100 + ')';
	}
	return element;
    },

    _getPageSize : function() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
	    xScroll = document.body.scrollWidth;
	    yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
	    xScroll = document.body.scrollWidth;
	    yScroll = document.body.scrollHeight;
	} else {
	    xScroll = document.body.offsetWidth;
	    yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {
	    windowWidth = self.innerWidth;
	    windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
	    windowWidth = document.documentElement.clientWidth;
	    windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
	    windowWidth = document.body.clientWidth;
	    windowHeight = document.body.clientHeight;
	}

	if (yScroll < windowHeight)
	    pageHeight = windowHeight;
	else
	    pageHeight = yScroll;

	if (xScroll < windowWidth)
	    pageWidth = windowWidth;
	else
	    pageWidth = xScroll;

	return {
	    'pageWidth' :pageWidth,
	    'pageHeight' :pageHeight,
	    'windowWidth' :windowWidth,
	    'windowHeight' :windowHeight,
	    'yScroll' :yScroll,
	    'xScroll' :xScroll
	};
    }
};