rightClickWarning = "Nay-nay! All Images Are Copyright of RobArt Photography.";

function hasPath(sPath)
{
re = new RegExp("\/" + sPath + "(\/|$)");
return re.test(window.location)
}

if (hasPath("galleries"))
YD.addClass(document.body, "galleries");

/* ====Replace top level breadcrumb of name with Galleries, and direct to when clicked ===== */

function AdjustBreadcrumb()
{
    var str = this.innerHTML;
    str = str.replace(/\n/g, " ");
    str = str.replace(/^\s*<a .*?<\/a>/i, '<a href="/galleries" class="nav">Galleries</a>');
    this.innerHTML = str;
}

YE.onContentReady("breadCrumbTrail", AdjustBreadcrumb);


/************ flash slide shows in thumbnails **************/

function rand(n)
{
    return (Math.floor((Math.random() * n) + 1));
}

function InitiateRotateThumbs()
{
    // find all the random thumbs in the page
    var imgs = YD.getElementsByClassName("imgBorder", "img", "categoriesBox");
    var newImgs = YD.getElementsByClassName("imgBorder", "img", "subcategoriesBox");
    imgs = imgs.concat(newImgs);
    newImgs = YD.getElementsByClassName("imgBorder", "img", "galleriesBox");
    imgs = imgs.concat(newImgs);
    var randomImgs = new Array;
    for (var j in imgs)
    {
        if (imgs[j].src.search(/random\.mg/i) != -1)
        {
            randomImgs.push(imgs[j]);
        }            
    }
    if (randomImgs.length == 0)
    {
        return;
    }
    
    var count = 0;
    var skip = 4;
    if (randomImgs.length < skip)
    {
        skip = randomImgs.length;
    }
    
    var timer = window.setInterval(RotateNow, 2000);
    
    function RotateNow()
    {
        for (var i = count % skip; i < randomImgs.length; i += skip)
        {
            var img = randomImgs[i];
            var newURL = img.src.replace(/rand=\d+/, "rand=" + rand(9999));
            if (img.filters && (img.filters.length > 0))
            {
                img.filters[0].apply();
                img.src = newURL;
                img.filters[0].play();
            }
            else
            {
                img.src = newURL;
            }
        }
        ++count;
        if (count > 200)
        {
            window.clearInterval(timer);            // don't go forever
        }
    }

    
}

 YE.onDOMReady(InitiateRotateThumbs);


/*===============download enabled with originals on=============*/

function IsSmugmugView()
{
    return(YD.hasClass(document.body, "smugmug"));
}

function IsGalleryPage()
{
    return(YD.hasClass(document.body, "galleryPage"));
}

// code to insert a download button for any gallery that has originals enabled

onPhotoShow.subscribe(ProcessDownloadButton);

function ProcessDownloadButton()
{
    // set onlyInGalleries to true if you only want a download button in gallery views
    // set onlyInGalleries to false if you want a download button in other views too like (search, keywords, date, etc...)
    var onlyInGalleries = true;
    if (IsSmugmugView() && (IsGalleryPage() || !onlyInGalleries))
    {
        if (photoInfo[ImageID].albumOriginals && !photoInfo[ImageID]['protected'] && (photoInfo[ImageID].Format !== "MP4"))
        {
            var downloadParent = "cartButtonsWrapper";
            if (!document.getElementById("cartButtonsWrapper"))
            {
                downloadParent = "altViews";
            }
            InsertDownloadButton(downloadParent);
        }
        else
        {
            // disable the button
            var downloadButton = YAHOO.widget.Button.getButton("downloadButtonId");
            if (downloadButton)
            {
                downloadButton.set("disabled", true);
            }
        }
    }
}

