Ext.namespace('Ext.ux');

Ext.ux.Gallery = (function(){
	
	var initialized = false;
	
	
	return {
		
		classTarget	: 'thumb_select',
		
		init: function(){
			
			 if(!initialized) {
				this.getEl();
				initialized = true;
            }
		},
		
		getEl  : function(){
			
			Ext.Ajax.request({
				url:'php_ajax/tasks.php',
				method: 'POST',
				params: { 
					task: 'getAllEl'
				},
				scope:this,
				success:function(response,options){
					
					var responses = Ext.decode(response.responseText);
					if(responses.success === false){	
						return;
					}
					
					var t = new Ext.Template(
						'<div id="{id}" class="{class}" onclick=\'Ext.ux.Gallery.selectAlbum(this)\'>',
							'<img src="{thumb}" id="img-{id}" width="100px" height="60px" />',
						'</div>'
					);
					
					Ext.each(responses.rows, function(items, index, alls){
						t.append('creations_thumb', {id: items.id, 'class': 'thumb_select', thumb: 'gallery/'+items.thumb});							  
					});
					
					this.selectAlbum(responses.rows[0]);
					
				},
				failure: function(response, options){
					var responses = Ext.util.JSON.decode(response.responseText);
				}
			});

		},
		
		selectAlbum: function(e){
			
		
			
			if(e){
			
				Ext.select('.thumb_selected').removeClass('thumb_selected');
				
				var newId	= e.id;
				var el		= Ext.get(newId).addClass('thumb_selected');
				
				/* FLASH */
				
				var flash_frame = document.getElementById("flash_frame");
				flash_frame.src = 'php_ajax/flash container.php?id='+newId;
				
				/* INFO */
				
	
				
				Ext.Ajax.request({
					url:'php_ajax/tasks.php',
					method: 'POST',
					params: { 
						task: 'getEl',
						id	: newId,
						lang: pageLang
					},
					success:function(response,options){
						var responses = Ext.decode(response.responseText);
						if(responses.success === false){
							return;
						}
						
						var results = responses.rows[0];

						var dh = Ext.DomHelper;
						
						dh.overwrite('local_name', results.name);
						dh.overwrite('local_adress', results.adress);
						dh.overwrite('local_desc', results.project_desc);
						dh.overwrite('local_comments', results.comments);
						
						
					},
					failure: function(response, options){
						var responses = Ext.util.JSON.decode(response.responseText);
					}
				});
			}
		}
	};
	
})();

Ext.onReady(Ext.ux.Gallery.init, Ext.ux.Gallery);
