/*
 * jQuery Iframe Uploader
 * Author: Paulo Freitas
 * 
 * This plugin creates an hidden iframe on the page.
 * The form submits to this hidden iframe target
 * and onComplete returns the HTML from the action page.
 * 
 */
 
(function ($) {

	$.fn.extend({
		uploader2: function (options) {
			options = $.extend({},
			{
				iframe: 'f' + Math.floor(Math.random() * 99999),
				returnType: 'html',
				onComplete: function(form, data) { 
					// Fix: replaceWith() instead of html() to prevent getting div's inside div's inside div's...
					eval(data);
				}
			},
			options);
			
			$('body').append('<iframe id=' + options.iframe + ' name="' + options.iframe + '" width="0" height="0" frameborder="0" style="border: none; display: none; visibility: hidden;"></iframe>');
			var form = $(this);
			form.attr('target', options.iframe);
			form.submit();
			jQuery('#' + options.iframe).load(function () {
				var data = jQuery('#' + options.iframe).contents().find('body').html();
				if (options.returnType.toLowerCase() == 'json') eval('data=' + data);
				options.onComplete(form, data);
				jQuery('#'+ options.iframe).empty();
				jQuery('#'+ options.iframe).unbind('load');
			});
		}
	});
})(jQuery);