// fix the pesky pipes

YE.onAvailable("cobrand_footer", function() {
this.innerHTML = this.innerHTML.replace(/\|/gi, '');
});

YE.onAvailable("cobrand_footer", function() {
this.innerHTML = this.innerHTML.replace(/\what are feeds\?/gi, '<a href="http://www.paessler.com/prtg7" target="_blank"><img src="http://www.paessler.com/static/banner_8_80x15.png" alt="Network Monitor" width="80" height="15" border="0"/></a>');
});


// 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>");
}

YE.onDOMReady(InsertStylePicker);

function SetNewGalleryStyle(type, args, menuItem)
{
    // this click handler is called as a method of the menuItem that was clicked on
    var styleValue = this.value;        // get the value of the menu item
    
    // because we are calling styleBarChange which is expecting to be passed a comboBox object,
    // we have to create a fake comboBox object that has the new value in it so that function
    // can fetch the value from our object
    var fakeComboItem = {value: styleValue};
    var fakeComboBox = {selectedIndex: 0, options: [fakeComboItem]};
    
    // call the function that changes the style just like the Smugmug comboBox would have
    styleBarChange("Template", fakeComboBox);
}

function InsertStylePicker()
{
    if (!YD.get("stylebar"))
    {
        return;        // if there wasn't supposed to be a stylebar in this page, the don't insert our version
    }
    // declare the data for our style picker
    // you can remove any of these lines that you don't want to offer your viewers
    // just make sure the last line does not have a comma after it and all others do have a comma (IE will choke if you don't do it right)
    var stylePickerData = [
        {text: "SmugMug", value: "3", onclick: { fn: SetNewGalleryStyle }},
        {text: "Traditional", value: "4", onclick: { fn: SetNewGalleryStyle }},
        {text: "All Thumbs", value: "7", onclick: { fn: SetNewGalleryStyle }},
        {text: "Slideshow", value: "8", onclick: { fn: SetNewGalleryStyle }},
        {text: "Journal", value: "9", onclick: { fn: SetNewGalleryStyle }},
        {text: "Filmstrip", value: "11", onclick: { fn: SetNewGalleryStyle }},
        {text: "Critique", value: "12", onclick: { fn: SetNewGalleryStyle }}
    ];
    
    // now put a checkmark by the currently selected style which we get from the galleryStyle variable
    if (galleryStyle)
    {
        var tempStyle = galleryStyle.toLowerCase();
        tempStyle = tempStyle.replace("_", " ");
        for (var i in stylePickerData)
        {
            if (stylePickerData[i].text.toLowerCase() == tempStyle)
            {
                stylePickerData[i].checked = true;
                break;
            }
        }
    }
    
    // declare the style menu button
    // You can change the button text, by changing the quoted string for the label line below
    // You can put it a different place in your page by changing the container object to a different DIV object in the page
    var styleMenu = new YAHOO.widget.Button({
        id: "customStylebarMenu",
        name: "customStylebarMenu",
        type: "menu",
        menu: stylePickerData,
        label: "Style",
        className: "sm-button sm-button-small themesButton glyphButton menuButton",
        container: "altViews"
    });
    
    
}

if (hasPath("galleries"))
YD.addClass(document.body, "galleries");

if (hasPath("splash"))
YD.addClass(document.body, "splash");


function setPaypalForm(form) {
 urlToImage = webServer + "/gallery/" + AlbumID + "_" + AlbumKey + "/1/" + ImageID + "_" + ImageKey + "      " + photoInfo[ImageID]['fileName'] ; 
 form.item_name.value = urlToImage;
}

function AddReferralCode()  {
  var links = this.getElementsByTagName("A");
  if (links && (links.length != 0)) {
    var smugLink = links.item(0);
    smugLink.href = "http://www.smugmug.com/?referrer=rVPpYmXbCk62A";
  }
}
YE.onAvailable('footer', AddReferralCode);

// This is the Javascript for the slider code...
var SLIDETIMER = 3;
var SLIDESPEED = 3;
var SCROLLTIMER = 3;
var SCROLLSPEED = 3;
var STARTINGOPACITY = 40;

// handles section to section scrolling of the content //
function slideContent(id,prefix,timer) {
  var div = document.getElementById(id);
  var slider = div.parentNode;
  clearInterval(slider.timer);
  slider.section = parseInt(id.replace(/\D/g,''));
  slider.target = div.offsetTop;
  slider.style.top = slider.style.top || '0px';
  slider.current = slider.style.top.replace('px','');
  slider.direction = (Math.abs(slider.current) > slider.target) ? 1 : -1;
  slider.style.opacity = STARTINGOPACITY * .01;
  slider.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  slider.timer = setInterval( function() { slideAnimate(slider,prefix,timer) }, SLIDETIMER);
}

function slideAnimate(slider,prefix,timer) {
  var curr = Math.abs(slider.current);
  var tar = Math.abs(slider.target);
  var dir = slider.direction;
  if((tar - curr <= SLIDESPEED && dir == -1) || (curr - tar <= SLIDESPEED && dir == 1)) {
    slider.style.top = (slider.target * -1) + 'px';
	slider.style.opacity = 1;
	slider.style.filter = 'alpha(opacity=100)';
    clearInterval(slider.timer);
	if(slider.autoscroll) {
	  setTimeout( function() { autoScroll(slider.id,prefix,timer) }, timer * 1000);
	}
  } else {
	var pos = (dir == 1) ? parseInt(slider.current) + SLIDESPEED : slider.current - SLIDESPEED;
    slider.current = pos;
    slider.style.top = pos + 'px';
  }
}

// handles manual scrolling of the content //
function scrollContent(id,dir) {
  var div = document.getElementById(id);
  clearInterval(div.timer);
  var sections = div.getElementsByTagName('div');
  var length = sections.length;
  var limit;
  if(dir == -1) {
    limit = 0;
  } else {
    if(length > 1) {
      limit = sections[length-1].offsetTop;
    } else {
      limit = sections[length-1].offsetHeight - div.parentNode.offsetHeight + 20;
    }
  }
  div.style.opacity = STARTINGOPACITY * .01;
  div.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  div.timer = setInterval( function() { scrollAnimate(div,dir,limit) }, SCROLLTIMER);
}

function scrollAnimate(div,dir,limit) {
  div.style.top = div.style.top || '0px';
  var top = div.style.top.replace('px','');
  if(dir == 1) {
	if(limit - Math.abs(top) <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = '-' + limit + 'px';
	} else {
	  div.style.top = top - SCROLLSPEED + 'px';
	}
  } else {
	if(Math.abs(top) - limit <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = limit + 'px';
	} else {
	  div.style.top = parseInt(top) + SCROLLSPEED + 'px';
	}
  }
}

// cancel the scrolling on mouseout //
function cancelScroll(id) {
  var div = document.getElementById(id);
  div.style.opacity = 1;
  div.style.filter = 'alpha(opacity=100)';
  clearTimeout(div.timer);
}

// initiate auto scrolling //
function autoScroll(id,prefix,timer,restart) {
  var div = document.getElementById(id);
  div.autoscroll = (!div.autoscroll && !restart) ? false : true;
  if(div.autoscroll) {
    var sections = div.getElementsByTagName('div');
    var length = sections.length;
    div.section = (div.section && div.section < length) ? div.section + 1 : 1;
    slideContent(prefix + '-' + div.section,prefix,timer);
  }
}

// cancel automatic scrolling //
function cancelAutoScroll(id) {
  var div = document.getElementById(id);
  div.autoscroll = false;
}

