//====================================
//
//	PhotoLouis Smugmug Java Script 
//
//=====================================




/* Code for specific galleries */
	function getGalleryHeader()
	{
                retParms = new Array;
                retParms[0] = "transparent";     // Background Color
                retParms[1] = true;              // Slide show
		retParms[4] = "<ul>";
		retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com'>Home</a></li>";
		retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com/gallery/2053282'>About Me</a></li>";
		retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com/galleries'>Galleries</a></li>";
		retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com/gallery/2066047'>Weddings</a>";
		retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com/info'>Information</a></li>";
		retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com/guestbook'>Guestbook</a></li>";
		retParms[4] = retParms[4] + "</ul>"
		
		if (hasPath("gallery/2059076")==true || hasPath("gallery/2071128")==true || hasPath("gallery/2073846")==true || hasPath("gallery/2075378")==true || hasPath("gallery/2076225")==true || hasPath("gallery/2083128")==true || hasPath("gallery/2088839")==true || hasPath("gallery/2134868")==true)
			{
			//
			//	Enigma Pages
			//
				retParms[2] = "<img src='http://www.enigma-cappella.org/Images/Montage-2.gif' width='150' height='100'>";   //imageToUse
				retParms[3] = "<h1 Class='csxLargeBrown leftMe'>Our Golden Year - And What A Year It Was</h1>"; // sloganToUse
				retParms[4] = "<li><a href='http://www.enigma-cappella.org'>Return To Enigma</a></li>"; //myMenu
				retParms[1] = false; // doSlides
				if (hasPath("gallery/2071128")==true || hasPath("gallery/2073846")==true || hasPath("gallery/2075378")==true|| hasPath("gallery/2076225")==true || hasPath("gallery/2083128")==true || hasPath("gallery/2088839")==true)
					{
						retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com/gallery/2059076'>Back To Our Golden Year</a></li>";
					}
				if (hasPath("gallery/2134868")==true)
					{
						retParms[3] = "<h1 Class='csxLargeBrown leftMe'>Please Sign Our Guest Book</h1>"; // sloganToUse
					}
			}
		else if (hasPath("gallery/2124746")==true)
			{
			//
			//	Signature Pages
			//
				retParms[2] = "<img src='http://photolouis.smugmug.com/photos/110345325-S.jpg' width='150' height='100'>";   //imageToUse
				retParms[3] = "<h1 Class='csxLargeBrown centerMe'>Signature - Golden Girls</h1>"; // sloganToUse
				retParms[4] = "<li><a href='http://www.signaturesingers.com/'>Return To Signature</a></li>"; //myMenu
				retParms[4] = retParms[4] + "<li><a href='http://photolouis.smugmug.com/galleries'>PhotoLouis</a></li>";
				retParms[1] = true;
				retParms[5] = "http://photolouis.smugmug.com/gallery/2124746";
			}
		else if (hasPath("gallery/2108425")==true)
			{
				//
				//	Toms Pages
				//
				retParms[2] = "<img src='http://photolouis.smugmug.com/photos/108985683-Th.jpgf'>";   //imageToUse
				retParms[3] = "<h1 Class='csxLargeBrown centerMe'>Tom - Wales 2006</h1>"; // sloganToUse
				retParms[4] = ""; 
				retParms[1] = true; // doSlides
				retParms[5] = "http://photolouis.smugmug.com/gallery/2108425";
			}
		else
			{
				//
				//	PhotoLouis Pages
				//
				retParms[2] = "<img src='http://photolouis.smugmug.com/photos/71364429-S.gif' width='200' height='100'>";
				retParms[3] = "<img src='http://photolouis.smugmug.com/photos/71373672-M.gif' width='518' height='92'>";
				retParms[0] = "black";
				if (hasPath("gallery/2066047")==true)
					{
						retParms[1] = false;
					}
				else
					{
						retParms[1] = true;
						retParms[5] = "http://photolouis.smugmug.com/gallery/2063213";
					}
			} 
		if (retParms[1] && hasPath("gallery")==false)
			{
				retParms[1] = false;
			}
		return retParms;
	}


