$( function() {
	showImages();
});

var shotsets = new Array();

var currentImages = new Array();

var disabledShotSet = -1;

function showImages() {
	shotdivs = $('div.screenshots');

	position = calculateCenter();

	zindex = 100;
	shotdivs.each( function() {
		shots = $(this).children('a');
		shots.stop().css('z-index',zindex).fadeOut(0);
		shots.filter(':first').animate({left:position+'px'},0);
		shotsets.push( shots );
		zindex--;
	});

	for (offset=0; offset<shotsets.length;offset++) {
		altpos = calculatePosition(offset);
		shotsets[offset].filter(':gt(0)').animate({left:altpos+'px'},0);
	}

	img = shotsets[0].filter(':first');

	img.stop().css('z-index','200').fadeIn(1000);

	setTimeout( 'slidePeers(shotsets)', 2000); 
}

function calculateCenter() {
	area_width = 1000;
	image_width = 140;

	center = (area_width/2) - (image_width/2);

	return center;
}

function calculatePosition(offset) {
	area_width = 1000;
	padding_exterior = 150;
	image_width = 140;
	area_to_pad = area_width - (image_width*shotsets.length) - (padding_exterior*2);
	interior_pad = area_to_pad / (shotsets.length-1);

	position = padding_exterior + (offset*(image_width+interior_pad));

	return position;
}

function slidePeers() {
	for (offset=0; offset<shotsets.length; offset++) {
		peer = shotsets[offset].filter(':first');
		position = calculatePosition( offset );
		peer.stop().fadeIn(0).animate( {left: position+'px'},1000);
	}

	setInterval( 'flipImages()', 5000 );
}

function flipImages() {
	if (currentImages.length<shotsets.length) {
		for (offset=0;offset<shotsets.length;offset++) {
			currentImages.push(0);
		}
	}

	for (offset=0; offset<shotsets.length; offset++) {
		if (disabledShotSet!=offset) {
			filter = ':eq(' + currentImages[offset] + ')';
			imgold = shotsets[offset].filter(filter);
			imgold.stop().fadeOut(2000);
			if ( currentImages[offset] < shotsets[offset].length-1 ) {
				currentImages[offset]++;
			} else {
				currentImages[offset]=0;
			}
			filter = ':eq(' + currentImages[offset] + ')';
			imgnew = shotsets[offset].filter(filter);
			imgnew.stop().fadeIn(2000);
		}
	}	
}
