// On IE/FF set the title before the Document OnLoad takes place
document.title = "Ashish Pandey Photography";

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

addEvent( window, "load", OnLoad );

function OnLoad()
{
	CustomizeTitle();
	//GuestBook();
	//CustomizeFooter();
} // OnLoad

// change the link on the SmugMug Logo
YE.onAvailable('homelink', function(e) {
  this.href = "/";
  this.firstChild.alt = "Ashish Pandey Photography";
  this.firstChild.title = "Ashish Pandey Photography";
});

// removes the pipes characters in the toolbar header
YE.onAvailable('toolbar', function(e) {
  this.innerHTML = this.innerHTML.replace(new RegExp(/\|/g),"");
});

function hasPath(sPath)
{
re = new RegExp("\/" + sPath + "(\/|$)");
return re.test(window.location)
}

/*** Gallery Redirects ***/
var galleryRedirectTable = 
{
    "6717305_nenjy" : "http://photography.ashish-pragya.com/share/D0FvDTYkHfiwY",
    "6726098_xjcvy" : "http://photography.ashish-pragya.com/Travel",
    "6726100_zwo4t" : "http://photography.ashish-pragya.com/share/0kXY4OLTvLTYg",
    "6726103_wyqmj" : "http://photography.ashish-pragya.com/Events",
    "6726181_25ydm" : "http://photography.ashish-pragya.com/AllGalleries",
    "6989029_xvnev" : "http://photography.ashish-pragya.com/Gear%20and%20Equipment",
    "7116035_vow9h" : "http://photography.ashish-pragya.com/Personal"
};
var homepageRedirectTable = 
{
    "galleryindex.html" : "http://photography.ashish-pragya.com/AllGalleries",
    "chinaindex.html" : "http://photography.ashish-pragya.com/Travel/646779",
    "macau.html" : "http://photography.ashish-pragya.com/gallery/6461913_XF2Gk",
    "exttubemacro.html" : "http://photography.ashish-pragya.com/gallery/6462524_nYSsy",
    "dragonandphoenix.html" : "http://photography.ashish-pragya.com/gallery/6462339_7RgxJ",
    "cny07parade.html" : "http://photography.ashish-pragya.com/gallery/6462432_XEwxC"
};

function CheckRedirects()
 {
  if (! YD.hasClass(document.body, 'loggedIn'))
     {
         // get the path from the current URL, 
         // convert it to lowercase and remove the leading slash
         var path = window.location.pathname.toLowerCase().substr(window.location.pathname.lastIndexOf('/')+1); 
         
         var newURL = galleryRedirectTable[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
         }
         else if( window.location.pathname.toLowerCase().endsWith('.html') )
         {
             // try homepage redirects
             path = window.location.pathname.toLowerCase().substr(1);
             newURL = homepageRedirectTable[path];        // look it up in our table
             if (newURL && (newURL != window.location))
             {
                 window.location.replace(newURL);        // go to the new URL
             }
         }
     }
 }
/*** End Redirects ***/

function Trim( text )
{
	text = text.replace(/(^\s+)|(\s+$)/g, ""); // trim leading and trailing white space
	return text;
} // Trim

function GetText( node )
{
	if( !node )
		return "";
	if( node.innerText )
		return Trim( node.innerText ); // For IE
	if( node.textContent )
		return Trim( node.textContent ); // For others
	return "";
} // GetText

function GetPhotoTitle()
{
	// If the photo title is set, it starts with the breadcrumb.
	var breadCrumbStart = "Michael Fried > ";
	var mainPhoto = document.getElementById("mainPhoto");
	if( !mainPhoto || !mainPhoto.title || mainPhoto.title.indexOf( breadCrumbStart ) != 0 )
		return "";
	return Trim( mainPhoto.title.substr( breadCrumbStart.length ) );
} // GetPhotoTitle

/* Context Sensitive Page Title */
function CustomizeTitle()
{
	var baseTitle = "Ashish Pandey Photography";
	var separator = " - ";
	var albumTitle = GetText( document.getElementById("albumTitle") );
	var galleryTitle = GetText( document.getElementById("galleryTitle") );
	var singleImage = document.body.className && document.body.className.indexOf("singleImage") > -1 ? true : false;
	var pageTypeDefined = typeof( pageType ) != "undefined";
	var pageTypeDetailsDefined = typeof( pageTypeDetails ) != "undefined";

	// Don't change the title from the above document.title on the homepage
	if( document.body.className && document.body.className.indexOf("homepage") > -1 )
	{
		document.title = baseTitle + separator + "Home";
		return;
	}

	// The guestbook album gets a special title
	if( window.AlbumID && window.AlbumID == "6477629" )
	{
		// Guest Book
		document.title = "Ashish Pandey's Guestbook";
		return;
	}

	// An album page (holds a bunch of photos, could be "zoomed in" on a specific photo)
	if( albumTitle )
	{
		var photoTitle = GetPhotoTitle();
		if( photoTitle )
			document.title = baseTitle + separator + albumTitle + separator + photoTitle;
		else
			document.title = baseTitle + separator + albumTitle;
		return;
	}

	// A gallery page (holds a bunch of albums and/or a bunch of sub categories)
	if( galleryTitle )
	{
		// Strip " sub-categories" off the end of the category text
		var finalPositionCategory = galleryTitle.indexOf(" sub-categories");
		if( finalPositionCategory > -1 )
			galleryTitle = galleryTitle.substr( 0, finalPositionCategory );























		else
		{
			// Strip " galleries" off the end of the category/sub-category text
			var finalPositionSubCategory = galleryTitle.indexOf(" galleries");
			if( finalPositionSubCategory > -1 )
				galleryTitle = galleryTitle.substr( 0, finalPositionSubCategory );
		}
		document.title = baseTitle + separator + galleryTitle;
		return;
	}

	// A single image (like the kind you get when you click an image in a keyword page)
	if( singleImage )
	{
		var photoTitle = GetPhotoTitle();
		if( photoTitle )
			document.title = baseTitle + separator + photoTitle;
		else
			document.title = baseTitle + separator + "untitled photo";
		return;
	}

	// A single keyword page
	if( pageTypeDefined && pageType == 'Keyword' && pageTypeDetailsDefined )
	{
		// Keyword page
		document.title = baseTitle + separator + "Keyword: " + pageTypeDetails;
		return;
	}

	// A multiple keyword page
	if( pageTypeDefined && pageType == 'Keywords' && pageTypeDetailsDefined )
	{
		// Multiple keywords page
		// Put " + " between each keyword instead of -
		var keywordList = pageTypeDetails;
		keywordList = keywordList.replace(/\-/g, " + ");
		document.title = baseTitle + separator + "Keywords: " + keywordList;
		return;
	}

	// The main keywords page. Note: single image takes precedence over this type
	if( document.body.className && document.body.className.indexOf("keywordPage") > -1 )
	{
		// Main Keywords Page
		document.title = baseTitle + separator + "Keywords";
		return;
	}

	// None of the rules above set the title! Oh well, fall back on the base title.
	// It would be nice to do some kind of error reporting here, such as injecting
	// some code here to request a special page from another another server which
	// includes this page's URL... Something like a 0x0 pixel image with the source
	// link from one of my Techhouse pages might do the trick, and then I could use
	// a cgi-bin script to mail myself with the URLs and browser tags when this was
	// hit... techhouse members use an interesting variety of browsers.
	document.title = baseTitle;
} // CustomizeTitle
