/**************************************************************\
*                   TheOtherVoice Ajax v. 1.0                  *
*      Copyright (C) 2008 Tozzini Alessio TheOtherVoce.it      *
*  rilasciato secondo i termini di GNU GENERAL PUBLIC LICENSE  *
*--------------------------------------------------------------*
* ajax.js: oggetto x ajax con i vari div                       *
\**************************************************************/

/**********************************************************\
* oggetto che si dei menu                                  *
\**********************************************************/
var tovMenuPage = Class.create();
tovMenuPage.prototype = {
	funct : null, // funzione da avviare con opzione
	icone : "", // icona del menu'
	name : "", // nome opzione
	title : "", // titolo di pagina e menu'
	//
	// INIZIALIZZO
	//
	initialize : function(options) {}
};


/**********************************************************\
* oggetto che si occupa delle gestione delle pagine        *
\**********************************************************/
var tovDataPage = Class.create();
tovDataPage.prototype = {
	actual_ajax_file : "", // file ajax attuale da caricare
	addr_server : "http://localhost", // indirizzo server
	ajax_file : [], // array con vari file ajax
	div : {
		actual_page : "page", // div pagina attuale
		credits : "credits", // div con credits
		loading : "divLoading", // div loading
		menu : "menu", // menu' principale
		message_loading : "messageLoading", // dove scrivere messaggio loading
		page : [], // array con vari div pagina
		sub_menu : "sub_menu", // menu' di coda
		title : "title_page_div", // titolo pagina
		title_mouse : "txt_menu", // visualizza titolo quando mouse su icona
		w3c : "controll_w3c" // contiene controlli w3c
	},
	menu : [], // array con gli oggetti menu'
	menu_mouse : {
		icone_over : "45px", // dim. icona quando si posa il mouse
		icone_out : "35px" // dim. icona quando mouse esce
	},
	mysql_table : "", // specifica la tabella mysql
	responseFunction : "", // funzione di risposta per ajax
	//
	// INIZIALIZZO
	//
	initialize : function(options) {},
	//
	// AGGIUNGO DIV PAGINA
	//
	addDivPage : function( nome, div )
	{
		this.div.page[ nome ] = div;
	},
	//
	// AGGIUNGI VOCE DI MENU' CON TITOLO E FUNZIONE
	//
	addMenu : function( nome, titolo, funzione, icona )
	{
		var num = this.menu.length;
		this.menu[ num ] = new tovMenuPage();
		this.menu[ num ].name = nome;
		this.menu[ num ].title = titolo;
		this.menu[ num ].funct = funzione;
		this.menu[ num ].icone = icona;
	},
	//
	// RESTITUISCO FILE AJAX DALLA LISTA
	//
	ajaxPage : function( indice )
	{
		return this.ajax_file[ indice ];
	},
	//
	// CAMBIO PAGINA
	//
	changePage : function( pagina, stringa )
	{
		window.scrollTo(0,0);
		this.clearPage( "" );
		this.printTitle( pagina );
		this.menu[ pagina ].funct( pagina, stringa );
	},
	//
	// PULISCO PAGINA
	//
	clearPage : function( page )
	{
		if( ( page != "" ) )
		{
			$( this.div.page[ page ] ).innerHTML = "";
		} else {
			//for( var i = 0; i < this.div.page.length; i++ )
			for( i in this.div.page )
			{
				$( this.div.page[ i ] ).innerHTML = "";
			}
		}
	},
	//
	// VISUALIZZO/NASCONDO CREDITS
	//
	divCredits : function( type )
	{
		switch( type )
		{
			case 'show':
				Element.show( this.div.credits );
				break;
			case 'hide':
				Element.hide( this.div.credits );
				break;
			default:
		}
	},
	//
	// RESTITUISCE NOME PAGINA
	//
	getNamePage : function( num )
	{
		return this.menu[ num ].name;
	},
	//
	// RESTITUISCE TITOLO PAGINA
	//
	getTitlePage : function( num )
	{
		return this.menu[ num ].title;
	},
	//
	// CARICO LISTA DATI CON REQUEST
	//
	loadList : function( stringa )
	{
		// carico la lista
		var myAjax = new Ajax.Request(
			this.actual_ajax_file,
			{
				method: 'post',
				parameters: stringa,
				onComplete: this.responseFunction
			}
		);
	},
	//
	// PASSAGGIO MOUSE SU ICONA MENU' PRINCIPALE
	//
	menuMouse : function ( type, oggetto, titolo )
	{
		var stringa = "";
		switch( type )
		{
			case 'over':
				oggetto.style.width = this.menu_mouse.icone_over;
				stringa = titolo + " &nbsp; &nbsp; ";
				break;
			case 'out':
				oggetto.style.width = this.menu_mouse.icone_out;
				stringa = "men&ugrave;";
				break;
			default:
		}
		$( this.div.title_mouse ).innerHTML = stringa;
	},
	//
	// AGGIUNGO FILE AJAX ALLA LISTA
	//
	newAjaxPage : function( indice, nome )
	{
		this.ajax_file[ indice ] = nome;
	},
	//
	// SCRIVO DIRETTAMENTE SU DIV
	//
	printDivPage : function( num, stringa )
	{
		$( this.div.page[num] ).innerHTML = stringa;
	},
	//
	// SCRIVO MENU' INIZIO PAGINA
	//
	printMenu : function()
	{
		// numero di voci
		var num_voice = this.menu.length;
		// scrivo il menu'
		var stringa = "";
		for( i = 0; i < num_voice; i++ )
		{
			id_menu = "menu_img_" + i;
			stringa += " <a href=\"javascript: obj_data_page.changePage(" +
					i + ");\"><img style=\"width:35px;\" src=\"" +
					this.menu[i].icone +
					"\" alt=\"" + this.menu[i].title +
					"\" title=\"" + this.menu[i].title +
					"\" onmouseover=\"obj_data_page.menuMouse('over',this,'" +
					this.menu[i].title +
					"');\" onmouseout=\"obj_data_page.menuMouse('out',this);\"/></a>";
		}
		$( this.div.menu ).innerHTML = stringa;
	},
	//
	// CAMBIO MESSAGGIO LOADING
	//
	printMessageLoading : function ( messaggio )
	{
		$( this.div.message_loading ).innerHTML = messaggio;
	},
	//
	// SCRIVO MENU' FINE PAGINA
	//
	printSubMenu : function()
	{
		// numero di voci
		var num_voice = this.menu.length;
		// scrivo il menu'
		var stringa = "|";
		for( i = 0; i < num_voice; i++ )
		{
			stringa += " <a href=\"javascript: obj_data_page.changePage(" +
					i + ");\">" + this.menu[i].title + "</a> |";
		}
		$( this.div.sub_menu ).innerHTML = stringa;
	},
	//
	// SCRIVO SOTTOTITOLO PAGINA
	//
	printSubtitle : function( sottotitolo )
	{
		if( sottotitolo != "" )
		{
			$( "subtitle" ).innerHTML = sottotitolo;
		}
	},
	//
	// CAMBIO TITOLO PAGINA
	//
	printTitle : function( pagina )
	{
		var titolo = this.getTitlePage( pagina );
		if( titolo != "" )
		{
			var stringa = "<h1 class=\"title_page\">" +
					titolo + "</h1><div id=\"subtitle\"></div>";
			$( this.div.title ).innerHTML = stringa;
		}
	},
	//
	// STAMPO CONTROLLI W3C
	//
	printW3c : function( www, css )
	{
		var stringa = "";
		if( www == 1 )
		{
			stringa = "<a href=\"http://validator.w3.org/check?uri=referer\">" +
				"<img src=\"http://www.w3.org/Icons/valid-xhtml10\" alt=\"Valid XHTML 1.0 Strict\" height=\"31\" width=\"88\" /></a> ";
		}
		if( css == 1 )
		{
			stringa += "<a href=\"http://jigsaw.w3.org/css-validator/validator?profile=css21&amp;warning=0&amp;uri=" +
			this.addr_server + "\">" +
			"<img style=\"width:88px;height:31px\" src=\"http://jigsaw.w3.org/css-validator/images/vcss-blue\" alt=\"CSS Valido!\" /></a>";
		}
		$( this.div.w3c ).innerHTML = stringa;
	},
	//
	// LEGGE CONTENUTI DI UN FORM
	//
	readForm : function( div_form )
	{
		// legge lista input
		var form = $( div_form );
		var lista = form.getElements();
		var stringa = "";
		var valore = "";
		lista.each( function( s, key )
			{
				if( $F(s.id) != null )
				{
					valore = $F( s.id );
				} else {
					valore = "-1";
				}
				stringa += "&" + s.id + "=" + encodeURIComponent( valore );
			}
		);
		return stringa;
	},
	//
	// SPEDISCO O SCRIVO EMAIL
	//
	sendMail : function( type )
	{
		switch( type )
		{
			case 'form':
				this.actual_page = 0;
				this.updatePage( "type_data=" + type );
				break;
			case 'send':
				var stringa = this.readForm( "form_email" );
				this.actual_page = 0;
				this.updatePage( "type_data=" + type + "&" + stringa );
				break;
			default:
		}
	},
	//
	// AGGIORNO IL DIV CON UPDATER
	//
	updatePage : function( stringa )
	{
		var num = this.div.actual_page;
		var myAjax = new Ajax.Updater(
			this.div.page[ num ],
			this.actual_ajax_file,
			{
				method: 'post',
				parameters: stringa
			}
		);
	},
	//
	// VISUALIZZO DIV LOADING
	//
	visibilityLoading : function ( type )
	{
		switch( type )
		{
			case 'show':
				Element.show( this.div.loading );
				break;
			case 'hide':
				Element.hide( this.div.loading );
				break;
			default:
		}
		
	}
};

// creo oggetto
var obj_data_page = new tovDataPage(); // dati generali


// Immagine di loading
var myGlobalHandlers = {
	onCreate: function()
	{
		Element.show( obj_data_page.div.loading );
	},
	onComplete: function()
	{
		if( Ajax.activeRequestCount == 0 )
		{
			Element.hide( obj_data_page.div.loading );
		}
	}
};
Ajax.Responders.register( myGlobalHandlers );

// chiamata quando finisce di caricare la pagina
var endLoadingPage = function()
{
	// stampo menu'
	obj_data_page.printMenu();
	obj_data_page.printSubMenu();
	// stampo controlli w3c
	obj_data_page.printW3c();
	// nascondo il loading
	Element.hide( obj_data_page.div.loading );
	// carico pagina iniziale
	obj_data_page.changePage( 0 );
};