﻿var divBackground = null;
var imgViewed = null;

function ViewImage(img) {
    var url = GetImageUrl(img); 
    ViewBackground();
    if (imgViewed == null) {
        imgViewed = document.createElement("img");
        imgViewed.onload = function() {
            imgViewed.style.display = "block";
            var width = imgViewed.width;
            var height = imgViewed.height;
            imgViewed.style.top = GetScrollTop() + Math.round((divBackground.clientHeight - height) / 2) + "px";
            imgViewed.style.left = Math.round((divBackground.clientWidth - width) / 2) + "px";
        };
        imgViewed.onclick = function() {
            HideBackground();
        };
        document.body.appendChild(imgViewed);
        imgViewed.style.position = "absolute";
        imgViewed.style.display = "none";
    };
    imgViewed.src = url;
}

function EnterImage(img) {
  
}


function ExitImage(img) {

}


function GetImageUrl(img) {
    return img.getAttributeNode("image").value;
}


function ViewBackground() {
    if (divBackground == null) {
        divBackground = document.createElement("div");
        divBackground.style.background = "black";
        divBackground.style.position = "absolute";
        
        divBackground.style.width = "100%";
        divBackground.style.height = "100%";

        divBackground.onclick = function() {
            HideBackground();
        };

        SetAlpha(divBackground, 0.8);
        document.body.appendChild(divBackground);
    }
    var activeX = document.getElementById("ctl00_WebUserControl01_Xaml1_parent");
    activeX.style.display = "none";

    document.body.style.overflow = "hidden";
    divBackground.style.top = GetScrollTop() + "px";
    divBackground.style.left = "0px";
    divBackground.style.display = "block";

    
    
    
} // function ViewBackground()

function HideBackground() {
    var activeX = document.getElementById("ctl00_WebUserControl01_Xaml1_parent");
    activeX.style.display = "block";

    document.body.style.overflow = "auto";

    if (imgViewed != null)
        imgViewed.style.display = "none";
    
    if (divBackground != null) 
        divBackground.style.display = "none";
} // function HideBackground()




// 0..1
function SetAlpha(element, factor) {
    if (factor > 1) factor = 1;
    else
        if (factor < 0) factor = 0;

    try {
        if (element.style.filter == "")
            element.style.filter = "Alpha(opacity=" + Math.floor(factor * 100) + ")";
        else
            element.filters.alpha.opacity = Math.floor(factor * 100);
    } catch (e) {
    }

    // FF   
    element.style.MozOpacity = factor;

    // Autre
    element.style.opacity = factor;

}


function GetScrollTop() {

    var ScrollTop = document.body.scrollTop;


    if (ScrollTop == 0) {
        if (window.pageYOffset)
            ScrollTop = window.pageYOffset;
        else
            ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }

    return ScrollTop;

}
