var mPopup = {
	
	opened: false,
	masque: null,
	texte: null,
	contener: null,
	title: null,
	
	load_image_src: LOCAL_PATH+'images/loading.gif',
	close_image_src: LOCAL_PATH+'images/closelabel.gif',
	
	create: function (url, params, method, title) {
		// Close popup if opened
		if (this.opened) {
			this.close();
		}
		var body = document.getElementsByTagName("body").item(0);
		this.masque = document.createElement("div");
		this.masque.setAttribute('id', 'masque');
		var pageSizes = this.getPageSize();
		this.masque.style.height = pageSizes[1]+'px';
		body.appendChild(this.masque);
		this.contener = document.createElement('div');
		this.contener.setAttribute('id', 'contener');
		var left = (pageSizes[0] / 2) - 250;
		var top = this.getYScroll() + 100;
		this.contener.style.top = top+'px';
		this.contener.style.left = left+'px';
			var close = document.createElement('img');
			close.className = 'close';
			close.src = this.close_image_src;
			close.onclick = this.close;
			this.contener.appendChild(close);
			this.title = document.createElement('h2');
			this.title.setAttribute('id', 'title');
			this.title.innerHTML = title;
			this.contener.appendChild(this.title);
			this.texte = document.createElement('div');
			this.texte.setAttribute('id', 'texte');
			this.texte.innerHTML = '<p align="center"><img class="load" src="'+this.load_image_src+'" /></p>';
			this.contener.appendChild(this.texte);
		body.appendChild(this.contener);
			
		// Ajoutes l'évenement clavier (pour touche ECHAP)
		this.addElementEventListener(document, 'keyup', this.keyboardAction);
		this.addElementEventListener(this.masque, 'click', this.close);
		
		this.getData(url, params, method);
		
		this.opened = true;
	},
	
	getData: function (url, params, method) {
		
		var xhr = this.mkXhr();
	    
	    xhr.open(method, url, true);
	    xhr.onreadystatechange = function() {
	        if (xhr.readyState == 4) {
	            mPopup.texte.innerHTML = xhr.responseText;
	        }
	    }
	    xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	    xhr.send(params);
	},
	
	mkXhr: function () {
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		}
	    else if (window.ActiveXObject) {
	    	return new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    else {
	        alert('JavaScript : votre navigateur ne supporte pas les objets XMLHttpRequest...');
	        return false;
	    }
	},
	
	close: function () {
		var body = document.getElementsByTagName("body").item(0);
		body.removeChild(mPopup.masque);
		body.removeChild(mPopup.contener);
		mPopup.opened = false;
	},
	
	getPageSize: function () {
	
		var xScroll, yScroll, windowWidth, windowHeight;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			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;
		}
		if (self.innerHeight) {	// all except Explorer
			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 = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	},
	
	getYScroll: function (){

		var yScroll;
	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		return yScroll;
	},
	
	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
		}

		key = String.fromCharCode(keycode).toLowerCase();
		
		if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){	// close lightbox
			mPopup.close();
		}
	},
	
	addElementEventListener: function (el, type, myFunction) {
        if (el && el.addEventListener) {
            el.addEventListener (type,myFunction,false);
        } else if (el && el.attachEvent) {
            el && el.attachEvent ('on'+type,myFunction);
        } else if (el) {
            el['on'+type] = myFunction;
        } else {
            return false;
        }
        
        return true;
    }
	
};