/*
 * This script is used on the main page for ads
 * It makes it possible to quickly look at all the ad's images by clicking on thumbnails
 */

function setMainImage(evt) {
    evt.preventDefault();
    adImage = dojo.query('.adImagesSummary .adImage')[0];
    thumbnails = dojo.query('.imageThumbnails .adImageThumbnail');
 
    /*
     * Loop over all the thumbnails and find the one that has been clicked.
     * Then get the url to the bigImage for that thumbnail from the
     * JSON object created by the template engine
     */   
    var i = 0;
    dojo.forEach(thumbnails,
        function(thumbnail) {
            if (thumbnail == evt.target) {
                adImage.src = adImageThumbnails[i].bigImage;
            }
            i += 1;
        }
    );
}

function initImageBrowser() {
    var imageThumbnails = dojo.query('.imageThumbnails .adImageThumbnail');
    dojo.forEach(imageThumbnails,
        function(thumbnail) {
            dojo.connect(thumbnail, 'onclick', 'setMainImage');
        }
    );
}

dojo.addOnLoad(initImageBrowser);
