(function($) {
	jQuery.fn.ajaxUpload = function() {
		var event = {
			init : function() {
				$('iframe#ajaxUploadFrame').remove(); 	
				$('body').append('<iframe src="about:blank" id="ajaxUploadFrame" name="ajaxUploadFrame"></iframe>');
				$('iframe#ajaxUploadFrame').toggle();
				$("iframe#ajaxUploadFrame").load(function(){			
					event.loaded(this);						
				});
				return 'ajaxUploadFrame';											
			},
			
			submit : function(_form) {
				$('#photo_loader').hide();
				_form.setAttribute('target', event.init()); 
			},
			
			loaded : function(id) {
				var d = (id.contentDocument) ? id.contentDocument : ((id.contentWindow)  ? id.contentWindow.document : window.frames[id].document);
				event.buildResponse(eval('(' + d.body.innerHTML + ')'));
				
			},
			
			buildResponse: function(o) {
				/*
				$('#ufile_multi_photo').html('<div><input type="file" name="load_photo_multiplefile[]" style="width: 220px;" /></div><div><input type="file" name="load_photo_multiplefile[]" style="width: 220px;" /></div><div><input type="file" name="load_photo_multiplefile[]" style="width: 220px;" /></div><div><input type="file" name="load_photo_multiplefile[]" style="width: 220px;" /></div><div><input type="file" name="load_photo_multiplefile[]" style="width: 220px;" /></div>');
				$('#load_photo_file').replaceWith('<input type="file" name="load_photo_file" id="load_photo_file" />');
				$('#load_photo_title').val('');
				$('#load_photo_tags').val('');
				$('#load_photo_source').val('');
				$('#load_photo_link').val('');
				*/
				resetForm('ajaxUploadForm');
				Alert.show(AlertText.photo['title'],AlertText.photo[o.msg],o.type);
				if(o.data) {
					$('#photo_count_item').html(o.data[0].count);
					var html = '';
					for(var i = 0;i<o.data.length;i++)
					{
						if(o.data[i].visible == 0) {
							moder = ' on_moderation';
						} else {
							moder = '';
						}
						html += '<li id="photo_item_' + o.data[i].id + '" class="person_foto_one' + moder + '">';
						html += '	<a href="'+o.data[i].src_big +'" rel="prettyPhoto[gallery]" link="' + o.data[i].title + '" id="' + o.data[i].id + '"> <img src="' + o.data[i].src + '" alt=""><span class="foto_frame"></span></a>';
						html += '</li>';
					}
					$('#photo_gallery').html(html);
					$("a[rel^='prettyPhoto']").prettyPhoto();
					//pageTracker._trackPageview('/analytics/addcontent/photo/');
				}				
			}
		}
		$('form#ajaxUploadForm').submit(function(e){
			return event.submit(this);
		});
	};	
})(jQuery);

function resetForm(id) {
	$('#'+id).each(function(){
	        this.reset();
	});
}
