function removeBottomBorders () {
	if(!document.getElementById || !document.getElementById('section-content')) return false;
	var sectionContent = document.getElementById('section-content');
	var openUl = sectionContent.getElementsByTagName('ul')[1];
	if(!openUl) return false;

	var lastOuterLi = sectionContent.getElementsByTagName('li');
	if(!lastOuterLi) return false;

	lastOuterLi[lastOuterLi.length-1].getElementsByTagName('a')[0].style.borderBottomWidth="0";
	var innerLi = openUl.getElementsByTagName('li');
	innerLi[innerLi.length-1].getElementsByTagName('a')[0].style.borderBottomWidth="0";
	innerLi[innerLi.length-1].getElementsByTagName('a')[0].style.paddingBottom="8px";
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


// ==============
// = Thumbnails =
// ==============
function thumbnailInit()
{
	var fullImages = getElementsByClass("item-image-full", document.getElementById('item-images'), 'img');
	for (var i=0; i < fullImages.length; i++) {
		if (i !== 0) 
		{
			fullImages[i].style.display = "none";
		};
	};
}

// position is 0 index
function showImage (position)
{
	var fullImages = getElementsByClass("item-image-full", document.getElementById('item-images'), 'img');
	for (var i=0; i < fullImages.length; i++) {
		fullImages[i].style.display = "block";
		if (i !== position)
		{
			fullImages[i].style.display = "none";
		};
	};
}

// ============
// = Carousel =
// ============
function carouselNext () {
	var itemCount = window.carouselList.getElementsByTagName('li').length;
	
	if (getPixels(window.carouselList.style.left) >= window.maxOffset)
	{
		if ((getPixels(window.carouselList.style.left) - window.carouselSpace) >= (-1 * (itemCount - 1) * (window.carouselSpace - 14)))
		{
			window.carouselList.style.left = subtractLeft(124);
		}
		else
		{
			window.carouselList.style.left = window.maxOffset + "px";
		}	
	}
}

function carouselPrev () {
	if (getPixels(window.carouselList.style.left) <= -124)
	{
		window.carouselList.style.left = addLeft(124);
	}
	else
	{
		window.carouselList.style.left = "0px";
	}
}

// Returns the integer value of pixels
function getPixels(value) {
	return parseInt(value.substring(0, value.length - 2));
}

// Adds pixels to the left value of carouselList.style.left
function addLeft (amount) {
	var currentOffset = window.carouselList.style.left;
	var currentOffsetInt = parseInt(currentOffset.substring(0, currentOffset.length - 2));
	return (currentOffsetInt + amount) + "px";	
}

// Subtracts pixels to the left value of carouselList.style.left
function subtractLeft(amount) {
	var currentOffset = window.carouselList.style.left;
	var currentOffsetInt = parseInt(currentOffset.substring(0, currentOffset.length - 2));
	return (currentOffsetInt - amount) + "px";
}

function carouselInit () {
	if(!document.getElementById || !document.getElementById('carousel-clip-region') || !document.getElementById('carousel-list')) return false;
	window.carousel = document.getElementById('carousel-clip-region');
	window.carouselList = document.getElementById('carousel-list');
	window.carouselListItems = carouselList.getElementsByTagName('li');
	
	window.carouselList.style.left = "0px";
	
	// The space consumed by each li
	window.carouselSpace = 124;
	
	// 902 comes from 892 pixel width of carousel-clip-region and 10px for padding at the end of the li
	window.maxOffset = 902 - (window.carouselListItems.length * window.carouselSpace);
	
	document.getElementById("carousel-prev").onmouseup = carouselPrev;
	document.getElementById("carousel-next").onmouseup = carouselNext;
}

function slideshows() {
	if (typeof jQuery != 'undefined' && getElementsByClass('slideshow300',document.getElementById('main-section'),'ul')) {
		$('ul.slideshow300 li').show();
		$('ul.slideshow300').innerfade({
			speed: 'slow',
			timeout: '2500',
			containerheight: '300'
		});
	}
	
	if (typeof jQuery != 'undefined' && getElementsByClass('slideshow356',document.getElementById('main-section'),'ul')) {
		$('ul.slideshow356 li').show();
		$('ul.slideshow356').innerfade({
			speed: 'slow',
			timeout: '2500',
			containerheight: '300'
		});
	}
}

addLoadEvent(slideshows);

// addLoadEvent is Simon Willison's function that aloows for convenient
// addition of multiple fuctions that are all supposed to be triggered
// on window.onload event. 
// takes function name as the argument. e.g. addLoadEvent(doPopups);

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    };
  }
}

addLoadEvent(removeBottomBorders);
addLoadEvent(carouselInit);
addLoadEvent(thumbnailInit);
