/* 
*  jQuery Carousel plugin
*  by Zach Waugh
*  v0.4 - 7/16/2008
* 
*/

(function($){
	
$.fn.carousel = function(options) {
	// Default settings, can be overridden when function is called
	var settings = $.extend({
		next: 'a.next',
		previous: 'a.previous',
		page: 'a.page',
		item: 'li',
		items: '.carousel_items',
		container: '.carousel_container',
		duration: 1000,
		padding: 5,
		perpage: false
	}, options);
	
	$(this).each(function(){
		// Calculate width of displayed area
		var width = $(this).find(settings.container).css('width');
		
		// Width not set in CSS in IE6/IE7
		if (width == 'auto')
		{
			width = $(this).find(settings.container).innerWidth() + settings.padding;
		}
		else
		{
			width = parseFloat(width) + settings.padding;
		}
		
		// Calculate height of items
		var height = $(this).find(settings.container).css('height');
		if(height == '0px' || height == 'auto')
		{
			// Add height to container element if not hardcoded in CSS
			$(this).find(settings.container).css('height', $(this).find(settings.container).find(settings.items).innerHeight());
		}
		
		// Get number of items per "page"
		if(!settings.perpage)
		{
			var itemsize = $(this).find(settings.items).children(settings.item).innerWidth();
			var perpage = Math.round(width / itemsize);
		}
		else
		{
			var perpage = settings.perpage;
		}

		var total = $(this).find(settings.items).children(settings.item).size();

		
		// Figure out how many pages are needed
		// Based on total / x, where x = items per page
	  var pages = Math.ceil(total / perpage);
	
		// Add controls if more than one page
		if (pages > 1)
		{
			var controls = '<div class="controls">\n<a href="" class="previous inactive"></a>';
			for(var i=0; i < pages; i++) {
				if (i == 0)
				{
					controls += '<a href="" class="page active"></a>\n';
				}
				else
				{
					controls += '<a href="" class="page"></a>\n';
				}
			}
			controls += '<a href="" class="next"></a>\n</div>';
			// Add controls node to DOM
			$(this).prepend(controls);
		}

		// calc max 
	  var max = (pages - 1) * width;
	  max = '-' + max + 'px';
		
		// Internal data store
		var info = {carousel: this, width: width, max: max};

		// Bind events
		$(this).find(settings.next).click(function(){
			if($(info.carousel).find(settings.items).css('left') != max)
			{ 
				slideRight(info, info.width);
			}

			return false;
		});

		$(this).find(settings.previous).click(function() {
			if ($(info.carousel).find(settings.items).css('left') != '0px')
			{
				slideLeft(info, info.width);
			}

			return false;
		});

		$(this).find(settings.page).click(function(){
			if(!$(this).hasClass('active'))
			{
				slideTo(info, $(info.carousel).find(settings.page).index(this));
			}

			return false;
		});
	
		$(this).find(settings.items).find(settings.item).children('a.bordered_img').click(function(){
			var content = $(this).next('div').html();
			$(this).parents(settings.items).find('a.bordered_img').removeClass('selected');
			$(this).addClass('selected');
			$(this).parents(info.carousel).next('.related_content').html(content);
			return false;
		});
	
	});

	// slideTo
	// slide to a particular page in the current carousel
	function slideTo(info, page)
	{
		var current_page = currentPage(info);
		if(page < current_page)
		{
			slideLeft(info, (current_page - page) * info.width);
		}
		else
		{
			slideRight(info, (page - current_page) * info.width);
		}
	}
	
	function slideLeft(info, distance)
	{
		$(info.carousel).find(settings.items).animate({left: '+=' + distance + 'px'}, settings.duration, function(){
			$(info.carousel).find(settings.next).removeClass('inactive');
			selectPage(info);
			if ($(info.carousel).find(settings.items).css('left') == '0px')
			{
				$(info.carousel).find(settings.previous).addClass('inactive');
			}
		});
	}
	
	function slideRight(info, distance)
	{
		$(info.carousel).find(settings.items).animate({left: '-=' + distance + 'px'}, settings.duration, function(){
			$(info.carousel).find(settings.previous).removeClass('inactive');
			selectPage(info);
			if ($(info.carousel).find(settings.items).css('left') == info.max) { 
				$(info.carousel).find(settings.next).addClass('inactive');
			}
		});
	}
	
	// selectPage - Helper
	// handles highlighting which page is active
	function selectPage(info)
	{
		var page = currentPage(info);
		$(info.carousel).find(settings.page).removeClass('active');
		$(info.carousel).find(settings.page).slice(page, page + 1).addClass('active');
	}
	
	// currentPage - Helper
	// calculates which page we're on
	function currentPage(info)
	{
		var left = $(info.carousel).find(settings.items).css('left');
		if (left == 'auto')
		{
			page = 0;
		}
		else
		{
			var position = Math.abs(left.substr(0, left.length - 2));
			var page = position / info.width;
		}

		return page;
	}
}
})(jQuery);