//----------------------------------------------------------------------------
//  This is a script to assign the "x" key as a shortcut key 
// for Tools/This Photo/Delete.
//----------------------------------------------------------------------------
// only install this keystroke handler if we're logged in
if (YD.hasClass(document.body, "loggedIn"))
{
    YE.onContentReady("smugmug", ConfigureKeyHandlers);
}

function GetKeyCode(letter)
{
    return(letter.charCodeAt(0));
}

function ConfigureKeyHandlers()
{
    // lowercase x key
    var keyhandler1 = new YAHOO.util.KeyListener(document, {keys:GetKeyCode("X")}, HandleKeyFunc);
    keyhandler1.enable();
    // uppercase x key    
    var keyhandler2 = new YAHOO.util.KeyListener(document, {shift:true, keys:GetKeyCode("X")}, HandleKeyFunc);
    keyhandler2.enable();
    
    function HandleKeyFunc(type, args, obj)
    {
        // find out what object the key was targeted to
        var nodeName = YE.getTarget(args[1]).nodeName.toLowerCase();
        // don't do anything if the key is in an input field or a text area
        if (nodeName == "input" || nodeName == "textarea" || nodeName == "select")
        {
            return;        // do nothing
        }
        // http://jfriend.smugmug.com/photos/tools.mg?ImageID=344290962&ImageKey=h6JjS&tool=delete&url=http%3A%2F%2Fjfriend.smugmug.com%2Fgallery%2F5608869_vwzCG%23344290962_h6JjS
        // find the next image in the view so after deletion, we take the user there
        var nextImageID = null;
        var nextLocation = location.href;        // default to current location
        for (var i = 0; i < imageIDs.length; i++)
        {
            if (imageIDs[i] == ImageID)
            {
                // fetch the next imageID (if there is one)
                if ((i + 1) < imageIDs.length)    
                {
                    nextImageID = imageIDs[i + 1];
                }
                // fetch the previous imageID (if there is one)
                else if (i > 0)
                {
                    nextImageID = imageIDs[i - 1];
                }
                break;
            }
        }
        if (nextImageID)
        {
            // construct the URL for the next image so we don't lose our place in the view after deleting
            var nextImageKey = photoInfo[nextImageID].ImageKey;
            var curLocation = new String;
            curLocation = location.href;
            nextLocation = curLocation.replace(/#.*$/, "#" + nextImageID + "_" + nextImageKey);
        }
        location.href = "/photos/tools.mg?ImageID=" + ImageID + "&ImageKey=" + ImageKey + "&tool=delete&url=" + encodeURIComponent(nextLocation);
    }
}

// Script to change the breadcrumb link to your homepage to point to your galleries page instead of your slideshow
// It also changes that link to say "Galleries" instead of your user name

function AdjustBreadcrumb()
{
    // there are something like six different forms of the breadcrumb including search and keyword and date and communities, categories and galleries that we have to make this work for
    var tags = YD.getElementsByClassName("nav", "a", this);        // get all the <a> tags with class "nav"
    var filteredTags = new Array;
    // filter out any that aren't at the top level in the breadCrumbTrail (this gets rid of the relatedDate tags)
    for (var i in tags)
    {
        if (tags[i].parentNode == this)
        {
            filteredTags.push(tags[i]);
        }
    }
    if (filteredTags.length == 0)
    {
        return;
    }
    // default to targeting the first filtered tag
    var targetTag = filteredTags[0];
    
    // see if we have a community here
    if (filteredTags.length > 1)
    {
        // if we have a community here, then the user top level is in the 2nd position
        if (filteredTags[0].href.search(/\/community\//) != -1)
        {
            targetTag = filteredTags[1];
        }
    }
    // make sure URL ends with a slash
    var str = targetTag.href;
    if (str.search(/\/$/) == -1)
    {
        str += "/";
    }
    str +="galleries";
    targetTag.href = str;
    targetTag.innerHTML = "Galleries";
}

YE.onContentReady("breadCrumbTrail", AdjustBreadcrumb);