var C_showAlbums=new Class ({
	type:null,
	holder:null,
	
	init: function(_type, id) {
		this.type=_type;
		this.holder=$_(id);
		this.renderAlbums();
	},
	renderAlbums:function(){
		this.holder.innerHTML='';
		ajaxPetition("resources/PHP/API_galleries.php","command=getAlbums&type="+this.type,function(ajax){
			var r = eval('('+ajax.responseText+')');
			if (r.errorCode>0){ alert(r.errorDescription); return;}
			
			$A(r.data).each(function(elem){
				var itemHolder = $C('DIV', {className:'itemHolder', onclick:function(){this.renderPhotos(elem.id);}.bind(this)}, this.holder);
				var photoHolder = $C('DIV', {className:'photoHolder'}, itemHolder);
				var photoFooter = $C('DIV', {className:'photoFooter'}, itemHolder);
				$C('DIV', {className:'texto_peque', innerHTML:elem.title}, photoFooter);				
				ajaxPetition("resources/PHP/API_galleries.php","command=getPhotos&type="+this.type+"&album="+elem.id,function(ajax){
					var r = eval('('+ajax.responseText+')');
					if (r.errorCode>0){ alert(r.errorDescription); return;}
					$C('IMG', {src:'data/albums/'+this.type+'/'+elem.id+'/'+r.data[0].id.replace('img', 'crop_176x200')}, photoHolder);					
				}.bind(this));
			}.bind(this));

			$C('DIV', {'.clear':'both'}, this.holder);

		}.bind(this));
	},
	renderPhotos: function(album){
		this.holder.innerHTML='';
		ajaxPetition("resources/PHP/API_galleries.php","command=getPhotos&type="+this.type+"&album="+album,function(ajax){
			var r = eval('('+ajax.responseText+')');
			if (r.errorCode>0){ alert(r.errorDescription); return;}
			
			$A(r.data).each(function(elem){
				var photoHolder = $C('DIV', {className:'photoHolder photoDetailHolder'}, this.holder);	
				var caption=null;
				if (elem.title || elem.description) caption="<h1>"+elem.title+"</h1><div style=\"font-style:italic\">"+elem.description+"</div>";
				var linkLightBox = $C('A',{className:'thickbox', title:caption, href:'data/albums/'+this.type+'/'+album+'/'+elem.id},photoHolder);
				$C('IMG', {'.border':'0px',title:' ',  src:'data/albums/'+this.type+'/'+album+'/'+elem.id.replace('img', 'crop_176x200')}, linkLightBox);					
			}.bind(this));
			
			$C('DIV', {'.clear':'both'}, this.holder);
			$C('DIV', {className:'button', innerHTML:'Volver', onclick:function(){location.href='#nuestros_trabajos';this.renderAlbums();}.bind(this)}, this.holder);
			$C('DIV', {'.clear':'both'}, this.holder);

			loadScript('js/thickbox.js', function(){});
		}.bind(this));
	}
});
