WL.alertCallback = null;
WL.alert = function( text, title, mode, defaultValue, mask )
{
	var handler = WL.alertCallback;
	WL.alertCallback = null;
	if( title == null ) title = "System information";
	if(  mode == null ) mode  = WL.Mode.OK;
	
	if( WL.messageBox == null && document != null && document.forms.length != 0 )
	{
		WL.messageBox = new WL.MessageBox();
		WL.messageBox.create();
	}
	
	if( WL.messageBox && WL.popup_show( WL.$(WL.messageBox.c_id), {closeWithClick:true} ) )
		WL.messageBox.init( text, title, mode, defaultValue, mask, handler );
	else
	{
		var result = null;
		switch( mode )
		{
			case WL.Mode.OK: window.alert( text ); break;
			case WL.Mode.OKCancel: result = window.confirm( text ) ? WL.Result.OK : WL.Result.Cancel; break;
			case WL.Mode.YesNo:
			case WL.Mode.YesNoCancel: result = window.confirm( text ) ? WL.Result.Yes : WL.Result.No; break;
			case WL.Mode.Prompt: result = window.prompt( text, defaultValue ); break;
		}
		if( handler ) handler( result );
	}
}

WL.confirm = function( text, title )
{
	var handler = WL.alertCallback;
	WL.Utils.assert( handler == null, "WL.confirm have not WL.alertCallback" );
	WL.alertCallback = handler ? function(result) { handler(result==WL.Result.Yes); } : null;	
	WL.alert( text, title, WL.Mode.YesNo );
}

WL.prompt = function( text, defaultValue, title, mask )
{
	WL.Utils.assert( WL.alertCallback == null, "WL.prompt have not WL.alertCallback" );
	WL.alert( text, title, WL.Mode.Prompt, defaultValue, mask );
}

WL.Mode = {
	OK: 0,
	OKCancel: 1,
	YesNo: 2,
	YesNoCancel: 3,
	Prompt: 4
};

WL.Result = {
	OK: 0,
	Cancel: 1,
	Yes: 2,
	No: 3
};

WL.MessageBox = WL.Class( Object,
function( base, type )
{
	this.constructor = function()
	{
		this.handler = null;
		this.mode = 0;
	}

	this.create = function()
	{
		var container = WL.$e("div");
			container.className = "message-box popup-zone";
		this.c_id = WL.uniqueID( container );
		
	//	document.forms[0].appendChild( container );
		document.body.insertBefore( container, document.body.firstChild );
		
		this.info = WL.$e("div");
		this.info.className = "message-info";
		container.appendChild( this.info );
		
		this.text = WL.$e("input");
		this.text.type = "text";
		this.text.className = "message-input";
		container.appendChild( this.text );
		
		var buttons = WL.$e("div");
		buttons.className = "message-buttons";
		container.appendChild( buttons );	
			
		this.btn_cancel = createButton( WL.S(15) || "Cancel", this.close.bind( this, WL.Result.Cancel ) );
		buttons.appendChild(this.btn_cancel);

		this.btn_ok = createButton( WL.S(10) || "OK", this.close.bind( this, WL.Result.OK ) );
		buttons.appendChild(this.btn_ok);
			
		this.btn_no = createButton( WL.S(12) || "No", this.close.bind( this, WL.Result.No ) );
		buttons.appendChild(this.btn_no);
		
		this.btn_yes = createButton( WL.S(11) || "Yes", this.close.bind( this, WL.Result.Yes ) );
		buttons.appendChild(this.btn_yes);
						
		WL.Utils.enter_action( this.text, default_enter_action.bind(this) );
	}
	
	var createButton = function( text, handler )
	{
		var a = WL.$e("a");
		a.href = "#";
		a.className = "button";
		a.title = text;
		a.onclick = handler ? a_close.bind(handler) : null;
		var span = WL.$e("span");
			span.className = "b-b";
		var txt = WL.$e("span");
			txt.className = "b-t";
			txt.appendChild( document.createTextNode( text ) );
		span.appendChild(txt);
		a.appendChild( span );		
		return a;
	}
	var a_close = function(e)
	{
		this();
		return WL.returnEvent( WL.getEvent(e), false, true );
	}

	var on_focus = function() { this.style.borderWidth = "0px"; }
	var on_blur  = function() { this.style.borderWidth = "0px"; }
	
	var default_enter_action = function() { this.close( WL.Result.OK ); }
	
	this.init = function( text, title, mode, defaultValue, mask, handler )
	{
		this.mode = mode;
		this.handler = handler;
	
		this.btn_yes.style.display = "none";
		this.btn_no.style.display = "none";
		this.btn_cancel.style.display = "none";
		this.btn_ok.style.display = "none";
		this.text.style.display = "none";
		
	//	WL.Utils.setNew( this.btn_yes, "className", "button simple-button" );
	//	WL.Utils.setNew( this.btn_no, "className", "button simple-button" );
	//	WL.Utils.setNew( this.btn_cancel, "className", "button simple-button" );
	//	WL.Utils.setNew( this.btn_ok, "className", "button simple-button" );
		
		this.info.innerHTML = text;
		
		var defaultFocus;
		switch( mode )
		{
			case WL.Mode.Prompt:	this.text.value = defaultValue;
						if( mask ) WL.mask_attach( this.text, mask, defaultValue );
									this.text.style.display = "";				  
									this.btn_cancel.style.display = "";
									this.btn_ok.style.display = "";
								//	WL.Utils.setNew( this.btn_ok, "className", "button default-button" );
									defaultFocus = textFocus;
									break;
			case WL.Mode.OKCancel:	this.btn_cancel.style.display = "";
			case WL.Mode.OK:		this.btn_ok.style.display = "";
								//	WL.Utils.setNew( this.btn_ok, "className", "button default-button" );
									defaultFocus = okFocus;									
									break;
			case WL.Mode.YesNoCancel:	this.btn_cancel.style.display = "";
			case WL.Mode.YesNo:			this.btn_no.style.display = "";
										this.btn_yes.style.display = "";
								//		WL.Utils.setNew( this.btn_yes, "className", "button default-button" );
										defaultFocus = yesFocus;
										break;
		}
				
		window.setTimeout( defaultFocus, 0 );
	}
	var textFocus = function() { try { WL.messageBox.text.select(); } catch(e){} }
	var okFocus = function() { try { WL.messageBox.btn_ok.focus(); } catch(e){} }
	var yesFocus = function() { try { WL.messageBox.btn_yes.focus(); } catch(e){} }
	
	this.close = function( result )
	{
		if( this.mode == WL.Mode.Prompt )
		{
			if( result == WL.Result.OK )
				result = this.text.value;
			else result = null;
		}
		WL.popup_hide();
		if( this.handler ) this.handler( result );
	}
} );

