$(document).ready(function() {
	/*
	$("body").keyup(function() {		
		alert(event.type);
	});
	
	$("body").keyup(function() {
		if(window.event.type == "keyup" && window.event.keycode==27) {
			$(document.getElementById('more')).slideToggle("slow", function() {
				$("div#filtre").css("display","none");
				//document.body.style.overflow = "auto";
				var htmlElement = document.getElementsByTagName('html')[0];
				htmlElement.style.overflow = 'auto';			
			});
		}
	});
	
	$("div").keyup(function() {
		if(window.event.type == "keyup" && window.event.keycode==27) {
			$(document.getElementById('more')).slideToggle("slow", function() {
				$("div#filtre").css("display","none");
				//document.body.style.overflow = "auto";
				var htmlElement = document.getElementsByTagName('html')[0];
				htmlElement.style.overflow = 'auto';			
			});
		}
	});	
	*/
	
	$("a#more_fiches").click(function() {
		current = $(this);
		idDiv = $(this).parent('.bottom_link').attr("id");
		
		var objOverlay      = document.getElementById('filtre');
		var objLoadingImage = document.getElementById('load');
		var objLightbox     = document.getElementById('more');
		var arrayPageSize   = getPageSize();
		var arrayPageScroll = getPageScroll();						
		
		// affiche le filtre noir pour qu'il occupe toute la page
		objOverlay.style.height = (arrayPageSize[1] + 'px');				
		$("div#filtre").css("display","block");

		// affiche l'image de loading		
		document.getElementById('load').innerHTML = '<img src="img/site/loading.gif"/><div>chargement en cours</div>';
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 35) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - 35) / 2) + 'px');		
		$("div#load").css("display","block");
		
		// définit la position de la lightbox pour qu'elle soit centrée				
		var lightboxTop = arrayPageScroll[1] + 50;		
		var lightboxLeft = ((arrayPageSize[2]) / 4);			
		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
				
		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');		
		
		pause(250);
		$.get("ajax/lettre.ajax.php?do=getFiches&id="+idDiv, function(data){			
   			document.getElementById('contenu_more').innerHTML = data;
   			$("div#load").css("display","none");												
			$(document.getElementById('more')).slideDown("slow", function() {
			});
		});
		
		return false;
	});
	
	$("a#more_livres").click(function() {
		current = $(this);
		idDiv = $(this).parent('.bottom_link').attr("id");
			
		var objOverlay      = document.getElementById('filtre');
		var objLoadingImage = document.getElementById('load');
		var objLightbox     = document.getElementById('more');
		var arrayPageSize   = getPageSize();
		var arrayPageScroll = getPageScroll();				
		
		// affiche le filtre noir pour qu'il occupe toute la page
		objOverlay.style.height = (arrayPageSize[1] + 'px');				
		$("div#filtre").css("display","block");
		
		// affiche l'image de loading		
		document.getElementById('load').innerHTML = '<img src="img/site/loading.gif"/><div>chargement en cours</div>';
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 35) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - 35) / 2) + 'px');		
		$("div#load").css("display","block");		
		
		// définit la position de la lightbox pour qu'elle soit centrée				
		var lightboxTop = arrayPageScroll[1] + 50;		
		var lightboxLeft = ((arrayPageSize[2]) / 4);		
		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
				
		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');				
		
		pause(250);
		//alert(idDiv);
		$.get("ajax/livres.ajax.php?do=getLivresFromThemes&id="+idDiv, function(data){			
   			document.getElementById('contenu_more').innerHTML = data;
   			$("div#load").css("display","none");												
			$(document.getElementById('more')).slideDown("slow", function() {				
			});
		});
				
		return false;
	});

	$("a.close_more").click(function() {
			$(document.getElementById('more')).slideToggle("slow", function() {
			$("div#filtre").css("display","none");
			//document.body.style.overflow = "auto";
			var htmlElement = document.getElementsByTagName('html')[0];
			htmlElement.style.overflow = 'auto';			
		});
		return false;
	});		
});


/************************************************************************************************************
Ajax dynamic list
Copyright (C) September 2005  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	
function showToolTip(e,text, id){
	if(document.all)e = event;	
	var obj = document.getElementById('bubble_tooltip');
	var obj2 = document.getElementById('bubble_tooltip_content');
	obj2.innerHTML = text;
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	//var leftPos = e.clientX + 20;
	var leftPos = e.clientX - 320;
	if(leftPos<0)leftPos = 0;
	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight -10 + st + 'px';
}	

function hideToolTip(id)
{
	document.getElementById('bubble_tooltip').style.display = 'none';	
}

function showToolTipLivre(e,titre,soustitre,de,editeur,text,id){	
	if(document.all)e = event;
	if(de=='') de = 'n/a';
	if(editeur=='') editeur = 'n/a';
	var obj = document.getElementById('bubble_tooltip');
	var obj2 = document.getElementById('bubble_tooltip_content');
	obj2.innerHTML = '<h1>'+titre+'</h1>'+'<p class="livre_soustitre">'+soustitre+'</p>'+'<p>'+text+'</p>';	
	obj.style.visibility = 'hidden';
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	//var leftPos = e.clientX + 5;
	var leftPos = e.clientX - 320;
	if(leftPos<0)leftPos = 0;
	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight -10 + st + 'px';	
	obj.style.visibility = 'visible';
}	

function hideToolTipLivre(id)
{
	document.getElementById('bubble_tooltip').style.display = 'none';	
}


/************************************************************************************************************
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)
	
	Table of Contents
	-----------------
	Configuration
	
	Functions
	- getPageScroll()
	- getPageSize()
	- pause()
	- getKey()
	- listenKey()
	- showLightbox()
	- hideLightbox()
	- initLightbox()
	- addLoadEvent()
	
	Function Calls
	- addLoadEvent(initLightbox)

************************************************************************************************************/

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = 'loading.gif';		
var closeButton = 'close.gif';


// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
function getPageScroll() 
{
	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;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	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;
	}	
	
	// 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;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objCaption = document.getElementById('lightboxCaption');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLightboxDetails = document.getElementById('lightboxDetails');

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	// preload image
	imgPreload = new Image();

	imgPreload.onload=function(){
		objImage.src = objLink.href;

		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


		objLightboxDetails.style.width = imgPreload.width + 'px';
		
		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			//objCaption.style.width = imgPreload.width + 'px';
			objCaption.innerHTML = objLink.getAttribute('title');
		} else {
			objCaption.style.display = 'none';
		}
		
		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		} 

		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }

		// Hide select boxes as they will 'peek' through the image in IE
		selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }

	
		objLightbox.style.display = 'block';

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		
		// Check for 'x' keypress
		listenKey();

		return false;
	}

	imgPreload.src = objLink.href;
	
}





//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}
