Solaros.gallery={};

Ext.apply(Solaros.gallery,
{
	init:function()
	{
	    Solaros.linkManager.addHandler(
	    	'gallery',
	    	function(path)
			{
				if(Solaros.gallery.galleries)
				{
					Solaros.gallery.galleries.selectPath(
						'/'+path.join('/'),null,
						function(success,node)
						{
							if(success)
							{
								Solaros.gallery.nodeSelected(node);
							}
							else
							{
								Ext.Msg.alert('Error!','The gallery you are attempting to view is protected.');
							}
						}
					); 	
				}
				else
				{
					if(Solaros.navigation.selectTab('gallery',false,true))
					{
						var dt=new Ext.util.DelayedTask();
						
						dt.delay(
							500,
							function(dt,path)
							{
								if(Solaros.gallery.galleries)
								{
									Solaros.gallery.galleries.selectPath(
										'/'+path.join('/'),null,
										function(success,node)
										{
											if(success)
											{
												Solaros.gallery.nodeSelected(node);
											}
											else
											{
												Ext.Msg.alert('Error!','The gallery you are attempting to view is protected.');
											}
										}
									);
								}
								else
								{
									dt.delay(500);
								}
							},
							this,
							[dt,path]
						);
					}
					else
					{
						Ext.Msg.alert('Error!','The galleries section is currently protected.');
					}
				}
			}
		);
	},
	
	nodeSelected:function(node)
	{
		if(node.attributes.category)
		{
			Solaros.linkManager.goTo('gallery'+node.getPath());
			Solaros.gallery.showGallery(
				{
					category:   node.attributes.category,
					personal:   node.attributes.personal,
					gallery:    node.attributes.galleryReference,
					container:  'contentArea',
					editEnabled:node.attributes.canEdit,
					memberId:   node.attributes.memberId
				}
			);
		}
	},
	
	loadPreviews:function()
	{
	    new Ext.data.Connection({}).request(
	        {
	            url:'servlet/GalleryImageServlet?_dc='+new Date().getTime(),
	            params:{mode:'loadPreviews'},
	            method:'post',
	            callback:function(options,success,response)
	            {
					var images=Ext.decode(response.responseText);
					
					for(var i=0;i<images.length;i++)
					{
						var el=Ext.get('gallery-random-'+i);
					
						el.dom.style.backgroundImage=
							'url('+new Date().getTime()+'.gal?mode=view&type=thumb&image='+
							images[i].file+'&gallery='+images[i].gallery+')';
							
						el.on(
							'click',
							function()
							{
								Ext.get('gallery-preview-image').dom.style.backgroundImage=
									'url('+new Date().getTime()+'.gal?mode=view&type=image&imageId='+this.id+'&image='+
									this.file+'&gallery='+this.gallery+')';
								Ext.get('gallery-preview-photographer').dom.innerHTML=this.photographer;
								Ext.get('gallery-preview-caption').dom.innerHTML=this.caption;
							},
							images[i]
						);
						
						if(i==0)
						{
							Ext.get('gallery-preview-image').dom.style.backgroundImage=
								'url('+new Date().getTime()+'.gal?mode=view&type=image&imageId='+images[i].id+'&image='+
								images[i].file+'&gallery='+images[i].gallery+')';
							Ext.get('gallery-preview-photographer').dom.innerHTML=images[i].photographer;
							Ext.get('gallery-preview-caption').dom.innerHTML=images[i].caption;
						}
					}
	            },
	            scope:this
	        }
	    );
	},
	
	reloadGalleries:function(node)
	{
		if(node)
		{
			if(node.isLeaf())
			{
				node=node.parentNode;
			}
		}
		else
		{
			node=this.galleriesRoot;
		}
		
		this.galleries.loader.load(node);
	},
	
	getGalleriesRoot:function()
	{
		return(this.galleriesRoot);
	},
	
	getGalleries:function()
	{
		if(!this.galleries)
		{
			this.galleries=new Ext.tree.TreePanel(
			    {
			        el:'gallery-tree',
			        autoScroll:true,
			        animate:true,
			        enableDD:false,
			        containerScroll: false, 
			        autoHeight:true,
			        rootVisible:false,
			        border:false,
			        lines:false,
			        bodyStyle:{backgroundColor:'#DDDDB6'},
			        loader: new Ext.tree.TreeLoader(
				        {
				            dataUrl:'servlet/GallerySidebarServlet'
				        }
			        ),
			        listeners:
			        	{
			        		'click':
			        		{
			        			fn:function(node,e)
			        			{
			        				Solaros.gallery.nodeSelected(node);
			        			}
			        		}
			        	}
			    }
		    );
		    
		    this.galleriesRoot = new Ext.tree.AsyncTreeNode(
			    {
			        text: 'GALLERY_ROOT',
			        draggable:false,
			        id:1
			    }
		    );
		    
		    this.galleries.setRootNode(this.galleriesRoot);
		}
		
		return(this.galleries);
	},
	
	showGallery:function(config)
	{
		this.gallery=new Cinera.Gallery.GalleryView(config);
	},
	
	showAddGalleryPictures:function(gallery,callback,scope)
	{
		var dialog = new Ext.ux.UploadDialog.Dialog({
		  url: 'servlet/GalleryImageServlet?&mode=add&gallery='+gallery,
		  reset_on_hide: false,
		  allow_close_on_upload: false,
		  upload_autostart: true,
		  modal:true
		});
					
		dialog.on(
			'hide',
			function()
			{
				if(callback)
				{
					callback.call(scope);
				}
				
				this.destroy();
			},
			dialog
		);

		dialog.show();
	},
	
	showCategoryForm:function(memberId,parent,category)
	{
		new Cinera.Gallery.CategoryForm(memberId,parent,category);
	},
	
	showGalleryForm:function(memberId,parent,gallery)
	{
		new Cinera.Gallery.GalleryForm(memberId,parent,gallery);
	},
	
	showPersonalGalleryForm:function(memberId,parent,gallery)
	{
		new Cinera.Gallery.PersonalGalleryForm(memberId,parent,gallery);
	},
	
	nodeContextMenuListener:function(node,e)
	{
		var menu=new Ext.menu.Menu(
			{
				shadow:true
			}
		);

		if(node.attributes.canEdit && node.attributes.galleryNodeType == 'gallery')
		{
			menu.add(
				{
					id:'editDetails',
					text:'Edit Gallery Details',
					href:
						node.attributes.personal?
						'javascript:Solaros.gallery.showPersonalGalleryForm('+node.attributes.memberId+',null,'+node.id+')':
						'javascript:Solaros.gallery.showGalleryForm('+node.attributes.memberId+',null,'+node.id+')'
				},
				{
					id:'addPictures',
					text:'Add Pictures To Gallery',
					href:'javascript:Solaros.gallery.showAddGalleryPictures('+node.attributes.galleryReference+')'
				}
			);
		}
		
		if(node.attributes.canEdit && node.attributes.galleryNodeType == 'category')
		{
			menu.add(
				{
					id:'editCategory',
					text:'Edit This Category',
					href:'javascript:Solaros.gallery.showCategoryForm('+node.attributes.memberId+','+node.parentNode.id+','+node.id+')'
				}
			);
		}
		
		if(node.attributes.canAdd && node.attributes.galleryNodeType == 'category')
		{
			menu.add(
				{
					id:'addCategory',
					text:'Add Category Here',
					href:'javascript:Solaros.gallery.showCategoryForm('+node.attributes.memberId+','+node.id+')'
				},
				{
					id:'addGallery',
					text:'Add Gallery Here',
					href:'javascript:Solaros.gallery.showGalleryForm('+node.attributes.memberId+','+node.id+')'
				}
			);
		}
		
		if(node.attributes.canAdd && node.attributes.galleryNodeType == 'personalCategory')
		{
			menu.add(
				{
					id:'yourGallery',
					text:((node.attributes.personalGallery != 0)?'Edit':'Create')+' Personal Gallery',
					href:'javascript:Solaros.gallery.showPersonalGalleryForm('+node.attributes.memberId+','+node.id+','+node.attributes.personalGallery+')'
				}
			);
		}

		if(menu.items.getCount() > 0)
		{
			menu.show(node.getUI().getTextEl());
		}
	}
});