/**
 * submStats
 * NCBI Peptidome browsing application
 *
 *
 */

Ext.BLANK_IMAGE_URL = '/core/extjs/ext-2.1/resources/images/default/s.gif';
Ext.ns( 'submStats' );


submStats.app = function()
{
	var query_url = "cgi/queryms.cgi";
	var dataFormat = 'json'; // json. DO NOT use XML	
	
	var show_subm_stats = function( counts, level,role )
	{		 
		var get_subm_html = function( counts )
		{
			var html = '<h3><span>Total Holdings</span></h3><div id="holdingsContent"><table border="0" cellpadding="0" cellspacing="0" class="stats_table" summary="Submission numbers">' ;
			var totalStudies = 0, totalSamples = 0, totalProteins = 0 , totalPeptides = 0, totalSpectra = 0;
			var publicStudies = 0, publicSamples = 0;
			if(checkIfExists(counts.public)){
				publicStudies = checkIfExists(counts.public.studies)?counts.public.studies : 0;
				publicSamples = checkIfExists(counts.public.samples)?counts.public.samples : 0;				
			}
			var privateStudies = 0, privateSamples = 0;
			if(checkIfExists(counts.private)){
				privateStudies = checkIfExists(counts.private.studies)?counts.private.studies : 0;
				privateSamples = checkIfExists(counts.private.samples)?counts.private.samples : 0;				
			}
			
			html += '<tbody>';
			html +=	'<tr><th>Studies</th><td class="' + level + '" id="public-series-' + level + '">' ;
			html +=	(publicStudies > 0)?'<a href="browse/">' + publicStudies + '</a>':publicStudies;
			html += '</td></tr>';
			html += '<tr><th>Samples</th><td class="' + level + '" id="public-series-' + level + '">' ;
			html += (publicSamples  >0 )?'<a href="browse/?view=samples">' + publicSamples+ '</a>':publicSamples;
			html +='</td></tr>';
			if(checkIfExists(counts.total)){
				totalStudies = checkIfExists(counts.total.studies)?counts.total.studies : 0;
				totalSamples = checkIfExists(counts.total.samples)?counts.total.samples : 0;	
				totalProteins = checkIfExists(counts.total.proteins)?counts.total.proteins : 0;
				totalPeptides = checkIfExists(counts.total.peptides)?counts.total.peptides : 0;	
				totalSpectra = checkIfExists(counts.total.spectra)?counts.total.spectra : 0;	
				
				
				html += checkIfExists(totalProteins)? '<tr><th class="row">Proteins</th><td class="mols" id="public-series-mols">' + totalProteins + '</td> </tr>': '';	
				html += checkIfExists(totalPeptides)? '<tr><th class="row">Peptides</th><td class="mols" id="public-samples-mols">' + totalPeptides + '</td> </tr>': '';	
				html += checkIfExists(totalSpectra)? '<tr><th class="row">Spectra</th><td class="mols" id="public-samples-mols">' + totalSpectra + '</td></tr>': '';
				//html += '<tr><td colspan=3 id="spacer">&nbsp;</td></tr>';
				
			}		
								
			html += '</tbody>';			
			html +=	'</table></div>';

			return html;
		};

		var subm_stats_html = get_subm_html( counts );
		

		var all_subm_panel = new Ext.Panel(
		{
			// title: title,
			renderTo: 'submission-stats-' + level,
			//width: 150,
			frame: false,
			border: false,		
			html: subm_stats_html
		});
	};

	var show_mol_stats = function( counts, level )
	{
		var get_mol_html = function( counts )
		{ 
			var html = '<h3><span>My Holdings</span></h3><div id="holdingsContent">';
			var totalStudies = 0, totalSamples = 0, publicStudies = 0, publicSamples = 0 ,privateStudies = 0, privateSamples = 0;
			if(checkIfExists(counts.total)){
				totalStudies = checkIfExists(counts.total.studies)?counts.total.studies : 0;
				totalSamples = checkIfExists(counts.total.samples)?counts.total.samples : 0;					
			}
			if(checkIfExists(counts.public)){
				publicStudies = checkIfExists(counts.public.studies)?counts.public.studies : 0;
				publicSamples = checkIfExists(counts.public.samples)?counts.public.samples : 0;				
			}
			
			if(checkIfExists(counts.private)){
				privateStudies = checkIfExists(counts.private.studies)?counts.private.studies : 0;
				privateSamples = checkIfExists(counts.private.samples)?counts.private.samples : 0;				
			}
			
			html += '<table border="0" cellpadding="0" cellspacing="0" class="stats_table" summary="Submission numbers"><tbody>';
			
			html +=	'<tr><th>Studies</th><td class="' + level + '" id="public-series-' + level + '">' ;
			html +=	(totalStudies > 0)?'<a href="browse/?view=mystudies">' + totalStudies + '</a>':totalStudies;
			html += '</td></tr>';
			html += '<tr><th>Samples</th><td class="' + level + '" id="public-series-' + level + '">' ;
			html += (totalSamples  >0 )?'<a href="browse/?view=mysamples">' + totalSamples+ '</a>':totalSamples;
			html +='</td></tr>';
			
			html += 	'</tbody></table></div>';				

			return html;
		};

		var subm_mols_html = get_mol_html( counts );

		var mol_stats_panel = new Ext.Panel(
		{
			renderTo: 'submission-stats-' + level,			 
			frame: false,
			border: false,
			html: subm_mols_html
		});
	};

	var get_subm_stats = function()
	{
		var params = 'cmd=counts&format='+dataFormat;

		Ext.Ajax.request(
		{
			url: query_url,
			params: params,

			success: function ( result, request ) 
			{
				var obj = '';
				if(dataFormat === 'xml'){ 
					obj = result.responseText;
					if ( obj !== undefined ){
						show_subm_stats( result.responseXML, 'studies' );
					}
				} 
				else {	
					obj = Ext.decode( result.responseText );										
					if(obj instanceof Object && obj.MS_QueryResponse){
						
						var c = Ext.get('submission-stats-studies');
						if(c){c.update('');}
		
						if( obj.MS_QueryResponse.all_counts ){
							show_subm_stats( obj.MS_QueryResponse.all_counts, 'studies', obj.MS_QueryResponse.userinfo.role );
						}
						if( obj.MS_QueryResponse.user_counts ){
							show_mol_stats( obj.MS_QueryResponse.user_counts, 'private', obj.MS_QueryResponse.userinfo.role );
						}
						else{}
					}
				}
				handleUserStatus(obj);
			},
			failure: function () 
			{ 
				handleUserStatus('');	
				var c = Ext.get('submission-stats-studies');
				if(c){c.update('');}
			}
		});	
	};

	return {
		init: function () 
		{
			get_subm_stats();
		}
	}
}();

Ext.onReady( submStats.app.init, submStats.app );