/* affiliation program */

function AddReferralCode() {
var links = this.getElementsByTagName("A");
if (links && (links.length != 0)) {
var smugLink = links.item(0);
smugLink.href = "http://www.smugmug.com/?referrer=BbraTCWftpNCs";
}
}
YE.onAvailable('footer', AddReferralCode);

/* what the right-click warning will say */

rightClickWarning = "All photos are property of Paul Zizka Photography. All rights reserved. Unauthorized use is prohibited."; 

/* gets rid of text bubbles */

function delHover() {
    oLst = YD.getElementsBy(function(el) {return (YD.hasClass(el,'imgBorder') || YD.hasClass(el,'imgBorderOn'))},'img');
    for (i=0; i < oLst.length ; i++) {
        if (oLst[i].title && oLst[i].title != '') oLst[i].title = '';
        if (oLst[i].alt && oLst[i].alt != '') oLst[i].alt = '';
    }
}
YE.onContentReady('bodyWrapper', function() {if (!YD.hasClass(document.body, 'smugmug_ajax')) delHover()});
onPhotoShow.subscribe(function(){YE.onAvailable('mainImage', delHover)});

/* Category descriptions */

function addCategoryDescription() {
    var categoryDescription = {
        "Destination Snapshots": "Photos sorted by location.",
        "Around the World": "Planet-wide themes."
    };

    var divTag, re, matches, i;

    if ((YD.hasClass(document.body, "category")) && (!YD.hasClass(document.body, "subcategory"))) {
        re = /category_(\S+)/i;
        // category class names we are trying to match have underscores instead of spaces so...
        // to make this work for category names with spaces and underscores in the names, we have to convert spaces into underscores in the match table
        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
        }
        matches = re.exec(document.body.className);
        if (matches && (matches.length> 1)) {
            var breadCrumb = YD.get("breadcrumb");
            if (breadCrumb && categoryDescription[matches[1]]) {
                divTag = document.createElement("div");
                divTag.className = "categoryDescription";
                divTag.appendChild(document.createTextNode(categoryDescription[matches[1]]));
                breadCrumb.parentNode.insertBefore(divTag, breadCrumb.nextSibling);
            }
        }
    }
    if (YD.hasClass(document.body, "homepage")) {
        re = /\>([^\<]+)<\/a>/i;

        divTag = YD.get("categoriesBox");
        if (divTag) {
            var divTags = YD.getElementsByClassName("albumTitle", "p", divTag);

            for (i = 0; i < divTags.length; i++) {
                matches = re.exec(divTags[i].innerHTML);
                if (matches && (matches.length> 1) && categoryDescription[matches[1]] != undefined) {
                    var pTag = document.createElement("p");
                    pTag.className = "categoryDescription";
                    pTag.appendChild(document.createTextNode(categoryDescription[matches[1]]));
                    divTags[i].parentNode.insertBefore(pTag, divTags[i].nextSibling);
                }
            }
        }
    }
}
YE.addListener(window, "load", addCategoryDescription);

/* duplication of homepage */

function hasPath(sPath)
{
re = new RegExp("\/" + sPath + "(\/|$)");
return re.test(window.location)
}

if (hasPath("galleries"))
YD.addClass(document.body, "galleries");

/* to make News, Bio and Hire unclickable*/

function doOnLoad()
{
    var galleriesToRemoveClicking = [6272793,6362531,6201972,5191894,6272800,6202275,7497565,6202112,6272813,6210911,6210901,6728632,6744864,6745035,6747665,6744815,6744874,6747470];
    
    if (window.AlbumID)
    {
        for (var i in galleriesToRemoveClicking)
        {
            if (galleriesToRemoveClicking[i] == window.AlbumID)
            {
                removeLinkFromImg();
                return;
            }
        }
    }
}

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;

        divElm = divElm.parentNode;

        if (!divElm)

            continue;

        if (divElm.className.indexOf("photo")<0)

            continue;

        link.href = "javascript:void(0);";

    }

} 

/* to hide categories on home page */

function delCategory() {
  re = /\/(Randonnees|Portfolio|Products)$/;

  if (!YD.hasClass(document.body, 'homepage'))
    return;

  var oList = YD.getElementsByClassName('miniBox', 'div', YD.get('categoriesBox'));

  for (i = 0; i < oList.length; i++) {
    if (re.test(oList[i].getElementsByTagName('a')[0].href))
      oList[i].parentNode.removeChild(oList[i]);
  }
}

