/*
 * VitaSlider
 * Разработка - Скубаев Алексей (Test-Templates.com)
 * 
*/
jQuery.fn.VitaSlider = function(options){
	
	var options = jQuery.extend({

	    SliderTime: 10000, // Time
	    autoPlay: true // 
	   
	  },options);
	
	return this.each(function() {
			
		////////////////////////////
		var thisSlider = $(this);
		
		thisSlider.find('.slider-item:first').addClass('slaider-activ').fadeIn(500);
		
		if (options.autoPlay === false) { 
			options.SliderTime = 90000000;
		} 
	
		var currentHeightSlider = thisSlider.find('.slider-item:first img:last').height();
		thisSlider.css({'height':currentHeightSlider, 'font-size':currentHeightSlider/47});
		
		$(window).resize(function(){
			var currentHeightSlider = $('.slaider-activ img:last').height();
			thisSlider.css({'height':currentHeightSlider, 'font-size':currentHeightSlider/47});
			var currentWinSize = $(window).width();
			var currentBlockMargin = $('.slaider-activ .slider-text').css('left');
			currentBlockMargin = parseInt(currentBlockMargin, 10);
			if ( currentWinSize < currentBlockMargin + 530 ) {
			  $('.slaider-activ .slider-text').css({'left':'auto', 'right':'20px'}).attr('rel',(currentBlockMargin+530));
			}
		});
		
		function showNext() {
			clearTimeout(timerId);
			$('.slider-item, .slider-item h3, .slider-text').queue("fx", []);
			$('.slider-item, .slider-item h3, .slider-text').stop();
			var currentBlock = $('.slaider-activ');
			var nextBlock = currentBlock.next('.slider-item').length ? currentBlock.next('.slider-item') : $('.slider-item:first');
			$('.slider-item').fadeOut(400);
			$('.slider-item h3, .slider-item .slider-text').css({'opacity':'0'});
			$('.slider-item').removeClass('slaider-activ');
			nextBlock.addClass('slaider-activ').fadeIn(1000, function(){
				var dataTime = $('.slaider-activ h3').data('time');
				var dataTime2 = $('.slaider-activ .slider-text').data('time');
				var datatransition = $('.slaider-activ h3').data('transition');
				var datatransition2 = $('.slaider-activ .slider-text').data('transition');
				var leftM = $('.slaider-activ h3').data('x');
				var topM = $('.slaider-activ h3').data('y');
				var leftM2 = $('.slaider-activ .slider-text').data('x');
				var topM2 = $('.slaider-activ .slider-text').data('y');
				$('.slaider-activ h3').animate({
					'opacity': '1',
					'left':''+leftM+'%',
					'top':''+topM+'%'
				}, dataTime, ''+datatransition+'', function(){
						$('.slaider-activ .slider-text').animate({
							'opacity': '1',
							'left':''+leftM2+'%',
							'top':''+topM2+'%'
						}, dataTime2, ''+datatransition2+'', function(){
							var currentWinSize = $(window).width();
							var currentBlockMargin = $('.slaider-activ .slider-text').css('left');
							currentBlockMargin = parseInt(currentBlockMargin, 10);
							if ( currentWinSize < currentBlockMargin + 530 ) {
								 $('.slaider-activ .slider-text').css({'left':'auto', 'right':'20px'}).attr('rel',(currentBlockMargin+530));
							}
						});
					});
			});
			timerId = setTimeout(showNext, options.SliderTime);
			
			$('.slider-item h3').each(function() {
				var randomX = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				var randomY = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				$(this).css({
					'left':randomX,
					'top':randomY
				});
			});
			
			$('.slider-item .slider-text').each(function() {
				var randomX = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				var randomY = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				$(this).css({
					'left':randomX,
					'top':randomY
				});
			});
		};
		
		timerId = setTimeout(showNext, options.SliderTime);

		
		$('.next').click(showNext);
		
		function showPrev() {
			clearTimeout(timerId);
			$('.slider-item, .slider-item h3, .slider-text').queue("fx", []);
			$('.slider-item, .slider-item h3, .slider-text').stop();
			var currentBlock = $('.slaider-activ');
			var prevBlock = currentBlock.prev('.slider-item').length ? currentBlock.prev('.slider-item') : $('.slider-item:last');
			$('.slider-item').fadeOut(500);
			$('.slider-item h3, .slider-item .slider-text').css({'opacity':'0'});
			$('.slider-item').removeClass('slaider-activ');
			prevBlock.addClass('slaider-activ').fadeIn(1000, function(){
				var dataTime = $('.slaider-activ h3').data('time');
				var dataTime2 = $('.slaider-activ .slider-text').data('time');
				var datatransition = $('.slaider-activ h3').data('transition');
				var datatransition2 = $('.slaider-activ .slider-text').data('transition');
				var leftM = $('.slaider-activ h3').data('x');
				var topM = $('.slaider-activ h3').data('y');
				var leftM2 = $('.slaider-activ .slider-text').data('x');
				var topM2 = $('.slaider-activ .slider-text').data('y');
				$('.slaider-activ h3').animate({
					'opacity': '1',
					'left':''+leftM+'%',
					'top':''+topM+'%'
				}, dataTime, ''+datatransition+'', function(){
						$('.slaider-activ .slider-text').animate({
							'opacity': '1',
							'left':''+leftM2+'%',
							'top':''+topM2+'%'
						}, dataTime2, ''+datatransition2+'', function(){
							var currentWinSize = $(window).width();
							var currentBlockMargin = $('.slaider-activ .slider-text').css('left');
							currentBlockMargin = parseInt(currentBlockMargin, 10);
							if ( currentWinSize < currentBlockMargin + 530 ) {
								 $('.slaider-activ .slider-text').css({'left':'auto', 'right':'20px'}).attr('rel',(currentBlockMargin+530));
							}
						});
					});
			});
			
			timerId = setTimeout(showNext, options.SliderTime);
			
			$('.slider-item h3').each(function() {
				var randomX = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				var randomY = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				$(this).css({
					'left':randomX,
					'top':randomY
				});
			});
			
			$('.slider-item .slider-text').each(function() {
				var randomX = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				var randomY = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
				$(this).css({
					'left':randomX,
					'top':randomY
				});
			});
			
		};
		
		$('.prev').click(showPrev);
		
		$('.slider-item').hover(
			function() {
			clearTimeout(timerId);
		}, function() {
			timerId = setTimeout(showNext, options.SliderTime);
		});	
		
		
		$('.slider-item h3').each(function() {
			var randomX = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
			var randomY = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
			$(this).css({
				'left':randomX,
				'top':randomY
			});
		});
		
		$('.slider-item .slider-text').each(function() {
			var randomX = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
			var randomY = Math.floor(Math.random() * (500 - (-500) + 1) + -500);
			$(this).css({
				'left':randomX,
				'top':randomY
			});
		});
		
		var dataTime = $('.slaider-activ h3').data('time');
		var dataTime2 = $('.slaider-activ .slider-text').data('time');
		var datatransition = $('.slaider-activ h3').data('transition');
		var datatransition2 = $('.slaider-activ .slider-text').data('transition');
		var leftM = $('.slaider-activ h3').data('x');
		var topM = $('.slaider-activ h3').data('y');
		var leftM2 = $('.slaider-activ .slider-text').data('x');
		var topM2 = $('.slaider-activ .slider-text').data('y');
		$('.slaider-activ h3').animate({
			'opacity': '1',
			'left':''+leftM+'%',
			'top':''+topM+'%'
		}, dataTime, ''+datatransition+'', function(){
				$('.slaider-activ .slider-text').animate({
					'opacity': '1',
					'left': ''+leftM2+'%',
					'top': ''+topM2+'%'
				}, dataTime2, ''+datatransition2+'', function(){
					var currentWinSize = $(window).width();
					var currentBlockMargin = $('.slaider-activ .slider-text').css('left');
					currentBlockMargin = parseInt(currentBlockMargin, 10);
					if ( currentWinSize < currentBlockMargin + 530 ) {
						 $('.slaider-activ .slider-text').css({'left':'auto', 'right':'20px'}).attr('rel',(currentBlockMargin+530));
					}
				});
			});
			
		////////////////////////////
		
	});
	
};