﻿// add onload="scrollGallery(theimages, imgPlaces, imgWidth, imgHeight, imgSpacer, dir, newWindow)" to the opening BODY tag
// 0 = Open a new window for links 0 = no  1 = yes
// dir = 0 - Left / dir = 1 - Right / dir = 2 - up / dir = 3 - down

moz = document.getElementById && !document.all

step = 1
timer = ""
nextPic = 0
initPos = new Array()
nowDivPosH = new Array()
nowDivPosV = new Array()

function scrollGallery(theimages, imgPlaces, imgWidth, imgHeight, imgSpacer, dir, speed, newWindow) {

    this.theimages = theimages;
    this.imgPlaces = imgPlaces;
    this.imgWidth = imgWidth;
    this.imgHeight = imgHeight;
    this.imgSpacer = imgSpacer;
    this.dir = dir;
    this.newWindow = newWindow;
    this.speed = speed;

    if (theimages.length == 0)
        return;
        
    for (var i = 0; i < imgPlaces + 1; i++) { // create image holders
        newImg = document.createElement("IMG")
        newImg.setAttribute("id", "pic_" + i)
        newImg.setAttribute("src", "")
        newImg.style.position = "absolute"
        
        newImg.style.width = imgWidth + "px"
        newImg.style.height = imgHeight + "px"
        
        newImg.style.border = 0
        newImg.alt = ""
        newImg.i = i
        newImg.onclick = function() { his3Win(theimages[this.i][2]) }
        document.getElementById("display_area").appendChild(newImg)
    }

    containerEL = document.getElementById("his3container")
    displayArea = document.getElementById("display_area")
    pic0 = document.getElementById("pic_0")

    containerBorder = (document.compatMode == "CSS1Compat" ? 0 : parseInt(containerEL.style.borderWidth) * 2)

    if (dir == 0 || dir == 1) {
        containerWidth = (imgPlaces * imgWidth) + ((imgPlaces - 1) * imgSpacer)
        containerHeight = imgHeight
    }
    else {
        containerWidth = imgWidth
        containerHeight = (imgPlaces * imgHeight) + ((imgPlaces - 1) * imgSpacer)
    }
    containerEL.style.width = containerWidth + (!moz ? containerBorder : "") + "px"
    containerEL.style.height = containerHeight + (!moz ? containerBorder : "") + "px"
    displayArea.style.width = containerWidth + "px"
    displayArea.style.clip = "rect(0," + (containerWidth + "px") + "," + (containerHeight + "px") + ",0)"

    displayArea.onmouseover = function() { stopHIS3() }
    displayArea.onmouseout = function() { scrollHIS3() }

    imgPos = -pic0.width

    if (dir == 3)
        imgPos = -pic0.height
        
    for (var i = 0; i < imgPlaces + 1; i++) {
        currentImage = document.getElementById("pic_" + i)

        // if left - start left side of first picture at left edge of container
        if (dir == 0) {
            imgPos += pic0.width + imgSpacer
        }
        else if (dir == 2) {
            imgPos += pic0.height + imgSpacer
        }

        initPos[i] = imgPos

        if (dir == 0) {
            currentImage.style.left = initPos[i] + "px"
        }
        else if (dir == 1) {
            document.getElementById("pic_" + [(imgPlaces - i)]).style.left = initPos[i] + "px"
            imgPos += pic0.width + imgSpacer
        }
        else if (dir == 2) {
            currentImage.style.left = "0px"
            currentImage.style.top = initPos[i] + "px"
        }
        else {
            currentImage.style.left = "0px"
            document.getElementById("pic_" + [(imgPlaces - i)]).style.top = initPos[i] + "px"
            imgPos += pic0.height + imgSpacer
        }
        
        if (nextPic == theimages.length) { nextPic = 0 }
        currentImage.src = theimages[nextPic][0]
        currentImage.alt = theimages[nextPic][1]
        currentImage.i = nextPic
        currentImage.onclick = function() { his3Win(theimages[this.i][2]) }
        nextPic++
    }

    scrollHIS3()
}

timer = ""
function scrollHIS3() {
    
    // Empty Gallery
    if (theimages.length == 0)
        return;
        
    clearTimeout(timer)
    for (var i = 0; i < imgPlaces + 1; i++) {
        currentImage = document.getElementById("pic_" + i)

        nowDivPosH[i] = parseInt(currentImage.style.left)
        nowDivPosV[i] = parseInt(currentImage.style.top)

        if (dir == 0) { nowDivPosH[i] -= step }
        if (dir == 1) { nowDivPosH[i] += step }
        if (dir == 2) { nowDivPosV[i] -= step }
        if (dir == 3) { nowDivPosV[i] += step }

        if (dir == 0 || dir == 1) {
            if (dir == 0 && nowDivPosH[i] <= -(pic0.width + imgSpacer) || dir == 1 && nowDivPosH[i] > containerWidth) {

                if (dir == 0) { currentImage.style.left = containerWidth + imgSpacer + "px" }
                if (dir == 1) { currentImage.style.left = -pic0.width - (imgSpacer * 2) + "px" }

                if (nextPic > theimages.length - 1) { nextPic = 0 }

                currentImage.src = theimages[nextPic][0]
                currentImage.alt = theimages[nextPic][1]
                currentImage.i = nextPic
                currentImage.onclick = function() { his3Win(theimages[this.i][2]) }

                nextPic++
            }
            else {
                currentImage.style.left = nowDivPosH[i] + "px"
            }
        }
        else {
            if (dir == 2 && nowDivPosV[i] <= -(pic0.height + imgSpacer) || dir == 3 && nowDivPosV[i] > containerHeight) {

                if (dir == 2) { currentImage.style.top = containerHeight + imgSpacer + "px" }
                if (dir == 3) { currentImage.style.top = -pic0.height - (imgSpacer * 2) + "px" }

                if (nextPic > theimages.length - 1) { nextPic = 0 }

                currentImage.src = theimages[nextPic][0]
                currentImage.alt = theimages[nextPic][1]
                currentImage.i = nextPic
                currentImage.onclick = function() { his3Win(theimages[this.i][2]) }

                nextPic++
            }
            else {
                currentImage.style.top = nowDivPosV[i] + "px"
            }
        }
    }
    timer = setTimeout("scrollHIS3()", speed)

}

function stopHIS3() {
    clearTimeout(timer)
}

function his3Win(loc) {
    if (loc == "") { return }
    if (newWindow == 0) {
        location = loc
    }
    else {
        //window.open(loc)
        newin = window.open(loc, 'win1', 'left=430,top=340,width=300,height=300') // use for specific size and positioned window
        newin.focus()
    }
}