// ---------------------------------------------
// Slideshow parameter modification
// ---------------------------------------------
function MyEmbedSWF(swfURL, containerID, swfID, version, backgroundColor, expressInstall)
{
	// catch all exceptions here so, if there's an error, we just continue on as if we weren't here
	try {
		var overrides = null;
		if (this._type == "gallery")
		{
			overrides = this.gallerySlideshowOverrides;
		}
		else if (this._type == "fullScreen")
		{
			overrides = this.fullScreenSlideshowOverrides;
		}
		if (overrides)
		{
			// copy all overrides over into the objects flashVars
			for (var i in overrides)
			{
				this._flashVars[i] = overrides[i];
			}
		}
	} catch (e) {}
	oldEmbedSWF.apply(this, arguments);		// call the regular handler for this method
}


function SetSlideshowOverrides()
{
	var globalGallerySlideshowConfigOverride = {
		showButtons: "false",
		showSpeed: "false",
		showThumbs: "false",
		captions: "false",
		transparent: "true"		// no comma after the last entry in this table or IE7 will barf
	};

	var globalFullScreenSlideshowConfigOverride = {
		captions: "false"		// no comma after the last entry in this table or IE7 will barf
	};
	
	SM.flash.Slideshow.prototype.gallerySlideshowOverrides = globalGallerySlideshowConfigOverride;
	SM.flash.Slideshow.prototype.fullScreenSlideshowOverrides = globalFullScreenSlideshowConfigOverride;
	
	// now check to see if anything was defined just for this gallery and use it instead
	if (typeof(localGallerySlideshowConfigOverride) == "object")
	{
		SM.flash.Slideshow.prototype.gallerySlideshowOverrides = localGallerySlideshowConfigOverride;
	}
	if (typeof(localFullScreenSlideshowConfigOverride) == "object")
	{
		SM.flash.Slideshow.prototype.fullScreenSlideshowOverrides = localFullScreenSlideshowConfigOverride;
	}
}

// SM.flash.Slideshow is only defined in a galleries page
if (typeof(SM.flash.Slideshow) == "function")
{
	var oldEmbedSWF = SM.flash.Slideshow.superclass._embedSWF;
	SM.flash.Slideshow.superclass._embedSWF = MyEmbedSWF;
	SetSlideshowOverrides();
}

// ---------------------------------------------
// end of slideshow customization
// ---------------------------------------------




// This is a script to combine the categories with the gallery listing to have just one continuous listing of thumbs
YE.onContentReady("subcategoriesBox", CombineCategoriesWithGalleries);

function IsArrayEmpty(testVal)
{
    return(!testVal || (testVal.length == 0));
}

function CombineCategoriesWithGalleries()
{
    // get miniBoxes in the subcategoriesBox object
    var miniBoxes = YD.getElementsByClassName("miniBox", "div", this);
    // get target galleriesBox object
    var galleriesObj = document.getElementById("galleriesBox");
    var galleryMiniBoxes = new Array;

    // now find the right miniBox in the galleriesBox in order to insert the sub-categories
    if (galleriesObj)
    {
        galleryMiniBoxes = YD.getElementsByClassName("miniBox", "div", galleriesObj);
    }
    
    // if we don't have everything we need, then return without doing anything
    if (!galleriesObj || IsArrayEmpty(miniBoxes) || IsArrayEmpty(galleryMiniBoxes) || (window.location.hash == "#stop"))
    {
        this.style.display = "block";        
        return;
    }
    
    // move all the sub-categories over to the gallery listing
    for (var i in miniBoxes)
    {
        miniBoxes[i].parentNode.removeChild(miniBoxes[i]);
        galleryMiniBoxes[0].parentNode.insertBefore(miniBoxes[i], galleryMiniBoxes[0]);
    }
}








function redirectPath() { 
  re = /((www.)?borealphoto.smugmug.com)/; 
  tmp = window.location.href; 
  if (re.test(tmp)) { 
    tmp = tmp.replace(re, 'www.borealphoto.com'); 
    window.location.href = tmp; 
  } 
}
redirectPath();





