/***************************/
//@Author: Michel Urvoy
//@Date: 01-11-2008
//@website: www.pixiwave.com
//@email: infos@pixiwave.com
//@license: Feel free to use it, but keep this credits please!	
/***************************/
(function($j){
	$j.fn.extend({
		modalPanel: function() {
			//Create our overlay object
			var overlay = $j("<div id='modal-overlay'></div>");
			//Create our modal window
			var modalWindow = $j("<div id='modal-window'></div>");
			//0 means disabled; 1 means enabled;
			var modalStatus = 0;
			var isIe6 = 0;
			
			return this.each(function() {
				//Listen for clicks on objects passed to the plugin
				$j(this).click(function(e) {
					//Prevent the anchor link from loading
					e.preventDefault();
					this.blur();
					
					if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
						$j("body","html").css({height: "100%", width: "100%"});
						$j("html").css("overflow","hidden");
						isIe6 = 1;
						
					};
					
					//Activate a listener 
					$j(document).keydown(handleEscape);	
					
					//RESIZING THE WINDOW
					$j(window).resize(centerModal);
					
					if(modalStatus==0){
						modalStatus = 1;
						//Display the overlay
						createOverlay();
						//Display the loader
						createLoader();
						//Grab content url
						var contentUrl = $j(this).attr("href");
						//Display the modal window
						createModal(contentUrl);
					};
				});
			});
			
			function createOverlay(){
				//Set the css and fade in our overlay
				overlay.css("opacity", 0.8).fadeIn(150);
				//Append the overlay to the document body
				$j("body").append(overlay.click( function() { modalHide(); }));
			};
			//Our function for hiding the modalbox
			function modalHide() {
				//disables popup only if it is enabled
				if(modalStatus==1){
					modalStatus = 0;
					$j(document).unbind("keydown", handleEscape);
					var remove = function() { $j(this).remove(); };
					
					overlay.fadeOut(remove);
					modalWindow.fadeOut(remove).empty();
					$j("#modal-load").remove();
				};
			};
			function createLoader(){
				//Add a loader to our page
				$j("#logo").before("<div id='modal-load'></div>");
			};
			
			function createModal(contentUrl){
				var modalClose = '<div id="modal-close">Fermer</div>';
				var modalContent = '<div id="modal-content"></div>';
				//Insert elements in modal window
				modalWindow.append(modalClose).append(modalContent);
				//Insert the layers into the DOM
				$j("body").append(modalWindow);
				//Activate a listener 
				$j("#modal-close").click(modalHide);
				//Center in window
				centerModal();
				//Load content in modal window
				$j("#modal-content").load(contentUrl);
				//Show modal
				modalWindow.fadeIn("slow");
			};
			//centering modal
			function centerModal(){
				//request data for centering
				var modalHeight = modalWindow.height();
				var modalWidth = modalWindow.width();
				
				var pageXOffset = document.documentElement.scrollLeft;
				var pageYOffset = document.documentElement.scrollTop;
				var innerHeight = document.documentElement.clientHeight;
				var innerWidth = document.documentElement.clientWidth;
				
				//centering
				modalWindow.css({
					"position": "absolute",
					"top": pageYOffset + (innerHeight/2-modalHeight/2),
					"left": pageXOffset + (innerWidth/2-modalWidth/2)
				});
			};
			
			//Our function that listens for escape key.
			function handleEscape(e) {
				if (e.keyCode == 27) {
					modalHide();
				};
			};
		}
	});
})(jQuery);

/*
$j(document).ready(function(){     
	//INSERT SLIDEMENU
	//slideMenu(30000);
	//ASSIGN BEHAVIOUR TO LINKS
	var contentUrl = "http://www.tirel-guerin.com/Jscripts/modals/visit.asp";
	$j('a[@rel*=modalPanel]').modalPanel(contentUrl);         
});
*/