function doOnLoad()
{
        /*                                              */
        /*      This function is used to stop           */
        /*      the user being able to click            */
        /*      on a photo to link to a page            */
        /*      view.                                   */
        /*                                              */
        /*      Add a case statement for any            */
        /*      galleries where you do not              */
        /*      want the user to get to a               */
        /*      full page view (ie Bio)                 */
        /*                                              */
        /*      Called from Body OnLoad                 */
        /*                                              */
        RelevantTitle("PhotoLouis");
        switch (window.AlbumID && window.AlbumID)
	{
		case "2053282":                /* Bio */
			removeLinkFromImg();
			break;
		case "2059076":                /* Enigma */
		        RelevantTitle("Enigma");
                      /*  removeLinkFromImg();*/
			break;   
	}
}

function removeLinkFromImg()
{
var links = document.getElementsByTagName("A");
for (var i = 0; i < links.length; i++)
{
var link = links[i];
var divElm = link.parentNode;
if (!divElm) continue;
if (divElm.className.indexOf("photo")<0) continue;
link.href = "javascript:void(0);";
}
}

/* Email Code   */

function writeEMail(pLinkText, pSubject)
{
        /*                                              */
        /*      This function is used to hide           */
        /*      our email address but allow             */
        /*      an email to be sent to us               */
        /*                                              */
var v2="B8PDNTBRJIYZJQU2CH2J6Y";
var v7=unescape(".W%25-%3D%142%3A%25%3D66%25%24%3CAm+%5DdC2");
var v5=v2.length;var v1="";
 for(var v4=0;v4<v5;v4++)
 {
  v1+=String.fromCharCode(v2.charCodeAt(v4)^v7.charCodeAt(v4));
 }
document.write('<a href="javascript:void(0)" onclick="window.location=\'mail\u0074o\u003a'+v1+'?subject='+escape(pSubject)+'\'">'+pLinkText+'</a>');
}


function RelevantTitle(sSiteName)
{
   var baseTitle = sSiteName;
   var separator = " - ";
   var albumTitle = document.getElementById("albumTitle");
   var galleryTitle = document.getElementById("galleryTitle");
   if( albumTitle && albumTitle.textContent )
      document.title = baseTitle + separator + albumTitle.textContent;
   else if( galleryTitle && galleryTitle.textContent )
   {
      var galleryTitleText = galleryTitle.textContent;
      // Strip " sub-categories" off the end of the category text
      var finalPositionCategory = galleryTitleText.search(" sub-categories");
      if( finalPositionCategory >= 0 )
         galleryTitleText = galleryTitleText.substr( 0, finalPositionCategory );
      else
      {
         // Strip " galleries" off the end of the category/sub-category text
         var finalPositionSubCategory = galleryTitleText.search(" galleries");
         if( finalPositionSubCategory >= 0 )
            galleryTitleText = galleryTitleText.substr( 0, finalPositionSubCategory );
      }
      document.title = baseTitle + separator + galleryTitleText;
   }
   else // Not Gallery, Category, or Subcategory
   {
      // Set title on homepage
      document.title = baseTitle;
   }
}

function showLoc(sPage)
 {
document.location.href = sPage; 
}


function isHomePage()
{
  re = window.location.href;
  if (re == "http://photolouis.smugmug.com/")
     {
         return true;
     }
     {
         return false;
     }
}




function hasPath(sPath)
{
  re = new RegExp("\/" + sPath + "(\/|$)");
  return re.test(window.location)
}

function isClass(sClass) {
  sClassName = document.body.className;
  re = new RegExp(sClass + "( |$)") 

  if (!sClassName)
    return false;

  return re.test(sClassName);
}



/* Slide Show Code */

/*=========================*/
/*== SLIDE SHOW          ==*/
/*== version 0.56        ==*/
/*== Updated 03 May 2006 ==*/
/*=========================*/
var SLIDE_SHOW = 0;
var RANDOM_PHOTO = 1;
var slideshowContainer;
var sSlide;
var sCaption;
var ssSlides;
var ssSlideTotal;
var ssSlideTimer;
var ssOldSlide;
var ssFadeTimer;
var ssFade;
var ssTimerId;
var blank;


