/**
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 */
jQuery.easing['jswing']=jQuery.easing['swing'];jQuery.extend(jQuery.easing,{def:'easeOutQuad',swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d)},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b},easeOutQuad:function(x,t,b,c,d){return-c*(t/=d)*(t-2)+b},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return-c/2*((--t)*(t-2)-1)+b},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b},easeOutQuart:function(x,t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return-c/2*((t-=2)*t*t*t-2)+b},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b},easeInSine:function(x,t,b,c,d){return-c*Math.cos(t/d*(Math.PI/2))+c+b},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b},easeInOutSine:function(x,t,b,c,d){return-c/2*(Math.cos(Math.PI*t/d)-1)+b},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b},easeInOutExpo:function(x,t,b,c,d){if(t==0)return b;if(t==d)return b+c;if((t/=d/2)<1)return c/2*Math.pow(2,10*(t-1))+b;return c/2*(-Math.pow(2,-10*--t)+2)+b},easeInCirc:function(x,t,b,c,d){return-c*(Math.sqrt(1-(t/=d)*t)-1)+b},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1)return-c/2*(Math.sqrt(1-t*t)-1)+b;return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return-(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);if(t<1)return-.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*.5+c+b},easeInBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*(t/=d)*t*((s+1)*t-s)+b},easeOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+b},easeOutBounce:function(x,t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2)return jQuery.easing.easeInBounce(x,t*2,0,c,d)*.5+b;return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*.5+c*.5+b}});



/**
 * @author Khapov Alexey
 */

var Teaser = function(){
	this.init();
};

