YAHOO.namespace("YAHOO.jimbourke");

YAHOO.jimbourke.artwork_modules = new Array();
YAHOO.jimbourke.ids_to_modules = new Object();

function artworkClickHandler(e, o) {
    // Prevent the browser from following the link:
    YAHOO.util.Event.preventDefault(e);
    for ( i = 0; i < YAHOO.jimbourke.artwork_modules.length; i++ ) {
        YAHOO.jimbourke.artwork_modules[i].hide();
    }
    o.show();
}

YAHOO.jimbourke.initModules = function() {
    /* init thumbnails */
    var thumbnails = YAHOO.util.Dom.getElementsByClassName("gallery_thumbnail", "a");

    firstModule = false;

    // register an image loader for the images browseable through the gallery,
    // triggered on mouse over of the gallery box with a 1 s time out
    var imgLdr = new YAHOO.util.ImageLoader.group("thumbnails" , "mouseover" , 1);

    for (i = 0; i < thumbnails.length; i++) {
        thumbnail = thumbnails[i];
        thumbnailId = thumbnail.id;
        moduleId = "im" + thumbnailId.substring(1);
        module = new YAHOO.widget.Module(moduleId);

        if ( firstModule == false && YAHOO.util.Dom.hasClass(thumbnail, "first_image") ) {
            firstModule = module;
        } else {
            module.hide();

            // get the URL of the full image to load
            fullImageHref = thumbnail.href;

            // create the DOM id of the img via a heuristic
            fullImgId = "f" + thumbnailId.substring(1);

            // register the image to be loaded with the image loader
            imgLdr.registerSrcImage(fullImgId, fullImageHref);
        }

        YAHOO.jimbourke.artwork_modules.push( module );
        YAHOO.jimbourke.ids_to_modules[moduleId] = module;

        YAHOO.util.Event.addListener(thumbnail, "click", artworkClickHandler, module); 
    }
}

// Additional links to existing modules - links only, don't create
// new modules. This is used to link back to the main image in a set
// of 3
YAHOO.jimbourke.initAdditionalModuleLinks = function() {
    // get all 'module link elements'
    additional_gallery_links = YAHOO.util.Dom.getElementsByClassName("gallery_link", "a");
    // for each module link
    for ( i=0; i < additional_gallery_links.length; i++ ) {
        // retrieve the module id from the link href
        targetModuleString = additional_gallery_links[i].href;
        // since the module ID is in the href, the browser turns it into
        // a full URL - here we snip off the prefix and obtain just the id
        targetModuleId = targetModuleString.substr( targetModuleString.lastIndexOf("/") + 1);
        // look up the module in the hash
        targetModule = YAHOO.jimbourke.ids_to_modules[targetModuleId];
        // add an event handler to the element to display that module
        YAHOO.util.Event.addListener(additional_gallery_links[i], "click", artworkClickHandler, targetModule);
    }
}

YAHOO.util.Event.onDOMReady( YAHOO.jimbourke.initModules );
YAHOO.util.Event.onDOMReady( YAHOO.jimbourke.initAdditionalModuleLinks );
