// LightBox.js
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Historique de mise à jour
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// 2009-01-26 : Vincent
//				Création du script



function LightBox(lightboxID, params) {
	var lightboxContainer = null;
	
	if ( lightboxContainer = document.getElementById(lightboxID) ) {
		
		// Properties
		this.lightbox      = null;
		this.lightboxID    = "LightBox-" + lightboxID;
		this.background    = null;
		this.contentWindow = null;
		this.content       = null;
		this.closeButton   = null;
		
		this.params   = typeof(params) == "object" ? params : new Object();
		this.lightboxContents = lightboxContainer.innerHTML; // Creates a copy of the content
		
		// Sets default properties parameters if none are defined
		if ( this.params.closeButtonLabel == undefined )
			this.params.closeButtonLabel = "Close Window";
		if ( this.params.backgroundOpacity == undefined )
			this.params.backgroundOpacity = .8;
		if ( this.params.useFade == undefined )
			this.params.useFade = true;
		if ( this.params.fadeDuration == undefined )
			this.params.fadeDuration = 400;
		
		// Methods
		this.create        = LightBox_Create;
		this.openWindow    = LightBox_Open;
		this.closeWindow   = LightBox_Close;
		this.resizeWindow  = LightBox_Resize;
		this.deleteContent = LightBox_DeleteContent;
		
		// Removes the container
		lightboxContainer.parentNode.removeChild(lightboxContainer);
	}
}



// Opens the LightBox
function LightBox_Open() {
	
	if ( !this.lightbox )
		this.create();
	
	var lightboxObject = this;
	
	this.content.innerHTML = this.lightboxContents;
	document.documentElement.getElementsByTagName("body")[0].appendChild(this.lightbox);
	this.resizeWindow();
	
	if ( this.params.useFade ) {
		/*
		var fxObject = { duration: this.params.fadeDuration, wait: false };
		if ( this.params.shownedFunction != undefined )
			fxObject.onComplete = function() { lightboxObject.params.shownedFunction(); };
		var fx = new Fx.Styles( this.lightbox, fxObject );
		fx.start({'opacity': 1});
		*/
		
		var shownedFunc = function() { lightboxObject.lightbox.style.filter = ""; };
		if ( this.params.shownedFunction != undefined )
			shownedFunc = function() { lightboxObject.params.shownedFunction(); lightboxObject.lightbox.style.filter = ""; };
		$("#" + this.lightboxID).animate({opacity: 1}, 500, shownedFunc);
	} else {
		/*
		this.lightbox.style.opacity = "1";
		this.lightbox.style.filter  = "alpha(opacity=100)";
		*/
		this.lightbox.style.opacity = "";
		this.lightbox.style.filter  = "";
		if ( lightboxObject.params.shownedFunction != undefined )
			lightboxObject.params.shownedFunction();
	}
}



// Closes the LightBox
function LightBox_Close() {
	
	var lightboxObject = this;
	
	if ( this.params.hideFunction != undefined )
		this.params.hideFunction();
	
	if ( this.params.useFade ) {
		if ( !document.all ) {
			/*
			var fxObject = { duration: this.params.fadeDuration, wait: false, onComplete: function() { lightboxObject.deleteContent(); } };
			var fx = new Fx.Styles(this.lightbox, fxObject );
			fx.start({'opacity': [1, 0]});
			*/
			$("#" + this.lightboxID).animate({opacity: 0}, 500, function() { lightboxObject.deleteContent(); });
		} else
			this.deleteContent();
	} else
		this.deleteContent();
}



// Deletes content from the page
function LightBox_DeleteContent () {
	var lightboxObject = this;
	
	if ( window.removeEventListener ) {
		window.removeEventListener("resize", function(e) { lightboxObject.resizeWindow(); }, false);
		window.removeEventListener("scroll", function(e) { lightboxObject.resizeWindow(); }, false);
	} else if ( window.detachEvent ) {
		window.detachEvent("onresize", function() { lightboxObject.resizeWindow(); } );
		window.detachEvent("onscroll", function() { lightboxObject.resizeWindow(); } );
	}
	
	this.lightbox.style.opacity = "0";
	this.lightbox.style.filter  = "alpha(opacity=0)";
	this.content.innerHTML = "";
	this.lightbox.parentNode.removeChild(this.lightbox);
}



// Creates the LightBox HTML
function LightBox_Create() {
	var lightboxObject = this;
	
	this.lightbox = document.createElement("div");
	this.lightbox.id             = this.lightboxID;
	this.lightbox.style.position = "absolute";
	this.lightbox.style.zIndex   ="100000";
	this.lightbox.style.opacity  = "0";
	this.lightbox.style.filter   = "alpha(opacity=0)";
	
	// LightBox Background (content overlay)
	this.background = document.createElement("div");
		this.background.className = "LightBox-Background";
		this.background.style.opacity = String(this.params.backgroundOpacity);
		this.background.style.filter  = "alpha(opacity=" + String(this.params.backgroundOpacity * 100) + ")";
		this.background.onclick = function() { lightboxObject.closeWindow(); }
	
	// Content Window
	this.contentWindow = document.createElement("div");
		this.contentWindow.className = "LightBox-Window png";
		if ( this.params.windowClassName )
			this.contentWindow.className += " " + this.params.windowClassName;
		this.contentWindow.style.position = "absolute";
		this.contentWindow.style.zIndex   = "100001";
	
	// Content
	this.content = document.createElement("div");
		this.content.className = "LightBox-Content";
		//this.content.innerHTML = this.lightboxContents;
	
	// Close Button
	this.closeButton = document.createElement("a");
		this.closeButton.href = "#";
		this.closeButton.className = "LightBox-CloseButton";
		var closeButtonSpan = document.createElement("span");
			closeButtonSpan.appendChild(document.createTextNode(this.params.closeButtonLabel));
		this.closeButton.appendChild(closeButtonSpan);
		this.closeButton.onclick = function () {
				lightboxObject.closeWindow();
				return false;
			};
	
	
	this.contentWindow.appendChild(this.closeButton);
	this.contentWindow.appendChild(this.content);
	
	this.lightbox.appendChild(this.background);
	this.lightbox.appendChild(this.contentWindow);
	
	var lightboxObject = this;
	if ( window.addEventListener ) {
		window.addEventListener("resize", function(e) { lightboxObject.resizeWindow(); }, false);
		window.addEventListener("scroll", function(e) { lightboxObject.resizeWindow(); }, false);
	} else if ( window.attachEvent ) {
		window.attachEvent("onresize", function() { lightboxObject.resizeWindow(); } );
		window.attachEvent("onscroll", function() { lightboxObject.resizeWindow(); } );
	}
}



// Resizes the LightBox to fit window size
function LightBox_Resize() {
	this.lightbox.style.width  = String(document.documentElement.clientWidth) + "px";
	this.lightbox.style.height = String(document.documentElement.clientHeight) + "px";
	this.lightbox.style.top    = window.pageYOffset != undefined ? String(window.pageYOffset) + "px" : document.documentElement.scrollTop + "px";
	this.lightbox.style.left   = window.pageXOffset != undefined ? String(window.pageXOffset) + "px" : document.documentElement.scrollLeft + "px";
	
	this.background.style.width  = this.lightbox.style.width;
	this.background.style.height = this.lightbox.style.height;
	
	this.contentWindow.style.left = String((document.documentElement.clientWidth / 2) - (this.contentWindow.offsetWidth / 2)) + "px";
	this.contentWindow.style.top  = String((document.documentElement.clientHeight / 2) - (this.contentWindow.offsetHeight / 2)) + "px";
}

