var slides;
var totalSlides;
var previousSlideID;
var previousSlide;
var i;
var nextSlideID;
var nextSlide;
var timerID;
var thisSlide;

function resetPositions( obj ) {
	// will run style resetting on any group of elements passed
	if( obj ) {
		obj
			.find(".slideshow-bg img")		// have to do this to work with limitations of collage components
				.css("opacity","0")
				.end()
			.find(".product-border")
				.css("right", "-20px")
				.css("opacity","0")
				.end()
			.find( ".copy" )
				.css( "left", "-20px")
				.css("opacity","0")
				.end()
			.css("visibility","visible");
	}
	// else invalid obj passed
}
function start() {
	
	if (i == 0) {
		previousSlideID					= 1;
		nextSlideID						= totalSlides;
	} else if (i == totalSlides) {
		previousSlideID					= 0;
		nextSlideID						= i - 1;
	} else {
		previousSlideID					= i + 1;
		nextSlideID						= i - 1;
	}
	
	try {
		
		// define some reference objects
		previousSlide					= slides.eq(previousSlideID);
		thisSlide						= slides.eq(i);
		nextSlide						= slides.eq(nextSlideID);
		
		// reset the position of this slide so we've got a clean slate
		resetPositions( thisSlide );
		
		thisSlide
			.stop()
			.css("z-index", parseInt(previousSlide.css("z-index"))+1)	// place current element above last element
			.find(".slideshow-bg img")		// again, thank collage for direct element type ref
				.animate(
					{
						opacity: "1"
					},
					1800
				);
		
		setTimeout(function() {
			var _products = thisSlide.find(".product-border");
			var _numProducts = _products.length;
			var _lastObj = false;
			var _padding = 10; 
			var _targetPos = 0;
			var _accumulatingProductWidth = 0;
			
			thisSlide.find(".product-border").each( function( _index ) {
				
				$( this )
					.delay( 500 * _index )
					.animate(
						{
							right: _accumulatingProductWidth + ( _padding * _index ) + ( _padding / 2 ) + "px",
							opacity:"1"
						},
						500 + ( 100 * _index )
					)
					.end()
				
				_accumulatingProductWidth += $( this ).width();
			} );
			thisSlide
				.find( ".copy" )
					.animate(
						{
							left:"0px",
							opacity:"1"
						},
						500
					);
			
		}, 2500 );

		
		i ? i-- : i = totalSlides;
		
		myPause();
	} catch( err ) {
		/*
			somewhere, an error
			an intermittent error was occurring in jquery.color.js which would kill this slideshow
			if the error bubbles, this should just stop things altogether for
			safety's sake... if not, no harm done
		*/
		stopSlideshow();
	}
	
}

// restarts timer to refire start()
function myPause() {
	timerID = setTimeout( start, 7000);
}

// stops slideshow; for debugging purposes
function stopSlideshow() {
	try {
		clearTimeout( timerID );
	} catch( err ) {
		//
	}
}

function init() {
	slides=$('.container');	
	totalSlides = slides.length-1;
	i = totalSlides;
	// combo defining z-index and passing all slides to resetPositions()
	resetPositions( slides.css("z-index","1") );
	start();
}

jQuery(document).ready(init); // document ready
