var slide_speed = 0;
var slide_aim = 0;
var slide_change = 10;
var slide_pos = 0;
var slide_length = 0; 
var container_length = 0;

function moveslides() {
	
	//var my_advance = parseInt($(".slides").css("margin-left").replace(/px/,""));
	//$("#status").html(slide_speed);
	if (slide_aim != slide_speed) {
		slide_speed = slide_speed + (slide_aim-slide_speed)/slide_change;
	}
	
	if (slide_speed != 0) {
		slide_pos = slide_pos+slide_speed;
		if (slide_pos <= -(slide_length)) {
			slide_pos=slide_pos+slide_length;
		}
		if (slide_pos > (container_length)) {
			slide_pos=slide_pos-slide_length;
		}
		$(".slides").css("margin-left",(slide_pos+slide_speed)+"px");
	}
	window.setTimeout('moveslides()', 25);
}


$(function() {
	$("#container").prepend("<div id=\"status\"> </div>");

	
	if ($("#inhalt .slides").length) {
		$("#inhalt .slides").before("<div class=\"vorhang\"> </div>");
		
		$.preload("#inhalt .slides img", {onFinish:function(){
				var my_first_pos = $("#inhalt .slides").children("img:first").position(); 
				var my_last_pos = $("#inhalt .slides").children("img:last").position();
				slide_length = my_last_pos.left - my_first_pos.left +  $("#inhalt .slides").children("img:last").width();
				my_container_length = $("#inhalt").width();
				
				$("#inhalt .slides").append($("#inhalt .slides").html());
				/*$("#inhalt .slides img").bind("mouseenter",function(){
					$(this).animate({opacity: 0.4},120);
				});
			    $("#inhalt .slides img").bind("mouseleave",function(){
					$(this).stop().animate({opacity: 1},75);
				});*/
				slide_aim = -2;
				slide_speed = -2;
				window.setTimeout('moveslides()', 25);
				$("#inhalt .vorhang").fadeOut();
			}
		});
	

		
		$("#inhalt .slides").bind("mouseover", function(){
			var last_state="";
			var my_state="stop";
			
			var my_container_length = $(this).parents("#inhalt").width();
			var my_offset = $(this).parents("#inhalt").offset();
		
			$(this).bind("mousemove", function(my_pos){
				if (my_pos.pageX < (my_offset.left+250)) {my_state = "scroll_left";} 
				else if (my_pos.pageX > (my_offset.left+my_container_length-250)) {my_state = "scroll_right";}
				else {my_state="stop";}
		
				if (my_state != last_state) {
					last_state = my_state;
					/*var my_first_pos = $(this).children("img:first").position(); 
					var my_last_pos = $(this).children("img:last").position();
					var my_length = my_last_pos.left - my_first_pos.left +  $(this).children("img:last").width();
					
					var my_advance = parseInt($(this).css("margin-left").replace(/px/,""));
					var my_ratio = my_advance/(my_length-my_container_length)*(-1);*/
			
					if (my_state == "scroll_left"){	
						//$(this).stop().animate({marginLeft:0},3000*(my_ratio),"linear");
						slide_aim = 7;
					}
					if (my_state == "scroll_right"){
						//$(this).stop().animate({marginLeft:-(my_length-my_container_length)},3000*(1-my_ratio),"linear");
						slide_aim = -7;
					}
					if (my_state == "stop"){
						//$(this).stop();
						slide_aim = 0;
					}
			}
			});
		});
		
		$("#inhalt .slides").bind("mouseleave", function(){
				if (slide_speed<-0.25) {slide_aim = -2;} 
				else if (slide_speed>0.25) {slide_aim = 2;}
				//window.setTimeout("slide_speed=-4", 2000); /*funktioniert noch unzuverlässig*/
		});
	
	}
	
});
