(function($){
	$.fn.logoRotator = function(options) {   
		var defaults = {			
			elementSource: {ajaxModule: '', ajaxCommand: ''},			
			data: null			
		};
	
		var options = $.extend(defaults, options);
		var $dom = $("<div>").attr("class", "logoRotatorMask");
		var $that = this;
		
		$.ajax({
			url: "/ajaxServer.php",
			dataType: 'json',
			data: {
				AjaxRequest: {
					command: options.elementSource.ajaxCommand, 
					module: options.elementSource.ajaxModule
				}
			},
			success: function(data){				
				options.data = data;
				$.each(options.data, function(key, value){
					$dom.append(
						$("<a>").attr("href", value.url).attr({alt: value.name, target: (value.url.indexOf("http://") == -1) ? "_self" : "_blank"}).append(
							$("<img>").attr("src", "/partnerlogos/thumb/" + value.image).attr("title", value.name)
						)
					);	
					
				});				
				$dom.find("img").imgpreload(						
					function(){
						var width = 0;
						$dom.find("a").each(function(){
							width += $(this).width() + parseInt($(this).css("margin-left")) + parseInt($(this).css("margin-right"));
						});						
						$dom.css("width", width + "px");
						
						$that.append($dom);
						rotate();
					}
				);				
			}
		});
		
		var rotate = function(){
			var $first = $dom.find("a:first");			
			var $last = $dom.find("a:first").clone();			
			var width = $first.width() + parseInt($first.css("margin-left")) + parseInt($first.css("margin-right"))
			$dom.animate(
				{left: '-=' + width + 'px'},
				2000,
				"linear",
				function(){
					$first.remove();
					$dom.css("left", 0);
					$dom.append($last);
					rotate();
				}
			);

		};
		
		return this.each(function() {			
			$(this).append($dom);			
		});
	};	
})(jQuery);
