﻿/*
	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)

*/

var loadingImage = '/design/lightBox/ajax-loader3.gif';		
//var closeButton = '/design/lightBox/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;
	}

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//

function showLightbox()
	{
	onOffElements();
	// prep objects
	var objFondNoir = document.getElementById('fondNoir');
	var objLoadingImage = document.getElementById('loadingImage');
	
	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
	objFondNoir.style.height = (arrayPageSize[1] + 'px');
	objFondNoir.style.display = 'block';
	document.getElementById('conteneurAjax').style.display = 'block';
	document.getElementById('loadingImage').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);		
		
		// 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";
        	}

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objFondNoir.style.height = (arrayPageSize[1] + 'px');

		return true;
		}

	   
	
	}

function onOffElements()
	{ 
	var i = 1;
	var fin;
	if(document.getElementById('masqueLightBox1'))
		{
		do
			{
			if(document.getElementById('masqueLightBox'+i))
				{
				if(document.getElementById('masqueLightBox'+i).style.visibility == "hidden")
				document.getElementById('masqueLightBox'+i).style.visibility = "visible";
				else document.getElementById('masqueLightBox'+i).style.visibility = "hidden";
				}
			else fin = true;
			i++; 
			}
		while (fin != true);
		}	 

	} 
	
function onOffElementsSepares() //id's
	{ 
	for(var i = 0; i < arguments.length; i++)
		{
		if(document.getElementById(arguments[i]))
			{
			if(document.getElementById(arguments[i]).style.visibility == "hidden")
			document.getElementById(arguments[i]).style.visibility = "visible";
			else document.getElementById(arguments[i]).style.visibility = "hidden";
			}
		}
	}

	
function ajoutSelection(idBien)
	{
	resAjout = ajoutSelBien(idBien); // alert(resAjout);
	if(resAjout == 1) texte = "1 secteur ajouté"; else if(resAjout == 2) texte = "secteur déjà ajouté";
	texte = "<a href='/selection.php' class='infos'><strong>"+ texte +"</strong></a>";
	// faire un css pour box et un pour index
	document.getElementById('nbBiensSel').innerHTML = texte;
	if(!siclignote) clignote('nbBiensSel');
	return false;
	}
	
function contacts(idBien) 
	{
	ajoutSelection(idBien)
    document.getElementById('envGet').action = "/selection.php";
    document.getElementById('envGet').submit();
    return false;
	}
	
function ajoutSelBien(idBien)
	{
	if(texte = getFileContentAjax("/include/classes/mooble/selectionBiensAjax.php?ajax=1&action=ajout&idBien="+ idBien))
	return texte;
	}

var siclignote;
var a;	
function clignote(id)
	{
	siclignote = 1;
	if(document.getElementById(id).style.display == 'block')
		document.getElementById(id).style.display = 'none';
	else
		document.getElementById(id).style.display = 'block';	
	a = setTimeout("clignote('"+ id +"')", 500);
	}
	
function getFileContentAjax(fichier)
	{
	http = createRequestObject();
	http.open("GET", fichier, false);
	http.send(null);
	return http.responseText ;
	}
     
function afficheLienSelection()
	{
	http = createRequestObject();
	http.open("GET", "/include/classes/mooble/selectionBiensAjax.php?action=afficheLienSelection", true);
    http.onreadystatechange = function()
        {
        if(http.readyState == 1) document.getElementById('nbBiensSel').innerHTML = "<strong>Chargement en cours...</strong>";
        if(http.readyState == 4) 
        	{
        	document.getElementById('nbBiensSel').innerHTML = "<a href='/selection.php' class='infos' rel='nofollow'><strong>"+ http.responseText; +"</strong></a>";
			}
        }
    http.send(null);
	}

function hideLightbox()
	{
	//alert("- mettre alt dans img et title dans a");
	onOffElements(); 
	document.getElementById('fondNoir').style.display = 'none';
	document.getElementById('conteneurAjax').style.display = 'none';
	document.getElementById("conteneurAjax").innerHTML = ''; 
	}

function initLiensIndex()
	{
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++)
		{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox"))
			anchor.onclick = function () {getPageAjax(this); return false;}
		}
	}
	
function initCompteursPages()
	{
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementById("conteneurBiens").getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++)
		{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lienSite"))
			anchor.onclick = function () {nvllePageCompteur(this); return false;};
		}
	}
	
function createRequestObject()
	{
	var http=null;
	try
		{
		http = new ActiveXObject("Msxml2.XMLHTTP");
		}
	catch(b)
		{
		try
			{
			http = new ActiveXObject("Microsoft.XMLHTTP");
			}
		catch(c)
			{http = null}
		}
	if(! http &&typeof XMLHttpRequest!="undefined")
		http = new XMLHttpRequest;
	return http;
	}
	
function getPageAjax(obj)
	{
	var idDerVignette;
	idDerVignette = 'img0';
	location.href="#haut";
	http = createRequestObject();
	http.open("GET", obj.href +'?ajax=1', true);
    http.onreadystatechange = function()
        {
        if(http.readyState == 1) showLightbox();
        if(http.readyState == 4) 
        	{
        	document.getElementById('loadingImage').style.display = 'none';
   			document.getElementById("conteneurAjax").innerHTML = http.responseText;
   			evalScriptsHTML("conteneurAjax");
   			
			var divleft = document.getElementById("conteneurAjax").offsetHeight;
    		if(document.getElementById('fondNoir').style.height < divleft + 10)
    			{
    			hauteur = divleft + 10;
    			document.getElementById('fondNoir').style.height = hauteur + 'px';
				}
    		
    		initLiensPageBien();
    		document.getElementById('liensBas').className = 'liensBasAjax';
			}
        }
    http.send(null);
	}
	
