// -----------------------------------------------------------
// Code to add descriptions to categories and/or sub-categories
// -----------------------------------------------------------

function addCategoryTitleToBreadcrumb(description) 
{
    var breadCrumb = YD.get("breadcrumb");
    if (breadCrumb)
    {
        var divTag = document.createElement("div");
        divTag.className = "categoryDescription";
        divTag.innerHTML = description;
        breadCrumb.parentNode.insertBefore(divTag, breadCrumb.nextSibling);
    }
}

function addCategoryTitleToThumbs(descriptionObject, boxObjectName) 
{
    var re = /\>([^\<]+)<\/a>/i;    // pattern to find the category name between the <a> and </a> tags
    var divTag = YD.get(boxObjectName);
    if (divTag) 
    {
        var divTags = YD.getElementsByClassName("albumTitle", "p", divTag);
        for (var i = 0; i < divTags.length; i++) 
        {
            var matches = re.exec(divTags[i].innerHTML);    // get just the category name
            // if we found a category name and the category name exists in our categoryDescription object, then add the description
            if (matches && (matches.length > 1) && descriptionObject[matches[1]] != undefined) 
            {
                var pTag = document.createElement("p");
                pTag.className = "categoryDescription";
                pTag.innerHTML = descriptionObject[matches[1]];
                divTags[i].parentNode.insertBefore(pTag, divTags[i].nextSibling);
            }
        }
    }
}

/* Category descriptions */
function addCategoryDescription() 
{
    var categoryDescription = {
        // list "categoryname" : "category title",
        // or "categoryname.subcategoryname" : "subcategory title",
        // Examples:
        // "Kenya"  : "Our vacation to Kenya",
        // "Kenya.Highlights" : "The highlights galleries from our vacation to Kenya",
        "Theater"  : "The Theatre collection contains galleries of photos from different theatres and productions.  Most photos are from photo calls.  If you are looking for a theatre photographer please feel free to contact me!",
        "Nature" : "The Nature collection contains galleries of photos of the world around us.  Many photos have come from trips that I have taken or from outdoor activities that I have taken part in.",
        "Travel" : "This collection of galleries is from travel that I have done.  Photos here may be more 'touristy' however I always look for a good shot.",
        "Links"  : "Here are links to some of my favorite sites and my friends sites.",
        "Pioneer_Theatre_Company" : "<p style='text-align:center;'><span class='spcat'><b>The Pioneer Theatre Archives</b></span></p><div align='justify'><p>As the current staff photographer for the Pioneer Theatre Company, here you will find all of the photos that I have taken for PTC.  This includes publicity, rehearsal, and archival photos as well as other PTC events.  Photos are devided into galleries based on the season in which they were shot and what the photo shoot was for.  All images from rehearsals and archival photo calls will be available in the 'Archival' category for any given season.  All photos taken for media and publicity will be available in the 'Publicity' category.  Photos that don't fall into either category will be placed in the 'Misc.' galleries.</p><p>All photos are available for sale as prints, downloads and as various pieces of merchandise.  All images sold through this website are for <u>personal use only</u>.  If you wish to use images for media or marketing purposes please contact the Pioneer Theatre Company <a href='http://ww2.pioneertheatre.org/press-room'>marketing department</a>.</p><p>Please note that at this time we are <b>not</b> offering a CD/DVD of show images.  Due to the overwhelming ease of duplication of CD's and DVD's we have decided to not continue to offer this as a distribution option.  We will continue to explore ways to make this an option in the future, and if we do we will make it available for any shows hosted on this site.</p><p>There is an easy way to share images from this site on many of your favorite social networking services.  At the top of every page you will find a 'Share' button.  From there you can get direct links to photos which you can post in your blog, or on your favorite internet forum. You can also connect to services like Facebook, Twitter, MySpace, and many more.  All of these options offer free sharing, the only caveat is that shared images will carry the same watermark that you see when viewing them in the galleries.  Please make sure that if you choose to share images you follow any applicable union rules such as listing the names of cast members appearing in the photos.</p><p>If you have any questions or comments about the photos or services offered here please feel free to <a href='http://icewolfphoto.smugmug.com/gallery/8203623_xXREb'>contact me.</a>  Also please feel free to leave comments or critiques in the individual galleries or in the <a href='http://www.icewolfphotography.com/gallery/3617541'>guestbook!</a></p></div><p></p><hr><p><span style='font-size:150%'>Announcement!</span></p><p>I am trying out a new feature and offering holiday greeting cards with photos from 'A Chrsitmas Story.' You can find the cards for purchase in the <a href='http://www.icewolfphotography.com/Pioneer-Theatre-Company/09-10-Misc/cs09cards/'>'A Christmas Story' Cards gallery</a>. Please leave some feedback so I can help get you cards that you would like to see. Let me know if you like the cards, what other photos you would like to see as cards, and any other feedback you have. Enjoy!</p><p></p><hr><p style='font-size:80%; text-align:center'>All photos that appear on this site are ©IceWolf Photography as of the date that they were posted to this site.  All rights reserved.  IceWolf Photography will not be responsible for any misuse of photos by visitors to this site, and reserves the right to report any misuse of photos to the appropriate parties.</p><hr><p style='font-size:80%; text-align:center'>Photos from seasons prior to the 2009-2010 season are still available for purchase.  If you are interested in past shows please call ahead and visit the PTC marketing department.</p></p><p></p><hr><p></p>",
    };
    
    var re, matches, i;        // various local variables

    // now fix it so that it works automatically even if the category or sub-category name has spaces in it
    // we replace those spaces with underscores (which is what the classname does) and add those to our object so we can match those too
    for (i in categoryDescription) 
    {
        var newName = i.replace(/ /g, "_");
        categoryDescription[newName] = categoryDescription[i];    // add a property to the object that has only underscores in the name
    }
    // on the homepage, we want to check for category names and add a description if a match found
    // on a category page, we want to check to see if the category that the page is needs a description under the breadcrumb
    //      and, we need to see if any of the sub-category items on the page need us to add a description under the name
    // on a sub-category page, we want to check to see if the sub-category that the page is needs a description under the breadcrumb
    if (YD.hasClass(document.body, "category")) 
    {
        // fetch the category name
        re = /category_(\S+)/i;
        matches = re.exec(document.body.className);
        if (matches && (matches.length > 1)) 
        {
            var categoryName = matches[1];
            // now see if we have a subcategory too
            if (YD.hasClass(document.body, "subcategory")) 
            {
                re = /subcategory_(\S+)/i;
                matches = re.exec(document.body.className);
                if (matches && (matches.length > 1)) 
                {
                    var subcatName = matches[1];
                    // category and subcategory so we are on a subcategory page showing a list of galleries in this subcategory
                    // we need to just add a subcategory title to this page if the category-subcategory matches
                    var fullName = categoryName + "." + subcatName;
                    if (categoryDescription[fullName])
                    {
                        addCategoryTitleToBreadcrumb(categoryDescription[fullName]);
                    }
                }
            }
            // here we're on a category page
            // we need to add a category description for the category page
            // and potentially add subcategory descriptions to the subcategory names displayed on this page
            else 
            {
                if (categoryDescription[categoryName])
                {
                    addCategoryTitleToBreadcrumb(categoryDescription[categoryName]);
                }
                // now we need to build a temporary subcategoryDescription object that has only the subcategory names in it that are in this category
                var subcatDescriptions = {};
                re = new RegExp("^" + categoryName + "\\.(.+)$", "i");
                for (i in categoryDescription)
                {
                    matches = re.exec(i);
                    if (matches && (matches.length > 1))
                    {
                        subcatDescriptions[matches[1]] = categoryDescription[i];
                    }
                }
                addCategoryTitleToThumbs(subcatDescriptions, "subcategoriesBox");
            }
        }
    }

    // then see if we're on the homepage
    if (YD.hasClass(document.body, "homepage")) 
    {
        addCategoryTitleToThumbs(categoryDescription, "categoriesBox");
    }
}

