﻿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;
}

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;
}
function Overlay(centerElement) 
{ 
     Overlay(centerElement, 'false');
}

function closeOverlay () {
    $('#ContentGlobal select').removeClass("Hide");
    $('#OverlayPopup').css('display', 'none');
}

function Overlay(centerElement, isFLowerPower) {
    //On cache les SELECT a l'ouverture de la popup
    $('#ContentGlobal select').addClass("Hide");
    
	if (isFLowerPower && isFlowerActive)
	{
		pageLoaded = false;
		setTimeout('GenericOverlay(\'' + centerElement + '\',' + isFLowerPower + ')', flowerDelay);
	}
	else if(!isFLowerPower)
	{
		GenericOverlay(centerElement, isFLowerPower);   
	}
		
	CenterElementsByJavascript();
}

var pageLoaded = false;
function GenericOverlay(centerElement, isFLowerPower) {
    
	if(!isFLowerPower || !pageLoaded)
	{
		// Initialisation puis appel à chaque resize de la fenêtre
		refreshOverlayAndPopup(centerElement);
		if (!isFLowerPower) {
			$(window).resize(function() {
			    if($('#OverlayPopup').css('display')=='block') {
				    refreshOverlayAndPopup(centerElement);
			    }
			});
		}
	}
}

function refreshOverlayAndPopup(centerElement) {
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();
   
    var jqueryObjOverlay = $('#OverlayPopup');
    var objOverlay = document.getElementById('OverlayPopup');
    if (objOverlay != null) 
    {
        objOverlay.style.width = arrayPageSize[0] + 'px';
        objOverlay.style.height = (arrayPageSize[3] + 30) + 'px';
        jqueryObjOverlay.children().hide();
        objOverlay.style.display = 'block';
        
        var objOverlayImg = document.getElementById(centerElement);
        
        $(objOverlayImg).width();
        objOverlayImg.style.position = 'absolute';

        objOverlayImg.style.top = -10000;
        objOverlayImg.style.left = -10000;
        objOverlayImg.style.display = 'block';

        objOverlay.appendChild(objOverlayImg);

        objOverlayImg.style.top = ((arrayPageSize[3] - objOverlayImg.offsetHeight) / 2) + 'px';
        objOverlayImg.style.left = (((arrayPageSize[2] - 20 - objOverlayImg.offsetWidth) / 2) + 'px');
    }
}

/************************************************
*                   Popup Window                *
************************************************/
(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:300, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);
