gallery = function(limit) 
{      
    var currentImage = 0;
    var hideTimer = false;
    var imageCount = images.length;
    var tPages = Math.ceil(imageCount / limit);
    var currentPage = 1;
    var tLim = limit;
    var bLim = 0;
    
    hideThumbnail = function() 
    {
        $('thumbnailpreview').hide();
        clearTimeout(hideTimer);
    }
    
    page = function(dir) 
    {
        if(currentPage < 1)
        {
            currentPage = tPages;
        }
        else if(currentPage > tPages)
        {
            currentPage = 1;
        }
        for(i = 0; i < imageCount; i++) 
        {
            if(i < bLim  || i > tLim)
                $('thumb_' + i).hide();
            else
                $('thumb_' + i).show();
        }
        if(dir != null) 
        {     
            if(dir == "up") 
            {
                if(currentImage > tLim || (currentImage == 0 && currentPage > 1))
                {
                    currentPage++;                    
                    if(tLim >= imageCount)
                    {
                        tLim = limit;
                        bLim = 0;
                    }
                    else
                    {
                        bLim = tLim + 1;
                        tLim = bLim + limit;
                    }
                    page();
                }
            }
            else if(dir == "down") 
            {
                if(currentImage < bLim || (currentImage == imageCount - 1 && currentPage == 1))
                {
                    currentPage--;
                    tLim = bLim - 1;
                    bLim -= limit + 1;
                    if(bLim < 0)
                    {
                        bLim = imageCount - (imageCount % (limit + 1)); // That's the modulus operator, it you didn't know :) x % y divides x by y and returns remainder
                        tLim = imageCount - 1;
                    }
                    page();
                }
            }
            
        }
    }        
        
    var thumbnails = getElementsByClassName("thumbnail", "IMG");
    for (i = 0; i < thumbnails.length; i++) 
    {
        thumbnails[i].imagePath = i;
        thumbnails[i].onmouseover = function() 
        {

            if(hideTimer) {
                clearTimeout(hideTimer);
            }
            $('previewImage').src = "/uploads/images/thumb_" + images[this.imagePath];
            $('thumbnailpreview').show();
            $('thumbnailpreview').style.left = (this.offsetLeft - 18) + "px";
        }
        thumbnails[i].onclick = function() 
        {
            if ($('thumb_' + currentImage).className.indexOf(" currentThumb") >= 0)
                $('thumb_' + currentImage).className = $('thumb_' + currentImage).className.replace(new RegExp(' currentThumb\\b'), '');
            $('gallImg').src = "/uploads/images/" + images[this.imagePath];
            $('gallImg').alt = captions[this.imagePath];
            $('gallImg').title = captions[this.imagePath];
            $('gallImg').width = width[this.imagePath];
            $('gallImg').height = height[this.imagePath];
            if(credits[this.imagePath])
                $('gallCredit').innerHTML = 'Credit: ' + credits[this.imagePath];
            else
                $('gallCredit').innerHTML = '';
            $('gallCaption').innerHTML = captions[this.imagePath];
            currentImage = this.imagePath;
            $('thumb_' + currentImage).className += ' currentThumb';
        }
        thumbnails[i].onmouseout = function() 
        {
            hideTimer = setTimeout('hideThumbnail()', 250);
        }
    }
    
    nextImage = function() 
    {
        if ($('thumb_' + currentImage).className.indexOf(" currentThumb") >= 0) 
        {
            $('thumb_' + currentImage).className = $('thumb_' + currentImage).className.replace(new RegExp(' currentThumb\\b'), '')
        }
        if (currentImage >= thumbnails.length - 1) {
            currentImage = 0;
        }
        else 
        {
            currentImage++;
        }
        $('thumb_' + currentImage).className += ' currentThumb';
        page('up');      
        return currentImage;
    }
    
    previousImage = function() 
    {
        if ($('thumb_' + currentImage).className.indexOf(" currentThumb") >= 0)
            $('thumb_' + currentImage).className = $('thumb_' + currentImage).className.replace(new RegExp(' currentThumb\\b'), '');
        if (currentImage <= 0) 
        {
            currentImage = thumbnails.length - 1;
        }
        else {
            currentImage--;
        }
        $('thumb_' + currentImage).className += ' currentThumb';
        page('down');
        return currentImage;
    }
    if($('next')) 
    {
        $('next').onclick = function() 
        {
            nextImg = nextImage()
            $('gallImg').src = "/uploads/images/" + images[nextImg];
            $('gallImg').alt = captions[nextImg];
            $('gallImg').title = captions[nextImg];
            $('gallImg').width = width[nextImg];
            $('gallImg').height = height[nextImg];
            if (credits[nextImg])
                $('gallCredit').innerHTML = 'Credit: ' + credits[nextImg];
            else
                $('gallCredit').innerHTML = '';
            $('gallCaption').innerHTML = captions[nextImg];
        }
    }
    if($('previous')) 
    {
        $('previous').onclick = function() 
        {
            prevImg = previousImage()
            $('gallImg').src = "/uploads/images/" + images[prevImg];
            $('gallImg').alt = captions[prevImg];
            $('gallImg').title = captions[prevImg];
            $('gallImg').width = width[prevImg];
            $('gallImg').height = height[prevImg];
            if(credits[prevImg])
                $('gallCredit').innerHTML = 'Credit: ' + credits[prevImg];
            else
                $('gallCredit').innerHTML = '';
            $('gallCaption').innerHTML = captions[prevImg];
        }
    }
}
womAdd('gallery(18)');

