/*	
Part of the code comes from : 
    Lightbox JS: Fullsize Image Overlays
    by Lokesh Dhakar - http://www.huddletogether.com
*/

//Définition des attributs
var loadingLayout = {
    layout : document.createElement("div"),
    layoutId : "global-loading",
    loadingImageWidth : 128,
    loadingImageHeight : 128,
    documentBody : null
}

//Initialise l'objet
loadingLayout.init = function() {
    loadingLayout.layout.id = loadingLayout.layoutId;
    loadingLayout.documentBody = document.getElementsByTagName("body")[0];
}

//Affiche le layout de chargement sur l'ensemble de la page
loadingLayout.show = function() {
    var pageSize = this.getPageSize();
    var pageScroll = this.getPageScroll();
    var scrollX = (pageScroll[0]) ? pageScroll[0] : 0;
    var scrollY = (pageScroll[1]) ? pageScroll[1] : 0;
    var imagePositionLeft = scrollX + ((pageSize[2] - this.loadingImageWidth) / 2);
    var imagePositionTop = scrollY + ((pageSize[3] - this.loadingImageHeight) / 2);
    
    //Define layout CSS style pour IE 6
    this.layout.style.height = pageSize[1] + "px";
    this.layout.style.backgroundPosition = imagePositionLeft + "px " + imagePositionTop + "px";
    this.layout.style.position = "absolute";
    
    //Append layout
    this.documentBody.appendChild(this.layout);
}

//Masque le layout de chargement
loadingLayout.hide = function() {
    this.documentBody.removeChild(this.layout);
}

// Returns array with x,y page scroll values.
loadingLayout.getPageScroll = function() {
	var yScroll, xScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// Returns array with page width, height and window width, height
loadingLayout.getPageSize = function() {
	
	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
	var pageHeight;
    if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	var pageWidth;
    if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//Initialize
$(document).ready(function(){
    loadingLayout.init();
});
