﻿// -----------------------------------------------------------------------------------
//
//	LightPage v1.0
//	by Michel de Lambilly 
//	06/07/2007
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), Thomas Fuchs(mir.aculo.us), and others.
//
// This script is based on lightbox technologies and will (I hope) a part of it in the future
//
// -----------------------------------------------------------------------------------
//
//	Configuration
//
var overlayOpacity = 0.7;	// controls transparency of shadow overlay

var animate = true;			// toggles resizing animations
var resizeSpeed = 1;		// controls the speed of the image resizing animations (1=slowest and 10=fastest)

var borderSize = 10;		//if you adjust the padding in the CSS, you will need to update this variable

// -----------------------------------------------------------------------------------

//
//	Global Variables
//

if(animate == true){
	overlayDuration = 0.2;	// shadow fade in/out duration
	if(resizeSpeed > 10){ resizeSpeed = 10;}
	if(resizeSpeed < 1){ resizeSpeed = 1;}
	resizeDuration = (11 - resizeSpeed) * 0.15;
} else { 
	overlayDuration = 0;
	resizeDuration = 0;
}

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

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

// -----------------------------------------------------------------------------------
//
// getHTTPObject()
// Pour créer l'objet qui va gérer la requête http
//

function getHTTPObject() 
{
	var xmlhttp;
	
	if (window.XMLHttpRequest) 
	{
       xmlhttp = new XMLHttpRequest();

       // Évite un bug du navigateur Safari :
       if (xmlhttp.overrideMimeType) 
       {
         xmlhttp.overrideMimeType("text/xml");
       }

    } 
    else 
    {
       if (window.ActiveXObject) 
       {

         try 
         { // essaie de charger l'objet pour IE
           xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
         } 
         catch (e) 
         {
           try 
           { // essaie de charger l'objet pour une autre version IE
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           } 
           catch (e) 
           {
             window.alert("Votre navigateur ne prend pas en charge l'objet XMLHTTPRequest.");
           } // try-catch
         } // try-catch
       }
    } // if-else

  	return xmlhttp;
}

//
// LoadDiv()
// Envoi la requête http en synchrone et récupère le résultat
//
function LoadDiv( divName, url )
{
	// Initialization
	var http = getHTTPObject();						
	var dest = document.getElementById(divName);  

	// send request
	http.open("GET", url, true);
	http.onreadystatechange = function()
							  {
						        if ( http.readyState == 4  && http.status == 200 ) 
						        {
						            var html = http.responseText;
						            html = html.replace( /<form.*>/gi, " " );
						            html = html.replace( /<\/form.*>/gi, " " );
						            html = html.replace( /<input type="hidden".*>/gi, " " );
						            Element.setInnerHTML( divName, html );
						            myLightbox.updateImageList();
						        }
							  };
	http.send(null);
}

// -----------------------------------------------------------------------------------
//
//	LightPage Class Declaration
//	- initialize()
//  - updatePageList()
//	- start()
//	- end()
//
//	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
//
var LightPage = Class.create();

LightPage.prototype = 
{

	// initialize()
	// Constructor runs on completion of the DOM loading.
	// The function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the page container.
	//
	initialize: function() 
	{
	    this.updatePageList();
		
		var objBody = document.getElementById("content");
//        var objBody = document.getElementsByTagName("body").item(0);
/*		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','pageoverlay');
		objOverlay.style.display = 'none';
		objOverlay.onclick = function() { myLightPage.end(); }
		objBody.appendChild(objOverlay);
*/
		var objLightPage = document.createElement("div");
		objLightPage.setAttribute('id','lightpage');
		objLightPage.style.display = 'none';
		objLightPage.onclick = function(e) 
		{	// close LightPage is user clicks shadow overlay
			if (!e) var e = window.event;
			var clickObj = Event.element(e).id;
			if ( clickObj == 'lightpage')
			{
				myLightPage.end();
			}
		};
		objBody.appendChild(objLightPage);
	},
    
    //
	// updatePageList()
	// Loops through anchor tags looking for 'lightpage' references and applies onclick
	// events to appropriate links.
	//
	updatePageList: function() 
	{	
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');
		var areas = document.getElementsByTagName('area');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++)
		{
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightpage' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightpage')))
			{
				anchor.onclick = function () {myLightPage.start(this); return false;}
			}
		}
	},
    
    //
	//	start()
	//	Display overlay and lightpage.
	//
	start: function(pageLink) 
	{
	    // stretch overlay to fill page and fade in
//		var arrayPageSize = getPageSize();
//		Element.setWidth('pageoverlay', arrayPageSize[0]);
//		Element.setHeight('pageoverlay', arrayPageSize[1]);

//		new Effect.Appear('pageoverlay', { duration: overlayDuration, from: 0.0, to: overlayOpacity });
		
		LoadDiv( 'lightpage', pageLink.getAttribute('href') );
		
		new Effect.BlindDown('lightpage', { duration: resizeDuration} );
	},
	
	//
	//	end()
	//
	end: function() {
//		new Effect.Fade('pageoverlay', { duration: overlayDuration});
		new Effect.BlindUp('lightpage', { duration: resizeDuration} );
	}
}
// -----------------------------------------------------------------------------------

function initPagebox() { myLightPage = new LightPage(); }
Event.observe(window, 'load', initPagebox, false);
