
var PBBAcpBox = new Class({
	Implements: [Options],
	getOptions: function(){
		return {
			name: 'PBBAcp',
			zIndex: 65555,
			onReturn: false,
			onReturnFunction : Class.empty,
			BoxStyles: {
				'width': 500
			},
			OverlayStyles: {
				'background-color': '#000',
				'opacity': 0.7
			},
			showDuration: 400,
			showEffect: Fx.Transitions.linear,
			closeDuration: 10,
			closeEffect: Fx.Transitions.linear,
			moveDuration: 500,
			moveEffect: Fx.Transitions.backOut,
			moveOnScroll : true,
			dontCloseOnClick : false,
			LCMode : false,
			onShowStart : Class.empty,
			onShowComplete : Class.empty,
			onCloseStart : Class.empty,
			onCloseComplete : function(properties) {
				if ( !this.options.dontCloseOnClick )
					try{
					this.options.onReturnFunction(this.options.onReturn);
					} catch(z){}
			}.bind(this)
		};
	},
 
	initialize: function(options){
		this.setOptions(this.getOptions(), options);
 
		// création de l'overlay
		this.Overlay = new Element('div', {
			'id': 'BoxOverlay',
			'styles': {
				'display': 'none',
				'z-index': this.options.zIndex,
				'position': 'absolute',
				'top': '0',
				'left': '0',
				'background-color': this.options.OverlayStyles['background-color'],
				'opacity': 0,
				'height': window.getScrollHeight() + 'px',
				'width': window.getScrollWidth() + 'px'
			}
		});
 
		this.progressInterval = false;
		this.progressIndicator = false;
		this.opened = false;
		
		this.Content = new Element('div', {
			'id': this.options.name + '-BoxContent'
		})

		this.ContentHeader = new Element('div', {
			'id': this.options.name + '-BoxContentHeader'
		});
 
		this.InBox = new Element('div', {
			'id': this.options.name + '-InBox'
		}).adopt(this.ContentHeader,this.Content);
	 
		this.Box = new Element('div', {
			'id': this.options.name + '-Box',
			'styles': {
				'display': 'none',
				'z-index': this.options.zIndex,
				'position': 'absolute',
				'top': '0',
				'left': '0',
				'width': this.options.BoxStyles['width'] + 'px'
			}
		}).adopt(this.InBox);
 
		if ( this.options.name != 'simple' )
		{
			this.Overlay.injectInside(document.body);
		}
		this.Box.injectInside(document.body);
		if	(isset(this.options.move) )
		{
			myMove = new Drag.Move( this.Box,{ } );
		}
		if	(isset(this.options.width) ) this.Box.setStyle('width',this.options.width);
		if	(isset(this.options.height) ) this.Box.setStyle('height',this.options.height);
		if	(isset(this.options.resizable) )
		{
			$(this.options.name + '-InBox').makeDraggable();
		}
 
		// Si le navigateur est redimentionné
	
		window.addEvent('resize', function() {
			if(this.options.display == 1) {
				this.Overlay.setStyles({
					'height': window.getScrollHeight() + 'px',
					'width': window.getScrollWidth() + 'px'
				});
				this.replaceBox();
			}
		}.bind(this));
 
		if ( this.options.name != 'simple' )
		{
			window.addEvent('scroll', this.replaceBox.bind(this));
		}
	},
 
	
	showAjaxProgress:function()
	{
		this.ShowAjax = new Element('div', {
			'id': 'BoxOverlay',
			'styles': {
				'z-index': this.options.zIndex+1000,
				'position': 'fixed',
				'top': '0',
				'left': '0',
				'text-align' : 'center',
				'padding': '5px',
				'font-size': '12px',
				'background-color': '#ffe075',
				'opacity': 1,
				'height': 20+'px',
				'width': 'auto'
			}
		});
		this.ShowAjax.innerHTML=this.options.showAjaxText;
		this.progressIndicator = this.ShowAjax.injectInside(document.body);
		$clear( this.progressInterval );
	},
	
	/*
	Property: display
		Show or close box
 
	Argument:
		option - integer, 1 to Show box and 0 to close box (with a transition).
	*/	
	display: function(option){
		// Stop la transition en action si elle existe	
		//if(this.Transition)
		//	this.Transition.stop();				
 
		// Show Box	
		if(this.options.display == 0 && option != 0 || option == 1) {
			if ( this.opened == false )
			{
				this.opened = true;
				this.Overlay.setStyle('display', 'block');
				this.options.display = 1;
				if ( this.options.ajax )
				{
					this.options.OverlayStyles = {
						'background-color' : "#000",
						'opacity' : 0.01
					};
				}
				if ( this.options.progress )
				{
					this.options.OverlayStyles = {
						'background-color' : "#000",
						'opacity' : 0.4
					};
				}
					
				if ( this.Overlay.getStyle( 'height' ) != (window.getScrollHeight()+'px'))
				{
					this.Overlay.setStyles({
							'height': window.getScrollHeight() + 'px',
							'width': window.getScrollWidth() + 'px'
						});
				}
				if ( !this.options.ajax && this.options.noscroll)
				{
					if (document.all)
				    {               
				        document.body.scroll = "no";
				    }
				    else
				    {
				        var oTop = document.body.scrollTop;
				        document.body.style.overflow = "hidden";
				        document.body.scrollTop = oTop;
				    }
				}
				
				if ( this.options.ajax ) this.progressInterval = this.showAjaxProgress.delay(500, this);
				
	//			'height': window.getScrollHeight() + 'px',
	//				'width': window.getScrollWidth() + 'px'
	
	
				this.fireEvent('onShowStart', [this.Overlay]);
	 			// Nouvelle transition
	
				
				this.Transition = this.Overlay.set( 'tween', 
						{
							duration: this.options.showDuration,
							transition: this.options.showEffect,
							onComplete: function() {
								try{var a=this.options.ajax;}catch(z){var ajax=false;}
								if ( !this.options.ajax )
								{
									if ( bd.browser=="Explorer")
									{
										var elements = document.getElementsByTagName ( 'SELECT' );
										for ( var i = 0; i < elements.length; i++ )
										{
											try{elements[i].style.display='none';}catch(z){}
										}
									}				
									sizes = window.getSize();
									scrolls = { x: window.getScrollTop(), y: window.getScrollLeft() };
	
	
									this.Box.setStyles({
										'display': 'block',
										'left': (scrolls.x + (sizes.x - this.options.BoxStyles['width']) / 2).toInt()
									});
									
									this.replaceBox();
								}
								this.fireEvent('onShowComplete', [this.Overlay]);
							}.bind(this)
						} );
				
				this.Overlay.tween( 'opacity', this.options.OverlayStyles['opacity'], this.options.OverlayStyles['opacity'] );
	
				/*
				this.Transition = this.Overlay.tween(
						'opacity', 
						{
							duration: this.options.showDuration,
							transition: this.options.showEffect,
							onComplete: function() {
								try{var a=this.options.ajax;}catch(z){var ajax=false;}
								if ( !this.options.ajax )
								{
									if ( bd.browser=="Explorer")
									{
										var elements = document.getElementsByTagName ( 'SELECT' );
										for ( var i = 0; i < elements.length; i++ )
										{
											try{elements[i].style.display='none';}catch(z){}
										}
									}				
									sizes = window.getSize();
									this.Box.setStyles({
										'display': 'block',
										'left': (sizes.scroll.x + (sizes.size.x - this.options.BoxStyles['width']) / 2).toInt()
									});
									this.replaceBox();
								}
								this.fireEvent('onShowComplete', [this.Overlay]);
							}.bind(this)
						}, 0, this.options.OverlayStyles['opacity'] );
					*/
			}
		}
		// Close Box
		else {
			if ( this.opened )
			{
				this.opened = false;
				this.Box.setStyles({
					'display': 'none',
					'top': 0
				});
				this.options.display = 0;
				if ( this.progressIndicator ) this.progressIndicator.dispose();
				$clear( this.progressInterval );
				
				this.fireEvent('onCloseStart', [this.Overlay]);
	 
				// Nouvelle transition		
				this.Transition = this.Overlay.set( 'tween', {
						duration: this.options.closeDuration,
						transition: this.options.closeEffect,
						onComplete: function() {
	
						
							if ( bd.browser=="Explorer")
							{
								var elements = document.getElementsByTagName ( 'SELECT' );
								for ( var i = 0; i < elements.length; i++ )
								{
									try{elements[i].style.display='';}catch(z){}
								}
							}
						
							if ( !this.options.dontShowOnClick )
								this.fireEvent('onCloseComplete', [this.Overlay]);
						}.bind(this)
					}
				);
				this.Overlay.tween( 'opacity', this.options.OverlayStyles['opacity'], 0 );
				//this.Content.empty();
	            document.body.scroll = "";
	            document.body.style.overflow = "";
			}
		}			
	},
 
	/*
	Property: replaceBox
		Move Box in screen center when brower is resize or scroll
	*/
	replaceBox: function() {
		if(this.options.display == 1 && this.options.moveOnScroll ) {
			sizes = window.getSize();
 
			if(this.MoveBox)
				this.MoveBox.stop();

			sizes = window.getSize();
			scrolls = { x: window.getScrollLeft(), y: window.getScrollTop() };
			
			var left = ( scrolls.x + (sizes.x - this.options.BoxStyles['width']) / 2).toInt();
			var top = ( scrolls.y + (sizes.y - this.Box.offsetHeight) / 2).toInt();
	
			var m = new Fx.Tween( this.Box, {
						duration: this.options.moveDuration,
						transition: this.options.moveEffect
					} );
			if ( this.options.name!='simple')
				this.Box.setStyles( {
						'left': left, 
						'top' : top
				} );
			else
				this.Box.setStyles( {
					'top' : top
			} );
	
			
/*
			this.MoveBox = this.Box.effects({
				duration: this.options.moveDuration,
				transition: this.options.moveEffect
			}).start({
				'left': (sizes.scroll.x + (sizes.size.x - this.options.BoxStyles['width']) / 2).toInt(),
				'top': (sizes.scroll.y + (sizes.size.y - this.Box.offsetHeight) / 2).toInt()
			});
			*/
		}
	},
 
	/*
	Property: messageBox
		Core system for show all type of box
 
	Argument:
		type - string, 'alert' or 'confirm' or 'prompt'
		message - text to show in the box
		properties - see Options below
		input - text value of default 'input' when prompt
 
	Options:
		textBoxBtnOk - text value of 'Ok' button
		textBoxBtnCancel - text value of 'Cancel' button
		onComplete - a function to fire when return box value
	*/	
	messageBox: function(type, message, properties, input, options_save) {
		try{
			nowindow = properties['nowindow'];
		} catch(z){
			nowindow = false; 
		}
		properties = Object.extend( properties || {}, {
			'textBoxBtnOk': 'OK',
			'textBoxBtnCancel': 'Cancel',
			'textBoxInputPrompt': null,
			'onComplete': Class.empty,
			'nowindow' : nowindow
		} );

		this.options.onReturnFunction = properties.onComplete;

		if ( nowindow )
		{
			this.InBox.id='';
			this.Box.id='';
			this.Content.id='';
			this.Content.getParent().getParent().setStyle( 'text-align', 'center' );
		}

		if(type == 'alert' )
		{
			this.AlertBtnOk = new Element('input', {
				'id': 'BoxAlertBtnOk',
				'type': 'submit',
				'value': properties.textBoxBtnOk,
				'styles': properties.btnWidth ? {'width':properties.btnWidth} : {
					'width': '70px'
				}
			});
 
			this.AlertBtnOk.addEvent('click', function() {
				this.options.onReturn = true;
				this.display(0);
			}.bind(this));
 
			this.Content.setProperty('class','BoxAlert').set('html', message + '<br />');			
			
			try { var nobutt = properties.nobutton; } catch(z){var nobutt=false;}
			if ( !nobutt )
				this.AlertBtnOk.injectInside(this.Content);
			
			this.display(1);
		}
		else if(type == 'confirm') {
			this.ConfirmBtnOk = new Element('input', {
				'id': 'BoxConfirmBtnOk',
				'type': 'submit',
				'value': properties.textBoxBtnOk,
				'styles': {
					'width': '70px'
				}
			});
 
			this.ConfirmBtnCancel = new Element('input', {
				'id': 'BoxConfirmBtnCancel',
				'type': 'submit',
				'value': properties.textBoxBtnCancel,
				'styles': {
					'width': '70px'
				}
			});
 
			this.ConfirmBtnOk.addEvent('click', function() {
				this.options.onReturn = true;
				if ( !this.options.dontCloseOnClick )
				{
					this.display(0);
				}
				else
				{
					//
					//
				}
				// ezt vettem ki, miert?
				//this.options.options_save.ok();
			}.bind(this));
 
			this.ConfirmBtnCancel.addEvent('click', function() {
				this.options.onReturn = false;
				this.display(0);
			}.bind(this));		
 
			this.Content.setProperty('class','BoxAlert').set('html', message + '<br />');
			this.ConfirmBtnOk.injectInside(this.Content);
			this.ConfirmBtnCancel.injectInside(this.Content);
			this.display(1);
		}
		else if(type == 'prompt') {
			this.PromptBtnOk = new Element('input', {
				'id': 'BoxPromptBtnOk',
				'type': 'submit',
				'value': properties.textBoxBtnOk,
				'styles': {
					'width': '70px'
				}
			});
 
			this.PromptBtnCancel = new Element('input', {
				'id': 'BoxPromptBtnCancel',
				'type': 'submit',
				'value': properties.textBoxBtnCancel,
				'styles': {
					'width': '70px'
				}
			});			
 
			this.PromptInput = new Element('input', {
				'id': 'BoxPromptInput',
				'type': 'text',
				'value': input,
				'styles': {
					'width': '250px'
				}
			});
 
			this.PromptBtnOk.addEvent('click', function() {
				this.options.onReturn = this.PromptInput.value;
				if ( !this.options.dontCloseOnClick )
				{
					this.display(0);
				}
			}.bind(this));
 
			this.PromptBtnCancel.addEvent('click', function() {
				this.options.onReturn = false;
				this.display(0);
			}.bind(this));
 
			this.Content.setProperty('class','BoxPrompt').setHTML(message + '<br />');
			this.PromptInput.injectInside(this.Content);
			new Element('br').injectInside(this.Content);
			this.PromptBtnOk.injectInside(this.Content);
			this.PromptBtnCancel.injectInside(this.Content);
			this.display(1);
		}
		else {
			this.options.onReturn = false;
			this.display(0);		
		}
	},
 
	/*
	Property: alert
		Shortcut for alert
 
	Argument:
		properties - see Options in messageBox
	*/		
	alert: function(message, properties){
		this.messageBox('alert', message, properties);
	},
 
	/*
	Property: confirm
		Shortcut for confirm
 
	Argument:
		properties - see Options in messageBox
	*/
	confirm: function(message, properties){
		this.messageBox('confirm', message, properties);
	},
 
	/*
	Property: prompt
		Shortcut for prompt
 
	Argument:
		properties - see Options in messageBox
	*/	
	prompt: function(message, input, properties){
		this.messageBox('prompt', message, properties, input);
	},


	/*
		NOTICE WINDOW IMPLEMENT
			
	Options:
		name - name of the box for use different style
		zIndex - integer, zindex of the box
		onReturn - return value when box is closed. defaults to false
		onReturnFunction - a function to fire when return box value
		BoxStyles - stylesheets of the box
		OverlayStyles - stylesheets of overlay
		showDuration - duration of the box transition when showing (defaults to 200 ms)
		showEffect - transitions, to be used when showing
		closeDuration - Duration of the box transition when closing (defaults to 100 ms)
		closeEffect - transitions, to be used when closing
		onShowStart - a function to fire when box start to showing
		onCloseStart - a function to fire when box start to closing
		onShowComplete - a function to fire when box done showing
		onCloseComplete - a function to fire when box done closing

	Property: messageBox
		Core system for show all type of box
 
	Argument:
		type - string, 'alert' or 'confirm' or 'prompt'
		message - text to show in the box
		properties - see Options below
		input - text value of default 'input' when prompt
 
	Options:
		textBoxBtnOk - text value of 'Ok' button
		textBoxBtnCancel - text value of 'Cancel' button
		onComplete - a function to fire when return box value
	*/
	open: function( options )
	{
		//
		var type = ( options.notext=="" ) ? 'alert' : 'confirm';
		if ( options.nobutton ) type = 'alert';
		this.options.options_save = options;
		this.options.ajax = false;
		this.options.noscroll = options.noscroll;
		// 
		if ( bd.browser=="Explorer")
		{
			var elements = document.getElementsByTagName ( 'SELECT' );
			for ( var i = 0; i < elements.length; i++ )
			{
				try{elements[i].style.display='none';}catch(z){}
			}
		}
		//
		try{var a=options.btnWidth;}catch(z){options.btnWidth=0;}	
		try{var a=options.nobutton;}catch(z){options.nobutton=false;}
		
		if ( options.contentwindow )
		{
			this.Overlay.setStyle( 'z-index', this.options.zIndex );
			this.Box.setStyle( 'z-index', this.options.zIndex );
		}
		else
		{
			if ( options.name=='simple' )
			{
				this.Overlay.setStyle( 'z-index', this.options.zIndex-20 );
				this.Box.setStyle( 'z-index', this.options.zIndex-15 );
			}
			else
			{
				this.Overlay.setStyle( 'z-index', this.options.zIndex+20 );
				this.Box.setStyle( 'z-index', this.options.zIndex+25 );
			}
		}
		
		this.messageBox( type, options.content,
			{
				textBoxBtnOk: options.oktext,		
				textBoxBtnCancel: options.notext,
				btnWidth: options.btnWidth ? options.btnWidth : 0,
				nobutton: options.nobutton,
				nowindow: options.nowindow,
				onComplete: function( returnvalue )
				{			
					if ( returnvalue )
					{
						try{
						this.options_save.ok(this.options_save.options);
						}catch(z){}
					}
					else
					{
						try{
						this.options_save.no(this.options_save.options);
						}catch(z){}
					}
					return returnvalue;
				}
			},
			options // options to save
		);
	},
	// AJAX EXTENSION
	showOverlay: function( )
	{
		this.options.ajax = true;
		this.display(1);
	},
	close: function( )
	{
		this.display(0);
	},

	
	showProgress: function( text, buttonText, onCancel )
	{
		//
		params = {};
		this.options.progress=true;
		params.oktext = buttonText;
		params.btnWidth=100;
		if ( buttonText == undefined ) buttonText = "";
		params.nobutton = ( buttonText=="" ) ? true : false;
		params.ok=onCancel;
		params.notext = '';
		params.content = "<div class='loader' id='ajaxloader'>&nbsp;</div><div class='h10'></div><div class='w100'>"+text+"</div>";
		this.open( params );
		//
	},

	showProgress2: function()
	{
		//
		params = { content: '<img src="images/mb/loading2.gif" />', nowindow: true, nobutton : true }
		this.open( params );
		//
	},

	loadcontent: function( url, params )
	{
		//
//		this.options.LCMode = true;
		//
		//
//		this.Content.id = this.options.name + '-BoxContent760';
//		this.InBox.id = this.options.name + '-InBox760';
//		this.Box.id = this.options.name + '-Box760';
		//
//		console.log(this.Content.id);
//		this.options.BoxStyles.width = '760';
		//
		this.options.dontCloseOnClick = true;
		if ( !params ) params = { };
		params.root = this;
		var xhc = new XHConn( );
		if ( !xhc) { this.open( {content: 'no content' } ); return false; }
		//
		if ( !xhc.connect( url, "GET", "", this.load_complete, params) )
			{ this.open( {content: 'no content' } ); return false; }
	},
	load_complete: function( OXML, params )
	{
		params.root.options.moveOnScroll = false;
		params.content = OXML.responseText;
		params.root.open( params );
		if ( params.onContentLoaded ) params.onContentLoaded( );
		//
		params.root.options.tim = setInterval( 'noticeWindow_content.fixoverlay();', 500 );
		//
	},
	fixoverlay: function()
	{
		clearInterval( this.options.tim );
		this.display(1);		
	}
});
 
PBBAcpBox.implement(new Events, new Options);


