var elements;
var totalWidth;

/* Wait for the images to be fully loaded */
$(window).load(function () {
	elements 	= $('div#slider div.sliderWrapper').length;
	totalWidth 	= (elements * $(window).width());
	$('div#slider div.sliderWrapper').each(function() { 
		$(this).css('width', $(window).width() + 'px')
	});
	$('div#slider').css('width', totalWidth + 'px');
			
	startSliding();
	
	$(window).bind("resize", resizeWindow);
	function resizeWindow( e ) {
		totalWidth = $(window).width() * elements;
		$('div#slider').css('width', totalWidth + 'px');
		
		$('div#slider div.sliderWrapper').each(function() { 
			$(this).css('width', Math.floor(totalWidth / elements) + 'px')
		});
	}
	
	
});

function startSliding()
{
	/* Get the first image in the wrapper */
	$('div#slider div.sliderWrapper:first-child').delay(5000).animate({
	    width: '0px'
	  }, 2500, function() {
			size = Math.floor(totalWidth / elements); // get the width of the image
			$('div#slider div.clear').before($('div.sliderWrapper:first-child'));
			
			$('div.sliderWrapper').each(function() { 
				if (intval($(this).css('width'), 10) == 0)
					$(this).css('width', size + 'px');
			}); 
			
			startSliding();
	  });
}

function intval (mixed_var, base) {
  
    var tmp;

    var type = typeof(mixed_var);

    if (type === 'boolean') {
        return +mixed_var;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var)) {
        return mixed_var | 0;
    } else {
        return 0;
    }
}