// smartTruncate by Bill Gerrard, http://www.PrimaryColors.com
// Truncate to maximum of maxLen characters on nearest word boundary, append ellipsis
function smartTruncate(sText, maxLen) {
  if (sText.length <= maxLen) {
    return sText;
  } else {
    var words = sText.split(" ");
    var tmpText = "";
    var i = 0;

    while(i < words.length-1) {
      outText = tmpText;
      tmpText += words[i] + " "; 
      if (tmpText.length > maxLen) {
          break;
      }
      outText = tmpText;
      i++;
    }
    return outText.substring(0,outText.length-1) + "&nbsp;...";
  }
}

//
// expandGalleryDescription - Payne Cheng
//
function expandGalleryDescription() {
  re1 = /^([^:]*)\s*:\s*(.*)/;
  re2 = /^(.*)\s*\.\.\.\s*(.*)/;
  divTags = document.getElementsByTagName("div");
  for (i=0; i<divTags.length; i++)
  {
    if (divTags[i].className == "miniBox")
    {
      dTags = divTags[i].getElementsByTagName("div");
      pTags = divTags[i].getElementsByTagName("p");
      aTags = dTags[0].getElementsByTagName("a");
      iTags = aTags[0].getElementsByTagName("img");
      if ( re1.exec(iTags[0].getAttribute("title")) )
      {
        $desc = RegExp.$2;
      }
      if ( pTags[1] && re2.exec(pTags[1].innerHTML)             ) 
      {
        pTags[1].innerHTML = smartTruncate($desc + RegExp.$2, 150);
      }
    }
  }
}

if (YD.hasClass(document.body, 'HikingTrips')) { expandGalleryDescription(); }

// script to replace a user's name in the breadcrumb with "Zizka Home"
YE.onContentReady("breadCrumbTrail", ReplaceTopOfBreadcrumbWithHome);

function ReplaceTopOfBreadcrumbWithHome()
{
    var str = this.innerHTML.replace(/\n/g, " ");
    this.innerHTML = str.replace(/\>[^\<]+<\/a>/i, ">Zizka Home</a>");
}

//-------------------------------------------------------------------------
// Code to move the slideshow button to the same row 
// as the buy button
//-------------------------------------------------------------------------
function MyNewSlideshowInit(container, id, config)
{
    if (config && config.slideshowDiv && (config.slideshowDiv == "fsssButton"))
    {
        var cartDiv = YD.get("cartButtonsWrapper");
        if (cartDiv)
        {
            var ssDiv = YD.get("fsssButton");
            if (ssDiv)
            {
                ssDiv.parentNode.removeChild(ssDiv);
            }
            var newDiv = document.createElement("div");
            newDiv.id = "fsssButton";
            cartDiv.appendChild(newDiv);
            container = "cartButtonsWrapper";
        }
    }
    return (myOldSlideshowInit.apply(this, arguments));
}

var myOldSlideshowInit;
if (typeof(SM.buttons.slideshow) != "undefined")
{
    // hook the init function on the SM.buttons.slideshow object
    myOldSlideshowInit = SM.buttons.slideshow.prototype.init;
    SM.buttons.slideshow.prototype.init = MyNewSlideshowInit;
}

//------------------------------------------------------------------------------------
// 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
    }    
}

YE.onDOMReady(AddItemsToBuyButton);

function AddItemsToBuyButton()
{
    var buyButton = YAHOO.widget.Button.getButton("buyButton");
    if (buyButton)
    {
        var newBuyItem = { text: "Postcards", url: "http://www.zizka.ca/Products/Postcards/8325738_pUsVc/1/545515044_Udycs", target: "_blank"};
        buyButton.getMenu().addItem(newBuyItem);
    }
}

// ---------------------------------------------
// 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",
                borderThickness: "0",
                borderColor: 000000,
		transparent: "true"		// no comma after the last entry in this table or IE7 will barf
	};

	var globalFullScreenSlideshowConfigOverride = {
		showButtons: "true",
		showSpeed: "true"		// 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
// ---------------------------------------------

