var LightboxNewsletter = Class.create( {
	
		initialize : function( container, overlay, closeId ){

			this.container = $( container );
			if( null === this.container ) {
				throw ('container "' + container +'" est introuvable !');
			}
			$(document.body).insert({bottom:this.container});
			this.containerWidth = 350;
			this.containerHeight = 150;
			
			if( null === $( overlay ) ) {
				var overlay = new Element( 'div', {id:overlay,style:'display:none;background:#000;'});
				$(document.body).insert({bottom:overlay});
				this.overlay = overlay;
			} else {
				this.overlay = $( overlay );
			}
			this.overlay.absolutize();
			this.container.absolutize();
			
			this.closeId = $( closeId );
			if( null === this.closeId ){
				throw ('closeId "' + closeId + '" est introuvable !');
			}
			Event.observe( $( this.closeId ), 'click', this.close.bindAsEventListener( this ) );
			Event.observe( $( this.overlay ), 'click', this.close.bindAsEventListener( this ) );
			//Event.observe( document, 'dom:loaded', this.display.bindAsEventListener( this ) );
			//Event.observe( $( 'newsletter-home-validate-detail' ), 'submit', this.submit.bindAsEventListener( this ) );
			
		},
		
		display : function(){
			var arrayPageSize = this.getPageSize();
			var arrayPageScroll = this.getPageScroll();
			
			this.overlay.setStyle({
							opacity:0.8,
							width:arrayPageSize[2]+'px',
							height:arrayPageSize[3]+'px'
						});
			
			this.container.setStyle({
							width:this.containerWidth+'px',
							height:this.containerHeight+'px',
							top:arrayPageScroll[1] + ( (arrayPageSize[3]- this.containerHeight)/ 2)+'px',
							left:arrayPageScroll[0] + ( (arrayPageSize[2]- this.containerWidth)/ 2)+'px'
						});
			
			//this.overlay.show();
			//this.container.show();
			Effect.Appear( this.overlay, { duration: 1.0, to:0.6 });
			Effect.SlideDown( this.container, { duration: 1.0 });
			Effect.Appear( this.container, { duration: 2.0 });
			
			Event.observe(window, 'resize', this.resize.bindAsEventListener( this ) );
			document.observe( 'scroll', this.scroll.bindAsEventListener( this ) );
			Event.observe(window, 'resize', this.replace.bindAsEventListener( this ) );
			document.observe( 'scroll', this.replace.bindAsEventListener( this ) );
		},
			
		close : function( event ){
			event.stop();
			//this.container.hide();
			//this.overlay.hide();
			Effect.Fade( this.container, { duration: 0.5 } );
			Effect.Fade( this.overlay, { duration: 1.5, from:0.6, to:0 } );

		},
		
		resize : function(){
			var arrayPageSize = this.getPageSize();
			var arrayPageScroll = this.getPageScroll();
			this.overlay.setStyle( {
							width: arrayPageScroll[0] + arrayPageSize[2] + 'px',
							height: arrayPageScroll[1] + arrayPageSize[3] + 'px'
							} );
		},
		
		scroll : function(){
			var arrayPageSize = this.getPageSize();
			this.overlay.setStyle( {
							width: arrayPageSize[0] + 'px', 
							height: arrayPageSize[1] + 'px'
							} );
		},
		
		replace : function(){
			var arrayPageScroll = this.getPageScroll();
			var arrayPageSize = this.getPageSize();
			this.container.setStyle({
						top:arrayPageScroll[1] + ( (arrayPageSize[3]- this.containerHeight)/ 2)+'px',
						left:arrayPageScroll[0] + ( (arrayPageSize[2]- this.containerWidth)/ 2)+'px'
					});
		},
		
		//getPageScroll()
		getPageScroll : function(){

			var xScroll, yScroll;

			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}

			arrayPageScroll = new Array(xScroll,yScroll) 
			return arrayPageScroll;
		},
		
		//getPageSize()
		 getPageSize : function(){
			
			var xScroll, yScroll;
			
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ //all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { //Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			
			var windowWidth, windowHeight;
			
			//console.log(self.innerWidth);
			//console.log(document.documentElement.clientWidth);

			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			
			//for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}

			//console.log("xScroll " + xScroll)
			//console.log("windowWidth " + windowWidth)

			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			//console.log("pageWidth " + pageWidth)

			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
			return arrayPageSize;
		}
	}
);