// use double slashes to make notes or describe code

//====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);
}


//-----Seperate homepage and gallery code
function hasPath(sPath)
{
re = new RegExp("\/" + sPath + "(\/|$)");
return re.test(window.location)
}

if (hasPath("galleries"))
YD.addClass(document.body, "galleries");

//-----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);

//----- removes word gallery and stuff----- 
//function RemoveGalleryWord()
//{
//    this.innerHTML = this.innerHTML.replace(/ Galleries$| Sub-Categories$/, "");
//}
//
//YE.onAvailable("subCatGalleryTitle", RemoveGalleryWord);
//YE.onAvailable("galleryTitle", RemoveGalleryWord);