var quot = {};

quot.templates =  {};

quot.templates [ 'primo_piano' ] = '<table border="0" class="quot">' + 
		'<tr><td valign="top" class="data">%(date)s &raquo;<\/td><td valign="top" class="occhiello">%(OCCHIELLO)s<\/td><\/tr>' +
		'<tr><td><\/td><td class="titolo"><a href="javascript:site.utils.show_hide(\'%(ID)s_abs\')">%(TITOLO)s<\/a><\/td><\/tr>' +
		'<tr><td><\/td><td class="abstract">' +
		'		<div id="%(ID)s_abs" style="display: none;">' +
		'			<div class="autore">%(AUTORE)s<\/div>' +
		'			%(ABSTRACT)s' +
		'		<\/div>' +
		'<\/td><\/tr>'  +
		'<tr><td><\/td><td class="link"><a href="%(link)s">Vai al documento<\/a><\/td><\/tr>'  +
		'<\/table>';

quot.templates [ 'appr' ] = '<table border="0" class="quot">' + 
		'<tr><td valign="top" class="data">%(date)s &raquo;<\/td><td valign="top" class="occhiello">%(OCCHIELLO)s<\/td><\/tr>' +
		'<tr><td><\/td><td class="titolo"><a href="javascript:site.utils.show_hide(\'%(ID)s_abs\')">%(TITOLO)s<\/a><\/td><\/tr>' +
		'<tr><td><\/td><td class="abstract">' +
		'			<div id="%(ID)s_abs" style="display: none;">' +
		'			<div class="autore">%(AUTORE)s<\/div>' +
		'				%(ABSTRACT)s' +
		'			<\/div>' +
		'		<\/td><\/tr>'  +
		'<tr><td><\/td><td class="link"><a href="%(link)s">Vai al documento<\/a><\/td><\/tr>'  +
		'<\/table>';

quot.show_hide = function ( id_name )
{
	var h = $( id_name );

	if ( h.style.display == 'none' )
		h.style.display = 'block';
	else
		h.style.display = 'none';
};

quot.init = function ()
{
	var d = new Date ();
	
	var today = d.getFullYear () + "-" + ( d.getMonth () + 1 ) + "-" + d.getDate ();

	quot._fill_primo_piano ( today );
	quot._fill_news ( today );
	quot._fill_appr ();
};

// {{{ _fill_primo_piano ()
quot._fill_primo_piano = function  ( today )
{
	var fq = new FulQuery ();
	var a = {};

	fq.db_name = 'QUOTYAP';
	fq.opera = 'AP';
	fq.mode = 'QUERY';


	fq.set_fields ( 'ID', 'OCCHIELLO', 'DESCR_TIPO', 'DATA', 'TITOLO', 'AUTORE', 'ABSTRACT' );
	//fq.set_fields ( 'ID', 'OCCHIELLO', 'DESCR_TIPO', 'DATA', 'TITOLO', 'AUTORE', 'ABSTRACT', 'TIPO_DOC' );
	fq.add ( 'flag_anteprima', 'EQUAL', 'S' );
	fq.add ( 'flag_primopiano', 'EQUAL', 'S' );
	fq.add ( 'tipo', 'EQUAL', '92' );
	//fq.add ( 'data', 'EQUAL', today );
	fq.set_id ();

	//fq.order_by = "DATA DESC";
	fq.lines = 3;

	fq.fill ( a );

	liwe.AJAX.request ( '/cgi-bin/FulQuery', a, function ( v ) { 
		var s = '';

		if ( v [ 'rows' ] == 0 )
		{
			s = "Non ci sono risultati";
		} else {
			s = quot._format_results ( v, quot.templates [ 'primo_piano' ] );			
		}

		$( "q_tab1" ).innerHTML = s;
		local.tabs.send_event ( "q_tab1", "click" );
	}, true );
};
// }}}
// {{{ _fill_news ()
quot._fill_news = function ( today )
{
	var fq = new FulQuery ();
	var a = {};

	fq.db_name = 'QUOTYAP';
	fq.opera = 'AP';
	fq.mode = 'QUERY';

	fq.set_fields ( 'ID', 'OCCHIELLO', 'DESCR_TIPO', 'DATA', 'TITOLO', 'AUTORE', 'ABSTRACT' );
	//fq.set_fields ( 'ID', 'OCCHIELLO', 'DESCR_TIPO', 'DATA', 'TITOLO', 'AUTORE', 'ABSTRACT', 'TIPO_DOC' );
	fq.add ( 'flag_anteprima', 'EQUAL', 'S' );
	fq.add ( 'flag_primopiano', 'EQUAL', 'N' );
	fq.add ( 'tipo', 'EQUAL', '92' );
	//fq.add ( 'data', 'EQUAL', today );
	fq.set_id ();

	//fq.order_by = "DATA DESC";
	fq.lines = 3;

	fq.fill ( a );

	liwe.AJAX.request ( '/cgi-bin/FulQuery', a, function ( v ) { 
		var s = '';

		if ( v [ 'rows' ] == 0 )
		{
			s = "Non ci sono risultati";
		} else {
			s = quot._format_results ( v, quot.templates [ 'primo_piano' ] );			
		}

		$( "q_tab2" ).innerHTML = s;
	}, true );
};
// }}}
// {{{ _fill_appr ()
quot._fill_appr = function ()
{
	var fq = new FulQuery ();
	var a = {};

	fq.db_name = 'QUOTYAP';
	fq.opera = 'AP';
	fq.mode = 'QUERY';

	fq.set_fields ( 'ID', 'OCCHIELLO', 'DATA', 'TITOLO', 'ABSTRACT', 'AUTORE' );
	//fq.set_fields ( 'ID', 'OCCHIELLO', 'DATA', 'TITOLO', 'ABSTRACT', 'AUTORE', 'TIPO_DOC' );
	fq.add ( 'tipo', 'EQUAL', '87' );
	//fq.order_by = "DATA DESC";
	fq.lines = 1;
	fq.set_id ();

	fq.fill ( a );

	liwe.AJAX.request ( '/cgi-bin/FulQuery', a, function ( v ) { 
		var s = '';

		if ( v [ 'rows' ] == 0 )
		{
			s = "Non ci sono risultati";
		} else {
			s = quot._format_results ( v, quot.templates [ 'appr' ] );			
		}

		$( "q_tab3" ).innerHTML = s;
	}, true );
};
// }}}
// {{{ _format_results ()
quot._format_results = function ( data, templ )
{
	var t, l = data [ 'rows' ];
	var s = '', row;

	for ( t = 0; t < l; t ++ )
	{
		row = data [ "row" + t ];
		if ( ! row ) continue;

		site.utils.make_doc_link ( row, null, true );

		row [ "date" ] = site.utils.order_date ( row [ "DATA" ], "-", "IT", "/" );

		s += String.formatDict ( templ, row );
	}

	return s;
};
// }}}
