/* Scrolling speed (milliseconds). */
var speed = 30;

/* Image margin. */
var iMargin = 40;

/* Vars. */
var pic, numImgs, arrLeft, i, totalWidth, n, myInterval; 

var iMargins = 0;

// When DOM is loaded.
jQuery(window).load(function(){

	// Get all pictures.
	pic = jQuery("#slider").children(".img");

	// Get amount of pictures.
	numImgs = pic.length;

	// Calculate total amount of margin.
	iMargins = numImgs * iMargin;

	// Create image array.
	arrLeft = new Array(numImgs);
var iCount = 0;
	// Walk through images.
	for (i = 0; i < numImgs; i++) {

		// Total width.
		totalWidth = 0;

		// Walk through images.
		for(n=0;n<i;n++){
			totalWidth += jQuery(pic[n]).width() + iMargin;
		}

		// Set width for image.	
		arrLeft[i] = totalWidth;

		// Set left offset for image.
		jQuery(pic[i]).css("left", totalWidth);
iCount++;
	}
	
	myInterval = setInterval("flexiScroll()",speed);
	jQuery('#imageloader').hide();
	jQuery(pic).show();	
});

function flexiScroll(){

	for (i=0;i<numImgs;i++){
		arrLeft[i] -= 1;		

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