Ext.namespace("Cinera.Gallery.Image");

Cinera.Gallery.Image.AccessForm = function(memberId,image)
{
	this.captionForm   =null;
	this.captionFormWin=null;
	
	this.image=image;
	
	Cinera.Gallery.Image.AccessForm.superclass.constructor.call(this);  

	this.galleryImagePhotographerEl=Ext.get(document.body).createChild({tag:'div',id:'gallery-image-access-win'});
	Ext.get(this.galleryImagePhotographerEl).createChild({tag:'div',id:'gallery-image-access-form'});

	this.accessForm =new Ext.FormPanel(
		{
			frame: true,
			labelAlign: 'top',
			labelWidth: 100,
			waitMsgTarget: document.body,
		
	        reader : new Ext.data.XmlReader(
		        {
		            record : 'element',
		            success: '@success'
		        },
		        [
		        	'accessLevel',
		        	'image',
		            'message',
		            'script'
		        ]
	        ),
			
			errorReader: new Xh.form.XmlErrorReader(),
			
			floating:false,
			autoShow:true,
			autoHeight:true,
			
			items:
			[
				new Ext.form.ComboBox(
					{
						fieldLabel:'Viewable By',
                        hiddenName:'accessLevel',
                        store:new Ext.data.SimpleStore(
                            {
                            	fields:['id','display'],
                            	data:[
                            		[memberId,'Yourself'],
                            		[0,       'Members'],
                            		[-1,      'Officers'],
                            		[-2,      'Everyone']
                            	]
                            }
                        ),
                        valueField:'id',
                        displayField:'display',
                        typeAhead: true,
                        mode: 'local',
                        triggerAction: 'all',
                        selectOnFocus:true,
                        anchor:'100%',
                        allowBlank:false,
                        editable:false,
                        forceSelection:true,
                        readOnly:true,
                        required:true,
                        value:0
                    }
                ),
				{
					xtype:'checkbox',
					name: 'applyToAll',
    				hideLabel:true,
			        boxLabel: 'Apply to all images in this gallery?',
					anchor:'100%'
				}
			]
		}
	);

	this.accessFormWin = new Ext.Window(
		{
			el:'gallery-image-access-win',
			layout:'fit',
			width:300,
			plain: true,
			title: 'Gallery Image: '+this.image.id,
			closable:false,
			resizable:false,
			autoHeight:true,
			modal:true,
			
			items: this.accessForm,

			buttons: 
			[
                {
                    text: 'Save',
                    handler: function()
                    {
                        this.accessForm.getForm().submit(
	    					{
						        url:'servlet/GalleryOperationServlet?operation=EditGalleryImage&_dc='+new Date().getTime(),
						        params:
							        {
							        	mode:'accessLevel',
							        	image:this.image.id,
							        	gallery:this.image.view.gallery
							        },
						        waitMsg:'Saving Gallery Image Information...',
						        scope:this,
						        success:Xh.form.successHandlerWithDestroy,
						        failure:Xh.form.failureHandler
						    }
                        );
                    },
                    scope:this
                },
				{
					text: 'Close',
					handler: function()
					{
						this.destroy();
					},
					scope:this
				}
			]
		}
	);
	
	this.accessFormWin.show(this);
	this.accessForm.render(Ext.get('gallery-image-access-form'));
    
    this.accessForm.load(
	    {
	        url:'servlet/GalleryOperationServlet?operation=GetGalleryImage&_dc='+new Date().getTime(),
	        params:
	        	{
	        		mode:'accessLevel',
	        		image:this.image.id
	        	},
	        waitMsg:'Loading Gallery Image Information...',
	        scope:this,
	        success:Xh.form.successHandler,
	        failure:
	        	function(form,action)
		        {
		        	this.destroy();
		        	Xh.form.failureHandler.call(this,form,action);
	        	}
	    }
    );
}

Ext.extend(Cinera.Gallery.Image.AccessForm, Ext.Component,
{
	destroy:function()
	{
		this.accessFormWin.hide();
		
		this.accessFormWin.destroy();
		this.accessForm.destroy();
	
		Cinera.Gallery.Image.AccessForm.superclass.destroy.call(this);  
	}
});