/*
 * Using Lightbox :
 *
 * CSS information
 *
 * lightboxOverlay : design the div which will hide the page
 * #lightboxOverlay{	
 *   background: #000;
 *   filter:alpha(opacity=80);	//IE
 *   -moz-opacity:0.8;					//Gecko
 *   opacity:.80; 							//Standard
 *   position: absolute;
 *   top: 0px;
 *   left: 0px;
 *   z-index: 90;
 *   width: 100%;
 *   height: 100%;
 * }
 * lightbox : box which will be under the real content box
 * #lightbox{
 *   background:#FFFFFF;
 *   margin: 0;
 *   padding: 0;
 *   border:1px solid #000000;
 *   overflow: auto;
 * }
 * lightboxContents : real box which handles the content
 * #lightbox #lightboxContents {
 *   padding:10px;
 * }
 *
 * Lightbox usage
 *
 * Lightbox needs prototype (tested with release 1.4.0_rc4 and 1.5.1_rc2
 * <script language="javascript" src="prototype.js"></script>
 * <script language="javascript" src="lightbox.js"></script>
 * <script language="javascript">
 *   Event.observe(window, 'load', Lightbox.init, false);
 * </script>
 *
 * Lightbox.showString('string to display', lbWidth, lbHeight, isModal);
 * Lightbox.showImage('image url'); // cannot be modal
 * Lightbox.showElement('element id', lbWidth, lbHeight, isModal);
 * Lightbox.showBoxByAJAX('ajax url', lbWidth, lbHeight, isModal);
 * Lightbox.hideBox(); // needed to close a modal box (see bellow)
 * 
 * Parameters : 
 *  lbWidth, lbHeight : size of the lightbox
 *  isModal (true/false) : 
 *    true : the box must have a "close" button
 *    false : the box can be closed by clicking on the overlay div
 *
 * TODO: 
 *   1. finish utility functions to easily handle unobstrusive 'Popups'
 *   2. find better naming scheme
 * bindShowString('elementToAttachTo', 'TextToDisplay');
 * bindShowImage('elementToAttachTo', 'href');
 * bindShowElement('elementToAttachTo', 'idOfHiddenElement');
 * bindShowAjax('elementToAttachTo', 'href');

 */
