function initGallery() {
    function fadeOut(id) {
        log("fading out "+id);
        runAnim( id, { opacity: { to: 0.0, duration: 0.2 } } );
    }

    function fadeIn(id) {
        log("fading in "+id);
        runAnim( id, { opacity: { to: 0.9, duration: 0.2 } } );
    }

    function runAnim( id, attributes ) {
        if (window.anim == null) window.anim = [];
        window.anim[id] = new YAHOO.util.Anim("message_" + id, attributes );
        window.anim[id].animate();
    }

    function log ( msg ) {
        if ( window.console ) console.log( msg );
    }

    function mouseHandler(e) {
        var target = YAHOO.util.Event.getTarget(e);
        while (target.id != "content") {
            if (target.nodeName.toUpperCase() == "DIV") {
                targetId = target.id || "";
                if ( targetId != "" &&
                     targetId != window.currentElement) {
                    currentId = window.currentId || 0;
                    currentElement = window.currentElement || "";
                    newId = targetId.indexOf("message_") == -1  &&
                            targetId.indexOf("image_") == -1
                        ? 0
                        : targetId.split("_")[1];

                    log("checking target "+targetId+" (newId: "+newId+", old id: "+currentId+", old element: "+currentElement+")");
                    if ( newId != 0 ) {
                        if ( newId != currentId ) {
                            if ( currentId != 0 ) {
                                fadeOut( currentId );
                            }
                            window.currentId = newId;
                            fadeIn(newId);
                        }

                    } else if ( currentElement.indexOf("image_") != -1) {
                        log( "mouse is leaving image to gallery. fade out..." );
                        fadeOut( currentElement.replace( "image_", "" ) );
                        window.currentId = 0;
                    }
                    window.currentElement = targetId;
                }
                break;
            }

            // traverse up the node hierarchy.
            target = target.parentNode;
        }
        Event.stopEvent(e);
    }

    // only one event handler, saves memory.
    YAHOO.util.Event.on("contentbox", "mouseover", mouseHandler);
}

loader = new YAHOO.util.YUILoader();
loader.require( "animation" );
loader.insert( initGallery );

