var Response = new Class({
	
	Implements: Options,
	
	
	options: {
   	divBgId: 'divBgResponse',
		divCtId: 'divCtResponse',
		useBgDiv: true,
		closeBouton: {
			active: true,
			border : 5,
			picPath : 'images/template/'
		},
		contentDiv: {
			width: 800,
			marginTop: 60,
			height : 'auto',
			border : '0px solid #95C5DE',
			//background : '#eaeaea',
			minHeight : 450
		}
	},



	initialize: function(response ,options){
		
		// Merge les options
		this.setOptions(options);
		
		// Reponse à printer
		this.response = response;
	},
	
	
	
	
	getResponseSkin : function(){

		// Gestion de la hauteur de la div (on accepte auto)
		var heightTmp = this.options.contentDiv.height;
		if (this.options.contentDiv.height != 'auto')
		{
			var heightTmp = this.options.contentDiv.height+'px';
		}

		// Gestion du content de la réponse
		this.divCtResponse = new Element('div',{
			'id': this.options.divCtId,
			'styles': {
				'position': 'absolute',
				'right': '50%',
				'margin' : '0px '+(-(this.options.contentDiv.width / 2))+'px 0px 0px',
				'top' : this.options.contentDiv.marginTop+'px',
				'z-index' : '30000',
				'width' : this.options.contentDiv.width+'px',
				'height' : heightTmp,
				'border' : this.options.contentDiv.border,
				//'background' : this.options.contentDiv.background,
				'min-height' : this.options.contentDiv.minHeight+'px'
			}
		});
		this.divCtResponse.inject($(document.body));
		this.divCtResponse.set("html", this.response);
		//this.printResponseInto();
		
		// Gestion du background
		scrollSize = window.getScrollSize();
		this.divBgResponse = new Element('div',{
			'id': this.options.divBgId,
			'styles': {
				'position' : 'absolute',
				'top': '0px',
				'left' : '0px',
				'z-index': '3000',
				'width' : '100%',
				'height' : (scrollSize.y)+'px',
				'background-color' : '#000000'
			}
		});
		this.divBgResponse.inject($(document.body));
		
		
		// Merci IE
		if (Browser.Engine.trident)
		{
			this.divBgResponse.setStyle('filter', 'alpha(opacity=50)');
		}
		else
		{
			this.divBgResponse.setStyle('opacity', '0.5');
		}
		
		
		// Gestion du bouton Fermer
		if (this.options.closeBouton.active)
		{
			this.closePicture = new Element('img',{
				'src': this.options.closeBouton.picPath+'close.png',
				'id': 'btnCloseResponse',
				'styles': {
					'float': 'right',
					'top': this.options.closeBouton.border+'px',
					'position' : 'absolute',
					'left': this.options.contentDiv.width - 16 - this.options.closeBouton.border+'px',
					'cursor': 'pointer'
				},
				'events':{
					'click': this.hide.bind(this)
				}
			});
			this.closePicture.inject(this.divCtResponse);
		}

	},
	
	
	
	
	/**
	 * On affiche la reponse
	 */
	
	show : function(){
		if (this.options.useBgDiv)
		{
			this.getResponseSkin();
			
			var transition = new Fx.Scroll(window, {
				wait: false,
				duration: 1000,
				transition: Fx.Transitions.Quad.easeInOut
				});
			transition.toTop();
		}
		else
		{
			$(this.options.divCtId).set("html", this.response);
		}
	},
	
	
	
	/**
	 * On supprime la reponse
	 */
	
	hide : function(){
		if (this.options.useBgDiv)
		{
			this.divCtResponse.dispose();
			this.divBgResponse.dispose();
	   }
	}
});





Response.Exception = new Class({

	Extends: Response,
	Implements: [Options],
	
	options: {
		divCtId: 'divCtException',
		contentDiv: {
			top: 150,
			width: 400,
			//height : 110,
			
			border : '0px solid #990000',
			background : '#ffffff',
			minHeight: 0
		}
	},
	
	
	
	initialize: function(exception)
	{
		this.exception = exception;
		this.bodySize = window.getSize();
	},
	
	
	
	messageContent: function()
	{
   	this.title = new Element('h2',{
			'html': 'Erreur',
			'styles': {
				'text-indent':'30px',
				'text-align':'left',
				'color':'#990000',
				'margin':'0px'
			}
		});
		
		this.content = new Element('pre',{
			'html': this.exception,
			'styles': {
				'text-align':'center',
				'padding':'10px 5px',
				'font':'normal 11px Verdana, Tahoma, Arial,  sans-serif'
			}
		});
		
		this.button = new Element('input',{
			'id': 'btnCloseResponseException',
			'value': 'Ok',
			'type': 'button',
			'events':{
				'click': this.hide.bind(this)
			},
			'styles': {
				'width':'auto'
			}

		});
		
		this.messageDiv = new Element('div',{
			'styles': {
				'margin':'20px 0px',
				'text-align': 'center'
			}
		});
		
		
   },
	
	
	
	show: function(){
		this.getResponseSkin();
		this.messageContent();
		
	

		this.title.inject(this.messageDiv);
		this.content.inject(this.messageDiv);
		this.button.inject(this.messageDiv);
		this.messageDiv.inject(this.divCtResponse);
		
		
		if(this.options.contentDiv.height == 'auto')
		{
			var height = this.divCtResponse.getSize();
			this.options.contentDiv.height = height.y;
		}

		this.divCtResponse.setStyle('top', (this.bodySize.y - this.options.contentDiv.height) / 2);
	}
});







