// By Shiftyjelly, by P.Simpson
(function($) {

	$.fn.iphonefade = function(options) {
		return this.each(function() {   
			$.iphonefade(this, options);
		});
	};

	$.iphonefade = function(container, options) {
		var settings = {
			'images': [],
			'path': "",
			'timeout': 2000
		}
		if (options) {
			$.extend(settings, options);
		}

		if (settings.images.length > 0 && $(container).children().length == 0) {
			for (var i=0; i<settings.images.length; i++) {
				$(container).append("<div style='z-index:"+(settings.images.length-i)+";background:#000 url("+settings.path+settings.images[i]+") no-repeat top left;'><!-- --></div>");
			}
		
			var children = $(container).children();
			setTimeout(function() {
				$.iphonefade.next(children, settings, 1, 0);
			}, settings.timeout);
		}
	}
	
	$.iphonefade.next = function(elements, settings, current, last) {
		$(elements[current]).css('width',202);
		$(elements[last]).animate({width:0});
        
		if ((current + 1) < elements.length) {
            current = current + 1;
            last = current - 1;
        } else {
            current = 0;
            last = elements.length - 1;
        }
		setTimeout((function() {
            $.iphonefade.next(elements, settings, current, last);
        }), settings.timeout);
	}
	
})(jQuery);