$(document).ready(
	function()
	{	
		var animation_duration = 1000;
		var animation_pause = 7000;
		
		$('.we-teaserbox').each(
			function()
			{
				if(!$(this).parents('body').hasClass('we-welcome'))
				{
					var teaserbox = $(this);
					
					if(teaserbox.find('li').length > 1)
					{
						var ul_width = 0;
						var box_counter = 0;
						
						teaserbox.find('> ul:first-child').addClass('we-teaserbox-ul');
						
						teaserbox.find('.we-teaserbox-ul').append('<li class="we-teaserbox-duplicated-li">' + teaserbox.find('li:first-child').html() + '</li>');
						teaserbox.find('.we-teaserbox-duplicated-li').css({backgroundImage: teaserbox.find('li:first-child').css('backgroundImage')});
						teaserbox.find('.we-teaserbox-ul li').each(function(){	ul_width += $(this).width();	});
						teaserbox.find('.we-teaserbox-ul').width(ul_width);
						teaserbox.find('.we-teaserbox-ul').css({marginLeft: 0});
						teaserbox.find('.we-teaserbox-ul li:first-child').addClass('active');
						
						setTimeout(function(){	teaserboxMove(teaserbox);	}, animation_pause);
						
						if (teaserbox.find('div.we-teaserbox-overlay').length == 0) {
              teaserbox.append('<div class="we-teaserbox-overlay"><ul></ul></div>');
              teaserbox.find('.we-teaserbox-ul li').each(function(){	if(!$(this).hasClass('we-teaserbox-duplicated-li')){	teaserbox.find('.we-teaserbox-overlay ul').append('<li>' + box_counter + '</li>'); box_counter++; }	});
						}
						
						teaserbox.find('.we-teaserbox-overlay li:first').addClass('active');
						teaserbox.find('.we-teaserbox-overlay li').click(teaserboxClick);
						teaserbox.find('.we-teaserbox-overlay').width(teaserbox.width() - 20);
						//teaserbox
					}
				}			
			}
		);
		
		function teaserboxMove(teaserbox)
		{
			if(!teaserbox.find('.we-teaserbox-ul').hasClass('we-stop-animation'))
			{
				var li_active = teaserbox.find('.we-teaserbox-ul li.active');
				
				if(!li_active.next().hasClass('we-teaserbox-duplicated-li'))
				{				
					var li_width = li_active.width();
					var ul_marginleft = Number(teaserbox.find('.we-teaserbox-ul').css('marginLeft').replace('px', ''));
					
					var button_active = teaserbox.find('.we-teaserbox-overlay li.active');
							
					button_active.removeClass('active');
					button_active.next().addClass('active');
									
					teaserbox.find('.we-teaserbox-ul').animate({marginLeft: ul_marginleft + (-1 * li_width)}, animation_duration, function()
						{
							li_active.removeClass('active');
							li_active.next().addClass('active');
													
							setTimeout(function(){	teaserboxMove(teaserbox);	}, animation_pause);
						}
					);
				}
				
				else
				{				
					var li_width = li_active.width();
					var ul_marginleft = Number(teaserbox.find('.we-teaserbox-ul').css('marginLeft').replace('px', ''));
					
					var button_active = teaserbox.find('.we-teaserbox-overlay li.active');
							
					button_active.removeClass('active');
					button_active.parent().find('li:first-child').addClass('active');
					
					li_active.removeClass('active');
					li_active.next().addClass('active');
					
					teaserbox.find('.we-teaserbox-ul').animate({marginLeft: ul_marginleft + (-1 * li_width)}, animation_duration, function()
						{
							$('.we-teaserbox ul').css({marginLeft: 0});
							
							li_active.removeClass('active');
							teaserbox.find('.we-teaserbox-ul li:first-child').addClass('active');
							
							
							
							setTimeout(function(){	teaserboxMove(teaserbox);	}, animation_pause);
						}
					);
				}
			}			
		}
		
		function teaserboxClick(event)
		{
      var target = $(event.target);
      if (target.parents('li').length>0) target = target.parent();
			var item_index = target.index();
			target.parent().find('.active').removeClass('active');
			target.addClass('active');
			target.parents('.we-teaserbox').find('.we-teaserbox-ul').addClass('we-stop-animation');
			target.parents('.we-teaserbox').find('.we-teaserbox-ul').stop().animate({marginLeft: -1 * 990 * item_index}, animation_duration);
		}
		
		adjustTeaserboxHeight = function(){
			var h = $(window).height();
			$('.we-teaserbox').removeClass('we-teaserbox-slim');
			//console.log(h);
			if (h<768) {
				var target_h = h-400;
				
				if (target_h<200) {
					$('.we-teaserbox').addClass('we-teaserbox-slim');
					target_h = 200;
					//$('.we-teaserbox p > span > a').unwrap();
					//$('.we-teaserbox p > a').unwrap();
				} 
					
				if (target_h<200) {
					target_h = 200;
				} else if (target_h>=300) {
					target_h = 300;
				}
				
				$('.we-text-page .we-teaserbox').height(target_h);
				$('.we-text-page .we-teaserbox-ul li').height(target_h);
			} else {
				$('.we-text-page .we-teaserbox').height(300);
				$('.we-text-page .we-teaserbox-ul li').height(300);
			}

		}
		
		if ($('.we-text-page .we-teaserbox-scaleable').length) {
      $(window).resize(function() {
  			adjustTeaserboxHeight();
  		});
	
      adjustTeaserboxHeight();
		}	
		
		
	}
);
