/*
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:			270,
			height:			180,
			speed:			1000,
			changeTimeout:	0,
			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 == 3 || 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 || now == 2)
					{
						temp = arLayers.pop();
						arLayers.unshift(temp);
					}
					else	 if(now == 4 || now == 5 || now == 6)
					{
						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);

						temp = arLayers.pop();
						arLayers.unshift(temp);
												
						animateLayers();
				
					}
					if(now == 1)
					{
						temp = arLayers.pop();
						arLayers.unshift(temp);
						
						temp = arLayers.pop();
						arLayers.unshift(temp);
					
						
					}
					else if(now == 2)
					{
						temp = arLayers.pop();
						arLayers.unshift(temp);

					}
					
					else if(now == 4)
					{
						temp = arLayers.shift();
						arLayers.push(temp);
					}
					else if(now == 5)
					{
						temp = arLayers.shift();
						arLayers.push(temp);

						temp = arLayers.shift();
						arLayers.push(temp);
						
					}

					else if(now == 6)
					{
						temp = arLayers.shift();
						arLayers.push(temp);

						temp = arLayers.shift();
						arLayers.push(temp);
						
						temp = arLayers.shift();
						arLayers.push(temp);
						
						animateLayers();
					}


					if( now == 1 || now == 2 ||  now == 4 || now == 5)
						animateLayers();
				}
			}

			return false;
		});
		
		animateLayers();
		
		
		len = arLayers.length;
		rad = Math.floor(len/2);
		
		item3 = arLayers[rad];

		while(item3.attr("id") != settings.cid)
		{
			temp = arLayers.pop();		
			arLayers.unshift(temp);

			item3 = arLayers[rad];
		}
		

	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;
			
			
			
			//alert(obj.width());
			// Нормальный тип анимации
			if( settings.type == 'normal' )
			for ( var index in arLayers )
			{
				var obw = obj.width();
				//if (obw == 0) obw = 960;
				
				var k = 0.5;
				var width = settings.width*k;
				var height = settings.height*k;
				var left = obw*0.5 - width*0.5;
				var marginBottom = 0;
				var zIndex = 0;
				
				if ( index == 0 )
				{
					k = 0.5;
					width = settings.width*k;
					height = settings.height*k;
					marginBottom = 15;
					left = obw*0.12 - width/2;
					zIndex = 5;
				}
				else if ( index == 1 )
				{
					k = 0.7;
					width = settings.width*k;
					height = settings.height*k;
					left = obw*0.24-width/2;
					marginBottom = 10;
					zIndex = 7;
				}
				else if( index == 2 )
				{
					k = 0.9;
					width = settings.width*k;
					height = settings.height*k;
					left = obw*0.36 - width/2;
					marginBottom = 5;
					zIndex = 10;
				}
				else if ( index == 3 )
				{
					k = 1;
					width = settings.width*k;
					height = settings.height*k;
					left = obw*0.5 - width/2;
					marginBottom = 0;
					zIndex = 13;
				}
				else if ( index == 4 )
				{
					k = 0.9;
					width = settings.width*k;
					height = settings.height*k;
					marginBottom = 5;
					left = obw * 0.64 - width/2;
					zIndex = 10;
				}

				else if ( index == 5 )
				{
					k = 0.7;
					width = settings.width*k;
					height = settings.height*k;
					marginBottom = 10;
					left = obw*0.76 - width/2;
					zIndex = 7;
				}

				else if ( index == 6 )
				{
					k = 0.5;
					width = settings.width*k;
					height = settings.height*k;
					marginBottom = 15;
					left = obw*0.88 - 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": (1*fontSize)+'px'})
						.animate({"padding-top1": (0*2)+"px", "padding-bottom1": (0*2)+"px", "width": (width)+'px', "z-index": zIndex+1}, settings.speed, settings.animationType, function(){
							if(!go2)
							{
								arLayers[0].find('span').css({"fontSize": (1*fontSize)+'px'})
								arLayers[1].find('span').css({"fontSize": (1*fontSize)+'px'})
								arLayers[2].find('span').css({"fontSize": (1*fontSize)+'px'})
								arLayers[3].find('span').css({"fontSize": (1*fontSize)+'px'})//, 100, settings.animationType)
								arLayers[4].find('span').css({"fontSize": (1*fontSize)+'px'})
								arLayers[5].find('span').css({"fontSize": (1*fontSize)+'px'})
								arLayers[6].find('span').css({"fontSize": (1*fontSize)+'px'})
							}
						})
						
						
					}
					else
						sobj.animate({"fontSize": (k*fontSize)+'px',"padding-top": (k*2)+"px", "padding-bottom": (k*2)+"px", "width": (width)+'px', "z-index": zIndex+1}, settings.speed, settings.animationType)
					
				})
				
			}

		}
		
		function stopAnimation()
		{
			$('li', obj).stop().find('img, span').stop()
		}
	}
	
	/*
	$(function(){
		$('#change_banners').click(function(){
			aobj = $(this)
			aobj.addClass('load');
			$.get('/bitrix/components/individ/advertising.banners/ajax.php', function(data){
				$('#carousel').html(data).find('span').css('opacity', '0.85');
				$('#carousel').carousel({type:'normal',binds:'click'});
				aobj.removeClass('load');
			})
		})
	})
	*/
	
})(jQuery);


