(function($) {
	jQuery.fn.kCarousel = function(options) {
		var defaults = {
			navContainer: '',
			topDistance: 0,
			itemWidth: 470,
			itemHeight: 270,
			itemScaleTo: 80,
			delay: 400,
			easing: '',
			onLoad: function() {},
			onScroll: function() {}
		};
		var options = $.extend(defaults, options);
		function percent(value, percent) {
			return value*(percent/100);
		}
		return $(this).each(function() {

			// building array
			var array = new Array();
			$(this).children().each(function() {
				array.push($(this).html());
			});
			//

			// build content based on array
			$(this).html('');
			for (i=0; i<array.length; i++) {
				$(this).append('<li rel="'+i+'">'+array[i]+'</li>');
				if(i==2) break;
			}

			var slide_in = false;
			var slide_out = false;
			function rotate(ul, direction) {
				if (!ul.children('li:first, li:last').is(":animated")) {
					ul.children('li').find('form').hide();
					if (direction == 'left') {
						ul.children('li:first').css({zIndex: 1}).animate(cssCenter, options.delay, options.easing);
						ul.children('li:eq(1)').animate(cssRight, options.delay, options.easing).css({zIndex: 0});
						ul.children('li:last').css({zIndex: -1}).animate(cssOut, options.delay, options.easing, function() {
							$(this).remove();
						});
						var next_item = ul.children('li:first').attr('rel');
						if(next_item == 0) {
							next_item = array.length-1;
						} else {
							next_item = parseFloat(next_item)-1;
						}
						ul.prepend('<li rel="'+next_item+'">'+array[next_item]+'</li>');
						ul.children('li').find('form').hide();
						ul.children('li:first').css({display: 'none'}).css(css).css(cssOut).animate(cssLeft, options.delay, options.easing, function() {
							ul.children('li:eq(1)').find('form').show();
							options.onLoad(this);
							options.onScroll(this);
						});
					} else if (direction == 'right') {
						ul.children('li:last').css({zIndex: 1}).animate(cssCenter, options.delay, options.easing);
						ul.children('li:eq(1)').animate(cssLeft, options.delay, options.easing).css({zIndex: 0});
						ul.children('li:first').css({zIndex: -1}).animate(cssOut, options.delay, options.easing, function() {
							$(this).remove();
						});
						var next_item = ul.children('li:last').attr('rel');
						if(next_item == array.length-1) {
							next_item = 0;
						} else {
							next_item = parseFloat(next_item)+1;
						}
						ul.append('<li rel="'+next_item+'">'+array[next_item]+'</li>');
						ul.children('li').find('form').hide();
						ul.children('li:last').css({display: 'none', zIndex: -1}).css(css).css(cssOut).animate(cssRight, options.delay, options.easing, function() {
							ul.children('li:eq(1)').find('form').show();
							options.onLoad(this);
							options.onScroll(this);
						});
					}
				}
			}

			var width = $(this).width();
			$(this).css({height: options.itemHeight+'px', overflow: 'hidden'});
			$(this).children('li').css({width: options.itemWidth+'px', height: options.itemHeight+'px'});

			var count = $(this).children('li').length;

			if (count == 1) {
				// only 1 item
				$(this).children('li').css({float: 'none', margin: 'auto'});
			} else if (count == 2) {
				// only 2 items
				$(this).children('li:last').css({float: 'right'}).prependTo($(this));
			} else if (count > 2) {

				// only 3 and more items
				css = new Object;
					css.position = 'absolute';
				cssCenter = new Object;
					cssCenter.top = 0;
					cssCenter.left = (width-options.itemWidth)/2+'px';
					cssCenter.width = options.itemWidth+'px';
					cssCenter.height = options.itemHeight+'px';
					// cssCenter.opacity = 1;
				cssLeft = new Object;
					// cssLeft.opacity = 1;
					cssLeft.top = (options.itemHeight-percent(options.itemHeight, options.itemScaleTo))/2+'px';
					cssLeft.left = 0;
					cssLeft.width = percent(options.itemWidth, options.itemScaleTo)+'px';
					cssLeft.height = percent(options.itemHeight, options.itemScaleTo)+'px';
					// cssLeft.opacity = 0.2;
				cssRight = new Object;
					// cssRight.opacity = 1;
					cssRight.top = (options.itemHeight-percent(options.itemHeight, options.itemScaleTo))/2+'px';
					cssRight.left = width-percent(options.itemWidth, options.itemScaleTo)+'px';
					cssRight.width = percent(options.itemWidth, options.itemScaleTo)+'px';
					cssRight.height = percent(options.itemHeight, options.itemScaleTo)+'px';
					// cssRight.opacity = 0.2;
				cssOut = new Object;
					cssOut.top = (options.itemHeight-(options.itemHeight/4))/2+'px';
					cssOut.left = (width-(options.itemWidth/4))/2+'px';
					cssOut.width = (options.itemWidth/4)+'px';
					cssOut.height = (options.itemHeight/4)+'px';
					// cssOut.opacity = 0;

				$(this).css({position: 'relative', zIndex: 0});
				$(this).children('li').css(css); // apply default css object
				$(this).children('li:eq(0)').css(cssLeft);
				$(this).children('li:eq(1)').css(cssCenter).css({zIndex: 1});
				$(this).children('li:eq(2)').css(cssRight);

				$(options.navContainer).prepend('<div class="sliderArrowHolderLeft" style="position:absolute;top:'+options.topDistance+'px;background-position: 0 -'+options.topDistance+'px"><div><a href="#" class="btn" rel="left"></a></div></div>');
				$(options.navContainer).prepend('<div class="sliderArrowHolderRight" style="position:absolute;top:'+options.topDistance+'px;background-position: 0 -'+options.topDistance+'px"><div><a href="#" class="btn" rel="right"></a></div></div>');

				// add click event
				var ul = $(this);
				$(options.navContainer).find('.btn').focus().bind('click keydown keyup', function(event) {
					if (event.type == 'keydown') {
						if (event.keyCode == 37) {
							rotate(ul, 'left');
							$('body').find('a[rel=left]').addClass('click');
						} else if (event.keyCode == 39) {
							rotate(ul, 'right');
							$('body').find('a[rel=right]').addClass('click');
						}
					} else if (event.type == 'keyup') {
						$('body').find('.btn').removeClass('click');
					} else {
						rotate(ul, $(this).attr('rel'));
					}
				});

				options.onLoad(this);

			} else {
				$(this).html('<li>.kCarousel loading error.</li>');
				$(this).css({width: 'auto', height: 'auto'});
				$(this).children('li').css({width: 'auto', height: 'auto'});
			}

		});
	};
})(jQuery);
