var xmlhttp = null;
var hulpArray = new Array();
var afbeeldingArray = new Array();
var x = 0;

function afbeeldingGet()
{
    hulpArray = document.getElementsByTagName("img")
    
    for ( i = 0; i < hulpArray.length; i ++ )
    {
        if ( hulpArray[i].title != "" )
        {
            hulpArray[i].setAttribute("id", afbeeldingArray.length);
            hulpArray[i].setAttribute("onclick", "afbeeldingSlideshow('" + afbeeldingArray.length + "');");

            afbeeldingArray.push(hulpArray[i]);
        }
    }
}

function afbeeldingSlideshow(id)
{
    xmlhttp.open("GET", "scripts/afbeelding.php?img=" + afbeeldingArray[id].src + "&title=" + afbeeldingArray[id].title + "&id=" + id + "&max=" + afbeeldingArray.length);
    
    xmlhttp.onreadystatechange = function()
    {
        if( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
        {
            document.getElementById("afbeeldingSlideshow").innerHTML = xmlhttp.responseText;
            
            document.getElementById("afb_" + id).style.maxWidth = getDocWidth() - 130 + "px";
            document.getElementById("afb_" + id).style.maxHeight = getDocHeight() - 160 + "px";
            
            document.getElementById("overlay").style.display = "block";
            document.getElementById("afbeeldingSlideshow").style.display = "block";
            
            if ( docHasVerticalScroll() )
            {
                if(!window.innerWidth)
                {
                    document.getElementById("afbeeldingSlideshow").style.left = Math.round((getDocWidth() - document.getElementById("afb_" + id).width - 30 - 6) / 2) + "px";
                }
                else
                {
                    document.getElementById("afbeeldingSlideshow").style.left = Math.round((getDocWidth() - document.getElementById("afb_" + id).width - 30 - 20 - 6) / 2) + "px";
                }
            }
            else
            {
                document.getElementById("afbeeldingSlideshow").style.left = Math.round((getDocWidth() - document.getElementById("afb_" + id).width - 30 - 6) / 2) + "px";
            }
            
            document.getElementById("afbeeldingSlideshow").style.top = Math.round((getDocHeight() - document.getElementById("afb_" + id).height - 30 - 20 - 6) / 2) + "px";
        }
    }
    
    xmlhttp.send(null);
}

function stopSlideshow()
{
    document.getElementById("afbeeldingSlideshow").innerHTML = "";
    
    document.getElementById("overlay").style.display = "none";
    document.getElementById("afbeeldingSlideshow").style.display = "none";
}

window.onload = function ()
{
    try
    {
            xmlhttp = new XMLHttpRequest();
    }
    catch(e)
    {
            try
            {
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                    alert("Your browser doesn't support AJAX.");
            }
    }

    afbeeldingGet();
}

function getDocHeight()
{
    var h = 0;

    //IE
    if(!window.innerWidth)
    {
        //strict mode
        if(!(document.documentElement.clientWidth == 0))
        {
            h = document.documentElement.clientHeight;
        }
        //quirks mode
        else
        {
            h = document.body.clientHeight;
        }
    }
    //w3c
    else
    {
        h = window.innerHeight;
    }
    return h;
}

function getDocWidth()
{
    var w = 0;

    //IE
    if(!window.innerWidth)
    {
        //strict mode
        if(!(document.documentElement.clientWidth == 0))
        {
            w = document.documentElement.clientWidth;
        }
        //quirks mode
        else
        {
            w = document.body.clientWidth;
        }
    }
    //w3c
    else
    {
        w = window.innerWidth;
    }
    
    return w;
}

function docHasVerticalScroll()
{
    var docHeight = getDocHeight();
    
    if ( docHeight < document.body.scrollHeight )
    {
        return true;
    }
    else
    {
        return false;
    }
}

