// ------------------------------------------------------------------------
// script for "Vanity Urls"
// ------------------------------------------------------------------------

var vanityTable = 
 {
     Homeshoots: "http://inhomephotography.com/Other/Personal/HomeShoots/9809688_GrCuj#666180820_ivntp",
     YachatsJuly2008: "http://inhomephotography.com/Other/Personal/July-4-2008-with-Family-1/9809792_GmFz9#666189792_kfzga",
     BeforeDisneyland2007: "http://inhomephotography.com/Other/Personal/Before-Disneylaynd-1/9808998_ibrKG#666119759_2MjdP",
     MiscAngelaCam: "http://inhomephotography.com/Other/Personal/Angelas-Camera-1/9807972_GM9Ui#666033640_APK7A",
     MiscPhotos: "http://inhomephotography.com/Other/Personal/Misc-Photos-1/9809312_RKbTm#666143481_5TtDG",
     MiscPics: "http://inhomephotography.com/Other/Personal/pics-1/9807949_RmT4J#666032153_QYXn3",
     SchoolFirstDay2009: "http://inhomephotography.com/Other/School-FirstDay/9844553_6wcJm#669414497_vXto5",
     MikeMisc: "http://inhomephotography.com/Other/109CANON-1/9844531_BP9g3#669411255_H7dSd",
     Leroy: "http://inhomephotography.com/Other/Leroy-1/9844543_CUgdm#669413458_Xd5Sy",
     Disneyland2007: "http://inhomephotography.com/Other/Disneyland-1/9817838_BiHDX#666967117_NdNCB",
     MileighPhone: "http://inhomephotography.com/Other/Mileigh-1/9817698_wFsSR#666954816_AEMpi",
     MiscPersonal: "http://inhomephotography.com/P/Personal/9752248_H5oEd#660556210_iCT2y"
 };



 function IsHomePage()
 {
    return(YD.hasClass(document.body, 'homepage'));
 }
 
 function CheckRedirects()
 {
     if (IsHomePage())    // only run this code on the home page
     {
         // get the path from the current URL, 
         // convert it to lowercase and remove the leading slash
         var path = window.location.pathname.toLowerCase().substr(1);
         
         var newURL = vanityTable[path];        // look it up in our table
         
         // if we found it in the table && newURL is different than where we are
         if (newURL && (newURL != window.location))
         {
             window.location.replace(newURL);        // go to the new URL
         }
     }
 }

 function IsHomePage()
 {
    return(YD.hasClass(document.body, 'homepage'));
 }
 
 function CheckRedirects()
 {
     if (IsHomePage())    // only run this code on the home page
     {
         // get the path from the current URL, 
         // convert it to lowercase and remove the leading slash
         var path = window.location.pathname.toLowerCase().substr(1);
         
         var newURL = vanityTable[path];        // look it up in our table
         
         // if we found it in the table && newURL is different than where we are
         if (newURL && (newURL != window.location))
         {
             window.location.replace(newURL);        // go to the new URL
         }
     }
 }

// ------------------------------------------------------------------------
// 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>");
}


// ------------------------------------------------------------------------
// 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";
}