// copyright 2007 pluto911.com
var req;

function toggle(divId, src)  
{
    var status;
    status = getStatus(divId);
    hideIntroText();
    changeImage(divId, src, status);
}
function getStatus(divId)
{
    elem = document.getElementById("lastFaded");
    lastFadedId = elem.innerHTML;

    if (lastFadedId != divId)
    {
        status = 'fade';
    }
    else
    {
        status = 'notFade';
    }
    return status;
}
function changeImage(divId, src, status)
{
    if (status == "fade")
    {
        //fade this one
        void( dojo.html.setOpacity(dojo.byId(divId), 0.5) );
        if (lastFadedId && lastFadedId != divId)
        {
            // unfade the previously viewed
            unfadeLastImage();
        }

        // mark this ID so we can fade it next time...
        setLastFaded(divId);

        //write out the picture
        drawPhoto( src );
    }
    else
    {
        // redundant click: don't worry about it!
    }
}
function drawPhoto( src )
{
    var bindArgs = {
        url: src,
        error: function(type, data, evt)
        {
            alert("Sorry!  There was an error getting this photo.");
        },
        mimetype: "text/html"
    };
    req = dojo.io.bind(bindArgs);
    dojo.event.connect(req, "load", this, "populateDiv");
}
function populateDiv( type, data, evt )
{
    var photoDiv = document.getElementById("photo");
    var pageTitle;

    if (!data)
    {
        photoDiv.style.display = "none";
    }
    else
    {
        photoDiv.innerHTML = data;
        photoDiv.style.display = "";
        // set the page title for each photo
        pageTitle = document.getElementById( "pageTitle" );
        document.title = (pageTitle ? pageTitle.innerHTML : 'another pluto911 image');
    }
}
function hideIntroText()
{
    elem = document.getElementById("introduction");
    elem.style.display = "none";

    photoElem = document.getElementById("photo");
    photoElem.style.display = "block";

}
function showIntroText()
{
    // unfade the previously viewed
    unfadeLastImage();

    elem = document.getElementById("introduction");
    elem.style.display = "block";

    photoElem = document.getElementById("photo");
    // first blank out the photo so the previous doesn't flash next we load an image
    photoElem.innerHTML = "";
    photoElem.style.display = "none";

    //clear out last faded so user can click on the last image viewed before reading intro text
    setLastFaded("");
    // finally, set the document title
    document.title = document.getElementById( "galleryDisplayTitle").innerHTML;
}
function setLastFaded(divId)
{
    // set up the "marker" to hold the ID of the most recently viewed pic so we can calculate what to do with it later
    marker = document.getElementById("lastFaded");
    marker.innerHTML = divId;

}
function unfadeLastImage()
{
    elem = document.getElementById("lastFaded");
    lastFadedId = elem.innerHTML;
    void( dojo.lfx.html.fadeIn(lastFadedId, 300).play() );
}