function afficheBienvenue(codeDep, codeVille)
	{
	httpb = createRequestObject();
	httpb.open("GET", '/bienvenue.php?dep=' + codeDep + '&ville=' + codeVille, true);
    httpb.onreadystatechange = function()
        {
        if(httpb.readyState == 1) showLightbox();
        if(httpb.readyState == 4) 
        	{
        	document.getElementById('loadingImage').style.display = 'none';
   			document.getElementById("conteneurAjax").innerHTML = httpb.responseText;
			}
        }
    httpb.send(null);
	}
        
function nvllePageCompteur(obj)
	{
	http = createRequestObject();
    http.open("GET", "/include/classes/mooble/compteursInternesAjax.php?compteur=site&idagence="+ obj.id, true);
    http.send(null); 
	window.open(obj.href); // 
	}
	
function evalScriptsHTML(id) 
	{
	var AllScripts = document.getElementById(id).getElementsByTagName("script")
	for (var i=0; i<AllScripts.length; i++) 
		{
		var s = AllScripts[i];
		if(s.innerHTML) eval(s.innerHTML);
		/* // régler le problème s.src en ajoutant l'appel dans page principale
		if (s.src && s.src!="") 
			{
			// Précédement asynchrone, mis en synchrone pour éviter des problèmes de dépendances de scripts
			eval(getFileContent(s.src));
			}
		else 
			{
			eval(s.innerHTML);
			}
		*/
		}
	}
   
/*	
    // SetInnerHTML Sécurisé
    function setInnerHTML(divContent, HTML) {
      divContent.innerHTML=HTML; 
      var All=divContent.getElementsByTagName("*");
      for (var i=0; i<All.length; i++) {
        All[i].id=All[i].getAttribute("id")
        All[i].name=All[i].getAttribute("name")
        All[i].className=All[i].getAttribute("class")
      }
      var AllScripts=divContent.getElementsByTagName("script")
      for (var i=0; i<AllScripts.length; i++) {
         var s=AllScripts[i];
         if (s.src && s.src!="") {
            // Précédement asynchrone, mis en synchrone pour éviter des problèmes de dépendances de scripts
            eval(getFileContent(s.src))
         }
         else {
            eval(s.innerHTML)
         }
      }
    }
 
 
    // Renvoie le texte de l'objet ActiveXObject le plus récent depuis une liste
    var pickRecentProgID = function (idList){
	    // found progID flag
        var bFound = false;
        for(var i=0; i < idList.length && !bFound; i++){
            try{
                var oDoc = new ActiveXObject(idList[i]);
                o2Store = idList[i];
                bFound = true;
            }catch (objException){
                // trap; try next progID
            };
        };
        if (!bFound)
		    throw ("Aucun ActiveXObject n'est valide sur votre ordinateur, pensez à mettre à jour votre navigateur");
        idList = null;
        return o2Store;
    }
 
    // Retourne un nouvel objet XmlHttpRequest
    var GetXmlHttpRequest_AXO=null
    var GetXmlHttpRequest=function () {
	    if (window.XMLHttpRequest) {
		    return new XMLHttpRequest()
	    }
	    else if (window.ActiveXObject) {
		    if (!GetXmlHttpRequest_AXO) {
			    GetXmlHttpRequest_AXO=pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]);
		    }
		    return new ActiveXObject(GetXmlHttpRequest_AXO)
	    }
	    return false;
    }
 

	*/
	
function initLightbox()
	{


	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay">
	//		<a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
	//	</div>
	// <div id="lightbox">
	//		<a href="#" onclick="hideLightbox(); return false;" title="Click anywhere to close image">
	//			<img id="closeButton" />		
	//			<img id="lightboxImage" />
	//		</a>
	//		<div id="lightboxDetails">
	//			<div id="lightboxCaption"></div>
	//			<div id="keyboardMsg"></div>
	//		</div>
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var divjFondNoir = document.createElement("div");
	divjFondNoir.setAttribute('id','fondNoir');
	// divjFondNoir.onclick = function () {hideLightbox(); return false;}
	divjFondNoir.style.display = 'none';
	divjFondNoir.style.position = 'absolute';
	divjFondNoir.style.top = '0';
	divjFondNoir.style.left = '0';
	divjFondNoir.style.zIndex = '90';
 	divjFondNoir.style.width = '100%';
	objBody.insertBefore(divjFondNoir, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
		
	var objLoadingImage = document.createElement("img");
	objLoadingImage.src = loadingImage;
	objLoadingImage.setAttribute('id','loadingImage');
	objLoadingImage.style.position = 'absolute';
	objLoadingImage.style.display = 'none';
	objLoadingImage.style.zIndex = '150';
	divjFondNoir.appendChild(objLoadingImage);
	
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','conteneurAjax');
	objLightbox.style.display = 'none';
	objLightbox.style.width = '100%';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objBody.insertBefore(objLightbox, divjFondNoir.nextSibling);

	}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//

function addLoadEvent(func)
	{	
		var oldonload = window.onload;
		if (typeof window.onload != 'function'){
    		window.onload = func;
		} else {
			window.onload = function(){
			oldonload();
			func();
			}
		}

	}

//var idDerVignette = 'img0'; // voir init dans showligthbox
// fonctions d'affichage pour changer image 

function chImage(ressA, idVignette)
	{
	if(idDerVignette) document.getElementById(idDerVignette).className= "";
	document.getElementById(idVignette).className= "imageSel";
	document.getElementById('imagePrinc').src = ressA.href;
	idDerVignette = idVignette;
	}




//addLoadEvent(initLiens);	// run initLightbox onLoad
// directement dans corps pour éviter clic avant onload page (et donc pas d'ajax)
