$(function(){
	// randomize start slide
	var rand_upper = $('#slideshow .carousel').children('li').size();
	var start_slide = (Math.floor(Math.random()*rand_upper)) + 1;
	start_slide = 1;
	// this empty slide fixes some offset issues caused by our nonstandard
	// implementation of jcarousel
	$('#slideshow .carousel').append($('<li>').addClass('placeholder'));
	// init jcarousel
	$('#slideshow .carousel').jcarousel({
		scroll: 1,
		start: start_slide, //randomized start slide
		initCallback: function(carousel){
			
			jQuery('.jcarousel-control a').bind('click', function() {
				carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
				return false;
			}); 
			
			if ($.browser.msie && $.browser.version.substr(0,1) == '6') {
				$('.content-container').addClass('ieWidth');
				slideshow.ieWrap();
			} else {
				// we have to track window resizing to keep this looking right
				$(window).resize(slideshow.resize).trigger('resize');
			}
			slideshow.isInitialized = true;
		},
		itemLastInCallback: function(carousel, item, index, state){
			// since the last item is the placeholder, disable the next button early
			// condition has to be a little different for IE6
			if ($.browser.msie && $.browser.version.substr(0,1) == '6') {
				var itemCheck = $(item).next().hasClass('placeholder')
			} else {
				var itemCheck = $(item).hasClass('placeholder')
			}
			if (itemCheck) {
				$('#slideshow .jcarousel-next').addClass('jcarousel-next-disabled');
			} else {
				$('#slideshow .jcarousel-next').removeClass('jcarousel-next-disabled');
			}
			var curPage = $(item).prev().attr('curpage');
			$('#filmstrip .fs-content a').attr('id', '');
			$('#filmstrip .fs-content a.slide'+curPage).attr('id', 'active');
		}
	});
});

slideshow = {
	// checks browser window size and alters slideshow as needed
	resize: function(){
		// figure out the new spacing around the content area
		var viewportWidth = $(window).width();
		var contentWidth = 960;
		var spacing = (viewportWidth - contentWidth) / 2;
		
		var gradientOverlap = 0;

		// gradient size/placement
		if (spacing >= 0) {
			var gradWidth = spacing + gradientOverlap;
			$('#slideshow .gradient').css('width', gradWidth+'px');
		} else {
			$('#slideshow .gradient').removeAttr('style');
		}
		
		// carousel offset
		if (spacing > 0) {
			var currentLeft = $('#slideshow .carousel').position().left;
			var offsetPositions = Math.floor(currentLeft / contentWidth);
			var newLeft = offsetPositions * contentWidth + spacing;
			$('#slideshow .carousel').css('left', newLeft+'px');
		}
		
		// carousel button placement
		if (spacing > 0) {
			staticSpacing = 10;
			var buttonSpacing = spacing + staticSpacing;
			$('#slideshow .jcarousel-prev').css('left', buttonSpacing+'px');
			$('#slideshow .jcarousel-next').css('right', buttonSpacing+'px');
			$('#slideshow .jcarousel-prev').append('<a href="#">Previous</a>');
			$('#slideshow .jcarousel-next').append('<a href="#">Next</a>');
		}
		
		// content-container width fix for ie6
		$('.content-container.ieWidth').css('width', viewportWidth+'px');
	},
	initOffset: function(){
		var viewportWidth = $(window).width();
		var contentWidth = 960;
		var offset = (viewportWidth - contentWidth) / 2;
		var currentLeft = $('#slideshow .carousel').position().left;
		var newLeft = currentLeft + offset;
		$('#slideshow .carousel').css('left', newLeft+'px');
	},
	ieWrap: function(){
		// this is a fix for correctly positioning the buttons and gradients in ie6
		var wrapper = $('<div>').addClass('ieWrapper').css({
			position: 'absolute',
			left: '0px',
			top: '0px',
			width: '960px'
		});
		$('#slideshow .jcarousel-prev, #slideshow .jcarousel-next').wrapAll(wrapper);
	},
	isInitialized: false,
	prevLeft: 0
}

