/* ------------------------------------------------------------------------
	Class: prettyGallery
	Use: Gallery plugin for jQuery
	Author: Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 1.1
------------------------------------------------------------------------- */
jQuery.fn.prettyGallery = function(settings) {
	settings = jQuery.extend({
		itemsPerPage : 0,
		animationSpeed : 'normal', /* fast/normal/slow */
		navigation : 'top',  /* top/bottom/both */
		of_label: ' of ', /* The content in the page "1 of 2" */
		previous_title_label: 'Previous page', /* The title of the previous link */
		next_title_label: 'Next page', /* The title of the next link */
		previous_label: 'Previous', /* The content of the previous link */
		next_label: 'Next', /* The content of the next link */
		gallery_id: 'gallery',
		navigation_cont: 'navig',
		width: 50,
		padding: '13px 0 0 4px',
		left: 0
	}, settings);

	return this.each(function(){
		// Global variables needed in multiple functions.	
		
		var currentPage = 1;
		var itemWidth = 0;
		var itemHeight = 0;
		var galleryWidth = 0;
		var pageCount = 0;
		var animated = false;
		var $gallery = $(this);
	
		
		//var $gallery = $("#"+settings.gallery_id);
		var $navig_cont = $("#"+settings.navigation_cont);
		
		var prettyGalleryPrevious = function(caller) {
		
			
			// Make sure not to double animate, and not animate of the button is disabled
			if(animated || $(caller).hasClass('disabled')) return;
			animated = true;

			$gallery.find('li:lt('+(currentPage * settings.itemsPerPage)+')').each(function(i){
				$(this).animate({'left': parseFloat($(this).css('left')) + (galleryWidth + itemMargin) }, settings.animationSpeed, function(){
					animated = false;
				});
			});

			$gallery.find('li:gt('+ ((currentPage * settings.itemsPerPage) - 1) +')').each(function(i){
				$(this).animate({'left': parseFloat($(this).css('left')) + (galleryWidth + itemMargin) }, settings.animationSpeed);
			});

			currentPage--;

			_displayPaging();
		};

		var prettyGalleryNext = function(caller) {

			// Make sure not to double animate, and not animate of the button is disabled
			if(animated || $(caller).hasClass('disabled')) return;
			animated = true;

			$gallery.find('li:lt('+(currentPage * settings.itemsPerPage)+')').each(function(i){
				$(this).animate({'left': parseFloat($(this).css('left')) - (galleryWidth + itemMargin) }, settings.animationSpeed, function(){
					animated = false;
				});
			});

			$gallery.find('li:gt('+ ((currentPage * settings.itemsPerPage) - 1) +')').each(function(i){
				$(this).animate({'left': parseFloat($(this).css('left')) - (galleryWidth + itemMargin) }, settings.animationSpeed);
			});

			currentPage++;

			_displayPaging();
		};

		var _formatGallery = function() {
			
			
				itemWidth = $gallery.find('li:first').width();
				itemMargin = parseFloat($gallery.find('li:first').css('margin-right')) + parseFloat($gallery.find('li:first').css('margin-left')) + parseFloat($gallery.find('li:first').css('padding-left')) + parseFloat($gallery.find('li:first').css('padding-right')) + parseFloat($gallery.find('li:first').css('border-left-width')) + parseFloat($gallery.find('li:first').css('border-right-width'));
				
				itemHeight = $gallery.find('li:first').height() + parseFloat($gallery.find('li:first').css('margin-top')) + parseFloat($gallery.find('li:first').css('margin-bottom')) + parseFloat($gallery.find('li:first').css('padding-top')) + parseFloat($gallery.find('li:first').css('padding-bottom'));
				galleryWidth = (itemWidth + itemMargin) * settings.itemsPerPage - parseFloat($gallery.find('li:first').css('margin-right')); // We don't want the margin of the last item, that's why we remove it.
	
				$gallery.css({
					'width': settings.width,
					'height': itemHeight,
					'overflow': 'hidden',
					'position': 'relative',
					'padding' : settings.padding,
					//'clear': 'left'
				});
				$gallery.find('li').each(function(i){
			
					$(this).css({
						'position':'absolute',
						'left':i * (itemWidth + itemMargin)+settings.left
					});
				});
			
		};

		var _displayPaging = function() {
			
			$navig_cont.find('a').removeClass('disabled');
			// Display the proper nav
			if(currentPage == 1){
				// Hide the previous button
				$navig_cont.find('a:[title="Previous"]').addClass('disabled');	
			} else if(currentPage == pageCount) {
				// Hide the next button
				$navig_cont.find('a:[title="Next"]').addClass('disabled');
			};
		};

		var _applyNav = function() {
			
			$navig_cont.find('a:[title="Previous"]').bind('click',function(){
				prettyGalleryPrevious(this);
				return false;
			});

			$navig_cont.find('a[title="Next"]').bind('click',function(){
				prettyGalleryNext(this);
				return false;
			});
		};
		
		// Check if we need the gallery
		if($(this).find('li').size() > settings.itemsPerPage) {		
			// Set the number of pages
			if($(this).find('li').size() > 0){
				pageCount = Math.ceil($(this).find('li').size() / settings.itemsPerPage);
			}
			// Format the gallery properly
			if($(this).find('li').size() > 0){
				
				_formatGallery();
			}
			
			// Build and display the nav
			_applyNav();
			
			// Display the proper paging
			_displayPaging(this);
			currentPage = 1;
		};
	});
};