WL.popup_show = function( p, options ) { if( !WL.createPopup() ) return false; return WL.popup_show( p, options ); }

WL.createPopup = function()
{
if( typeof(WL.popup_hide)=="function" ) return true;	
if( document == null || document.forms.length == 0 ) return false;
//if( document == null || document.body == null ) return false;

var body = WL.Utils.getBody();
var sbody = WL.Utils.getScrollBody();

var popupzones = [];
var backzone = WL.$e("div");	
	backzone.className = "popup-backzone";
var iframe = null;
var options = null;
if( !WL.browser.option.cssPositionFixed )
{
	backzone.style.position = "absolute";
	
	WL._updateScrollBackzone = function() 
	{
		backzone.style.left = sbody.scrollLeft+"px";
		backzone.style.top = sbody.scrollTop+"px";
		
		popupzones.each(function(p)
		{
			p.style.left = (sbody.scrollLeft+p.fixed_left)+"px";
			p.style.top = (sbody.scrollTop+p.fixed_top)+"px";
		});
	}
	
	WL._updateResizeBackzone = function() 
	{		
		backzone.style.width = body.clientWidth+"px";
		backzone.style.height = body.clientHeight+"px";
	};
}
if( WL.browser.option.selectOpacityBag )
{
	iframe = WL.$e("iframe");
	iframe.src = "";
	iframe.style.filter = "alpha(opacity:0)";
	iframe.style.width = "100%";
	iframe.style.height = "100%";
	iframe.scrolling="no";
	iframe.frameBorder="no";
	backzone.appendChild( iframe );
}
//document.forms[0].appendChild( backzone );
document.body.insertBefore( backzone, document.body.firstChild );

var closeWithClick = function() { if( options.closeWithClick ) WL.popup_hide(); }
var popup_esc_listener = function(e) { if( WL.getEvent(e).keyCode == 27 ) closeWithClick(); }
var noDragElements = ["INPUT","TEXTAREA","SELECT","A","LABEL","OBJECT","EMBED"];
var popup_mouse_down = function(e)
{
	e = WL.getEvent(e);	
	
	var p = e.target;
	if( options.noDragContent )
	{
		if( popupzones.indexOf( p ) == -1 ) return;
	}
	else
	{
		while( p && popupzones.indexOf(p) == -1 )
		{
			if( p.tagName && noDragElements.indexOf( p.tagName.toUpperCase() ) != -1 ) return;
			p = p.parentNode;
		}
	}
	
if( WL.browser.ie ) { if( e.button != 1 ) return; }
else				{ if( e.button != 0 ) return; }

	while( p && popupzones.indexOf(p) == -1 ) p = p.parentNode;
	
	var c = WL.Utils.getMouseCoords(e);
	var o = WL.Utils.getElementOffset(p);
	var bc = {x:c.x-o.left,y:c.y-o.top};
	if( WL.browser.option.cssPositionFixed )
	{
		bc.x -= sbody.scrollLeft;
		bc.y -= sbody.scrollTop;
	}
	var mousemove = function(e)
	{
		if( p == null ) { dragend(); return; }
			
		e = WL.getEvent(e);		
		WL.Utils.clear_selection();
				
		var coords = WL.Utils.getMouseCoords(e);		
		coords.y = Math.middle( coords.y-bc.y-sbody.scrollTop, 0, body.clientHeight-p.offsetHeight );
		coords.x = Math.middle( coords.x-bc.x-sbody.scrollLeft, 0, body.clientWidth-p.offsetWidth );
		
		if( !WL.browser.option.cssPositionFixed )
		{
			p.fixed_top = coords.y;
			p.fixed_left = coords.x;
							
			coords.y += sbody.scrollTop;
			coords.x += sbody.scrollLeft;
		}
			
		p.style.top = coords.y + "px";
		p.style.left = coords.x + "px";		
		return false;
	}
	var dragend = function(e)
	{
		p = null;
		WL.Utils.clear_selection();
		WL.detachListener( document, "mouseup", dragend );
		WL.detachListener( document, "mousemove", mousemove );
		if( iframe )
			WL.detachListener( iframe.contentWindow.document, "mousemove", mousemove );
	}
	
	WL.attachListener( document, "mouseup", dragend );
	WL.attachListener( document, "mousemove", mousemove );
	if( iframe )
		WL.attachListener( iframe.contentWindow.document, "mousemove", mousemove );
	return WL.returnEvent(e,true,true);
}
var updateBackzone = function()
{
	backzone.style.zIndex = popupzones.length<<1;
	var opacity = 0;
	popupzones.each(function(p){if(opacity<p.options.opacity)opacity=p.options.opacity;});	
	backzone.style.filter = "alpha(opacity:"+opacity+")";
	backzone.style.opacity = opacity/100;	
}

WL.popup_hide = function()
{
	var p = popupzones.splice(popupzones.length-1,1)[0];
	if( p == null ) return;
	
	WL.detachListener( p, "mousedown", popup_mouse_down );
	p.style.display = "none";
	
	updateBackzone();
	
	if( popupzones.length == 0 )
	{	
		WL.detachListener( document, "keypress", popup_esc_listener );
		backzone.onclick = null;
		if( iframe ) 
			iframe.contentWindow.document.onclick = null;
		if( !WL.browser.option.cssPositionFixed )
		{
			WL.detachListener( window, "scroll", WL._updateScrollBackzone );
			WL.detachListener( window, "resize", WL._updateResizeBackzone );
		}		
		backzone.style.display = "none";
	}	
}

WL.popup_show = function( p, opt )
{
	if( popupzones.indexOf(p) != -1 ) return false;

	WL.Utils.addCss(p,"popup-zone");
	if( !WL.browser.option.cssPositionFixed )
		p.style.position = "absolute";
	
	options = opt || {};
	if( typeof(options.opacity)=="undefined" ) options.opacity = 50;
	p.options = options;
	popupzones.push(p);	
	
	updateBackzone();
	
	if( popupzones.length == 1 )
	{
		WL.attachListener( document, "keydown", popup_esc_listener );
		backzone.onclick = closeWithClick;
		if( iframe )
			iframe.contentWindow.document.onclick = closeWithClick;
		backzone.style.display = "block";
	}
	
	p.style.display = "block";
	p.style.zIndex = (popupzones.length<<1)+1;
		
	if( !options.noDrag ) WL.attachListener( p, "mousedown", popup_mouse_down );
	if( options.relativeElement )
	{
		var o = WL.Utils.getElementOffset( options.relativeElement );
		options.y = o.top+o.height-sbody.scrollTop;
		options.x = o.left-body.scrollLeft;
	}
	
	if( !options.y ) options.y = Math.max( 0, Math.ceil((body.clientHeight-p.offsetHeight)/2) );
	if( !options.x ) options.x = Math.max( 0, Math.ceil((body.clientWidth-p.offsetWidth)/2) );
	
	options.y = Math.middle( 0, options.y, body.clientHeight-p.offsetHeight );
	options.x = Math.middle( 0, options.x, body.clientWidth-p.offsetWidth );

	if( !WL.browser.option.cssPositionFixed )
	{
		p.fixed_top = options.y;
		p.fixed_left = options.x;
			
		window.setTimeout( WL._updateScrollBackzone, 0 );
		window.setTimeout( WL._updateResizeBackzone, 0 );
		
		if( popupzones.length == 1 )
		{
			WL.attachListener( window, "resize", WL._updateResizeBackzone );
			WL.attachListener( window, "scroll", WL._updateScrollBackzone );
		}
	
		options.y += sbody.scrollTop;
		options.x += sbody.scrollLeft;
	}
		
	p.style.top = options.y + "px";
	p.style.left = options.x + "px";
	return true;
}
return true;
}