$(document).ready(function(){
	var start;
	var item;
	var dif;
	var distance;
	
	$('.image_teaser_img:first').fadeIn(0);
	$('.image_teaser_navi a:first').addClass('act');
	
	$('.image_teaser_navi a:not(.act)').live('click', function(){
		
		item = $(this).attr('rel').substr($(this).attr('rel').length-1,  $(this).attr('rel').length);
		start = $('.act').attr('rel').substr($(this).attr('rel').length-1,  $(this).attr('rel').length);
		dif = item-start;
		
		distance = teaser_width * dif;
		
		$('.act').removeClass('act');
		$(this).addClass('act');
		
		$('.teaser_wrapper').animate({
				'margin-left': '-='+distance+'px'
			}, 500, function() {
				
			});
	});
	
	
	// Die Variablen teaser_width und wrapper_width stellt class.tx_csysswitchteaser_pi1.php
	var wrapper_width_new = wrapper_width;
	var count = $('.teaser_wrapper').children().length;
	var teaser = $('.teaser_wrapper').html();
	
	/* NAVIGATION */
	// N?chster Teaser
	$('#arrow_right').live("click", function() {
		$(this).attr("id", "arrow_right2");
		if ($('.teaser_wrapper').css("marginLeft") == (teaser_width-wrapper_width)+"px") {		
			wrapper_width_new = parseInt(wrapper_width_new) + parseInt(wrapper_width);
			$('.teaser_wrapper').css("width", wrapper_width_new);
			$('.teaser_wrapper').append(teaser);
			// Dot auf Start
			$('.act').removeClass('act');
			$('.image_teaser_navi a:first').addClass('act');
			
			$('.teaser_wrapper').animate({
				'margin-left': '-=910px'
			}, 500, function() {
				$('.teaser_wrapper div:lt('+count+')').remove();
				$('.teaser_wrapper').css("margin-left", "0px"); 
				$('.teaser_wrapper').css("width", wrapper_width);
				$("#arrow_right2").attr("id", "arrow_right");
			});
		}
		else {
			$('.teaser_wrapper').animate({
				'margin-left': '-=910px'
			}, 500, function() {
				$("#arrow_right2").attr("id", "arrow_right");
			});
			$('.act').removeClass('act').next().addClass('act');
			
		}
	});
	
	// Vorheriger Teaser
	$('#arrow_left').live("click", function() {
		$(this).attr("id", "arrow_left2");
		if ($('.teaser_wrapper').css("marginLeft") == "0px") {	
			$('.teaser_wrapper').css("margin-left", "-"+wrapper_width+"px"); 
			// Dot auf Ende
			$('.act').removeClass('act');
			$('.image_teaser_navi a:last').addClass('act');
			
			wrapper_width_new = parseInt(wrapper_width_new) + parseInt(wrapper_width);
			$('.teaser_wrapper').css("width", wrapper_width_new);
			$('.teaser_wrapper').prepend(teaser);
			
			$('.teaser_wrapper').animate({
				'margin-left': '+=910px'
			}, 500, function() {
				$('.teaser_wrapper div:gt('+(count-1)+')').remove();
				$('.teaser_wrapper').css("width", wrapper_width);
				$("#arrow_left2").attr("id", "arrow_left");
			});
		}
		else {
			$('.teaser_wrapper').animate({
				'margin-left': '+=910px'
			}, 500, function() {
				$("#arrow_left2").attr("id", "arrow_left");
			});
			$('.act').removeClass('act').prev().addClass('act');
		}
	});
});
