<?php
/******************************************
# LIGHTBOX - version 2 - 16.08.2008
# Auteur : Julien Theler - www.twiip.ch
# Contact : julien.theler@twiip.ch
# Licence : CC-by-nc
******************************************/

/**************************/
//Variables de configuration
define('MAXWIDTH', 800); //largeur maximale d'affichage de l'image
define('MAXHEIGHT', 600); //hauteur maximale d'affichage de l'image
define('LIGHTBOXPATH', '/scripts/lightbox/'); //Chemin absolu du dossier contenant les fichiers de la librairie
/**************************/

//Autres constantes
define('BASEDIR', 'http://'.$_SERVER['HTTP_HOST']);

//Fonction appliquant ces variables au code javascript généré
function maxSizes($image){
	list($width, $height, $type, $attr) = getimagesize(BASEDIR.$image);
	if($width > MAXWIDTH){
		$height = (MAXWIDTH/$width)*$height;
		$width = MAXWIDTH;
	}
	if($height > MAXHEIGHT){
		$width = (MAXHEIGHT/$height)*$width;
		$height = MAXHEIGHT;
	}
	return array('width' => ceil($width), 'height' => ceil($height));
}
/**************************/

if(isset($_GET['url'])){
/************* Vérification de la taille d'une image *************/
	$dimensions = maxSizes($_GET['url']);
	$width = $dimensions['width'];
	$height = $dimensions['height'];
	header ("content-type: text/xml");
	echo '<?phpxml version="1.0" encoding="UTF-8" ?>'."\n";
	echo '	<dimensions>'."\n";
	echo '		<width>'.$width.'</width>'."\n";
	echo '		<height>'.$height.'</height>'."\n";
	echo '	</dimensions>'."\n";
}


else{
/******************* Déclaration de la class*******************/
	class Lightbox{
		//Insère la div servant à griser l'écran et celle destinée à contenir les box
		function Lightbox(){
			echo '<script language="javascript">'."\n";
			echo '	//Si les fichiers et les div ont déjà été ajoutées, on ne fait rien'."\n";
			echo '	if(!document.getElementById(\'lightbox\')){'."\n";
			echo '		if(isIE()){document.body.onload = function(){initLightbox(\''.LIGHTBOXPATH.'\')};}'."\n";
			echo '		else{document.body.setAttribute("onload","initLightbox(\''.LIGHTBOXPATH.'\')");}'."\n";
			echo '	}'."\n";
			echo '</script>';
		}
		
		//Affiche un lien ouvrant une lightbox avec une image
		function lightboxImage($lien, $image, $titre=''){
			$dimensions = maxSizes($image);
			$width = $dimensions['width'];
			$height = $dimensions['height'];
			$titres_js = Lightbox::titresInArray($titre);
			
			$lienlb = '<a onClick="setVariables(\'\', \''.$image.'\', \''.$width.'\', \''.$height.'\', \'\', \'0\', '.$titres_js.'); openbox(\'image\'); return false;" href="'.$image.'" target="blank">';
			$lienlb .= $lien;
			$lienlb .= '</a>';
			return $lienlb;
		}
		
		//Affiche l'image selon la taille spécifiée avec un lien ouvrant une lightbox
		function lightboxImageMini($image, $widthMini=null, $heightMini=null, $titre=''){
			$dimensions = maxSizes($image);
			$width = $dimensions['width'];
			$height = $dimensions['height'];
			$titres_js = Lightbox::titresInArray($titre);
			
			$mini = '<a onClick="setVariables(\'\', \''.$image.'\', \''.$width.'\', \''.$height.'\', \'\', \'0\', '.$titres_js.'); openbox(\'image\'); return false;" href="'.$image.'" target="blank">';
			$mini .= '<img src="'.$image.'" border="0"'.($widthMini ? ' width="'.$widthMini.'px"' : '').($heightMini ? ' height="'.$heightMini.'px"' : '').' />';
			$mini .= '</a>';
			return $mini;
		}

		//Affiche un lien ouvrant une lightbox avec une image et des liens de navigation
		function lightboxImageArray($lien, $array, $position=0, $titres=''){
			$dimensions = maxSizes($array[$position]);
			$width = $dimensions['width'];
			$height = $dimensions['height'];
			$array_js = 'new Array(\''.implode($array, '\', \'').'\')';
			$titres_js = Lightbox::titresInArray($titres);
			
			$lienlb = '<a onClick="setVariables(\'\', \''.$array[$position].'\', \''.$width.'\', \''.$height.'\', '.$array_js.', \''.$position.'\', '.$titres_js.'); openbox(\'image\'); return false;"  href="'.$array[$position].'" target="blank">';
			$lienlb .= $lien;
			$lienlb .= '</a>';
			return $lienlb;
		}
		
		//Affiche l'image selon la taille spécifiée avec un lien ouvrant une image et des liens de navigation
		function lightboxImageArrayMini($image, $array, $position=0, $widthMini=null, $heightMini=null, $titres=''){
			$dimensions = maxSizes($array[$position]);
			$width = $dimensions['width'];
			$height = $dimensions['height'];
			$array_js = 'new Array(\''.implode($array, '\', \'').'\')';
			$titres_js = Lightbox::titresInArray($titres);
			
			$mini = '<a onClick="setVariables(\'\', \''.$array[$position].'\', \''.$width.'\', \''.$height.'\', '.$array_js.', \''.$position.'\', '.$titres_js.'); openbox(\'image\'); return false;"  href="'.$array[$position].'" target="blank">';
			$mini .= '<img src="'.$image.'" border="0"'.($widthMini ? ' width="'.$widthMini.'px"' : '').($heightMini ? ' height="'.$heightMini.'px"' : '').' />';
			$mini .= '</a>';
			return $mini;
		}
		
		//Affiche un lien ouvrant une lightbox avec du texte
		function lightboxContenu($lien, $contenu, $width=MAXWIDTH, $titre=''){
			$titres_js = Lightbox::titresInArray($titre);
		
			$lienlb = '<a onClick="setVariables(\''.urlencode(utf8_encode($contenu)).'\', \'\', \''.$width.'\', \'\', \'\', \'0\', '.$titres_js.'); openbox(\'contenu\'); return false;" href="javascript:;">';
			$lienlb .= $lien;
			$lienlb .= '</a>';
			return $lienlb;
		}
		
		//Transforme un titre ou un tableau PHP de titres en une array js au contenu encodé
		function titresInArray($titres){
			if(!is_array($titres)){
				$titres = array($titres);
			}
			$i = 0;
			foreach($titres as $titre){
				$titres[$i] = urlencode($titre);
				$i++;
			}
			$titres_js = 'new Array(\''.implode($titres, '\', \'').'\')';
			return $titres_js;
		}
	}
}
?>