/* ------------------------------------------------------------------------
	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 : 2,
		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 */
	}, 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 mouseOver = false;
		var $gallery = $(this);
		
		var prettyGalleryPrevious = function(caller) {
			prettyGalleryGoToPage(-1, caller);
			currentPage--;
		};

		var prettyGalleryNext = function(caller) {
			prettyGalleryGoToPage(1, caller);
			currentPage++;
		};
		
		var prettyGalleryReadRel = function (caller){
			// find the corresponding page number
			var n = $(caller).find('a').attr('rel');
			var nc = n - currentPage;
			
			prettyGalleryGoToPage(nc, caller)
			
			
			// set current page
			currentPage = n;
		}
		
		
		var prettyGalleryGoToPage = function(n, caller) {
			
			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)*n) }, 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)*n) }, settings.animationSpeed);
			});
			
			// set paging
			_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 + 0) * 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': '460px',
			// 	'height': itemHeight,
			// 	'overflow': 'hidden',
			// 	'position': 'relative',
			// 	'clear': 'left',
			// 	'margin': '0'
			// });
			$gallery.find('li').each(function(i){
				$(this).css({
					'position':'absolute',
					'top':0,
					'left':i * (itemWidth + 0)
				});
			});

			$gallery.wrap('<div class="prettyGallery"></div>').addClass('prettyGallery');
		};

		var _displayPaging = function() {

			$cg = $gallery.parents('div.prettyGallery:first'); // The containing gallery

			$cg.find('ul.prettyNavigation span.current').text(currentPage);
			$cg.find('ul.prettyNavigation span.total').text(pageCount);

			// Make sure all the links are enabled
			//$cg.find('ul.prettyNavigation li a').removeClass('disabled');
			$cg.find('ul.prettyNavigation li a').show()
			
			
			// Display the proper nav
			if(currentPage == 1){
				// Hide the previous button
				$cg.find('ul.prettyNavigation li.prev a').hide();
			} else if(currentPage == pageCount) {
				// Hide the next button
				$cg.find('ul.prettyNavigation li.next a').hide();
			};
			
		};

		var _applyNav = function() {
			var template = '';
			template +='<div class="prettyNavContainer clearfix"><ul class="prettyNavigation clearfix">';
			
			for(var i=1; i<=pageCount; i++){
				if( i==1){
					var c = 'first'
				}
				else if( i==pageCount){
					var c = 'last'
				}
				else{
					var c = ''
				}
				template += '<li class="'+ c +'"><a href="#" title="Ga naar slide '+ i +'" rel="'+ i +'"><img src="/static/img/gallery_page_button.gif" alt="page button" /></a></li>'
			}

			template += '</ul></div>';

			switch(settings.navigation){
				case 'top':
					$gallery.before(template);
					break;
				case 'bottom':
					$gallery.after(template);
					break;
				case 'both':
					$gallery.before(template);
					$gallery.after(template);
					break;
			};

			// Adjust the nav to the gallery width
			$theNav = $gallery.parent('div.prettyGallery:first').find('ul.prettyNavigation');
			galleryBorderWidth = parseFloat($theNav.css('border-left-width')) + parseFloat($theNav.css('border-right-width'));
			// $theNav.width(galleryWidth - galleryBorderWidth);
			// $theNav.each(function(){
			// 	$(this).find('li:eq(1)').width(galleryWidth - galleryBorderWidth - parseFloat($(this).parent().find('ul.prettyNavigation li:first').width()) - parseFloat($(this).parent().find('ul.prettyNavigation li:last').width()));
			// });

			// Apply the functions to the buttons
			$theNav.find('li.prev a').bind('click',function(){
				prettyGalleryPrevious(this);
				return false;
			});

			$theNav.find('li.next a').bind('click',function(){
				prettyGalleryNext(this);
				return false;
			});
			

			// find list items and catch clickevent
			$theNav.find('li').bind('click',function(e){
			
				// event.stop
				e.preventDefault();
				
				// go to page
				prettyGalleryReadRel(this)
			})

		};

		var _mouseOver = function() {
			mouseOver = true;
		};
		
		var _mouseOut = function() {
			mouseOver = false;
		};

		
		var _autoAnimate = function() {
			setInterval(function() {
				if (!mouseOver){	// prevent animation while mouse is over the gallery
					if (currentPage < pageCount){
						prettyGalleryNext();
					} else {
						prettyGalleryGoToPage(1-currentPage);
						currentPage = 1;
					}
				}
			}, 2000)
		};
		
		// Check if we need the gallery
		if($(this).find('li').size() > settings.itemsPerPage) {
			// Set the number of pages
			pageCount = Math.ceil($(this).find('li').size() / settings.itemsPerPage);

			// Format the gallery properly
			_formatGallery();
			
			// Build and display the nav
			// _applyNav();
			
			// Display the proper paging
			_displayPaging(this);
			currentPage = 1;
			
			// set hover to prevent animation while mouse is over the gallery
			$(this).hover(_mouseOver,_mouseOut);
			
			_autoAnimate();
		};
	});
};function loadGallery(){
	$("ul.gallery").prettyGallery({
		itemsPerPage : 3,
		animationSpeed : 'normal', /* fast/normal/slow */
		navigation : 'bottom',  /* 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 */
	});

}

$(document).ready(loadGallery);
