(function($){
	$.fn.slidertool = function(options){
		var defaults =	{
			items: '.Items',
			popup: '.PopUp',
			btnClosePopUp: '.btnClose',
			visibleItems: 4,
			btnLeft: '.Left',
			btnRight: '.Right',
			cellWidth: null,
			type: 0,
			animateSpeed: 500,
			AutoplayInterval: 4500
		};
		
		var opt = $.extend(defaults, options);
		
		var obj = $(this);
		
		var posSlide = 0;
		var popupActive = -1;
		
		
		obj.find(opt.btnRight + ".Active").click(function(){
			posSlide++;
			if (posSlide > (obj.find(opt.items).find('.Item').length - opt.visibleItems)){
				posSlide = (obj.find(opt.items).find('.Item').length - opt.visibleItems);
			}
			NavsUpdate();
			MoveItems(posSlide);
		});
		obj.find(opt.btnLeft + ".Active").click(function(){
			posSlide--;
			if (posSlide < 0){
				posSlide = 0;
			}
			NavsUpdate();
			MoveItems(posSlide);
		});
		
		if (opt.visibleItems < obj.find(opt.items).find('.Item').length){
			obj.find(opt.btnRight).addClass('Active');
			obj.find(opt.btnLeft).removeClass('Active');
		} else if (opt.visibleItems == obj.find(opt.items).find('.Item').length){
			obj.find(opt.btnRight).removeClass('Active');
			obj.find(opt.btnLeft).removeClass('Active');
		}
		
		if (opt.type == 1){
			document.SlideItemsImageWidth = [];
			document.SlideItemsImageHeight = [];
			document.SlideItemsImagePosition = [];
			document.SlideItemsImageInnerWidth = [];
			document.SlideItemsImageInnerHeight = [];
			document.SlideItemsImageInnerPosition = [];
			for (var a = 0; a < obj.find(opt.items).find('.Item').length; a++){
				document.SlideItemsImageWidth[a] = $(this).find('.ImageBorder').eq(a).width();
				document.SlideItemsImageHeight[a] = $(this).find('.ImageBorder').eq(a).height();
				document.SlideItemsImagePosition[a] = $(this).find('.ImageBorder').eq(a).position();
				document.SlideItemsImageInnerWidth[a] = $(this).find('.ImageBorder').eq(a).find('.Image').width();
				document.SlideItemsImageInnerHeight[a] = $(this).find('.ImageBorder').eq(a).find('.Image').height();
				document.SlideItemsImageInnerPosition[a] = $(this).find('.ImageBorder').eq(a).find('.Image').find('img').position();
			}
			
			obj.find(opt.items).find('.Item').hover(function(){
				var index = obj.find(opt.items).find('.Item').index(this);
				$(this).find('.ImageBorder').stop().animate({
					'width': 178,
					'height': 133,
					'top': 5,
					'left': 5
					},opt.animateSpeed);
				$(this).find('.ImageBorder').find('.Image').stop().animate({
					'width': 178,
					'height': 133
					}, opt.animateSpeed);
				$(this).find('.ImageBorder').find('.Image').find('img').stop().animate({
					'top': 0,
					'left': 0
					}, opt.animateSpeed);
			}, function(){
				var index = obj.find(opt.items).find('.Item').index(this);
				$(this).find('.ImageBorder').stop().animate({
					'width': document.SlideItemsImageWidth[index],
					'height': document.SlideItemsImageHeight[index],
					'top': document.SlideItemsImagePosition[index].top,
					'left': document.SlideItemsImagePosition[index].left
					}, opt.animateSpeed);
				$(this).find('.ImageBorder').find('.Image').stop().animate({
					'width': document.SlideItemsImageInnerWidth[index],
					'height': document.SlideItemsImageInnerHeight[index]
					}, opt.animateSpeed);
				$(this).find('.ImageBorder').find('img').stop().animate({
					'top': document.SlideItemsImageInnerPosition[index].top,
					'left': document.SlideItemsImageInnerPosition[index].left
					}, opt.animateSpeed);
			});
		} else if (opt.type == 2){
			PopPositionUpdate();
			obj.find(opt.items).find('.Item').click(function(){
				var index = obj.find(opt.items).find('.Item').index(this);
				
				if (popupActive == -1){
					for (var a = 0; a < obj.find(opt.items).find('.Item').length; a++){
						if (a == index){
							obj.find(opt.items).find('.Item').eq(a).find(opt.popup).show();
							obj.find(opt.items).find('.Item').eq(a).css('z-index', 10);
							popupActive = 1;
						} else {
							obj.find(opt.items).find('.Item').eq(a).find(opt.popup).hide();
							obj.find(opt.items).find('.Item').eq(a).css('z-index', 0);
						}
					}
				} else {
					for (var a = 0; a < obj.find(opt.items).find('.Item').length; a++){
						obj.find(opt.items).find('.Item').eq(a).find(opt.popup).hide();
						obj.find(opt.items).find('.Item').eq(a).css('z-index', 0);
					}
					popupActive = -1;
				}
			});
			$(opt.btnClosePopUp).click(function(){
				$(this).parent().hide(1, function(){ popupActive = -1; });
			});
		} else if (opt.type == 5){
			$.each(obj.find(opt.items).find('.Item'), function(i, v){
				$(v).css({
					'position': 'absolute',
					'top': 0,
					'left': 0
				});
				if (i > 0){
					$(v).hide();
				}
			});
			
			var intervalType5 = setTimeout(function(){
				obj.find(opt.btnRight + ".Active").click();
				Interval();
			}, opt.AutoplayInterval);
			
			obj.hover(function(){
				clearTimeout(intervalType5);
				clearInterval(intervalType5);
			}, function(){
				intervalType5 = setTimeout(function(){
					ClickSimulation();
					Interval();
				}, opt.AutoplayInterval);
			});
			
			
			
			function Interval(){
				clearTimeout(intervalType5);
				intervalType5 = setInterval(function(){
					ClickSimulation();
					
				}, opt.AutoplayInterval);
			}
			
			function ClickSimulation(){
				posSlide++;
				if (posSlide > (obj.find(opt.items).find('.Item').length - opt.visibleItems)){
					posSlide = 0;
				}
				NavsUpdate();
				MoveItems(posSlide);	
			}
		}
		
		
		function NavsUpdate(){
			if (posSlide == 0){
				obj.find(opt.btnLeft).removeClass('Active');
			} else {
				obj.find(opt.btnLeft).addClass('Active');
			}
			if (posSlide == (obj.find(opt.items).find('.Item').length - opt.visibleItems)){
				obj.find(opt.btnRight).removeClass('Active');
			} else {
				obj.find(opt.btnRight).addClass('Active');
			}
			if (opt.type == 2){
				for (var a = 0; a < obj.find(opt.items).find('.Item').length; a++){
					obj.find(opt.items).find('.Item').eq(a).find(opt.popup).hide();
					obj.find(opt.items).find('.Item').eq(a).css('z-index', 0);
					PopPositionUpdate();
				}
				popupActive = -1;
			}
		}
		function PopPositionUpdate(){
			for (var a = 0; a < obj.find(opt.items).find('.Item').length; a++){
				if ((a > 1) && (a < (obj.find(opt.items).find('.Item').length - 2))){
					if (a - posSlide > 1){
						obj.find(opt.items).find('.Item').eq(a).find(opt.popup).removeClass('Left');
						obj.find(opt.items).find('.Item').eq(a).find(opt.popup).addClass('Right');
					} else {
						obj.find(opt.items).find('.Item').eq(a).find(opt.popup).removeClass('Right');
						obj.find(opt.items).find('.Item').eq(a).find(opt.popup).addClass('Left');
					}
				}
			}
		}
		
		function MoveItems(index){
			if (opt.type == 5){
				$.each(obj.find(opt.items).find('.Item'), function(i, v){
					if (i == index){
						$(v).fadeIn();
					} else {
						$(v).fadeOut();
					}
				});
			} else {
				if (opt.cellWidth != null){
					obj.find(opt.items).stop().animate({'left': -index * opt.cellWidth}, opt.animateSpeed);
				} else {
					obj.find(opt.items).stop().animate({'left': -index * (obj.find(opt.items).find('.Item').eq(0).width() + 2)}, opt.animateSpeed);
				}
			}
		}
	}
})(jQuery);
