UploadFieldSet = function(num) {
	UploadFieldSet.superclass.constructor.call(this,{
		title: 'Image #'+num,
		autoHeight: true,
		defaultType: 'textfield',
		labelWidth: 80,
		items: [
		{
			name: 'title'+num,
			fieldLabel: 'Title',
			minLength: 3,
			allowBlank:false,
			width: 250
		},
		{
			name: 'image'+num,
			fieldLabel: 'File',
			inputType: 'file',
			allowBlank:false,
			width: 250
		},
		{
			name: 'artist_comments'+num,
			fieldLabel: 'Comments',
			xtype: 'textarea',
			width: 250
		},
		{
			name: 'keywords'+num,
			fieldLabel: 'Keywords',
			xtype: 'textarea',
			width: 250
		}
		]
	});
};
Ext.extend(UploadFieldSet, Ext.form.FieldSet);
UploadImagesWindow = function() {
	this.numUploads = 1;
	this.formPanel = new Ext.form.FormPanel({
		fileUpload:true,
		autoScroll: true,
		items: [ new UploadFieldSet(this.numUploads) ],
		bbar: [{text: 'Add Another Upload', handler: this.addUploadImage, scope: this }, { text: 'Upload Photos', handler: this.uploadImages, scope: this }, { text: 'Cancel', handler: function() { this.close(); }, scope: this }]
	});
	UploadImagesWindow.superclass.constructor.call(this,{
		id: 'image-uploads',
		title: 'Upload Photos',
		width: 400,
		height: 450,
		layout: 'fit',
		items: [ this.formPanel ],
		modal: true
	});
}
Ext.extend(UploadImagesWindow,Ext.Window,{
	addUploadImage: function() {
		this.numUploads++;
		this.formPanel.add(new UploadFieldSet(this.numUploads));
		this.formPanel.doLayout();
	},
	uploadImages: function() {
		this.formPanel.getForm().submit({
			params: {
				numimages: this.numUploads
			},
			url: HOME_DIR+'user_images/add',
			success: function(f,a) {
				if ( a.result.success ) {
					var msg = '';
					for ( i = 1; i <= this.numUploads; i++ ) {
						msg += "Image Upload #" + i + ": ";
						if ( a.result.upload_status[i].success ) {
							msg += "Uploaded Successfully<br />";
						}
						else {
							msg += "Error: " + a.result.upload_status[i].error.reasons + "<br />";
						}
						Ext.Msg.show({
							title: 'Upload Status',
							msg: msg,
							buttons: Ext.MessageBox.OK,
							icon: Ext.Msg.INFO
						});
					}
					this.close();
					reloadImages();
				}
				else {
						Ext.Msg.show({
							title: 'Error!',
							msg: a.result.error.reasons,
							buttons: Ext.MessageBox.OK,
							icon: Ext.Msg.ERROR
						});
				}
			},
			failure: function(f,a) {

				if(a.result) {
				Ext.Msg.show({
					title: 'Error!',
					msg: a.result.error.reasons,
					buttons: Ext.MessageBox.OK,
					icon: Ext.Msg.ERROR
				});
				}
			},
			waitMsg: 'Please wait... Uploading Photos...',
			waitTitle: 'Uploading Photos',
			scope: this
		});
	}
});
function uploadImages() {
	var uploadWin = new UploadImagesWindow();
	uploadWin.show();
}
function reloadImages() {
	new Ajax.Updater('your_gallery_thumbs',HOME_DIR+'user_images/profile_list');
}