function InsertDownloadButton(parentId)
{
    // now add the download button
    var parentDiv = document.getElementById(parentId);
    var downloadButton = document.getElementById("downloadButtonId");
    if (downloadButton)
    {
        // make sure it is enabled
        YAHOO.widget.Button.getButton("downloadButtonId").set("disabled", false);
    }
    else if (parentDiv)
    {
        var downloadButtonInfo =
        {
            id: "downloadButtonId",
            label: "Download Image...",
            container: parentDiv,
            type: "button",
            className: "sm-button sm-button-small themesButton glyphButton",
            onclick: { fn: InitiateDownloadImage }
        };
        
        var dButtonObj = new YAHOO.widget.Button(downloadButtonInfo);
    }
}

function InitiateDownloadImage()
{
    // construct the download URL
    window.location = "/photos/" + ImageID + "_" + ImageKey + "-D.jpg";
}


// This is a script to combine the categories with the gallery listing to have just one continuous listing of thumbs
YE.onContentReady("subcategoriesBox", CombineCategoriesWithGalleries);

function IsArrayEmpty(testVal)
{
    return(!testVal || (testVal.length == 0));
}

function CombineCategoriesWithGalleries()
{
    // get miniBoxes in the subcategoriesBox object
    var miniBoxes = YD.getElementsByClassName("miniBox", "div", this);
    // get target galleriesBox object
    var galleriesObj = document.getElementById("galleriesBox");
    var galleryMiniBoxes = new Array;

    // now find the right miniBox in the galleriesBox in order to insert the sub-categories
    if (galleriesObj)
    {
        galleryMiniBoxes = YD.getElementsByClassName("miniBox", "div", galleriesObj);
    }
    
    // if we don't have everything we need, then return without doing anything
    if (!galleriesObj || IsArrayEmpty(miniBoxes) || IsArrayEmpty(galleryMiniBoxes) || (window.location.hash == "#stop"))
    {
        this.style.display = "block";        
        return;
    }
    
    // move all the sub-categories over to the gallery listing
    for (var i in miniBoxes)
    {
        miniBoxes[i].parentNode.removeChild(miniBoxes[i]);
        galleryMiniBoxes[0].parentNode.insertBefore(miniBoxes[i], galleryMiniBoxes[0]);
    }
}
function RemoveGalleryWord()
{
    this.innerHTML = this.innerHTML.replace(/ Galleries$| Sub-Categories$/, "");
}

YE.onAvailable("subCatGalleryTitle", RemoveGalleryWord);
YE.onAvailable("galleryTitle", RemoveGalleryWord);

/*===============remove theme view options in drop down =============*/
// Here we can remove items from any drop-down menu by just passing:
//    the ID of the YUI button that holds the menu
//    the function that starts this all
//    The array of menu items to remove (it must be an exact text match)

YE.onContentReady("viewingStylesButton", RemoveMenuItemsFromButton, ["All Thumbs", "Slideshow"]);

function RemoveMenuItemsFromButton(textItemsToRemoveArray)
{
    // now that the object is created, we can register an interest in the beforeShowEvent
    // we can't modify the menu until then because of lazyLoading
    var menu = YAHOO.widget.Button.getButton(this.id).getMenu();
    menu.beforeShowEvent.subscribe(RemoveMenuItems, textItemsToRemoveArray);
    
    function RemoveMenuItems(event, args, data)
    {
        // called in the context of the menu
        try
        {
            // for coding efficiency in lookup, lets change the passed in array to a hash table
            var textItemsToRemove = {};
            for (var j in data)
            {
                textItemsToRemove[data[j]] = true;
            }
            
            // get the list of menu items in our menu
            var menuItems = this.getItems();
            
            // look through each menu item to see if it matches anything in our stylesToRemove array
            for (var i in menuItems)
            {
                // get the text value from the menu item
                var text = menuItems[i].cfg.config.text.value;
                // check to see if this menu item is in our remove table
                if (textItemsToRemove[text])
                {
                    // because of problems with lazy loading and deferred creation of objects, we cannot just remove the menu item here
                    // so, instead we just hide it
                    YD.setStyle(menuItems[i].id, 'display', 'none');
                }
            }
        } catch (e) {}        // catch any exceptions and ignore them - errors will just cause the styles not to get removed, but not affect any other scripts
    }
}
