// Add Referral Code to Smugmug link
function AddReferralCode() {
var links = this.getElementsByTagName("A");
if (links && (links.length != 0)) {
var smugLink = links.item(0);
smugLink.href = "http://www.smugmug.com/?referrer=DnLmOKDRxEURs";
}
}
YE.onAvailable('footer', AddReferralCode);

// Copy home to make Galleries Page
function hasPath(sPath)
{
re = new RegExp("\/" + sPath + "(\/|$)");
return re.test(window.location)
}

if (hasPath("galleries"))
YD.addClass(document.body, "galleries");


// script to replace a user's name in the breadcrumb with "Home"
YE.onContentReady("breadCrumbTrail", ReplaceTopOfBreadcrumbWithHome);

function ReplaceTopOfBreadcrumbWithHome()
{
    var str = this.innerHTML.replace(/\n/g, " ");
    this.innerHTML = str.replace(/\>[^\<]+<\/a>/i, ">Home</a>");
}


// Redirect zoomdak to GloryOfCreation.net
//http://dgrin.smugmug.com/gallery/2160039
function redirectPath() { 
  re = /((www.)?zoomdak.smugmug.com)/; 
  tmp = window.location.href; 
  if (re.test(tmp)) { 
    tmp = tmp.replace(re, 'www.GloryOfCreation.net'); 
    window.location.href = tmp; 
  } 
}
redirectPath();  

// Ability to style or hide individual category, subcategory or gallery thumbs
// http://www.dgrin.com/showthread.php?p=1144549

//====TagThumbs====
// adds a CSS classname to the minibox for a category or sub-category thumb so you can then style them individually with CSS (including hiding them)
// in category or sub-category view, this adds a class name of the form thumbnail_catname_subcatname_galleryname
// for gallery thumbs, it also adds a class thumbnail_gallery_xxxxxxx (where xxxxxx is the gallery ID)
// If you are displaying your galleries as a flat list (no categories), 
//    then you only get the thumbnail_gallery_xxxxxxx class name (because category and subcategory are not known)
function TagThumbs() 
{
	// get current category and subcategory
	var info = GetCategoryInfo();
	
	// get list of miniBox divs in the "this" object
	var miniBoxList = YD.getElementsByClassName('miniBox', 'div', this);

	// for each miniBox, get the category or sub-category name from the albumTitle div
	for (var i = 0; i < miniBoxList.length; i++) 
	{
		// get the albumTitle p tag
		var titleTags = YD.getElementsByClassName("albumTitle", "p", miniBoxList[i]);
		if (titleTags && (titleTags.length > 0))
		{
			// get the link in the albumTitle
			var linkTags = titleTags[0].getElementsByTagName('a');
			if (linkTags && (linkTags.length > 0))
			{
				// grab the name of the category/subcategory from the thumb
				var thumbName = linkTags[0].innerHTML;
				thumbName = thumbName.replace(/\s+|\&[a-z]+;|[^_a-zA-Z0-9-]/g, "_");	// replace illegal CSS chars with underscore
				var newClassName = "thumbnail_";
				if (info.cat)
				{
					newClassName += info.cat + "_";
				}
				if (info.subcat)
				{
					newClassName += info.subcat + "_";
				}
				newClassName += thumbName;
				YD.addClass(miniBoxList[i], newClassName);
			}
		}
		
		// get the photo div in each miniBox
		var photoTags = YD.getElementsByClassName("photo", "div", miniBoxList[i]);
		if (!photoTags || (photoTags.length == 0))
		{
			photoTags = YD.getElementsByClassName("photoLarge", "div", miniBoxList[i]);
		}
		if (photoTags && (photoTags.length > 0))
		{
			// get the link in the photo tag
			var photoLinkTags = photoTags[0].getElementsByTagName('a');
			if (photoLinkTags && (photoLinkTags.length > 0))
			{
				// grab the URL of the gallery
				var link = photoLinkTags[0].href;
				// href should be "/gallery/5608799_ZJ27n"
				var matches = link.match(/\/gallery\/(\d+)_/);
				if (matches && (matches.length > 1))
				{
					YD.addClass(miniBoxList[i], "thumbnail_gallery_" + matches[1]);
				}
				
			}
			
		}
	}
}

YE.onContentReady('categoriesBox', TagThumbs);
YE.onContentReady('subcategoriesBox', TagThumbs);
YE.onContentReady('galleriesBox', TagThumbs);

//====End of TagThumbs====


//====GetCategoryInfo====
// Retrieves category and subcategory names from the body tag classes
// returns an object
function GetCategoryInfo()
{
    var info = new Object;
    info.cat = "";
    info.subcat = "";
    
    if (YD.hasClass(document.body, "category"))
    {
        var re = /category_(\S+)/i;
        var matches = re.exec(document.body.className);
        if (matches && (matches.length > 1))
        {
            info.cat = matches[1];
        }
        if (YD.hasClass(document.body, "subcategory"))
        {
            re = /subcategory_(\S+)/i;
            matches = re.exec(document.body.className);
            if (matches && (matches.length > 1))
            {
                info.subcat = matches[1];
            }
        }
    }
    return(info);
}
