/*
var arrAllImages = new Array();
arrAllImages[0] = "Images/Dummy/gal/1.gif";
arrAllImages[1] = "Images/Dummy/gal/2.gif";
arrAllImages[2] = "Images/Dummy/gal/3.gif";
arrAllImages[3] = "Images/Dummy/gal/4.gif";
arrAllImages[4] = "Images/Dummy/gal/5.gif";
arrAllImages[5] = "Images/Dummy/gal/6.gif";
arrAllImages[6] = "Images/Dummy/gal/7.gif";
arrAllImages[7] = "Images/Dummy/gal/8.gif";
arrAllImages[8] = "Images/Dummy/gal/9.gif";
arrAllImages[9] = "Images/Dummy/gal/10.gif";
arrAllImages[10] = "Images/Dummy/gal/11.gif";
arrAllImages[11] = "Images/Dummy/gal/12.gif";
arrAllImages[12] = "Images/Dummy/gal/13.gif";
arrAllImages[13] = "Images/Dummy/gal/14.gif";
arrAllImages[14] = "Images/Dummy/gal/15.gif";
arrAllImages[15] = "Images/Dummy/gal/16.gif";
arrAllImages[16] = "Images/Dummy/gal/17.gif";
arrAllImages[17] = "Images/Dummy/gal/18.gif";
arrAllImages[18] = "Images/Dummy/gal/19.gif";
arrAllImages[19] = "Images/Dummy/gal/20.gif";
*/
var GalleryPosition = 0;
var largeImagePostfix = "&width=315";
var smallImagePostfix = "&width=50";

//Load gallery on body onload
function LoadGallery()
{
    // determine if this is a page with a gallery on it, by seing if there is a arrAllImages array defined
    if(typeof(arrAllImages)=="undefined") return;
    CheckButtons();
    var imgLarge = document.createElement("img");
    imgLarge.setAttribute("src", arrAllImages[0]+largeImagePostfix);
    imgLarge.setAttribute("id", arrAllImages[0]);

    document.getElementById("galleryLarge").innerHTML = "";
    document.getElementById("galleryLarge").appendChild(imgLarge);
    
    var i = 0;
    for (i = 0; i <= 5; i++)
    {
        var img = document.createElement("img");
        img.setAttribute("src", arrAllImages[i]+smallImagePostfix);
        img.setAttribute("id", arrAllImages[i]);
        img.setAttribute("onclick", "ShowLargeImage(this.id); return true");
        img["onclick"]=new Function("ShowLargeImage(this.id);");
        //var listener = addEventListener(img, "click", "ShowLargeImage(this.id);");
        //document.getElementById("gallerySmall" + i).onclick = ShowLargeImage(arrAllImages[i]);
        //var listener = addEventListener(img, "click", ShowLargeImage(arrAllImages[i]));
        //img.onclick = function() { var temp = new Function("ShowLargeImage('"+arrAllImages[i]+"')"); temp(); };
                
        document.getElementById("gallerySmall" + i).innerHTML = "";
        document.getElementById("gallerySmall" + i).appendChild(img);
        //document.getElementById(arrAllImages[i]).onclick = ShowLargeImage(arrAllImages[i]);
        //var listener = addEventListener(img, "click", ShowLargeImage(arrAllImages[i]));
        
        GalleryPosition++;
        //console.log(arrAllImages[i]);
    }
}

//Show large image when click on thumb
function ShowLargeImage(ImgId)
{
    var img = document.createElement("img");
    img.setAttribute("src", ImgId+largeImagePostfix);
    img.setAttribute("id", ImgId);
    //img.setAttribute("style", "width: 400px; height: 300px;"); // skal slettes!
        
    document.getElementById("galleryLarge").innerHTML = "";
    document.getElementById("galleryLarge").appendChild(img);
    
    return true;
}

//Go back in gallery list
function GoBack()
{
    GalleryPosition -= 12;
    NavigateGallery(GalleryPosition, GalleryPosition+5);
    //NavigateGallery(GalleryPosition-5, GalleryPosition);
}

//Go forward (6 steps) in gallery list
function GoForward()
{
    if(GalleryPosition < arrAllImages.length)
        NavigateGallery(GalleryPosition, GalleryPosition+5);
}

//Go forward or back in gallery list
function NavigateGallery(Start, End)
{
    CheckButtons();
    //console.log("position:" + GalleryPosition +"    Start:" + Start + "     end:" + End);
    var i = Start;
    var divId = 0;
    for (i = Start; i <= End; i++)
    {
        //console.log("i:"+i);
        if(arrAllImages[i] != undefined)
        {
            var img = document.createElement("img");
            img.setAttribute("src", arrAllImages[i]+smallImagePostfix);
            img.setAttribute("id", arrAllImages[i]);
            img.setAttribute("onclick", "ShowLargeImage(this.id); return true");
            img["onclick"]=new Function("ShowLargeImage(this.id);");
            document.getElementById("gallerySmall" + divId).innerHTML = "";
            document.getElementById("gallerySmall" + divId).appendChild(img);
        }
        else
        {
            document.getElementById("gallerySmall" + divId).innerHTML = "";
        }
        //console.log("divId:"+divId);
        divId++;
        GalleryPosition++;
        //console.log("arrAllImages"+arrAllImages[i]);
    }
    CheckButtons();
}

//Sets the visibility of arrow buttons
function CheckButtons()
{
    var btn1 = document.getElementById("bntArrowForward");
    if(btn1){
	btn1.style.visibility = (GalleryPosition > arrAllImages.length) ? "hidden" : "visible";
    }

    var btn2 = document.getElementById("bntArrowBack");
    if(btn2){
        btn2.style.visibility = (GalleryPosition == 0 || GalleryPosition == 6) ? "hidden" : "visible";
    }
}