YE.onDOMReady(addCategoryDescription);

// ------------------------------------------------------------------
// End of code to add descriptions to categories and/or sub-categories
// ----------------------------------------------------------------- 

function RemoveGalleryWord()
{
    this.innerHTML = this.innerHTML.replace(/ Galleries$| Sub-Categories$/, "");
}

YE.onAvailable("subCatGalleryTitle", RemoveGalleryWord);
YE.onAvailable("galleryTitle", RemoveGalleryWord);

//------------------------------------------------------------------------------------
// Move share button to the cart line
//-------------------------------------------------------------------------------------

YE.onContentReady("shareButton", MoveToCartDiv);

function MoveToCartDiv()
{
    MoveObjToDiv(this, "cartButtonsWrapper");
}

function MoveObjToDiv(sourceObj, destDiv)
{
    var destDivObj = document.getElementById(destDiv);    // get the object for the dest div
    if (sourceObj && destDivObj)
    {
        sourceObj.parentNode.removeChild(sourceObj);    // remove object from it's current parent
        destDivObj.appendChild(sourceObj);                // add it to the new parent
    }    
}
//------------------------------------------------------------------------------------
// END Move share button to the cart line
//-------------------------------------------------------------------------------------

// ------------------------------------------------------------------------
// Code to insert a download button 
// 
// Works for any gallery that has originals enabled
// And right-click protection off
// And gallery is in smugmug or smugmug small view
// ------------------------------------------------------------------------

function IsAnySmugmugView()
{
    return(YD.hasClass(document.body, "smugmug") || YD.hasClass(document.body, "smugmug_small"));
}

function IsGalleryPage()
{
	return(YD.hasClass(document.body, "galleryPage"));
}

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 = false;
	if (IsAnySmugmugView() && (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";
}