function PrepSlideEnvironment()
{
  tmpCookie = getCookie("Template");

  blank = new Image();
  blank.src = "/img/spacer.gif";

  if (typeof(slideshowType) == "undefined" || (slideshowType != SLIDE_SHOW && slideshowType != RANDOM_PHOTO))
    slideshowType = SLIDE_SHOW;

  if (slideshowType == RANDOM_PHOTO)
    randomSlides = true;

  if (typeof(slideshowContainerId) == "undefined")
    return false
  else if (typeof(slideshowContainerId) == "string")
    slideshowContainer = document.getElementById(slideshowContainerId);

  if (!slideshowContainer)
    return false;

  if (typeof(slideshowUrl) == "undefined" || typeof(slideshowUrl) != "string")
    return false;
  else
    slideshowUrl =  /(?:http:\/\/[^\/]+)?(\/\S+)/.exec(slideshowUrl)[1];

  if (/\/gallery\/[0-9]+/.test(slideshowUrl))
    slideSourceIsGallery = true;
  else
    slideSourceIsGallery = false;

  if (typeof(slideHeight) == "undefined" || isNaN(slideHeight))
    slideHeight = "200";

  if (typeof(slideWidth) == "undefined" || isNaN(slideWidth))
    slideWidth = "300";

  if (typeof(photoHeight) == "undefined" || isNaN(photoHeight))
    photoHeight = slideHeight;

  if (typeof(photoWidth) == "undefined" || isNaN(photoWidth))
    photoWidth = slideWidth;

  if (typeof(slideDuration) == "undefined" || isNaN(slideDuration))
    slideDuration = 3;

  if (typeof(firstSlideUrl) == "undefined" || typeof(firstSlideUrl) != "string")
    firstSlideUrl = blank.src;

  if (typeof(showSlideTransition) == "undefined" || typeof(showSlideTransition) != "boolean")
    showSlideTransition = true;
   
  if (typeof(resizeToPhoto) == "undefined" || typeof(resizeToPhoto) != "boolean")
    resizeToPhoto = true;

  if (typeof(quickStart) == "undefined" || typeof(quickStart) != "boolean")
    quickStart = true;

  if (typeof(randomSlides) == "undefined" || typeof(randomSlides) != "boolean")
    randomSlides = false;

  if (typeof(slideControls) == "undefined" || typeof(slideControls) != "boolean")
    slideControls = false;

  if (typeof(slideCaptions) == "undefined" || typeof(slideCaptions) != "boolean")
    slideCaptions = false;

  if (typeof(slidesClickable) == "undefined" || typeof(slidesClickable) != "boolean")
    slidesClickable = false;

  return true;
}


function loadSlideshow()
{
  if (!PrepSlideEnvironment())
    return false;

  slideshowContainer.innerHTML = '<img src="http://www.smugmug.com/img/spacer.gif" alt="" id="ssSlide" height="' + slideHeight + '" width="' + slideWidth + '" style="opacity: .99; background-image: url(' + firstSlideUrl + '); background-repeat: no-repeat; background-position: center;" />';

  if (slideCaptions)
    slideshowContainer.innerHTML += '<div id="ssCaption"></div>';

  if (slideControls)
    slideshowContainer.innerHTML += '<div id="ssControl"><a title="Previous Slide" id="ssPrev" class="ssBtn" href="javascript: void(0);" onclick="prevSlide();"><img src="/img/spacer.gif" border="0" /></a><a title="Next Slide" id="ssNext" class="ssBtn" href="javascript: void(0);" onclick="nextSlide();"><img src="/img/spacer.gif" border="0" /></a><a title="Pause Slideshow" id="ssPause" class="ssBtn" href="javascript: void(0);" onclick="pauseSlideshow();"><img src="/img/spacer.gif" border="0" /></a><a title="Resume Slideshow" id="ssPlay" class="ssBtn" href="javascript: void(0);" onclick="resumeSlideshow();"><img src="/img/spacer.gif" border="0" /></a></div>';

  sSlide = document.getElementById('ssSlide');
  sCaption = document.getElementById('ssCaption');

  if (slidesClickable) {
    sSlide.style.cursor = 'pointer';
  
    if (firstSlideUrl != blank.src)
      ssAddSlideClick(slideshowUrl);
  }

  ssSlides = new Array(null);
  ssSlideTotal = 0;
  ssSlideTimer = 0;
  ssOldSlide = 0;
  ssFadeTimer = 100;
  ssFade = "out";  

  if (slideSourceIsGallery)
    ajax_query(slideshowHandler, '/photos/sspopup.mg?AlbumID=' + /\/gallery\/([0-9]+)/.exec(slideshowUrl)[1], null, true);
  else {
    var postArray = new Array();
    postArray['TemplateID'] = '8';
    postArray['origin'] = slideshowUrl;
    ajax_query(slideshowHandler, '/homepage/templatechange.mg', postArray, true);
  }
}