Response.Confirm = new Class({

	Extends: Response,
	Implements: [Options],
	
	options: {
		divCtId: 'divCtConfirm',
		contentDiv: {
			width: 400,
			height : 100,
			border : '1px solid #95C5DE',
			background : '#ffffff',
			minHeight: 0
		}
	},
	
	
	
	initialize: function(message)
	{
		this.message = message;
		this.bodySize = window.getSize();
		this.options.contentDiv.marginTop = (this.bodySize.y - this.options.contentDiv.height) / 2;
	},
	
	
	
	messageContent: function()
	{
		this.messageDiv = new Element('div',{
			'html': this.message,
			'styles': {
				'margin': ((this.options.contentDiv.height - 40) /2)+'px 0px 0px 0px',
				'width': '100%',
				'position' : 'absolute',
				'text-align': 'center'
			}
		});
		this.buttonDiv = new Element('div',{
			'styles': {
				'margin': (this.options.contentDiv.height - 30)+'px 0px 0px 0px',
				'width': '100%',
				'position' : 'absolute',
				'text-align': 'center'
			}
		});
		this.buttonValid = new Element('input',{
			'type': 'button',
			'value': 'Valider',
			'id': 'btnValidConfirm',
			'styles': {
				'width': 'auto'
			},
			'events':{
				'click': this.validFunction.bind(this)
			}
		});
		this.buttonCancel = new Element('input',{
			'type': 'button',
			'value': 'Annuler',
			'id': 'btnCancelConfirm',
			'styles': {
				'width': 'auto'
			},
			'events':{
				'click': this.cancelFunction.bind(this)
			}
		});
		
		this.buttonCancel.inject(this.buttonDiv);
		this.buttonValid.inject(this.buttonDiv);
	},
	
	
	
	show: function(){
		this.getResponseSkin();
		this.messageContent();
		this.buttonDiv.inject(this.divCtResponse);
		this.messageDiv.inject(this.divCtResponse);
	},
	
	
	cancelFunction:function()
	{
		this.hide();
		$('calendarFin').setProperty('value', '');
	},
	
	
	validFunction: function()
	{
		var bien = $('bien').getProperty('value');
		var deb = $('calendarDeb').getProperty('value');
		var fin = $('calendarFin').getProperty('value');
		
		var tmpDeb = deb.split('/');
		var tmpFin = fin.split('/');
		
		var deb = tmpDeb[2]+'-'+tmpDeb[1]+'-'+tmpDeb[0];
		var fin = tmpFin[2]+'-'+tmpFin[1]+'-'+tmpFin[0];
		
		this.request = new Request.HTML({
   		method: 'post',
   		url: document.location+'&mod=planning&act=supplement',
			evalScripts: true,
			//update: 'supplementResponse', 
   		data: 'bien='+bien+'&deb='+deb+'&fin='+fin,
			onSuccess: function(response, responseElements, responseHTML, responseJavaScript)
			{
				this.hide();
				
				var bool = false;
				responseElements.each(function(item, index){
					if (item.getProperty('id') == 'exception')
					{
			   		bool = true;
			   	}					
				});
				if (bool)
				{
					this.hide();
					this.reponse = new Response(responseHTML, {
					  	useBgDiv: true,
						closeBouton: {
							active: false
						},
					  	contentDiv: {
					  		width: 500,
					  		marginTop: 150,
					  		border: 'none',
					  		background: 'transparent'
					  	}
				   }).show();
					if ($('btnCloseResponseException'))
					{
						$('btnCloseResponseException').addEvent('click', function(e)
						{
							$('divCtResponse').dispose();
							$('divBgResponse').dispose();
						}.bind(this));
					}
				}
				else
				{
					$('supplementResponse').set('html', responseHTML);
					$('validInput').setProperty('disabled', '');
				}
   		}.bind(this)
   	}).send();
	}
});
