// Switches content with ease (haha)
// By Michael P. Geraci, 2010
// http://www.michaelgeraci.com

$(function(){
	master();
});

function master(){
	if ($('.indexEaseParent').length != 0) {
		// handle the click functions for left, bullet, and right
		
		$('.navigationLeft').click(function(){
			changeContent($(this), 'left');
		});
		
		$('.bullet').click(function(){
			changeContent($(this), 'index');
		});
		
		$('.navigationRight').click(function(){
			changeContent($(this), 'right');
		});
	}
}

function changeContent(element, direction){
	// width of the content that you're switching
	width = 640;
	
	// get the number of positions that you have
	maxIndex = element.parents('.prank_nav').children('.bullet').length;

	// get the active container
	container = element.parents('.prank_top').next('.pranks').children('.indexEaseParent').children('.indexEaseContainer');

	// get the current display
	currentIndex = element.parents('.prank_nav').children('.current').attr('class').replace(/.+(\d).+/, '$1');

	// set the next index based on direction
	switch (direction) {
		case 'left':
			// decrement the nextIndex
			nextIndex = currentIndex - 1;
			
			// if the nextIndex is zero, loop around to the max
			if (nextIndex == 0) {
				nextIndex = maxIndex;
			}

			break;
		case 'index':
			// get the clicked index
			nextIndex = element.attr('class').replace(/bullet /, '');

			break;
		case 'right':
			// increment the nextIndex
			nextIndex = parseInt(currentIndex) + 1;

			// if the nextIndex is greater than the max, loop around to 1
			if (nextIndex > maxIndex) {
				nextIndex = 1;
			}
			
			break;
	}

	// set the nextPosition
	nextPosition = (nextIndex - 1) * width * -1;
	nextPosition = nextPosition + 'px';
	

	// animate it
	container.animate({left: nextPosition}, 500, 'swing');
	
	// remove the current current	
	element.parent('.prank_nav').children('a').removeClass('current');
	
	// add the new current
	element.parent('.prank_nav').children('a.' + nextIndex).addClass('current');
}