// --- Logos animation - displays selected images in loop --- //
// --- Works only with the images having the same width --- //
// --- Copyright by Przemyslaw Jaworski --- //

var logos_direction = "-"; // defaul rolling direction of logotyps
var logos_step = 116;      // it's recommend to set logos_step as a distance beetwen beginning position of a first and a second image; says how many px has the animation pass through in logos_speed time;
var logos_speed = 3400;    // in how many ms has animation pass through logos_step distance
var logotypy_width;
var counter;

$(document).ready(
function() {
	animacjaLogotypy();	
})

function animacjaLogotypy() {

	// --- Show and hide the animation's menu buttons --- //	
	$(".fast_right, .fast_left, .slow_left, .slow_right").css({opacity:"0"});	
	$(".fast_right, .fast_left, .slow_left, .slow_right").css({display:'block'});
	
	counter = $(".animPic").length;

	// --- Get default width of animation's stripe --- //
	logotypy_width = counter*logos_step;		
	
	// --- Start animation --- // 	
	setTimeout (function() {
		animacja_logotypy("-");					
			
		$("#logotypy").bind("mouseenter",
		function() {
			$(".fast_right, .fast_left, .slow_left, .slow_right").css({opacity:"0.3"});		
		});
		
		$("#logotypy").bind("mouseleave",
		function() {
			$(".fast_right, .fast_left, .slow_left, .slow_right").css({opacity:"0"});		
		});
			
		$("#logotypy").mouseover(
		function() {
			$("#logotypy").stop('true');		
		});	
		
		$("#logotypy").mouseout(
		function() {
			animacja_logotypy(logos_direction); //turn of if to heavy
		});		
		
		$(".fast_right, .fast_left, .slow_right, .slow_left").mouseout( 
		function() {
			logos_speed = 3400;
			$(".fast_right, .fast_left").css({opacity:"0"});
			$(".slow_left, .slow_right").css({opacity:"0"});		
		});			
		
		// --- Animation's menu panel - animation's behavior --- //	
		$(".slow_right").mouseover(
		function() {
			$(".fast_right, .fast_left, .slow_left, .slow_right").css({opacity:"0.3"});
			$(".slow_right").css({opacity:"0.7"});
			$("#logotypy").stop('true');
			logos_direction = '+';
			logos_speed = 3400;
			animacja_logotypy(logos_direction);
		});	
		
		$(".fast_right").mouseover(
		function() {
			$(".fast_right, .fast_left, .slow_left, .slow_right").css({opacity:"0.3"});
			$(".fast_right").css({opacity:"0.7"});
			$("#logotypy").stop('true');
			logos_direction = '+';
			logos_speed = 500;
			animacja_logotypy(logos_direction);
		});		
		
		$(".slow_left").mouseover(
		function() {
			$(".fast_right, .fast_left, .slow_left, .slow_right").css({opacity:"0.3"});
			$(".slow_left").css({opacity:"0.7"});
			$("#logotypy").stop('true');
			logos_direction = '-';
			logos_speed = 3400;
			animacja_logotypy(logos_direction);
		});	
		
		$(".fast_left").mouseover(
		function() {
			$(".fast_right, .fast_left, .slow_left, .slow_right").css({opacity:"0.3"});
			$(".fast_left").css({opacity:"0.7"});
			$("#logotypy").stop('true');
			logos_direction = '-';
			logos_speed = 500;
			animacja_logotypy(logos_direction);
		});				
	},5000)	
}

function animacja_logotypy(logos_direction) {

	var margin_left = $("#logotypy").css("margin-left");
	margin_left = margin_left.replace(/px/,"");	
	
	var movement=parseInt(logotypy_width)+parseInt(margin_left);
	
	if (logos_direction == "-") {
		var last_logo=counter-1;
	    if (margin_left<-300) {
		    $(".animLink").eq(0).clone(true).insertAfter($(".animLink").eq(last_logo));	
		    $(".animLink").eq(0).remove();	
		    margin_left = parseInt(margin_left) + logos_step;
		    $("#logotypy").css({marginLeft:margin_left});
	    }   
	}
	
	if (logos_direction == "+") {
		var last_logo=counter-1;
	    if (margin_left>-300) {
		    $(".animLink").eq(last_logo).clone(true).insertBefore($(".animLink").eq(0));	
		    $(".animLink").eq(counter).remove();	
		    margin_left = parseInt(margin_left) - logos_step;
		    $("#logotypy").css({marginLeft:margin_left});
	    }   
	}	

	$("#logotypy").animate({
			marginLeft: logos_direction + "="+parseInt(logos_step)		
	},logos_speed,'linear',function(){animacja_logotypy(logos_direction)});	
}