/*
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:			378,
			height:			266,
			speed:			1000,
			changeTimeout:	6000,
			binds:			'click',
			type:			'normal',
			animationType:  'swing',
			stopped:        false
		},settings);
		if (this.length==0)
            return false
		var obj = this;
		var arLayers = Array();
		

		
		$('li', obj).each(function(){
			arLayers.push($(this));
		}).bind(settings.binds,function(){
			var now = $(this).attr('now');
			if(now == 2)
				return true;
			clearTimeout(TM);
			TM=setTimeout(changeLayers, settings.changeTimeout);
			
			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();
			return false;
		});
		
		for ( var index in arLayers )
        {
            arLayers[index].hover(
              function () {
                settings.stopped=true;
              }, 
              function () {
                settings.stopped=false;
              }
            );
        }
		
		animateLayers();
		obj.find('ul').show();
		
		$(window).resize(function(){stopAnimation();animateLayers();});
		
		var changeLayers=function()
		{
			if(!settings.stopped){
				len=arLayers.length;
				temp = arLayers.shift();
				arLayers[len-1]=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 )
			{
/*                if (index==0)
                    console.clear();
                console.log(index+' '+arLayers[index][0].style['border']);
*/
				var k = 0.72;
				var width = settings.width*k;
				var height = settings.height*k;
				var left = obj.width()*0.5 - width*0.5;
				var marginBottom = 25;
				var zIndex = 0;
                var zIndex = 0;
				if ( index == 0 )
				{
					left = obj.width()*0.23 - width/2;
					zIndex = 5;
				}
				else if ( index == 1 )
				{
					k = 0.9
					width = settings.width*k;
					height = settings.height*k;
					left = obj.width()*0.36-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.9;
					width = settings.width*k;
					height = settings.height*k;
					left = obj.width()*0.5 + obj.width()*0.16 - width/2;
					marginBottom = 5;
					zIndex = 7;
				}
				else if ( index == 4 )
				{
					left = obj.width()*0.79 - 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);
                    
                arLayers[index].find('object').attr({
                    "width": width,
                    "height": height
                }).find('embed').attr({
                    "width": width,
                    "height": height
                })
			}
		}
		
		function stopAnimation()
		{
			$('li', obj).stop().find('img, span').stop()
		}
	}
})(jQuery);