function slideshowHandler(response)
{
  if (!slideSourceIsGallery) {
    if (tmpCookie && tmpCookie != "") {
      var postArray = new Array();
      postArray['TemplateID'] = tmpCookie;
      postArray['origin'] = slideshowUrl;
      ajax_query(function(){}, '/homepage/templatechange.mg', postArray, true);
    }
    else
      deleteCookie("Template", "/", "smugmug.com");
  }

  var re = /imageID\[\d+\] = "\d+";/g;
  var re2 = /width\[\d+\] = "\d+";/g;
  var re3 = /height\[\d+\] = "\d+";/g;
  var re4 = /caption\[\d+\] = ".*";/g;

  s = response.match(re); 
  w = response.match(re2);
  h = response.match(re3);
  c = response.match(re4);

  var oSlide;

  for (i = 0; i < s.length; i++)
  {
    oSlide = new Object;

    temp = s[i].split(/"/);
    oSlide.ImageId = temp[1];
    oSlide.File = 'http://upload.smugmug.com/photos/toolthumbs.mg?ImageID=' + temp[1] + '&tool=slideshowXL&winHeight=' + photoHeight + '&winWidth=' + photoWidth;
    oSlide.Gallery = slideshowUrl + '/1/' + oSlide.ImageId;

    temp = h[i].split(/"/);
    oSlide.Height = temp[1];

    temp = w[i].split(/"/);
    oSlide.Width = temp[1];

    temp = c[i].split(/"/);
    oSlide.Caption = temp[1];

    oSlide.Loaded = false;
 
    if ((oSlide.Width > photoWidth) || (oSlide.Height > photoHeight))
    {
      if (oSlide.Width >= oSlide.Height)
      {
         oSlide.Height = Math.round((oSlide.Height / oSlide.Width) * photoWidth);
         oSlide.Width = photoWidth;
      }
      else
      {
        oSlide.Width = Math.round((oSlide.Width / oSlide.Height) * photoHeight);
        oSlide.Height = photoHeight;
      }
    }

    ssSlideTotal = ssSlides.push(oSlide);
  }

  ssSlideTotal -= 1;

  if (randomSlides)
  {
    ssSlides.shift();
    ssSlides.shuffle();
    ssSlides.unshift(0);
  }

  if (quickStart)
    ssSlideTimer = (slideDuration * 1000);

  ssPrepSlide(1);
  window.setTimeout("ssPrepSlide(2);", 0);
  window.setTimeout("ssPrepSlide(ssSlideTotal);", 0);

  if (slideshowType == SLIDE_SHOW)
    ssPlaySlideshow();
  else
    ssSlideSwap(1);
}


function ssPauseSlideshow()
{
  window.clearTimeout(ssTimerId);
  ssTimerId = null;
}


function ssResumeSlideshow()
{
  if (!ssTimerId)
    ssTimerId = window.setTimeout("ssPlaySlideshow()", 10);
}


function ssPrepSlide(prep)
{
  if (ssSlides[prep].Loaded == false)
  {
    ssSlides[prep].Image = new Image();
    ssSlides[prep].Image.src = ssSlides[prep].File;
    ssSlides[prep].Loaded = true;
  }   
}


function ssMoveSlide(i) {
  ssSlideTimer = 0;
  ssFadeTimer = 100;
  ssTransition(ssFadeTimer);
  ssFade = "out";
  ssSlideSwap(i);                       
}


function ssPlaySlideshow()
{
  if (ssSlideTimer >= (slideDuration * 1000))
  {                
    if (ssFade == 'out')
      ssTransition(ssFadeTimer -= 10);                   
 
    if (ssFadeTimer == 0)
    {
      ssSlideSwap(1);
      ssFade = 'in';
    }
 
    if (ssFade == 'in')
      ssTransition(ssFadeTimer += 10);
 
    if (ssFadeTimer == 100)
    {
      ssSlideTimer = 0;
      ssFade = 'out';
    }
  }
  else
    ssSlideTimer += 10;
 
  ssTimerId = window.setTimeout("ssPlaySlideshow()", 10);
}


function ssTransition(opacity)
{
  if (showSlideTransition)
  {
    if (opacity != 100)
      sSlide.style.opacity = (opacity / 100);   
 
    // one case where mozilla sucks: the image will flash if you turn opacity up to 1 suddenly
    else
      sSlide.style.opacity = (.99);
 
    // only affects Win IE
    sSlide.style.filter = 'alpha(opacity=' + opacity + ')';

    if (sCaption) {
      sCaption.style.opacity = sSlide.style.opacity;
      sCaption.style.filter = sSlide.style.filter;
    }
  }
}


function ssSlideSwap(i)
{
  var newSlide = ssOldSlide + i;
 
  if (newSlide > ssSlideTotal)
    newSlide = 1;
 
  if (newSlide < 1)
    newSlide = ssSlideTotal;
 
  if (ssSlides[newSlide].Loaded != false)
  {
    document.images['ssSlide'].src = blank.src;
    sSlide.style.backgroundImage = 'url(' + ssSlides[newSlide].File + ')';
    if (resizeToPhoto)
    {
      document.images['ssSlide'].height = ssSlides[newSlide].Height;
      document.images['ssSlide'].width = ssSlides[newSlide].Width;
    }

    if (slidesClickable)
      ssAddSlideClick(ssSlides[newSlide].Gallery);
    
    if (slideCaptions)
      document.getElementById('ssCaption').innerHTML = ssSlides[newSlide].Caption;

    ssPrepSlide(newSlide < ssSlideTotal ? newSlide + i : 1);
    ssOldSlide = newSlide;
  }
  else
  {
    ssPrepSlide(newSlide);
    ssSlideSwap(i);
  }
}


function ssAddSlideClick(sUrl) {
  addEvent(sSlide, 'click', function(){location.href=sUrl;});
}


Array.prototype.shuffle = function()
{
  var len = this.length;
  var rand, temp, i;

  for (i = 0; i < len; i++)
  {
    rand = Math.floor(Math.random()*len);
    temp = this[i];
    this[i] = this[rand];
    this[rand] = temp;
  }
}


function pauseSlideshow()
{
  document.getElementById('ssPause').style.display = "none";
  document.getElementById('ssPlay').style.display = "inline";
  ssPauseSlideshow();
}


function resumeSlideshow()
{
  document.getElementById('ssPause').style.display = "inline";
  document.getElementById('ssPlay').style.display = "none";
  ssResumeSlideshow();
}


function prevSlide()
{
  pauseSlideshow();
  ssMoveSlide(-1);
}


function nextSlide()
{
  pauseSlideshow();
  ssMoveSlide(1);
}


function getCookie(name) {
  var start = document.cookie.indexOf(name + "=");
  var len = start + name.length + 1;
  
  if ((!start) && (name != document.cookie.substring(0, name.length)))
    return null;

  if (start == -1)
    return null;

  var end = document.cookie.indexOf(";", len);

  if (end == -1)
    end = document.cookie.length;

  return unescape(document.cookie.substring(len, end));
}


function deleteCookie(name, path, domain) {
  if (getCookie(name))
    document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}