var popupObj;
var content = new Array();
var showPopUpIndicator;
var showErrorPopUpIndicator;

var popupUrls = ["/fondation/RenaultResources/popups/popup1.html", "/fondation/RenaultResources/popups/popup2.html", "/fondation/RenaultResources/popups/popup3.html", "/fondation/RenaultResources/popups/popup4.html"];
var currentPopUpActive;
function showPopUp (i) {

	try {
        // only for Firefox in local mode
        netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    } catch (e) {}
    $("#popupContent").load(popupUrls[i] + " .wrapPopUp");
	
    getContent(i);
	currentPopUpActive = i;
    setListObjStatus(); //Set ListPopUP Active State When Show PopUp
    popupObj.style.display = "block";
    showPopUpIndicator = "true";

    //Create an iFrame to overlay all the select items for IE
    var ie = document.all;
    if(ie) {
        var hPopup = popupObj.clientHeight;
        var wPopup = popupObj.clientWidth;
        
        var ifr = document.getElementById('helpHiddenSelect');
        
        if(hPopup > 510)
	        {ifr.style.height = "510px"}
        else
	        {ifr.style.height = hPopup + "px"}
        
        ifr.style.width = wPopup + "px";
    }
	
	creatingOverlays();//Initializing Overlays Layer
	setOverlaysSize();//Setting LightBox size
	setOverlaysPosition();
}
function hidePopUp () {
    popupObj.style.display = "none";
    showPopUpIndicator = "false";
	currentPopUpActive = null;
	setListObjStatus();
	
	document.getElementById("outer").removeChild(overlays);
	//document.body.removeChild(overlays);
	
    //JUST ENABLE THIS SCRIPT ONLY WHEN YOU WANT TO DISABLE THE VERTICAL-SCROLLBAR
    //enable scrollbar
    //var getBody = document.getElementsByTagName("body")[0];
    //var getHtml = document.getElementsByTagName("html")[0];
    //getBody.style.overflow = null;
    //getHtml.style.overflow = null;
}

function getContent (i) {
    var popupContentObj = document.getElementById("popupContent");
    popupContentObj.innerHTML = content[i];
}

function initializePopUp () {

    if (document.getElementById("popup") == null)
    {
        popupObj = document.createElement("div");
        var ie = document.all;
        if (ie) {
            popupObj.attributes("id").value = "popup";
        }
        else {
            popupObj.setAttribute("id","popup");
        }
        popupObj.innerHTML = 
                  "<div id=\"popupHeader\"><div class=\"wrapPopUpOuter\">\n" +
                  "    <a href=\"#\" onclick=\"hidePopUp();return false;\" title=\"Fermer\" class=\"closeBtn\"><span>Fermer</span></a>\n" +

                  "    <div id=\"popupContent\">\n" +
                  "    </div>\n" +

                  "</div></div><div id=\"popupFooter\">&nbsp;</div>\n"
        if(ie) {              
            popupObj.innerHTML += 
                  "<iframe id=\"helpHiddenSelect\" src=\"\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" ></iframe>\n"
        }
                  
        document.getElementById("outer").appendChild(popupObj);
    }
	//document.body.appendChild(popupObj);
}

function setListObjStatus () {
    var ULPopUpList = document.getElementById("popupList");
    var LIPopUpList = ULPopUpList.getElementsByTagName("li");

    for (var i = 0 ; i < LIPopUpList.length ; i++) {
        if (i == currentPopUpActive) {
            LIPopUpList[i].getElementsByTagName("a")[0].style.color = "#6e736f";
			LIPopUpList[i].style.backgroundImage = "url(../common/img/list_bullet.gif)";
        }
        else {
            LIPopUpList[i].getElementsByTagName("a")[0].style.color = "#000";
			LIPopUpList[i].style.backgroundImage = "url(../common/img/ico_popup_list.gif)";
        }
    }
}

//Centering PopUp Horizontal and Vertical
function setPopUpPosition () {
    var screenWidth = document.documentElement.clientWidth;
    var screenHeight = document.documentElement.clientHeight;//get the height of the window screen
    
    //Get the Scroll Top Position
    var ie = document.all; //If IE
    if (ie) {
        scrollTop = document.documentElement.scrollTop;
        scrollLeft = document.documentElement.scrollLeft;
    }
    else {
        scrollTop = window.pageYOffset;
        scrollLeft = window.pageXOffset;
    }

    //If the screen heigth is less than the PopUp's height, we can't never stop scrolling because the position of the PopUp is center, so it always has a part out of the screen at the bottom, so we can scroll over and over again. This solution here is we can set the PopUp always at the bottom of the screen
    
    if (popupObj.clientHeight > document.documentElement.clientHeight) {
        scrollTop = scrollTop + (screenHeight - popupObj.clientHeight)/2;
    }
    if (popupObj.clientWidth > document.documentElement.clientWidth) {
        scrollLeft = scrollLeft + (screenWidth - popupObj.clientWidth)/2;
    }
    
    //Set PopUp Position
    popupObj.style.left = (screenWidth - popupObj.clientWidth)/2 + scrollLeft + "px";
    popupObj.style.top = (screenHeight - popupObj.clientHeight)/2 + scrollTop + "px";
}

//Event
window.onresize = function () {
    if (showPopUpIndicator && showPopUpIndicator == "true") {
        setOverlaysSize();
		setOverlaysPosition();
    }
    if (showErrorPopUpIndicator && showErrorPopUpIndicator == "true") {
        setErrorPopUpPosition();
        setOverlaysSize();
		setOverlaysPosition();
    }
}
window.onscroll = function () {
    if (showPopUpIndicator && showPopUpIndicator == "true") {
        setOverlaysSize();
		setOverlaysPosition();
    }
    if (showErrorPopUpIndicator && showErrorPopUpIndicator == "true") {
        setErrorPopUpPosition();
        setOverlaysSize();
		setOverlaysPosition();
    }
}