function addRecentFeatured(text, days, thumbUrl) {
  if (YD.hasClass(document.body, "homepage")) {
    divTags = YD.getElementsByClassName("boxBottom", "div", YD.get("featuredBox"));

    for (i=0; i<divTags.length; i++) {
      miniBox = document.createElement("div");
      miniBox.className = "miniBox";

      photoBox = document.createElement("div");
      photoBox.className = "photo";
      miniBox.appendChild(photoBox);

      photoLink = document.createElement("a");
      photoLink.setAttribute("href", "javascript:showRecent(" + days + ");");
      photoBox.appendChild(photoLink);

      photoImg = document.createElement("img");
      photoImg.setAttribute("border", "0");
      photoImg.setAttribute("alt", text);
      photoImg.setAttribute("title", text);
      photoImg.src = thumbUrl;
      photoImg.className = "imgBorder";
      photoLink.appendChild(photoImg);

      albumTitle = document.createElement("p");
      albumTitle.className = "albumTitle";
      miniBox.appendChild(albumTitle);
      albumLink = document.createElement("a");
      albumLink.className = "nav";
      albumLink.setAttribute("href", "javascript:showRecent(" + days + ");");
      albumTitle.appendChild(albumLink);

      albumLinkText = document.createTextNode(text);
      albumLink.appendChild(albumLinkText);
      spacerDiv = document.createElement("div");
      spacerDiv.className = "spacer";
      miniBox.appendChild(spacerDiv);

      divTags[i].insertBefore(miniBox, divTags[i].childNodes[1]);

      break;
    }
  }
}






function showRecent(days) {
  endDate = new Date();
  oneDay = 1000*60*60*24;
  startDate = new Date(endDate.getTime() - (parseInt(days) * oneDay));
 
  sUrl = "/date/";
  sUrl += startDate.getFullYear() + "-" + padDate(startDate.getMonth() + 1) + "-" + padDate(startDate.getDate());
  sUrl += "/";
  sUrl += endDate.getFullYear() + "-" + padDate(endDate.getMonth() + 1) + "-" + padDate(endDate.getDate());
 
  window.location = sUrl;
}





function padDate(datePart) {
  if (datePart.toString().length != 1)
    return datePart
 
  return "0" + datePart;
}



YE.onContentReady('buyButton', function() {YAHOO.widget.Button.getButton("buyButton").set("label", "Buy Photo(s)");});





function AddReferralCode()  {
  var links = this.getElementsByTagName("A");
  if (links && (links.length != 0)) {
    var smugLink = links.item(0);
    smugLink.href = "http://www.smugmug.com/?referrer=81Ku6XUGqZKW6";
  }
}
YE.onAvailable('footer', AddReferralCode);








// code to insert a download button for any gallery that has originals enabled

function IsSmugmugView()
{
    return(YD.hasClass(document.body, "smugmug"));
}

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 = true;
	if (IsSmugmugView() && (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 original",
			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";
}

  
// code to force any gallery page to Smugmug view (even keyword and search pages)

function ForceSmugmugView()
{
    // if we're on a galleryPage, and the view isn't already smugmug
    if (!YD.hasClass(document.body, "smugmug"))
    {
        var url = window.location.toString();
        var re = new RegExp(/forceView=(\d+)/);
        var match = re.exec(url);
        var curTime = new Date();
        if (match && (match.length > 1))
        {
            var prevTime = new Date();
            prevTime.setTime(match[1]);
            // check to see if we've already done this in the last 60 seconds to avoid loops
            if (curTime - prevTime < (60*1000))
            {
                return;
            }
        }
        var postData = 'tool=setCookie&value=3&type=Template';
        var broken = function() {};
        var reloadPage = function() 
        {
            var newUrl;
            if (match)
            {
                newUrl = url.replace(/forceView=\d+/, "forceView=" + curTime.getTime());
            }
            else if (url.indexOf("?") != -1)
            {
                newUrl = url.replace("?", "?forceView=" + curTime.getTime() + "&");
            }
            else if (url.indexOf("#") != -1)
            {
                newUrl = url.replace("#", "?forceView=" + curTime.getTime() + "#");
            }
            else
            {
                newUrl = url + "?forceView=" + curTime.getTime();
            }
            window.location.replace(newUrl);
        };
        var callback = {
            success: reloadPage,
            failure: broken,
            scope: this
        };
        YAHOO.util.Connect.asyncRequest('POST','/rpc/settings.mg', callback, postData);
    }
}

// only trigger this function if there is a stylebar because that means the gallery isn't locked into a particular view
YE.onAvailable("stylebar", ForceSmugmugView);