jQuery.fn.dynamicSlideshow = function(attr) {
	attr = attr || {};
	attr.duration = attr.duration || 3000;
	attr.css = '{' + attr.css + '}' || "'position':'absolute', 'margin':20, 'left':0";
	eval('attr.css='+attr.css);
	function initSlider(container, img) {
		var curr = 1;
		setInterval( function(){
			if (curr == img.length) {
				curr = 0;
			}
			var i = new Image();
			
			$(i).load(function(){
				_resize_image_max(this,attr.maxH,attr.maxW);
				$(container).append(this);
				$(container).find('img:first').css({'z-index': 1});
				$(this).css({opacity: 0.0, 'z-index': 2}).animate({opacity: 1.0}, 1000, function() {
						$(container).find('img:first').remove();
					})
			}).attr('src', img[curr++]).css(attr.css).addClass(attr.classToAdd).css({'z-index':8}).bind('click',function(){
				eval(attr.click);
				return false;
			});
		}, attr.duration );
	};

	$(this).each(function(){
		var img = [];
		$(this).find("a").each(function(){
			img.push($(this).attr("href"));		
		});
		var j = new Image();
		var container = this;
		$(this).empty();
		curr = 1;
		img[0] = img[0] + '?id=' + new Date().getTime();
		$(j).attr('src', img[0]).css(attr.css).addClass(attr.classToAdd).css({'z-index':0}).load(function(){
			_resize_image_max(this,attr.maxH,attr.maxW);
			$(container).append(this);
			initSlider(container, img);
		}).bind('click',function(){
				eval(attr.click);
				return false;
			});
	});
	
	function _resize_image_max(objImagePreloader,wH,wW){
			
			var h;
			var w;
			
			if (objImagePreloader.width >= objImagePreloader.height)
            {
                h = (objImagePreloader.height / objImagePreloader.width) * wW;
				w = wW;
                if (h > wH)
                {
                    w = (objImagePreloader.width / objImagePreloader.height) * wH;
					h = wH;
                }
            }

            if (objImagePreloader.width < objImagePreloader.height)
            {
                w = (objImagePreloader.width / objImagePreloader.height) * wH;
                h = wH;
                if (w > wW)
                {
                    h = (objImagePreloader.height / objImagePreloader.width) * wW;
                    w = wW;
                }
            }
			
			objImagePreloader.width = w;
			objImagePreloader.height = h;
			
			return objImagePreloader;			
		}
}