Teaser.prototype = {
	motion_time: 1000,
	transition: 'easeInOutCubic',

	init: function(){
		this.teaser_measurer = $(document.createElement('div')).attr('id', 'teaser_measurer');
		$('body').append(this.teaser_measurer);
		this.teaser_measurer_width = this.teaser_measurer[0].offsetWidth;

		this.teaser_block = $('#teaser');
		this.teaser_items = this.teaser_block.find('.teaser_item');
		this.prev_teaser = this.teaser_items.filter('.prev');
		this.next_teaser = this.teaser_items.filter('.next');
		this.current_teaser = this.teaser_items.filter('.current');
		this.old_current_teaser = this.current_teaser;
		this.text_links = this.teaser_block.find('.link_text');

		this.new_prev_teaser = null;
		this.new_next_teaser = null;
		this.new_current_teaser = null;

		this.teaser_items_amount = this.teaser_items.size();

		this.animations_amount = 0;
		this.is_animate = false;
		this.flying = false;

		this.change_max_height();

		this.create_fade();

		this.start_animation();

		this.create_text_links();

		this.attach_events();

		var that = this;
		$(window).resize(function(){
			that.change_max_height();
		});
	},

	/**
	 * Добавляем по бокам элементы фэйда
	 */
	create_fade: function(){
		$('<div class="fade left_fade"/><div class="fade right_fade"/>').appendTo(this.teaser_block);
	},

	/**
	 * Возвращает максимальную высоту баннеров, которая будет присвоена родительскому блоку
	 */
	check_max_height: function(){
		var max_height = 0;

		for( var i = 0; i < this.teaser_items_amount; i++ ){
			if( max_height < this.teaser_items[i].offsetHeight ){
				max_height = this.teaser_items[i].offsetHeight;
			}
		}

		return max_height += 20; //Еще 20 пикселей для строки со ссылками
	},

	/**
	 * Создает текстовые переключатели и навешивает на них события
	 */
	create_text_links: function(){
		var
			that = this,
			links_amount = this.text_links.size(),
			links_block = $('<div id="text_links"/>');

		this.links_array = [];

		this.text_link_current;

		for( var i = 0; i < links_amount; i++ ){

			this.links_array[i] = function(x){
				var
					link = that.text_links.eq(x),
					teaser_item = that.teaser_items.eq(x);

				if(teaser_item[0] === that.next_teaser[0]){
					link.addClass('selected');
					that.text_link_current = link;
				}

				link.click(function(){
					var
						me = $(this),
						direction = 1;

					if( teaser_item[0] !== that.current_teaser[0] && !that.is_animate ){
						if( teaser_item[0] === that.prev_teaser[0] ){
							direction = -1;
						}

						that.motion(direction, false, teaser_item);

						that.text_link_current.removeClass('selected');
						that.text_link_current = me.addClass('selected');
					}
				}).appendTo(links_block);

				return link;
			}(i);
		}

		this.teaser_block.append(links_block);
	},

	/**
	 * Устанавливает высоту родительского блока равной максимальной высоте баннеров
	 */
	change_max_height: function(){
		this.teaser_block.css({height: this.check_max_height(), 'visibility': 'visible'});
		$('#content_content').attr('class', '');
	},

	/**
	 * Начальная анимация при загрузке страницы
	 */
	start_animation: function(){
		var content_width = this.teaser_block[0].offsetWidth + 40;

		this.next_teaser.css({marginLeft: -content_width});
		this.current_teaser.css({marginLeft: -content_width});
		this.prev_teaser.css({marginLeft: -content_width});

		this.motion(1, true, false);

		$('body').addClass('');
	},

	attach_events: function(){
		var that = this;
		this.flying = false;

		this.teaser_items.click(function(evt){
			var
				me = $(this),
				direction = 1,
				link_number = me.prevAll().size();

			if( me[0] !== that.current_teaser[0] ){
				that.flying = false;

				me.find('.shadow_c').stop();

				if( me[0] === that.prev_teaser[0] ){
					direction = -1;
				}

				that.motion(direction, false, false);

				that.text_link_current.removeClass('selected');
				that.text_link_current = that.links_array[link_number].addClass('selected');

				evt.stopPropagation();
				return false;
			}
		}).hover(
			function(){
				var
					me = $(this),
					content = me.find('.shadow_c');

				if( me[0] !== that.current_teaser[0] && me[0] !== that.new_current_teaser[0] ){
					that.mouseover_animate( content );
				} else {
					content.stop();
				}
			},
			function(){
				var
					me = $(this),
					content = me.find('.shadow_c');

				if( me[0] !== that.current_teaser[0] && me[0] !== that.new_current_teaser[0] ){
					that.mouseout_animate( content );
				}
			}
		);
	},

	/**
	 * Анимация при mouseover
 	 * @param obj {jQuery} ссылка на контент в блоке с баннером
	 */
	mouseover_animate: function( obj ){
		var that = this;

		obj.animate(
			{top: -20},
			500,
			function(){
				if( !that.flying ){
					that.flying = true;
					that.flying_content(obj);
				}
			}
		);
	},

	/**
	 * Анимация при mouseout
 	 * @param obj {jQuery} ссылка на контент в блоке с баннером
	 */
	mouseout_animate: function( obj ){
		var that = this;

		this.flying = false;
		obj.stop();

		obj.animate(
			{top: 0},
			500,
			function(){
				that.flying = false;
			}
		);
	},

	/**
	 * Анимация контента баннера при ховере
	 * @param obj {jQuery} ссылка на контент в блоке с баннером
	 */
	flying_content: function(obj){
		var that = this;

		if( this.flying ){
			obj.animate(
				{top: -15},
				400,
				'linear',
				function(){
					obj.animate(
						{top: -20},
						400,
						'linear',
						function(){
							that.flying_content(obj);
						}
					);
				}
			);
		} else {
			this.mouseout_animate(obj);
		}
	},

	/**
	 * Движение баннеров
	 * @param direction {int} Направление анимации. 1 - вперед, -1 - назад.
	 * @param is_start {boolean} Если анимация при загрузке страницы - true. Иначе - false.
	 * @param next_current {jQuery} Следующий баннер, который должен стать current.
	 */
	motion: function( direction, is_start, next_current ){
		var
			that = this,
			prev, next,
			motion_time = this.motion_time;

		if( this.animations_amount === 0 && !this.is_animate ){
			this.is_animate = true;
			var prev_animate = false;



			/* Если баннеров больше 3 */
			if( this.teaser_items_amount > 3 ){

				if( direction > 0 ){
					this.preprocess_teaser_next(next_current);

					this.next_teaser.css({marginLeft: this.next_teaser[0].offsetLeft});

					this.next_teaser.animate(
						{marginLeft: this.teaser_block[0].offsetWidth + 40},
						is_start ? motion_time : motion_time * 0.4,
						this.transition,
						function(){
							if( next_current ){
								that.new_current_teaser = next_current;
//							that.next_teaser.removeClass('next');
							} else {
								that.new_current_teaser = that.next_teaser;
							}

							that.new_current_teaser.find('.shadow_c')
//								.css({top: 0});
								.css({top: -20});
							that.new_current_teaser.css({marginLeft: that.next_teaser[0].offsetLeft}).addClass('animated').removeClass('next');

							that.new_current_teaser.animate(
								{marginLeft: that.teaser_block[0].offsetWidth * 0.25 + 10},
								is_start ? motion_time * 0.4 : motion_time * 0.6,
								that.transition,
								function(){
									that.new_current_teaser.removeAttr('style').removeClass('animated').addClass('current');
									that.mouseout_animate( that.new_current_teaser.find('.shadow_c') );
									that.redefine_variables_fwd();
								}
							);
						}
					);


					var motion_time_rest = is_start ? motion_time * 1.6 : motion_time;

					this.prev_teaser.css({marginLeft: this.prev_teaser[0].offsetLeft, 'z-index': 1});

					this.prev_teaser.animate(
						{marginLeft: this.teaser_block[0].offsetWidth * 0.8},
						motion_time_rest,
						this.transition,
						function(){
							that.prev_teaser.removeAttr('style').removeClass('prev').addClass('next').css({'z-index': 200});
							that.redefine_variables_fwd();
						}
					);


					this.current_teaser.css({marginLeft: this.current_teaser[0].offsetLeft, left: 0}).addClass('animated');
					prev = this.get_prev_teaser(next_current);

					this.current_teaser.animate(
						{marginLeft: -this.current_teaser[0].offsetWidth - 40},
						{
							duration: is_start ? motion_time_rest : motion_time_rest * 0.6,
							easing: this.transition,
							complete: function(){
								if( !is_start ){
									that.current_teaser.removeClass('animated current').addClass('prev').css({marginLeft: -that.current_teaser[0].offsetWidth - 20});
									if( next_current ){
										that.new_current_teaser = next_current;
										that.next_teaser.removeClass('next').removeAttr('style');
									} else {
										that.new_current_teaser = that.next_teaser;
									}

									that.current_teaser.animate(
										{marginLeft: 0},
										motion_time_rest * 0.4,
										that.transition,
										function(){
											that.current_teaser.removeAttr('style').css({'z-index': 200});
											that.redefine_variables_fwd();
										}
									);
								} else {
									that.current_teaser.removeAttr('style').removeClass('animated current');
									that.redefine_variables_fwd();
								}
							},
							step: function(){
								if( !is_start && !prev_animate && that.current_teaser[0].offsetLeft <= 10 ){
									prev_animate = true;

									that.prev_teaser.stop().removeAttr('style').removeClass('prev');
									that.new_prev_teaser = that.current_teaser;
									that.redefine_variables_fwd();

									prev.css({marginLeft: that.prev_teaser[0].offsetLeft + that.prev_teaser[0].offsetWidth/2, 'z-index': 1}).addClass('next');

									prev.animate(
										{marginLeft: that.teaser_block[0].offsetWidth * 0.8},
										motion_time_rest,
										'easeOutCubic',
										function(){
											prev_animate = false;
											that.is_animate = false;
											prev.removeAttr('style').removeClass('prev').addClass('next').css({'z-index': 200});
											that.new_next_teaser = prev;
											that.redefine_variables_fwd();
										}
									);
								}
							}
						}
					);


					if( is_start ){
						prev.css({marginLeft: -this.teaser_block[0].offsetWidth - 40, 'z-index': 1}).addClass('prev');

						prev.animate(
							{marginLeft: 0},
							motion_time_rest,
							this.transition,
							function(){
								prev.removeAttr('style').css({'z-index': 200});
								that.redefine_variables_fwd();
								that.is_animate = false;
								that.change_max_height();
							}
						);
					}

				} else {
					var next_animate = false;

					this.preprocess_teaser_prev(next_current);

					this.prev_teaser.css({marginLeft: this.prev_teaser[0].offsetLeft});

					this.prev_teaser.animate(
						{marginLeft: -this.prev_teaser[0].offsetWidth - 0},
						motion_time * 0.4,
						this.transition,
						function(){
							that.prev_teaser
								.removeClass('prev')
								.addClass('animated')
								.css({marginLeft: -that.prev_teaser[0].offsetWidth - 40})
//								.find('.shadow_c').css({top: 0});
								.find('.shadow_c').css({top: -20});

							that.prev_teaser.animate(
								{marginLeft: that.teaser_block[0].offsetWidth * 0.25 + 10},
								motion_time * 0.6,
								that.transition,
								function(){
									that.prev_teaser.removeAttr('style').removeClass('animated').addClass('current');
									that.mouseout_animate( that.prev_teaser.find('.shadow_c') );
									that.redefine_variables_prev();
								}
							);
						}
					);

					this.next_teaser.css({marginLeft: this.next_teaser[0].offsetLeft, 'z-index': 1});

					this.next_teaser.animate(
						{marginLeft: 0},
						motion_time,
						this.transition,
						function(){
							that.next_teaser.removeAttr('style').removeClass('next').addClass('prev').css({'z-index': 200});
							that.redefine_variables_prev();
						}
					);


					this.current_teaser.css({marginLeft: this.current_teaser[0].offsetLeft, left: 0}).addClass('animated');
					next = this.get_next_teaser(next_current);

					this.current_teaser.animate(
						{marginLeft: this.teaser_block[0].offsetWidth + 40},
						{
							duration: motion_time * 0.6,
							easing: 'easeInOutCubic',
							complete: function(){
								that.current_teaser.removeClass('animated current').addClass('next');

								that.current_teaser.animate(
									{marginLeft: that.teaser_block[0].offsetWidth * 0.8},
									motion_time * 0.4,
									that.transition,
									function(){
										that.current_teaser.removeAttr('style').css({'z-index': 200});
										that.new_current_teaser = that.prev_teaser;
										that.next_teaser.removeAttr('style');
										that.redefine_variables_prev();
									}
								);
							},
							step: function(){
								if( !next_animate && that.current_teaser[0].offsetLeft >= that.teaser_block[0].offsetWidth - that.current_teaser[0].offsetWidth ){
									next_animate = true;

									that.next_teaser.stop();

									that.new_next_teaser = that.current_teaser;
									that.redefine_variables_prev();

									next.css({marginLeft: that.next_teaser[0].offsetLeft - that.next_teaser[0].offsetWidth/2, 'z-index': 1}).addClass('prev');
									that.next_teaser.removeAttr('style').removeClass('next');

									next.animate(
										{marginLeft: 0},
										motion_time,
										'easeOutCubic',
										function(){
											next_animate = false;
											that.is_animate = false;
											next.removeAttr('style').removeClass('next').addClass('prev').css({'z-index': 200});
											that.new_prev_teaser = next;
											that.redefine_variables_prev();
										}
									);
								}
							}
						}
					);

				}
			}

			/* Если баннеров меньше 4 */
			else {
				if( direction > 0 ){
					this.preprocess_teaser_next(next_current);

					this.next_teaser.css({marginLeft: this.next_teaser[0].offsetLeft});

					this.next_teaser.animate(
						{marginLeft: this.teaser_block[0].offsetWidth + 40},
						is_start ? motion_time : motion_time * 0.4,
						this.transition,
						function(){
							if( next_current ){
								that.new_current_teaser = next_current;
							} else {
								that.new_current_teaser = that.next_teaser;
							}

							that.new_current_teaser.find('.shadow_c')
//								.css({top: 0});
								.css({top: -20});
							that.new_current_teaser.css({marginLeft: that.next_teaser[0].offsetLeft}).addClass('animated').removeClass('next');

							that.new_current_teaser.animate(
								{marginLeft: that.teaser_block[0].offsetWidth * 0.25 + 10},
								is_start ? motion_time * 0.4 : motion_time * 0.6,
								that.transition,
								function(){
									that.new_current_teaser.removeAttr('style').removeClass('animated').addClass('current');
									that.mouseout_animate( that.new_current_teaser.find('.shadow_c') );
									that.redefine_variables_fwd();
								}
							);
						}
					);


					var motion_time_rest = is_start ? motion_time * 1.6 : motion_time;

					this.prev_teaser.css({marginLeft: this.prev_teaser[0].offsetLeft, 'z-index': 1});

					this.prev_teaser.animate(
						{marginLeft: this.teaser_block[0].offsetWidth * 0.8},
						motion_time_rest,
						this.transition,
						function(){
							that.prev_teaser.removeAttr('style').removeClass('prev').addClass('next').css({'z-index': 200});
							that.redefine_variables_fwd();
						}
					);


					this.current_teaser.css({marginLeft: this.current_teaser[0].offsetLeft, left: 0}).removeClass('current');
					if( !is_start ){
						this.current_teaser.addClass('animated');
					}
					prev = this.get_prev_teaser(next_current);


					this.current_teaser.animate(
						{marginLeft: is_start ? 0 : -this.current_teaser[0].offsetWidth - 40},
						{
							duration: is_start ? motion_time_rest : motion_time_rest * 0.6,
							easing: this.transition,
							complete: function(){
								if( !is_start ){
									that.current_teaser.removeClass('animated current').addClass('prev').css({marginLeft: -that.current_teaser[0].offsetWidth - 20});
									if( next_current ){
										that.new_current_teaser = next_current;
										that.next_teaser.removeClass('next').removeAttr('style');
									} else {
										that.new_current_teaser = that.next_teaser;
									}

									that.current_teaser.animate(
										{marginLeft: 0},
										motion_time_rest * 0.4,
										that.transition,
										function(){
											that.current_teaser.removeAttr('style').css({'z-index': 200});
											that.redefine_variables_fwd();
										}
									);
								} else {
									that.current_teaser.removeAttr('style').removeClass('animated current');
									that.redefine_variables_fwd();
								}
								that.is_animate = false;
							}
						}
					);


					if( is_start ){
						prev.css({marginLeft: -this.teaser_block[0].offsetWidth - 40, 'z-index': 1}).addClass('prev');

						prev.animate(
							{marginLeft: 0},
							motion_time_rest,
							this.transition,
							function(){
								prev.removeAttr('style').css({'z-index': 200});
								that.is_animate = false;
								that.change_max_height();
							}
						);
					}
				} else {
					var next_animate = false;

					this.preprocess_teaser_prev(next_current);

					this.prev_teaser.css({marginLeft: this.prev_teaser[0].offsetLeft});

					this.prev_teaser.animate(
						{marginLeft: -this.prev_teaser[0].offsetWidth - 0},
						motion_time * 0.4,
						this.transition,
						function(){
							that.prev_teaser
								.removeClass('prev')
								.addClass('animated')
								.css({marginLeft: -that.prev_teaser[0].offsetWidth - 40})
//								.find('.shadow_c').css({top: 0});
								.find('.shadow_c').css({top: -20});

							that.prev_teaser.animate(
								{marginLeft: that.teaser_block[0].offsetWidth * 0.25 + 10},
								motion_time * 0.6,
								that.transition,
								function(){
									that.prev_teaser.removeAttr('style').removeClass('animated').addClass('current');
									that.mouseout_animate( that.prev_teaser.find('.shadow_c') );
									that.redefine_variables_prev();
								}
							);
						}
					);

					this.next_teaser.css({marginLeft: this.next_teaser[0].offsetLeft, 'z-index': 1});

					this.next_teaser.animate(
						{marginLeft: 0},
						motion_time,
						this.transition,
						function(){
							that.next_teaser.removeAttr('style').removeClass('next').addClass('prev').css({'z-index': 200});
							that.redefine_variables_prev();
						}
					);


					this.current_teaser.css({marginLeft: this.current_teaser[0].offsetLeft, left: 0}).addClass('animated').removeClass('current');
					next = this.get_next_teaser(next_current);

					this.current_teaser.animate(
						{marginLeft: this.teaser_block[0].offsetWidth + 40},
						{
							duration: motion_time * 0.6,
							easing: 'easeInOutCubic',
							complete: function(){
								that.current_teaser.removeClass('animated current').addClass('next');

								that.current_teaser.animate(
									{marginLeft: that.teaser_block[0].offsetWidth * 0.8},
									motion_time * 0.4,
									that.transition,
									function(){
										that.current_teaser.removeAttr('style').css({'z-index': 200});
										that.new_current_teaser = that.prev_teaser;
										that.next_teaser.removeAttr('style');
										that.redefine_variables_prev();
									}
								);
								that.is_animate = false;
							}
						}
					);

				}
			}

		}
	},

	/**
	 * Присваивает временным переменным значения, которые будут использованы для последующей анимации
	 */
	preprocess_teaser_next: function(next_current){
		this.old_current_teaser = this.current_teaser;
		this.new_current_teaser = this.next_teaser;
		this.new_next_teaser = this.prev_teaser;
		this.new_prev_teaser = this.get_prev_teaser(next_current);
	},

	/**
	 * Переопределяет переменные для движения вперед
	 */
	redefine_variables_fwd: function(){
		this.animations_amount++;

		if(
			(this.animations_amount === 4 && this.teaser_items_amount > 3) ||
			(this.animations_amount === 3 && this.teaser_items_amount <= 3)
		){
			this.current_teaser = this.new_current_teaser;
			this.next_teaser = this.new_next_teaser;
			this.prev_teaser = this.new_prev_teaser;

			this.animations_amount = 0;
		}
	},

	/**
	 * Присваивает временным переменным значения, которые будут использованы для последующей анимации
	 */
	preprocess_teaser_prev: function(next_current){
		this.old_current_teaser = this.current_teaser;
		this.new_current_teaser = this.prev_teaser;
		this.new_prev_teaser = this.next_teaser;
		this.new_next_teaser = this.get_next_teaser(next_current);
	},

	/**
	 * Переопределяет переменные для движения назад
	 */
	redefine_variables_prev: function(){
		this.animations_amount++;

		if(
			(this.animations_amount === 4 && this.teaser_items_amount > 3) ||
			(this.animations_amount === 3 && this.teaser_items_amount <= 3)
		){
			this.current_teaser = this.new_current_teaser;
			this.prev_teaser = this.new_prev_teaser;
			this.next_teaser = this.new_next_teaser;

			this.animations_amount = 0;
		}
	},

	/**
	 * Возвращает новый элемент при движении вперед
	 */
	get_prev_teaser: function(next_current){
		var prev = this.new_current_teaser.prev().size() > 0 ? this.new_current_teaser.prev() : this.teaser_items.eq(this.teaser_items_amount - 1);

		if( this.teaser_items_amount > 3 ){
			if( next_current ){
				while(
//				    prev[0] === this.new_next_teaser[0] ||
					prev[0] === this.new_current_teaser[0] ||
					prev[0] === this.old_current_teaser[0] ||
					prev[0] === next_current[0]
				){
					prev = prev.prev().size() > 0 ? prev.prev() : this.teaser_items.eq(this.teaser_items_amount - 1);
				}
			} else {
				while(
					prev[0] === this.new_next_teaser[0] ||
					prev[0] === this.new_current_teaser[0] ||
					prev[0] === this.old_current_teaser[0] ||
					prev[0] === next_current[0]
				){
					prev = prev.prev().size() > 0 ? prev.prev() : this.teaser_items.eq(this.teaser_items_amount - 1);
				}
			}
		}

		return prev;
	},

	/**
	 * Возвращает новый элемент при движении назад
	 */
	get_next_teaser: function(next_current){
		var next = this.new_current_teaser.next('.teaser_item').size() > 0 ? this.new_current_teaser.next('.teaser_item') : this.teaser_items.eq(0);

		if( this.teaser_items_amount > 3 ){
			while(
				next[0] === this.old_current_teaser[0] ||
				next[0] === this.new_current_teaser[0] ||
				next[0] === this.new_prev_teaser[0] ||
				next[0] === next_current[0]
			){
				next = next.next('.teaser_item').size() > 0 ? next.next('.teaser_item') : this.teaser_items.eq(0);
			}
		}

		return next;
	}
};


$(function(){
	new Teaser();

	/*var form_container = $('#teaser .form');
	form_container.find('select[name=product]').change(function(){
		if($(this).val() == 'deposit'){
			form_container.addClass('deposit');
		} else {
			form_container.removeClass('deposit');
		}
	});*/
});
