/*
This script has been made on the top of this one:

> http://calisza.wordpress.com/2009/04/25/circular-image-slide-with-jquery/

Thanks a lot!

I do not know all this license stuff but of course use this script as you wish. :-) Would be nice if you could not remove this link:

http://petiar.sk - webdesign, web applications, Drupal.

*/

// the speed while hovering the controls
var fast = 1;

// the normal speed
var normal = 40;

// the speed multiplicator
// as this script is quite unefficient, for faster movement we need to do more than 1px move
var multiplicator = 2;

var pic, numImgs, arrLeft, i, totalWidth, n, myInterval;
var width;

$(window).load(function(){
	
	width = $('#slider').width();
	
	$('.right').bind("mouseenter",function(){
		clearInterval(myInterval);
		myInterval = setInterval("flexiScrollRight()",fast);
		
	}).mouseout(function(){
		clearInterval(myInterval);
		myInterval = setInterval("flexiScrollRight()",normal);
	});

	$('.left').bind("mouseenter",function(){
		clearInterval(myInterval);
		myInterval = setInterval("flexiScrollLeft()",fast);
		
	}).mouseout(function(){
		clearInterval(myInterval);
		myInterval = setInterval("flexiScrollLeft()",normal);
	});

	
	pic = $("#slider").children("img");
	numImgs = pic.length;
	arrLeft = new Array(numImgs);

	for (i=0;i<numImgs;i++){

		totalWidth=0;
		for(n=0;n<i;n++){
			totalWidth += $(pic[n]).width();
		}

		arrLeft[i] = totalWidth;
		$(pic[i]).css("left",totalWidth);
	}

	myInterval = setInterval("flexiScrollLeft()",normal);
	$('#imageloader').hide();
	$(pic).show();
});

function flexiScrollLeft(){
	for (i=0;i<numImgs;i++){
		arrLeft[i] -= multiplicator;

		if (arrLeft[i] <= -($(pic[i]).width())){
			totalWidth = 0;
			for (n=0;n<numImgs;n++){
				if (n!=i){
					totalWidth += $(pic[n]).width();
				}
			}
			// as the images might be far left, we need to substract also the part between left border of the slide
			// and the right border fo the image from the total width
			arrLeft[i] =  totalWidth + (arrLeft[i] + $(pic[i]).width());
		}
		$(pic[i]).css("left",arrLeft[i]);
	}
}

function flexiScrollRight(){
	for (i=numImgs-1;i>-1;i--){
		arrLeft[i] += multiplicator;		

		if (arrLeft[i] >= width){
			totalWidth = 0;
			for (n=0;n<numImgs;n++){
					totalWidth += $(pic[n]).width();
			}
			arrLeft[i] =  arrLeft[i] - totalWidth;
		}
		$(pic[i]).css("left",arrLeft[i]);
	}
}