var Lightbox = {
	lightboxType : null,
	lightboxCurrentContentID : null,
	lightboxIsModal : false,
	lightboxLoaded : false,
	bhideBox : null,
	showString : function(content, boxWidth, boxHeight, isModal){
		if(isModal == true) this.lightboxIsModal = true; else this.lightboxIsModal = false;
		this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		var contents = $('lightboxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},
	showAlert : function(content, strOk, boxWidth, boxHeight){
		this.lightboxIsModal = true;
		this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		if(strOk !== undefined)
			$('lightboxAlertOk').innerHTML = strOk;
		Element.show($('lightboxAlert'));
		var contents = $('lightboxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},
	closeAlert : function(status) {
		Lightbox.hideBox();
		Element.hide($('lightboxAlert'));
		return(status);
	},
	showConfirm : function(content, strYes, strNo, boxWidth, boxHeight){
		this.lightboxIsModal = true;
		this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		if(strYes !== undefined)
			$('lightboxConfirmYes').innerHTML = strYes;
		if(strNo !== undefined)
			$('lightboxConfirmNo').innerHTML = strNo;
		Element.show($('lightboxConfirm'));
		var contents = $('lightboxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},
	closeConfirm : function(status) {
		Lightbox.hideBox();
		Element.hide($('lightboxConfirm'));
		return(status);
	},
	showImage : function(href) {
		this.lightboxIsModal = false;
		this.lightboxType = 'image';
		var contents = $('lightboxContents');
		var objImage = document.createElement("img");
		objImage.setAttribute('id','lightboxImage');
		contents.appendChild(objImage);
		imgPreload = new Image();
		imgPreload.onload=function(){
			objImage.src = href;
			Lightbox.showBox();
		}
		imgPreload.src = href;
		return false;
	},
	showElement : function(id, boxWidth, boxHeight, isModal) {
		if(isModal == true) this.lightboxIsModal = true; else this.lightboxIsModal = false;
		this.lightboxType = 'id';
		this.lightboxCurrentContentID = id;
		this.setLightboxDimensions(boxWidth, boxHeight);
		var element = $(id);
		var contents = $('lightboxContents');
		contents.appendChild(element);
		Element.show(id);
		this.showBox();
		return false;
	},
	showBoxByAJAX : function(href, boxWidth, boxHeight, isModal) {
		if(isModal == true) this.lightboxIsModal = true; else this.lightboxIsModal = false;
		this.lightboxType = 'ajax';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('lightboxContents');
		var myAjax = new Ajax.Updater(contents, href, {method: 'get'});
		this.showBox();
		return false;
	},
	setLightboxDimensions : function(width, height) {
		var windowSize = this.getPageDimensions();
		if(width && $('lightbox')!=null) {
			if(width < windowSize[0]) {
				$('lightbox').style.width = width + 'px';
			} else {
				$('lightbox').style.width = (windowSize[0] - 50) + 'px';
			}
		}
		if(height && $('lightbox')!=null) {
			if(height < windowSize[1]) {
				$('lightbox').style.height = height + 'px';
			} else {
				$('lightbox').style.height = (windowSize[1] - 50) + 'px';
			}
		}
	},
	showBox : function() {
		this.toggleSelects('hidden');
		Element.show('lightboxOverlay');
		this.center('lightbox');
		if(!this.lightboxIsModal) {
			Event.observe($('lightboxOverlay'), 'click', Lightbox.bhideBox);
		} 
		return false;
	},
	hideBox : function(){
		if(!this.lightboxIsModal) {
			Event.stopObserving($('lightboxOverlay'), 'click', Lightbox.bhideBox);
		}
		var contents = $('lightboxContents');
		if(this.lightboxType == 'id') {
			var body = document.getElementsByTagName("body").item(0);
			Element.hide(this.lightboxCurrentContentID);
			body.appendChild($(this.lightboxCurrentContentID));
		}
		contents.innerHTML = '';
		$('lightbox').style.width = null;
		$('lightbox').style.height = null;
		this.toggleSelects('visible');
		Element.hide('lightbox');
		Element.hide('lightboxOverlay');
		return false;
	},
	toggleSelects: function(visibility){
		selects = document.getElementsByTagName('select');
			for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	getPageDimensions : function(){
		var xScroll, yScroll;
	
		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;
		}
		var windowWidth, windowHeight;
		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;
		}	
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight) 
		return arrayPageSize;
	},
	center : function(element){
		try{
			element = document.getElementById(element);
		}catch(e){
			return;
		}
		var windowSize = this.getPageDimensions();
		var window_width  = windowSize[0];
		var window_height = windowSize[1];
		$('lightboxOverlay').style.height = windowSize[3] + 'px';
		element.style.position = 'absolute';
		element.style.zIndex   = 99;
	
		var scrollY = 0;
	
		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}
	
		var elementDimensions = Element.getDimensions(element);
		var setX = ( window_width  - elementDimensions.width  ) / 2;
		var setY = ( window_height - elementDimensions.height ) / 2 + scrollY;
	
		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;
	
		element.style.left = setX + "px";
		element.style.top  = setY + "px";
		Element.show(element);
	},
	init : function() {				  
		var lightboxtext = '<div id="lightboxOverlay" style="display:none"></div>';
		lightboxtext += '<div id="lightbox" style="display:none">';
		lightboxtext += '<div id="lightboxContents"></div>';
		lightboxtext += '<div id="lightboxConfirm" style="display:none">';
		lightboxtext += '<a href="javascript:void(0);" id="lightboxConfirmYes" onClick="Lightbox.closeConfirm(true);" />Oui</a>';
		lightboxtext += '<a href="javascript:void(0);" id="lightboxConfirmNo" onClick="Lightbox.closeConfirm(false);" />Non</a>';
		lightboxtext += '</div>';
		lightboxtext += '<div id="lightboxAlert" style="display:none">';
		lightboxtext += '<a href="javascript:void(0);" id="lightboxAlertOk" onClick="Lightbox.closeAlert(true);" />Fermer</a>';
		lightboxtext += '</div>';
		lightboxtext += '</div>';
		var body = document.getElementsByTagName("body").item(0);
		new Insertion.Bottom(body, lightboxtext);
		Lightbox.bhideBox = Lightbox.hideBox.bindAsEventListener(Lightbox);
		this.lightboxLoaded = true;
	}
}
/*
 * Utility functions
 */
// simple attach to element 
function bindShowString(eventElement, text) {
	Event.observe(window, 'load', function() {
		Event.observe($(eventElement), 'click', function(evt) {
			Event.stop(evt);
			var myEl = Event.element(evt);
			Lightbox.showString(text, 300, 200, false);
		});
	});
}
function bindShowElement(eventElement, targetElement) {
	Event.observe(window, 'load', function() {
		Event.observe($(eventElement), 'click', function(evt) {
			Event.stop(evt);
			var myEl = Event.element(evt);
			Lightbox.showElement(targetElement, 350, 300, false);
		});
	});
}
function bindShowImage(eventElement, parentAttribute) {
	if(parentAttribute === undefined)
		parentAttribute = 'href';
	Event.observe(window, 'load', function() {
		Event.observe($(eventElement), 'click', function(evt) {
			Event.stop(evt);
			Lightbox.showImage(Event.element(evt).ancestors()[0].readAttribute(parentAttribute));
		});
	});
}
function bindShowAjax(eventElement, targetAttribute) {
	if(targetAttribute === undefined)
		targetAttribute = 'href';
	Event.observe(window, 'load', function() {
		Event.observe($(eventElement), 'click', function(evt) {
			Event.stop(evt);
			var myEl = Event.element(evt);
			Lightbox.showBoxByAJAX(Event.element(evt).readAttribute(targetAttribute), 300, 300, true);
		});
	});
}

function alert(alertString, boxWidth, boxHeight, closeString) {
	if(boxWidth === undefined)
		boxWidth = 300;
	if(boxHeight === undefined)
		boxHeight = 200;
	Lightbox.showAlert(alertString, closeString, boxWidth, boxHeight);
}
function confirm(alertString, boxWidth, boxHeight, yesString, noString) {
	if(boxWidth === undefined)
		boxWidth = 300;
	if(boxHeight === undefined)
		boxHeight = 200;
	Lightbox.showConfirm(alertString, yesString, noString, boxWidth, boxHeight)
}

