/*
jQuery Plugin Carousel
v.0.2

*/

(function($) {

	$.fn.carousel = function(settings) {
		// Settings to configure the jQuery lightBox plugin how you like
		var settings = jQuery.extend({
			width:			500,
			height:			300,
			speed:			1000,
			changeTimeout:	7000,
			binds:			'mouseover',
			type:			'normal',
			animationType:  'swing'
		},settings);
		
		var obj = this;
		var arLayers = Array();
		
		var fontSize = parseFloat(obj.find('span').css('font-size'));

		
		$('li', obj).each(function(){
			arLayers.push($(this));
		}).bind(settings.binds,function(){
			var now = $(this).attr('now');
			if(settings.binds == 'click' && (settings.type == 'normal' && now == 2 || settings.type == 'leftright' && now == 4) )
				return true;
			clearTimeout(TM);
			TM=setTimeout(changeLayers, settings.changeTimeout);
			
			if( settings.type == 'normal' )
			{
				if( settings.binds == 'mouseover' )
				{
					if(now==0 || now == 1)
					{
						temp = arLayers.pop();
						arLayers.unshift(temp);
					}
					else if(now == 3 || now == 4)
					{
						temp = arLayers.shift();
						arLayers.push(temp);
					}
					animateLayers();
				}
				else if( settings.binds == 'click' )
				{
					if(now==0)
					{
						temp = arLayers.pop();
						arLayers.unshift(temp);

						temp = arLayers.pop();
						arLayers.unshift(temp);
						animateLayers();
					}
					if(now == 1)
					{
						temp = arLayers.pop();
						arLayers.unshift(temp);
					}
					else if(now == 3)
					{
						temp = arLayers.shift();
						arLayers.push(temp);
					}
					else if(now == 4)
					{
						temp = arLayers.shift();
						arLayers.push(temp);

						temp = arLayers.shift();
						arLayers.push(temp);
						animateLayers();
					}
					if( now == 1 || now == 3)
						animateLayers();
				}
			}
			else if( settings.type == 'leftright' || settings.type == 'leftright2' )
			{
				for(i=now;i<4;i++)
				{
					temp = arLayers.pop();
					arLayers.unshift(temp);
					if( settings.type=='leftright' )
						animateLayers();
				}
				if( settings.type=='leftright2' )
					animateLayers();
			}
			return false;
		});
		
		animateLayers();
		obj.find('ul').show();
		
		$(window).resize(function(){stopAnimation();animateLayers();});
		
		var changeLayers=function()
		{
			
			temp = arLayers.pop();
			arLayers.unshift(temp);
			temp = arLayers.pop();
			arLayers.unshift(temp);
			temp = arLayers.pop();
			arLayers.unshift(temp);
			animateLayers();
			clearTimeout(TM);
			TM=setTimeout(changeLayers, settings.changeTimeout);
		}
		
		var TM=setTimeout(changeLayers, settings.changeTimeout);
		
		// Анимация слоев
		function animateLayers(go2){
			if(go2 != true) go2 = false;
			else first = true;
			
			// Нормальный тип анимации
			if( settings.type == 'normal' )
			for ( var index in arLayers )
			{
				var k = 0.5;
				var width = settings.width*k;
				var height = settings.height*k;
				var left = obj.width()*0.5 - width*0.5;
				var marginBottom = 12;
				var zIndex = 0;
				
				if ( index == 0 )
				{
					left = obj.width()*0.15 - width/2;
					zIndex = 5;
				}
				else if ( index == 1 )
				{
					k = 0.7
					width = settings.width*k;
					height = settings.height*k;
					left = obj.width()*0.27-width/2;
					marginBottom = 5;
					zIndex = 7;
				}
				else if( index == 2 )
				{
					k = 1;
					width = settings.width;
					height = settings.height;
					left = obj.width()*0.5 - settings.width*0.5;
					marginBottom = 0;
					zIndex = 10;
				}
				else if ( index == 3 )
				{
					k = 0.7;
					width = settings.width*k;
					height = settings.height*k;
					left = obj.width()*0.5 + obj.width()*0.23 - width/2;
					marginBottom = 5;
					zIndex = 7;
				}
				else if ( index == 4 )
				{
					left = obj.width()*0.85 - width/2;
					zIndex = 5;
				}

				arLayers[index]
					.attr('now', index)
					.css({"z-index": zIndex})
					.animate({"left": left+'px', "height": height+'px', "marginBottom": marginBottom+'px'}, settings.speed, settings.animationType, function(){
						if(go2 && first)
						{
							first = false;
							animateLayers(false);
						}
						if(!go2)
						{
							$('li', obj).removeClass('green-arrow');
							$('li[now=2]', obj).addClass('green-arrow');
						}
					})
				.find('.bg-img')
					.animate({"width": width+'px', "height": height+'px'}, settings.speed, settings.animationType)
				.parent().find('span').each(function(){
					var sobj = $(this);
					if ($.browser.msie || 1)
					{
						sobj
						.css({"fontSize": (0.6*fontSize)+'px'})
						.animate({"padding": (k*14)+"px", "width": (width-k*27)+'px', "z-index": zIndex+1}, settings.speed, settings.animationType, function(){
							if(!go2)
							{
								arLayers[0].find('span').css({"fontSize": (.5*fontSize)+'px'})
								arLayers[1].find('span').css({"fontSize": (.7*fontSize)+'px'})
								arLayers[2].find('span').css({"fontSize": (1*fontSize)+'px'})//, 100, settings.animationType)
								arLayers[3].find('span').css({"fontSize": (.7*fontSize)+'px'})
								arLayers[4].find('span').css({"fontSize": (.5*fontSize)+'px'})
							}
						})
					}
					else
						sobj.animate({"fontSize": (k*fontSize)+'px',"padding": (k*14)+"px", "width": (width-k*27)+'px', "z-index": zIndex+1}, settings.speed, settings.animationType)
				})
			}

			// Слегка видоизменненный тип анимации
			else if( settings.type == 'leftright' || settings.type == 'leftright2' )
			for ( var index in arLayers )
			{
				var k = 0.6;
				var width = settings.width*k;
				var height = settings.height*k;
				var left = obj.width()*0.15 - width*0.5;
				var marginBottom = 15;
				var zIndex = 0;
				
				if ( index == 0 )
				{
					left = obj.width()*0.17 - width*0.5;
					zIndex = 5;
				}
				else if ( index == 1 )
				{
					left = obj.width()*0.27-width/2;
					marginBottom = 45;
					zIndex = 7;
				}
				else if( index == 2 )
				{
					left = obj.width()*0.37 - width*0.5;
					marginBottom = 75;
					zIndex = 10;
				}
				else if ( index == 3 )
				{
					left = obj.width()*0.47 - width/2;
					marginBottom = 115;
					zIndex = 15;
				}
				else if ( index == 4 )
				{
					k = 1;
					width = settings.width*k;
					height = settings.height*k;
					left = obj.width()*0.72 - width*0.5;
					marginBottom = 0;
					zIndex = 20;
				}

				arLayers[index]
					.attr('now', index)
					.css({"z-index": zIndex})
					.animate({"left": left+'px', "height": height+'px', "marginBottom": marginBottom+'px'}, settings.speed, settings.animationType, function(){
						if(go2 && first)
						{
							first = false;
							animateLayers(false);
						}
					})
					.find('.bg-img')
						.animate({"width": width+'px', "height": height+'px'}, settings.speed, settings.animationType)
					.parent().find('span').each(function(){
						var sobj = $(this);
						if (/MSIE (5\.5|6|7).+Win/.test(navigator.userAgent))
						{
							sobj
							.css({"fontSize": (0.6*fontSize)+'px'})
							.animate({"padding": (k*14)+"px", "width": (width-k*27)+'px', "z-index": zIndex+1}, settings.speed, settings.animationType, function(){
								arLayers[0].find('span').css({"fontSize": (.6*fontSize)+'px'})
								arLayers[1].find('span').css({"fontSize": (.6*fontSize)+'px'})
								arLayers[2].find('span').css({"fontSize": (.6*fontSize)+'px'})
								arLayers[3].find('span').css({"fontSize": (.6*fontSize)+'px'})
								arLayers[4].find('span').animate({"fontSize": fontSize+'px'})
							})
						}
						else
							sobj.animate({"fontSize": (k*fontSize)+'px',"padding": (k*14)+"px", "width": (width-k*27)+'px', "z-index": zIndex+1}, settings.speed, settings.animationType)
					})
			}
		}
		
		function stopAnimation()
		{
			$('li', obj).stop().find('img, span').stop()
		}
	}
	
	
})(jQuery);
