	/**
	 * Manage the popup
	 * 
	 * @author Marc Richard, <marc@belenvol.fr>
	 * @version 1.0.0
	 */

	var Popup = Class.create();
	
	var maskSelect = false;
	
	var background_init =false; //let's know if the popup is
	
	Popup.prototype = {
		overlayOpacity: 0.5, 		// controls transparency of shadow overlay
		
		overlayDuration: 0.2, 		//overlay duration effect
		
		contact_background: null, 	//HTML background object
		
		popupView: 'popup_content',			//Popup ID
		
		popupContentBody: 'popup_body',		//Popup body content ID
		
		popupContentStorageView: 'popup_storage_view',		//Popup storage view content ID
		
		popupContentToInsert: null,	//Popup content ID to add to the popup
	    
	    // initialize()
	    // Constructor runs on completion of the DOM loading. Calls
	    // the function inserts html at the bottom of the page which is used to display the shadow 
	    // overlay and the image container.
		//
		// @param source : link to open the popup
		// @param popupContentToInsert : Popup body content ID
	    //
	    initialize: function(source, popupContentToInsert) {
			var content = document.getElementById(source);
			
			this.popupContentToInsert = popupContentToInsert;
			
			if( content && !background_init ){
				this.keyboardAction = this.keyboardAction.bindAsEventListener(this);

		        //Create the overlay
		        var objBody = $$('body')[0];
		        var div = Builder.node('div',{id:'popup_background'});
		        div.className = 'popup_background';
				objBody.appendChild(div);
				objBody.appendChild($(this.popupView));
				
				this.popup_background = $('popup_background');

				$(this.popup_background).hide();
				$(this.popup_background).observe('click', (function() { this.end(); }).bind(this));

				var th = this;
		        (function(){
		            var ids = 'popup_background';   
		            $w(ids).each(function(id){ th[id] = $(id); });
		        }).defer();
		        
		        background_init = true;
			}else{
				if( background_init ){
					this.popup_background = $('popup_background');

					$(this.popup_background).hide();
					$(this.popup_background).observe('click', (function() { this.end(); }).bind(this));
				}
			}
			
			if( content && $(source) ){

		        //Add click listner - open
		        document.observe('click', (function(event){
		        	 var target = event.findElement('a[id^='+source+']');
		             if (target) {
		                 event.stop();
		                 this.start();
		             }else{
		            	 var target = event.findElement('li[id^='+source+']');
			             if (target) {
			                 event.stop();
			                 this.start();
			             } 
		             }
		        }).bind(this));
			}
		},
		
		/**
		 * Add close listner
		 * 
		 * @author Marc Richard, <marc@belenvol.fr>
		 * @version 1.0.0
		 */
		addCloseListner: function(){
	        //Add click listner - open
	        document.observe('click', (function(event){
	             var target = event.findElement('div[id^='+this.popupView+']');
	             if (target) {
	                 this.cancelEvt(event);
	             }
	             
	             var target = event.findElement('div[id^=popup_close2]');
	             if (target) {
	                 this.closePopup(event,target);
	             }
	             
	             var target = event.findElement('div[id^=popup_close]');
	             if (target) {
	                 this.closePopup(event,target);
	             }
	        }).bind(this));
		},
		
		/**
		 * Remove close listner
		 * 
		 * @author Marc Richard, <marc@belenvol.fr>
		 * @version 1.0.0
		 */
		removeCloseListner: function(){
	        //Add click listner - open
	        document.stopObserving('click', (function(event){
	             var target = event.findElement('div[id^='+this.popupView+']');
	             if (target) {
	                 this.cancelEvt(event);
	             }
	             
	             var target = event.findElement('div[id^=popup_close2]');
	             if (target) {
	                 this.closePopup(event,target);
	             }
	             
	             var target = event.findElement('div[id^=popup_close]');
	             if (target) {
	                 this.closePopup(event,target);
	             }
	        }).bind(this));
		},
		
		/**
		 * start Open the popup view
		 * 
		 * @author Marc Richard, <marc@belenvol.fr>
		 * @version 1.0.0
		 */
		start: function(){
			this.addCloseListner();
			
			if( maskSelect ){
				$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
			}
	        
			//define popup content
			$(this.popupContentBody).appendChild($(this.popupContentToInsert));
	        
	        // stretch overlay to fill page and fade in
	        var arrayPageSize = this.getPageSize();
	        this.popup_background.setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
	        
			$(this.popupView).style.top = (arrayPageSize[1] - $(this.popupView).getHeight())/2 + 'px';
			$(this.popupView).style.left = (arrayPageSize[0] - $(this.popupView).getWidth())/2 + 'px';
			
			
	        new Effect.Appear(this.popup_background, { duration: this.overlayDuration, from: 0.0, to: this.overlayOpacity , afterFinish: this.displayConcactPopup(this.popupView) });
	        
			this.enableKeyboardNav();
		},
		
		/**
		 * 
		 * Display popup contact content
		 * @author Marc Richard, <marc@belenvol.fr>
		 * @version 1.0.0
		 * @param Contact Id view
		 */
		displayConcactPopup: function(popupView){
			$(popupView).style.display = 'block';
		},
		
		/**
		 * Close the popup view
		 * 
		 * @author Marc Richard, <marc@belenvol.fr>
		 * @version 1.0.0
		 */
		closePopup: function(evt,relatedTarget){
			this.addCloseListner();
			
			if( evt && evt.srcElement && evt.srcElement.id == relatedTarget.id ){ //fix IE bug
				this.end();
			}else{
				if( !evt || !evt.srcElement ){
					this.end();
				}
			}

			//store popup content in the parking
			$(this.popupContentStorageView).appendChild($(this.popupContentToInsert));
			
			return false;
		},

	    //
	    //  end()
	    //
	    end: function() {
	        this.disableKeyboardNav();
	        $(this.popupView).style.display = 'none';
	        new Effect.Fade(this.popup_background, { duration: this.overlayDuration });
			
			if( maskSelect ){
				$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
			}

			//store popup content in the parking
			$(this.popupContentStorageView).appendChild($(this.popupContentToInsert));
	        
	    },

	    //
	    //  enableKeyboardNav()
	    //
	    enableKeyboardNav: function() {
	        document.observe('keydown', this.keyboardAction); 
	    },

	    //
	    //  disableKeyboardNav()
	    //
	    disableKeyboardNav: function() {
	        document.stopObserving('keydown', this.keyboardAction); 
	    },
		
		/**
		 * Listen key press
		 * 
		 * @author Marc Richard, <marc@belenvol.fr>
		 * @version 1.0.0
		 */
	    keyboardAction: function(e){ 
			var kC  = (window.event) ?    // MSIE or Firefox?
	            event.keyCode : e.keyCode;
			var Esc = (window.event) ?   
	              27 : e.DOM_VK_ESCAPE // MSIE : Firefox
	        
	        if(kC==Esc){
	        	document.onkeydown = null;
		    	this.end();
		     } 
		},
		
		//
	    //  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+250; //+250 FIX IE BUG
			}
			
			var windowWidth, windowHeight;
			
			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;
			}
		
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}

			return [pageWidth,pageHeight,windowHeight];
		},
		
		/**
		 * Stop the mouse event dispatching.
		 * 
		 * @author Marc Richard, <marc@belenvol.fr>
		 * @version 1.0.0
		 */
		cancelEvt: function(evt){
			if( !evt.srcElement ){ //doesn't wok with IE
				evt.stopPropagation();
			}
		}
	}
	
	document.observe('dom:loaded', function () { 
		new Popup('menu_contact_a','bloc_contact'); //contact editor
	});