function ssInit(carousel) {

    jQuery('#ss-'+carousel.options.slideshowType+'-controls a').bind('click', function() {
	
		// link value determination
		var name = $(this).attr('name').substring($(this).attr('name').lastIndexOf('_')+1);

		if (name == 0)
		{
			if (!$(this).children('img').hasClass('disabled')) {
				carousel.prev();
			}
		}
		else if (name == carousel.options.numOfButtons + 1)
		{
			if (!$(this).children('img').hasClass('disabled')) {
				carousel.next();
			}
		}
		else
		{
			// calculate the page to go to by selecting the first frame of the page
			var goTo = ((name * carousel.options.numToScroll) - carousel.options.numToScroll) + 1;
	
			// scroll to the given page
			carousel.scroll(jQuery.jcarousel.intval(goTo));			
		}
		
		// stop auto scrolling
		carousel.startAuto(0);
		
        return false;
    });
	
	// this has been added to make Safari load the initial dot by default (on load)
	$('#ss_nav_1').children('img').attr('src', '/images/template/slideshows/'+carousel.options.slideshowType+'/dot-current.gif');
	
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function ssUpdate(carousel, item, idx, state) {
	
	$('#ss-'+carousel.options.slideshowType+'-controls ul li a').each(function() {
		var name = $(this).attr('name').substring(7);
		if (name != 0 && name != carousel.options.numOfButtons + 1)
		{
			$(this).children('img').attr('src', '/images/template/slideshows/'+carousel.options.slideshowType+'/dot-option.gif');
		}
	});
	
	var offset = idx;
	//if (numToScroll == 1)
	//{
	//	offset = offset - 2;
	//}
	var thisName = '#ss_nav_'+carousel.options.slideshowType+'_' + Math.ceil(offset / carousel.options.numToScroll);
	$(thisName).children('img').attr('src', '/images/template/slideshows/'+carousel.options.slideshowType+'/dot-current.gif');
	
	$('#ss_nav_'+carousel.options.slideshowType+'_0').children('img').attr('src', '/images/template/slideshows/'+carousel.options.slideshowType+'/dot-prev.gif');
	$('#ss_nav_'+carousel.options.slideshowType+'_0').children('img').removeClass('disabled');
	$('#ss_nav_'+carousel.options.slideshowType+'_'+(carousel.options.numOfButtons+1)).children('img').attr('src', '/images/template/slideshows/'+carousel.options.slideshowType+'/dot-next.gif');
	$('#ss_nav_'+carousel.options.slideshowType+'_'+(carousel.options.numOfButtons+1)).children('img').removeClass('disabled');
	
	if (Math.ceil(offset / carousel.options.numToScroll) == 1)
	{
		// first page, so Previous should be hidden
		$('#ss_nav_'+carousel.options.slideshowType+'_0').children('img').addClass('disabled');
		$('#ss_nav_'+carousel.options.slideshowType+'_0').children('img').attr('src', '/images/template/slideshows/'+carousel.options.slideshowType+'/dot-prev-disabled.gif');
	}
	else if (Math.ceil(offset / carousel.options.numToScroll) == carousel.options.numOfButtons)
	{
		// last page, so Next should be hidden
		$('#ss_nav_'+carousel.options.slideshowType+'_'+(carousel.options.numOfButtons+1)).children('img').addClass('disabled');
		$('#ss_nav_'+carousel.options.slideshowType+'_'+(carousel.options.numOfButtons+1)).children('img').attr('src', '/images/template/slideshows/'+carousel.options.slideshowType+'/dot-next-disabled.gif');
	}
};
	

	
function buildSlideshow(o)
{
	
	var items = $('#ss-'+o.slideshowType).children('li').length;

	var numOfButtons = Math.ceil(items / o.numToScroll);
	
	if (numOfButtons > 0)
	{
		var ul = $('<ul></ul>');
		for (var i = 0; i <= numOfButtons + 1; i++)
		{
			var a = $( '<a></a>' );
			a.attr('href', '#');

			var li = $('<li></li>');

			var img = $('<img></img>');
			
			a.attr('name', 'ss_nav_'+o.slideshowType+'_'+i);
			a.attr('id', 'ss_nav_'+o.slideshowType+'_'+i);
			if (i == 0 || i == numOfButtons+1)
			{
				if (i == 0)
				{
					li.className = 'jcarousel-prev';					
					img.attr('src', '/images/template/slideshows/'+o.slideshowType+'/dot-prev.gif');
					img.attr('alt', 'Go to previous page');
				}
				else
				{
					li.className = 'jcarousel-next';
					img.attr('src', '/images/template/slideshows/'+o.slideshowType+'/dot-next.gif');
					img.attr('alt', 'Go to next page');
				}
			}
			else
			{				
				img.attr('src', '/images/template/slideshows/'+o.slideshowType+'/dot-option.gif');
				img.attr('alt', 'Go to page '+i);
								
			}
				  
			img.hover(
				function() { 
					if (!$(this).hasClass('disabled'))
					{
						$(this).attr("src", $(this).attr("src").split(".").join("-over.")); 
					}
				}, 
				function() { 
					if (!$(this).hasClass('disabled'))
					{
						$(this).attr("src", $(this).attr("src").split("-over.").join(".")); 
					}
				}
			);

			img.attr('width', 14);
			img.attr('height', 14);
	
			a.append(img);
			li.append(a);
			
			ul.append(li);
		}
		$('#ss-'+o.slideshowType+'-controls').append(ul);
		
		var directions = $('<div></div>');
		directions.addClass('ss-controls-directions');
		directions.text('Use the arrows to scroll');
		$('#ss-'+o.slideshowType+'-controls').append(directions);
		
		if (o.fullPortfolioLink)
		{
			var full = $('<div></div>');
			var full_a = $('<a>See the full portfolio</a>');
			full_a.attr('href', o.fullPortfolioLink);
			full.addClass('ss-controls-full');
			full.append(full_a);
			$('#ss-'+o.slideshowType+'-controls').append(full);
		}
	
		$("#ss-"+o.slideshowType+" li").children('.ss-'+o.slideshowType+'-item').children('.ss-'+o.slideshowType+'-content').each(function() {
			$(this).append($('<div></div>').attr('class', 'corner corner-tl'));
			$(this).append($('<div></div>').attr('class', 'corner corner-tr'));
			$(this).append($('<div></div>').attr('class', 'corner corner-bl'));
			$(this).append($('<div></div>').attr('class', 'corner corner-br'));
		});
	
		var maxHeight = 0;
		$('#ss-'+o.slideshowType).children('li').each(function () {
			if ($(this).height() > maxHeight)
			{
				maxHeight = $(this).height();
			}
		});

		// this is more comple
		$('#ss-'+o.slideshowType).children('li').each(function () {
			var a = $(this).find('div.ss-'+o.slideshowType+'-item > h3').outerHeight(true);
			var b = $(this).find('div.ss-'+o.slideshowType+'-item > div.ss-'+o.slideshowType+'-content > img').outerHeight(true);
			var d = $(this).find('div.ss-'+o.slideshowType+'-item > div.ss-'+o.slideshowType+'-more').outerHeight(true);
			var thisHeight = maxHeight - (a + b + d);
			if (o.slideshowType == 'portfolio') { thisHeight = thisHeight - 12; }
			$(this).children('.ss-'+o.slideshowType+'-item').children('.ss-'+o.slideshowType+'-content').children('.ss-'+o.slideshowType+'-text').height(thisHeight);
		});
		
		if (o.slideshowType == 'panel')
		{
			$('#ss-'+o.slideshowType).height(maxHeight + 18);
			$('#ss-panel-footer').remove();
			$('#ss-wrapper-'+o.slideshowType).append($('<div></div>').attr('id', 'ss-panel-footer'));
		}

		// remove right margin which was used for non-javascript layout
		$("#ss-"+o.slideshowType+" li").attr('class', '');
		$("#ss-"+o.slideshowType).jcarousel({
			auto: o.slideDelay,
			wrap: 'both',
			animation: o.animationTime * 1000,
			scroll: o.numToScroll,
			initCallback: ssInit,
			itemLastInCallback: ssUpdate,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null,
			numToScroll: o.numToScroll,
			fullPortfolioLink: o.fullPortfolioLink,
			slideshowType: o.slideshowType,
			numOfButtons: numOfButtons
		});
	}
}
