var totalWidth = 0, slideTime = 800, delayTime = 5000, recycleTimePerItem = 200;
$(document).ready(function() {

	$('#commodityCorner img').each(function() {
		totalWidth += 330;
		this.onload = function() {resize(this) };
		if( this.complete ) {
			resize(this);
		}
	
	});
	$('#slider').css('width',totalWidth+'px');
	setTimeout(rotate, delayTime);
	recycleTimePerItem *= (totalWidth/330)
});

function resize( image ) {
	if( typeof image == 'undefined' ) {
		image = this;
	}
	
	var width = parseInt($(image).width());
	var height = parseInt($(image).height());
	
	if( height > 75 ) {
	
		$(image).attr('height', 75);
		var pr = 75 / height;
		pr = 1 - pr;
		var da = width  * pr;
		width = width-da;
		$(image).attr('width', width);
		height = 75;
		
	}
	if( height == 75 ) {
		var padding = (100 - width) / 2;
		$(image).css('padding','0 '+padding+'px');
	}
	else if( width == 100 ) {
		var padding = (75 - height) / 2;
		$(image).css('padding', padding+'px 0');
	}
	
}

function rotate() {
	var leftPosition = $('#slider').css('left');
	leftPosition = leftPosition.replace('px','');
	if( (leftPosition*-1) == totalWidth - 330 ) {
		$('#slider').animate({left: '0px'}, recycleTimePerItem);
		setTimeout(function() {rotate()}, delayTime);
	}
	else {
		$('#slider').animate({left: (leftPosition-330)+'px'}, slideTime);
		setTimeout(function() {rotate()}, delayTime);
	}
}