//Utilisation
//mettre un lien vers la grande image au tour de la vignette
//Et ajouter l'atribut "class='LBox'" aux liens
//ex:
//<a href="Images/img1.gif" target="_blank "class="LBox"><img src="Images/img1-pt.gif" alt="" border="0"/></a>
//
//pour les images dans les frames il faut utiliser : onclick="top.Lbox(this);return false;" dans les liens

//Param
var SrcLoading = "Images/ImgLBox/Loading.gif";
var SrcLoadingW = 32;
var SrcLoadingH = 32;

var SrcClose = "Images/ImgLBox/close.gif";
var SrcCloseW = 26;
var SrcCloseH = 26;

var Spacer = "Images/ImgLBox/Spacer.gif";

var ClosePleineLargeur = true;
var AlphaAnim = false;
var RedimAnim = false;
var FondAlpha = 8; //0-10

var CadMargin = 150;
var CurImg;
var isShowing = false;
var WinW = 0;
var WinH = 0;
var Im;
var t;
var InitOverFlow = "auto";

function InitLbox()
{
    var Fond = document.createElement('div');
    var SFond = Fond.style;
    Fond.setAttribute('id', 'LboxFond');
    SFond.display = "none";
    SFond.position = "absolute";
    SFond.top = "0px";
    SFond.left = "0px";
    SFond.backgroundColor = "#000000";
    SFond.zIndex = 10000000;
    document.body.appendChild(Fond);

    var Cad = document.createElement('div');
    var SCad = Cad.style;
    Cad.setAttribute('id', 'LboxCad');
    SCad.display = "none";
    SCad.position = "absolute";
    SCad.width = "100px";
    SCad.height = "100px";
    SCad.zIndex = 10000002;
    SCad.backgroundColor = "#ffffff";
    Cad.align = "center";
    SCad.textAlign = "center";

    var ImgLoad = document.createElement('Img');
    ImgLoad.setAttribute('id', 'LboxLoad');
    ImgLoad.style.display = "none";
    ImgLoad.style.position = "absolute";
    ImgLoad.style.width = SrcLoadingW + "px";
    ImgLoad.style.height = SrcLoadingH + "px";
    ImgLoad.src = SrcLoading;
    ImgLoad.style.zIndex = 10000003;
    document.body.appendChild(ImgLoad);

    var Img = document.createElement('img');
    Img.setAttribute('id', 'LboxImg');
    Img.style.margin = "10px";
    Img.style.display = "none";
    Img.src = Spacer;
    Cad.appendChild(Img);
    document.body.appendChild(Cad);

    var Close = document.createElement('div');
    Close.setAttribute('id', 'LboxClose');
    SClose = Close.style;
    SClose.display = "none";
    SClose.position = "absolute";
    SClose.width = "100px";
    SClose.height = (SrcCloseH) + "px";
    SClose.zIndex = 10000000;
    SClose.backgroundColor = "#ffffff";
    Close.align = "center";
    SClose.textAlign = "center";

    var ImgClose = document.createElement('img');
    ImgClose.setAttribute('id', 'LboxImgClose');
    ImgClose.style.margin = "3px";
    ImgClose.style.width = SrcCloseW + "px";
    ImgClose.style.height = SrcCloseH + "px";
    ImgClose.align = "right";
    ImgClose.src = SrcClose;
    ImgClose.onclick = function() { HideImg(); };
    ImgClose.onmouseover = function() { this.style.cursor = "hand"; this.style.cursor = "pointer"; };
    ImgClose.onmouseout = function() { this.style.cursor = "default"; };
    Close.appendChild(ImgClose);
    document.body.appendChild(Close);

    InitOverFlow = document.body.style.overflow;


    for (var i = 0; i < document.links.length; i++)
    {
        var link = document.links[i];
        if (link.className == "LBox")
        {
            link.onclick = function() { Lbox(this); return false; };
        }
    }
}
function Resize()
{
    if (isShowing)
    {
        Start();
    }
}
function GetWinSize()
{
    document.body.style.overflow = "hidden";
    WinW = WinWSize();
    WinH = WinHSize();

    var Fond = getId("LboxFond").style;
    Fond.top = getScrollY() + "px";
    Fond.left = getScrollX() + "px";

    //document.body.style.overflow="auto";
}
function Lbox(Lk)
{
    CurImg = Lk;
    t = sT("Start()", 10);
}
function Start()
{
    clearTimeout(t);
    t = sT("StartAnim()", 100);
}
function StartAnim()
{
    GetWinSize();
    HideAll();
    ShowFond();
}
function HideImg()
{
    NegaAlpha(10, 0, 'LboxCad', '');
    NegaAlpha(10, 0, 'LboxClose', '');
    NegaAlpha(FondAlpha, 0, 'LboxFond', 'HideAll');
}
function HideAll()
{
    clearTimeout(t);
    getId("LboxFond").style.display = "none";
    getId("LboxCad").style.display = "none";
    getId("LboxLoad").style.display = "none";
    getId("LboxImg").style.display = "none";
    getId("LboxClose").style.display = "none";

    document.body.style.overflow = InitOverFlow;
    isShowing = false;
}
function ShowFond()
{
    if (CurImg)
    {
        isShowing = true;
        document.body.style.overflow = "hidden";
        var Fond = getId("LboxFond").style;
        Fond.display = "block";
        Fond.width = WinW + "px";
        Fond.height = WinH + "px";
        Alpha(0, FondAlpha, 'LboxFond', 'ShowCad');
    }
}
function ShowCad()
{
    var Cad = getId("LboxCad").style;
    Cad.display = "block";
    Cad.top = ((WinH / 2) + getScrollY() - (pI(Cad.height) / 2)) + "px";
    Cad.left = ((WinW / 2) + getScrollX() - (pI(Cad.width) / 2)) + "px";

    Alpha(0, 10, 'LboxCad', 'ShowLoading');
}
function ShowLoading()
{
    var ImgLoad = getId("LboxLoad").style;
    ImgLoad.display = "block";
    ImgLoad.top = ((WinH / 2) + getScrollY() - (pI(ImgLoad.height) / 2)) + "px";
    ImgLoad.left = ((WinW / 2) + getScrollX() - (pI(ImgLoad.width) / 2)) + "px";

    Im = new Image();
    Im.src = CurImg.href;

    Alpha(0, 10, 'LboxLoad', 'WaitImg');
}
function HideLoading()
{
    getId("LboxLoad").style.display = "none";
}
function WaitImg()
{
    if (Im.complete)
    {
        var ratio = Im.height / Im.width;
        if (Im.width > WinW - CadMargin - 10)
        {
            Im.width = WinW - CadMargin - 10;
            Im.height = Im.width * ratio;
        }
        if (Im.height > WinH - CadMargin - 10)
        {
            Im.height = WinH - CadMargin - 10;
            Im.width = Im.height / ratio;
        }
        t = sT("AgrandiHCad()", 0);
    }
    else
    {
        t = sT("WaitImg()", 100);
    }
}
function AgrandiHCad()
{
    var Cad = getId("LboxCad").style;
    var step = 20;
    if (pI(Cad.height) > Im.height) step = step * -1;
    Agrandi(pI(Cad.height) + step, Im.height + 20, step, "LboxCad", "h");
}
function AgrandiWCad()
{
    var Cad = getId("LboxCad").style;
    var step = 20;
    if (pI(Cad.width) > Im.width) step = step * -1;
    Agrandi(pI(Cad.width) + step, Im.width + 20, step, "LboxCad", "w");
}
function Agrandi(Val, Max, step, Id, Typ)
{
    if (!RedimAnim) Val = Max;
    var Finish = true;
    if (Typ == 'h')
    {
        getId(Id).style.height = Val + 'px';
        getId(Id).style.top = pI(getId(Id).style.top) - (step / 2) + 'px';
    }
    else
    {
        getId(Id).style.width = Val + 'px';
        getId(Id).style.left = pI(getId(Id).style.left) - (step / 2) + 'px';
    }
    if (step > 0)
    {
        if (Val < Max)
        {
            t = sT("Agrandi(" + (Val + step) + "," + Max + "," + step + ",'" + Id + "','" + Typ + "')", 20);
            Finish = false;
        }
    }
    else
    {
        if (Val > Max)
        {
            t = sT("Agrandi(" + (Val + step) + "," + Max + "," + step + ",'" + Id + "','" + Typ + "')", 20);
            Finish = false;
        }
    }
    if (Finish)
    {
        if (Typ == 'h')
        {
            AgrandiWCad();
        }
        else
        {
            ShowImg();
        }
    }
}
function ShowImg()
{
    HideLoading();
    var Cad = getId("LboxCad").style;
    Cad.display = "block";
    Cad.width = pI(Im.width + 20) + "px";
    Cad.height = pI(Im.height + 20) + "px";
    Cad.top = pI(((WinH - 20) / 2) + getScrollY() - (pI(Im.height) / 2)) + "px";
    Cad.left = pI(((WinW - 20) / 2) + getScrollX() - (pI(Im.width) / 2)) + "px";

    var Img = getId("LboxImg");
    Img.src = CurImg.href;
    Img.style.display = "block";
    Img.style.width = Im.width + "px";
    Img.style.height = Im.height + "px";
    Alpha(1, 10, 'LboxImg', 'ShowClose');
}
function ShowClose()
{
    var Cad = getId("LboxCad").style;
    var CadClo = getId("LboxClose").style;
    CadClo.display = "block";
    if (!ClosePleineLargeur)
    {
        CadClo.width = (SrcCloseW + 6) + "px";
        CadClo.height = (SrcCloseH + 6) + "px";
        CadClo.top = (pI(Cad.top) - pI(CadClo.height)) + "px";
        CadClo.left = (pI(Cad.left) + pI(Cad.width) - SrcCloseW - 6) + "px";
    }
    else
    {
        CadClo.width = pI(Cad.width) + "px";
        CadClo.height = (SrcCloseH + 6) + "px";
        CadClo.top = (pI(Cad.top) - pI(CadClo.height)) + "px";
        CadClo.left = pI(Cad.left) + "px";
    }
    Alpha(1, 10, 'LboxClose', '');
}
function WinWSize()
{
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number')
    {
        myWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}
function WinHSize()
{
    var myHeight = 0;
    if (typeof (window.innerWidth) == 'number')
    {
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}
function getScrollX()
{
    var scrOfX = 0;
    if (typeof (window.pageYOffset) == 'number')
    {
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        scrOfX = document.documentElement.scrollLeft;
    }
    return scrOfX;
}
function getScrollY()
{
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number')
    {
        scrOfY = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}
function SetAlpha(CurObj, Transp)
{
    if (CurObj.style.MozOpacity)
    {
        CurObj.style.MozOpacity = Transp;
    }
    else if (CurObj.filters)
    {
        CurObj.style.filter = 'alpha(opacity=' + (Transp * 100) + ')';
    }
    else
    {
        CurObj.style.opacity = Transp;
    }
}
function Alpha(from, to, id, CallFunc)
{
    var Obj = getId(id);
    if (AlphaAnim)
    {
        SetAlpha(Obj, (from / 10));
    }
    else
    {
        SetAlpha(Obj, to);
        from = to;
    }
    SetAlpha(Obj, (from / 10));
    if (from < to)
    {
        t = sT("Alpha(" + (from + 2) + "," + to + ",'" + id + "','" + CallFunc + "')", 20);
    }
    else if (CallFunc != '')
    {
        t = sT(CallFunc + "()", 100);
    }
}
function NegaAlpha(from, to, id, CallFunc)
{
    var Obj = getId(id);
    if (AlphaAnim)
    {
        SetAlpha(Obj, (from / 10));
    }
    else
    {
        SetAlpha(Obj, to);
        from = to;
    }
    if (from > to)
    {
        t = sT("NegaAlpha(" + (from - 2) + "," + to + ",'" + id + "','" + CallFunc + "')", 20);
    }
    else if (CallFunc != '')
    {
        t = sT(CallFunc + "()", 100);
    }
}
function addLoadEvent(_function)
{
    var _onload = window.onload;
    if (typeof window.onload != 'function')
    {
        if (window.onload)
        {
            window.onload = _function;
        }
        else
        {
            var _addEventListener = window.addEventListener || document.addEventListener;
            var _attachEvent = window.attachEvent || document.attachEvent;
            if (_addEventListener)
            {
                _addEventListener('load', _function, true);
                return true;
            }
            else if (_attachEvent)
            {
                var _result = _attachEvent('onload', _function);
                return _result;
            }
            else
            {
            }
        }
    }
    else
    {
        window.onload = function()
        {
            _onload();
            _function();
        }
    }
}
function addResizeEvent(_function)
{
    var _resize = window.resize;
    if (typeof window.resize != 'function')
    {
        if (window.resize)
        {
            window.resize = _function;
        }
        else
        {
            var _addEventListener = window.addEventListener || document.addEventListener;
            var _attachEvent = window.attachEvent || document.attachEvent;
            if (_addEventListener)
            {
                _addEventListener('resize', _function, true);
                return true;
            }
            else if (_attachEvent)
            {
                var _result = _attachEvent('onresize', _function);
                return _result;
            }
            else
            {
            }
        }
    }
    else
    {
        window.resize = function()
        {
            _resize();
            _function();
        }
    }
}
addLoadEvent(function() { InitLbox(); });
addResizeEvent(function() { Resize(); });
function pI(a) { return parseInt(a); }
function getId(a) { return document.getElementById(a); }
function sT(a, b) { return setTimeout(a, b); }
