/**
 * Image carousel v0.1
 * Paul Bland
 * January 22 2011
 */
	 
$(document).ready(function() {
	
	/** 
	 * Run function to disable necessary links on page load
	 */
	$('.carousel_parent').each(function() {
		check_carousel_links($(this));
	});

	/**
	 * Bind function to control carousel left click
	 */
	$('.carousel_left').click(function() {
	
		// If the link is disabled return false
		if ($(this).hasClass('disabled')) {
			return false;
		}
		
		// Temporarily disable link so it can't be clicked while animation is occuring
		// (Note: the class will be removed by the 'check_carousel_links' callback function)
		$(this).addClass('disabled');
		
		var carousel_parent = $(this).parents('.carousel_parent');
		var carousel_inside = carousel_parent.find('.carousel_inside');
		var carousel_container = carousel_parent.find('.carousel_container');
		
		var container_width = carousel_container.outerWidth(true);
		var inside_width = carousel_inside.outerWidth(true);
		var content_position = (-1 * carousel_inside.position().left);
		var li_right_margin = parseInt(carousel_inside.find('LI').css('margin-right'));
		
		var shift_width;
		
		// Move the width of the container, or is there's not enough content, move to end on content.
		if (content_position < container_width) {
			shift_width = content_position;
		} else {
			shift_width = (container_width + li_right_margin);
		}
		
		/*
		alert(
			'container_width: ' + container_width + '\n' + 
			'inside_width: ' + inside_width + '\n' + 
			'content position: ' + content_position + '\n' + 
			'shift width: ' + shift_width + '\n' + 
			'li width: ' + carousel_inside.find('LI').outerWidth()
			);
		*/
		
		carousel_inside.animate(
			{'left': '+='+shift_width},
			function() {
 				check_carousel_links(carousel_parent);
			});
		
		return false;
	});
	
	
	
	/**
	 * Bind function to control carousel right click
	 */
	$('.carousel_right').click(function() {
	
		// If the link is disabled return false
		if ($(this).hasClass('disabled')) {
			return false;
		}
		
		// Temporarily disable link so it can't be clicked while animation is occuring
		// (Note: the class will be removed by the 'check_carousel_links' callback function)
		$(this).addClass('disabled');
		
		var carousel_parent = $(this).parents('.carousel_parent');
		var carousel_inside = carousel_parent.find('.carousel_inside');
		var carousel_container = carousel_parent.find('.carousel_container');
		
		var container_width = carousel_container.outerWidth(true);
		var inside_width = carousel_inside.outerWidth(true);
		var content_position = (-1 * carousel_inside.position().left);
		var li_right_margin = parseInt(carousel_inside.find('LI').css('margin-right'));
	
		var shift_width;
		
		// Calculate the size of the unseen portion (content_position + content_width - outside_width)
		var unseen_portion = (inside_width - content_position - container_width);
		
		// If unseen portion is smaller than movement width, use that, otherwise use default
		if (unseen_portion < container_width) {
			shift_width = (unseen_portion - li_right_margin);
		} else {
			shift_width = (container_width + li_right_margin);
		}
		
		/*
		alert(
			'container_width: ' + container_width + '\n' + 
			'inside_width: ' + inside_width + '\n' + 
			'content position: ' + content_position + '\n' + 
			'unseen portion: ' + unseen_portion + '\n' + 
			'shift width: ' + shift_width + '\n' + 
			'li width: ' + carousel_inside.find('LI').outerWidth()
			);
		*/
		
		carousel_inside.animate(
			{'left': '-='+shift_width},
			function() {
 				check_carousel_links(carousel_parent);
			});
		
		return false;
	});

});
/* End $(document).ready() */



/**
 * Function to add/remove disabled class from carousel links
 * (Called on page load and after a link has been clicked)
 */
function check_carousel_links(carousel_parent) {
	
	var carousel_inside = carousel_parent.find('.carousel_inside');
	var carousel_container = carousel_parent.find('.carousel_container');
		
	var container_width = carousel_container.outerWidth(true);
	
	// This if statement added to display each section before checking its 
	// width (since is has no width when display:none)
	//if (carousel_parent.css("display") == "none") {
	
	if (carousel_parent.is(":hidden")) {
		carousel_parent.show();
		var inside_width = carousel_inside.outerWidth(true);
		carousel_parent.hide();
	} else {
		var inside_width = carousel_inside.outerWidth(true);
	}
	
	var content_position = (-1 * carousel_inside.position().left);
	var li_right_margin = parseInt(carousel_inside.find('LI').css('margin-right'));

	// Check left link - disable if position is greater than or equal to zero
	if (content_position <= 0) {
		carousel_parent.find('.carousel_left').addClass('disabled');
	} else {
		carousel_parent.find('.carousel_left').removeClass('disabled');
	}
	
	// Check right link --- disable if content width minus content position 
	// (including margin) is less than or equal to outside box width. 
	if ((inside_width - content_position - li_right_margin) <= container_width) {
		carousel_parent.find('.carousel_right').addClass('disabled');
	} else {
		carousel_parent.find('.carousel_right').removeClass('disabled');
	}
	
	/*
	alert(
		'container_width: ' + container_width + '\n' + 
		'inside_width: ' + inside_width + '\n' + 
		'content position: ' + content_position
		);
	*/
}



