//delay is the time between the movements
//target is the items inside the parent to move
//duration is how long the movement takes to occur
//action is what animation to call. options:scroll,drop,fade
$.fn.imageSlide=function(options){
	var defaults = {
		'delay': 3000,
		'target':'div',
		'duration':1000,
		'action':'scroll',
		'linkChange':'none'
	};
	
	var called = $(this);
	var opts = $.extend(defaults, options);
	var targets = $(this).find(defaults.target);
	var count = targets.length;
	var length = $(targets).length - 1;
	var slideCount = 0;
	
	if (count > 1) {
		for (i = 0; i <= length; i++) {
			$(called).find(defaults.target).filter(':eq(' + i + ')').attr('position', i);
		}
		
		switch (defaults.action) {
			case 'scroll':
				$(targets).css('position', 'relative');
				break;
			case 'drop':
				$(targets).each(function(){
					$(this).prependTo($(this).parent());
				});
				
				$(targets).css('position', 'absolute');
				break;
			case 'fade':
				$(targets).each(function(){
					$(this).prependTo($(this).parent());
				});
				
				$(targets).css('position', 'absolute');
				break;
		}
		
		if (defaults.action != 'fade' && defaults.linkChange != 'none') {
			var copyWidth = $(this).width();
			var copyHeight = $(this).height();
			var copyFloat = $(this).css('float');
			var copyMarginT = $(this).css('margin-top');
			var copyMarginR = $(this).css('margin-right');
			var copyMarginB = $(this).css('margin-bottom');
			var copyMarginL = $(this).css('margin-left');
			var allMargins = copyMarginT + ' ' + copyMarginR + ' ' + copyMarginB + ' ' + copyMarginL;
			$(this).css('margin', '0px');
			$(this).wrap('<div id="newWrapper" style="overflow:visible;float:' + copyFloat + ';margin:' + allMargins + ';width:' + copyWidth + 'px;height:' + copyHeight + 'px;position:relative;"></div>');
			var linking = $(defaults.linkChange);
			$('#newWrapper').append(linking);
			if (jQuery.browser.version == '6.0') {
				$(linking).wrap('<span></span>');
			}
		}
		
		
		var first = $(called).find(defaults.target).filter('[position=' + slideCount + ']');
		var height = 0 - $(this).height();
		var width = 0 - $(this).width();
		$(targets).css({
			'margin-bottom': '0px',
			'height': Math.abs(height),
			'overflow': 'hidden',
			'width': Math.abs(width)
		});
		
		$.fn.doMove = function(){
		
			switch (defaults.action) {
				case 'scroll':
					$(this).animate({
						marginTop: height + 'px'
					}, defaults.duration, function(){
						$(called).append(this);
						$(this).css('margin-top', '0px');
						var newLink = $(first).attr('link');
						$(defaults.linkChange).attr('href', newLink);
					});
					break;
				case 'drop':
					$(this).animate({
						top: Math.abs(height) + 'px'
					}, defaults.duration, function(){
						$(called).prepend(this);
						$(this).css('top', '0px');
						var newLink = $(first).attr('link');
						$(defaults.linkChange).attr('href', newLink);
					});
					break;
				case 'fade':
					$(this).fadeOut(defaults.duration, function(){
						$(called).prepend(this);
						$(this).show();
						var newLink = $(first).attr('link');
						$(defaults.linkChange).attr('href', newLink);
					});
					break;
			};
			slideCount++;
			
			if (slideCount > length) {
				slideCount = 0;
			}
			
			first = $(called).find(defaults.target).filter('[position=' + slideCount + ']');
			point = $(first);
			slideTimer = setTimeout('point.doMove();', defaults.delay);
		};
		
		var initLink = $(first).attr('link');
		$(defaults.linkChange).attr('href', initLink);
		point = $(first);
		slideTimer = setTimeout('point.doMove();', defaults.delay);
	}
};