var currentImage = 1;
var hideTimer = false;
    
gallery = function() {      
    
    hideThumbnail = function() {
        $('thumbnailpreview').hide();
        clearTimeout(hideTimer);
    }        
        
    var thumbnails = getElementsByClassName("thumbnail", "IMG");
    for (i = 0; i < thumbnails.length; i++) {
        thumbnails[i].imagePath = i + 1;
        thumbnails[i].onmouseover = function() {

            if(hideTimer) {
                clearTimeout(hideTimer);
            }
            $('previewImage').src = "images/img" + this.imagePath + "Thumb.jpg";
            $('thumbnailpreview').show();
            $('thumbnailpreview').style.left = (this.offsetLeft - 18) + "px";
        }
        thumbnails[i].onclick = function() {
            $('gallImg').src = "images/img" + this.imagePath +".jpg";
            currentImage = this.imagePath
        }
        thumbnails[i].onmouseout = function() {
            hideTimer = setTimeout('hideThumbnail()', 250);
        }
    }
    
    nextImage = function() {
        if (currentImage >= thumbnails.length) {
            currentImage = 1;
            return 1;
        }
        else {
            currentImage++;
            return currentImage;
        }
    }
    
    previousImage = function() {
        if (currentImage <= 1) {
            currentImage = thumbnails.length;
            return thumbnails.length;
        }
        else {
            currentImage--;
            return currentImage;
        }
    }
    if($('next')) {
        $('next').onclick = function() {
            nextImg = nextImage()
            $('gallImg').src = "images/img" + nextImg + ".jpg";
        }
    }
    if($('previous')) {
        $('previous').onclick = function() {
            prevImg = previousImage()
            $('gallImg').src = "images/img" + prevImg + ".jpg";
        }
    }
}
womAdd('gallery()');
