var currentCaptionId = 'caption-1';
var currentThumbId = 'thb1';
var slideWidth = '20px';
var debug = false;

function info (info) {
    if (debug) alert(info);
}

function changeMainImage (thumb, mainImagePath, mainImgSrc) {
    //change large image
	var lgImgObj    = document.getElementById('mainImg');
	lgImgObj.src    = 'images/'+ mainImagePath +'/'+ mainImgSrc +'.jpg';
	lgImgObj.alt    = thumb.alt;
	lgImgObj.title  = thumb.title;

    //change caption
    changeCaption(mainImgSrc);
}

function changeCaption (newCaptionId) {
    if (document.getElementById(currentCaptionId)) {
        info('changing caption from '+ currentCaptionId + ' to '+ newCaptionId);
        if (currentCaptionId != null) document.getElementById(currentCaptionId).className='';
        currentCaptionId = 'caption-'+ newCaptionId;
        document.getElementById(currentCaptionId).className='current';
    }
}

function slideThumb(thumb, direction) {
    //get current thumb
    var current = document.getElementById(currentThumbId);

    //if new thumb is not current thumb
    if (currentThumbId != thumb.id) {
        switch (direction) {
            case 'left': {
                //slide new thumb
                thumb.style.marginLeft = '0';
                //slide old thumb back
                current.style.marginLeft = slideWidth;
                break;
            }
            case 'right': {
                thumb.style.marginLeft = slideWidth;
                current.style.marginLeft = '0';
                break;
            }
        }
        //set the current thumb
        currentThumbId = thumb.id;
    }
    //remove focus from thumb
    thumb.blur()
    document.body.focus();
}

function slideLeft (thumb, mainImagePath, mainImgSrc) {
    changeMainImage(thumb, mainImagePath, mainImgSrc);
    slideThumb(thumb, 'left'); 
}

function slideRight (thumb, mainImagePath, mainImgSrc) {
    changeMainImage(thumb, mainImagePath, mainImgSrc);
    slideThumb(thumb, 'right'); 
}

function preloadImages (theimages) {
    for ( var i = 0; i < theimages.length; i++ ) {
        var oImage = new Image;
        oImage.src = theimages[i];
    }
}

