/* */


/*
$('#clickme').click(function() {
  $('#book').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'
  }, 5000, function() {
    // Animation complete.
  });
});
*/

(function($) {
	
	$.ileeteCarousel = function (element, options) {
		
		var animation = false;
		
		var element = $(element);
		var items = null;
		var items_num = null;
		var items_width = [];
		
		var container_width = parseInt(options.width) || 800;
		var animation_speed = parseInt(options.animation_speed) || 200;
		
		var nav_left = null;
		var nav_right = null;
//		var nav_pages = options.nav_pages || false; // если true - навигация будет выполняться постранично, иначе - поштучно 
		var nav_pages = false; // если true - навигация будет выполняться постранично, иначе - поштучно 
		var current_position = options.current_position || 0;
		var is_first_page = null;
		var is_last_page = null;
		
		if($(options.nav_left).length) {
			var nav_left = $(options.nav_left);
		}
		if($(options.nav_right).length) {
			var nav_right = $(options.nav_right);
		}
		
		function refresh()
		{
			items = element.children();
			items_num = items.length - 1;
			if(items_num) {
				for(i = 0; i < items_num; i++) {
					items_width[i] = $(items[i]).outerWidth() + parseInt($(items[i]).css('margin-left')) + parseInt($(items[i]).css('margin-right'));
				}
			}
			
			checkFirstPage();
			checkLastPage();
		}
		
		function forward()
		{
			if(nav_pages) {
				return nextPage();
			}
			
			if(animation) {return false;}
			animation = true;
//			alert('next');
			
			if(is_last_page) {
				return false;
			}
			
			element.animate({
				right: '+=' + items_width[current_position],
				}, animation_speed, 
				function() {
					// Animation complete.
				}
			);
			
//			element.css('right', parseInt(element.css('right')) + items_width[current_position] + 'px');
			current_position += 1;
			
			checkFirstPage();
			checkLastPage();
			
			animation = false;
		}
		
		function nextPage()
		{
			if(animation) {return false;}
			animation = true;
			
			if(is_last_page) {
				return false;
			}
			
			alert('next page');
			animation = false;
		}
		
		function backward()
		{
			if(nav_pages) {
				return prevPage();
			}
//			alert('prev');
			if(animation) {return false;}
			animation = true;
			
			if(is_first_page) {
				return false;
			}
			
			element.animate({
				right: '-=' + items_width[current_position - 1],
				}, animation_speed, 
				function() {
					// Animation complete.
				}
			);
			
//			element.css('right', parseInt(element.css('right')) - items_width[current_position - 1] + 'px');
			current_position -= 1;
			
			checkFirstPage();
			checkLastPage();
			
			animation = false;
		}
		
		function prevPage()
		{
			if(animation) {return false;}
			animation = true;
			alert('prev page');
			
			if(is_first_page) {
				return false;
			}
			
			animation = false;
		}
		
		function checkFirstPage()
		{
			if(current_position == 0) {
				nav_left.hide();
				element.css('right', '0px');
				return true;
			}
			nav_left.show();
			return false;
		}
		
		function checkLastPage()
		{
			var right = parseInt(element.css('right'));
			var sum = 0;
			var i = current_position;
			
			while(items_width[i] && (sum <= container_width)) {
				sum += items_width[i];
				if(!items_width[i + 1]) {
					sum -= parseInt($(items[i]).css('margin-right'));
				}
				i++;
			}
			
			if(items_width[i + 1] || (sum > container_width)) {
				nav_right.show();
				return false;
			}
			else {
				nav_right.hide();
				return true;
			}
		}
		
		refresh();
		
		function init()
		{
//			alert('init');
			if(nav_right) {
				nav_right.bind('click', function() {
					forward();
				});
				
//				nav_right.bind('dblclick', function() {
//					nextPage();
//				});
			}
			
			if(nav_left) {
				nav_left.bind('click', function() {
					backward();
				})
				
//				nav_left.bind('dblclick', function() {
//					prevPage();
//				});
			}
		}
		
		init();
	}
	
	$.fn.ileeteCarousel = function(options) {
		options = options || {};
		
		return this.each(function () {
			if (!$(this).data('ileeteCarousel')) {
				$(this).data('ileeteCarousel', new $.ileeteCarousel(this, options));
			}
		});
	};
	
})(jQuery);
