var fumetti = liwe.module ( "fumetti" );

fumetti._news_categs = {};
fumetti._menu_categs = [ 'Tutti', 'autori', 'appuntamenti', 'associazione', 'manifestazione', 'fumetti', 'corsi',  'progetti',  'notizie' ];
fumetti._cur_categ = '';


fumetti.init = function ()
{
	if ( ! fumetti.templates )
	{
		liwe.AJAX.easy ( { action: "fumetti.ajax.get_templates" }, function ( v ) {
			fumetti.templates = v [ 'templates' ];
		} );
	}

	liwe.AJAX.easy ( { action: "news.ajax.list_categs" }, function ( v ) {
		fumetti._news_categs = v [ 'categs' ];
		var t, l = fumetti._news_categs.length;
		var cs = {};
		for ( t = 0; t < l; t ++ )
		{
			var c = fumetti._news_categs [ t ];
			cs [ c.id ] = c;
		}

		fumetti._news_categs = cs;
	} );
};

fumetti.show_news = function ( id_news )
{
	if ( ! id_news ) return;

	$ ( 'cnt_news_full' ).innerHTML = String.formatDict ( fumetti.templates [ 'HP_BOX_DEFAULT' ], { _txt: '<div id="cnt_show_news"></div>' } );
	$ ( 'cnt_show_news' ).innerHTML = fumetti.templates [ 'wait' ];
	$ ( 'cnt_news_full' ).style.display = 'block';
	site.show_page ( 'show_news' );
	news.cbacks [ 'show' ] = fumetti._show_news_done;
	news.show ( id_news, 'cnt_show_news' );
};

fumetti._show_news_done = function ( v )
{
	var media_items = v [ 'media' ] && v [ 'media' ].length > 0 ? media_manager.set_items ( v [ 'media' ] ) : null;
	var str_imgs = '';
	v [ '_media_panel' ] = '';
	if ( media_items )
	{
		v [ '_media_panel' ] = media_manager.panel ( media_items, "icon", 6 );
		str_imgs = '<div class="mm_images_title">Galleria Immagini</div><div class="cnt_mm_images">' + v [ '_media_panel' ] + '</div>';
	}

	$ ( 'cnt_show_news' ).innerHTML = v [ '_html' ] + str_imgs;
};

fumetti.tag_search = function ( tag_name )
{
	if ( ! tag_name ) tag_name = '';
	fumetti._tag_name = tag_name;

	$ ( 'cnt_news_full' ).innerHTML = fumetti.templates [ 'wait' ];
	$ ( 'cnt_news_full' ).style.display = 'block';
	site.show_page ( 'show_news' );
	news.cbacks [ 'show_results' ] = fumetti.tag_search_done;
	news.tag_search ( tag_name, 'cnt_news_full', 9999 );
};

fumetti.tag_search_done = function ()
{
	$ ( 'filter_ds' ).innerHTML = 'Cerca per: ' + fumetti._create_sel_categs ();
	$ ( 'filter_ds_top' ).innerHTML = 'Cerca per: ' + fumetti._create_sel_categs ();
	$ ( 'corr_title' ).innerHTML = 'Correlati: ' + ( fumetti._tag_name ? fumetti._tag_name : 'tutti' );
};

fumetti._create_sel_categs = function ()
{
	var t, l = fumetti._menu_categs.length;
	var sel = '<select onchange="fumetti.categ_search(this.value)">';
	for ( t = 0; t < l; t ++ )
	{
		var c = fumetti._menu_categs [ t ];
		var chk = "";

		if ( t == 0 )
		{
			sel += 	'<option value="">' + c + '</option>';
		}
		else
		{
			fumetti._news_categs.iterate ( function ( v, k ) {
				chk = '';
				if ( fumetti._news_categs [ k ] [ 'text' ].toLowerCase() == c )
				{
					if ( fumetti._cur_categ == k ) chk = ' selected="selected" ';
					c = fumetti._news_categs [ k ];
					
					sel += 	'<option value="' + c.id + '" ' + chk + '>' + c.text + '</option>';
				}
			} );
		}
			
	}
	sel += '</select>';

	return sel;
};


fumetti.categ_search = function ( id_categ )
{
	fumetti._cur_categ = id_categ;
	var c = fumetti._news_categs.get ( id_categ )

	$ ( 'cnt_news_full' ).innerHTML = fumetti.templates [ 'wait' ];
	$ ( 'cnt_news_full' ).style.display = 'block';
	site.show_page ( 'show_news' );

	news.cbacks [ 'show_results' ] = fumetti.categ_search_done;
	fumetti._tag_name = c ? c.text : '' ;

	news.categ_search ( id_categ, 'cnt_news_full', '', 9999 );
};

fumetti.categ_search_done = function ()
{
	 fumetti.tag_search_done ();
};

fumetti.create_home_page = function ()
{
	$ ( 'list_box' ).innerHTML = fumetti.templates [ 'wait' ];
	$ ( 'list_box' ).style.display = 'block';
	liwe.AJAX.easy ( { action: "fumetti.ajax.hp_news_list", home: 1, quant: 3, tags: 'home lista box' }, function ( v ) {
		$ ( 'list_box' ).innerHTML = v [ 'html' ];
	} );
};

fumetti.show_contatti = function ()
{
	var s = String.formatDict ( fumetti.templates [ 'cnt_frm_cont' ], { title: 'Contattaci' } );
	$ ( 'contatti_sects' ).innerHTML = String.formatDict ( fumetti.templates [ 'HP_BOX_DEFAULT' ], { _txt: s } );

	var f = new liwe.form.instance ( "contatti" );

	var dest_email = [
		{ 'label': 'Associazione', 'value': 10 },
		{ 'label': 'Manifestazione', 'value': 20 },
		{ 'label': 'Redazione', 'value': 30 },
		{ 'label': 'Informazioni', 'value': 40 }
	];


	f.hidden ( "action", "fumetti.ajax.send_email" );
	
	f.text ( { label: 'Nome', name: 'nome', size: 30 } );
	f.text ( { label: 'Cognome', name: 'cognome', size: 30 } );
	f.email ( { label: 'Email', name: 'email', size: 30, mandatory: true } );
	f.select ( { label: 'Destinatario', name: 'dest', options: dest_email, mandatory: true } );
	f.text ( { label: 'Oggetto', name: 'oggetto', size: 30, mandatory: true } );
	f.textarea ( { label: 'Testo', name: 'testo', mandatory: true, rows: 15, cols: 40 } );

	f.button ( { value: 'Invia', name: 'btn_go', onclick: 'fumetti.send_contatti()' } );

	f.set ( 'cnt_frm_cont' );
};

fumetti.send_contatti = function ()
{
	var f = liwe.form.get ( "contatti" );

	if ( ! f.check () )
	{
		alert ( "Valorizzare i campi: email, oggetto e testo" );
		return;
	}

	liwe.AJAX.easy ( f.get_values (), function ( v ) {
		site.show_page ( 'home' );
	} );
};

fumetti.show_page = function ( page, db )
{
	staticpage.get_page ( page, db, function ( p ) {
		$ ( 'static_page_cnt' ).innerHTML = String.formatDict ( fumetti.templates [ 'HP_BOX_DEFAULT' ], { '_txt': p [ 'html' ] } );
		$ ( 'static_page_cnt' ).style.display = 'block';
	} );
};

