	/* Variabili globali */
	var curret_event_id;
	
	/* Gestione del Login */
	function do_xhr_login(){
		var kw = {
			url: "login_response.php",
			handleAs: "text",
			content: {
				type: "login"
			},
			load: function(response){
				dojo.byId('login_response').innerHTML = response;
				window.location.reload();
			},
			error: function(data){
				alert("An error occurred: " + data);
			},
			timeout: 2000,
			form: "loginForm"
		};
		
		dojo.xhrPost(kw);
	}
	function do_xhr_logout(){
		var kw = {
			url: "login_response.php",
			handleAs: "text",
			content: {
				type: "logout"
			},
			load: function(response){
				window.location.reload();
			},
			error: function(data){
				alert("An error occurred: " + data);
			},
			timeout: 2000
		};
		
		dojo.xhrPost(kw);
	}

	/* Colore celle grid */
	function colorDescriptions(inRow){
		inRow.customStyles = 'background-color:green';
		inRow.customStyles = 'color:yellow';				
	}
	
	/* Creazione grid eventi */
	dojo.require("dojo.data.ItemFileReadStore");
	dojo.require("dojox.grid.Grid");
	dojo.require("dojox.grid._data.model");
	dojo.require("dojo.parser");
	dojo.require("dojox.data.QueryReadStore");
	
	/********** GRIDS **********/
	/* Events */
	var view1 = {
		cells: [[{
			name: 'Titolo',
			field: "title",
			width: "400px"
		}, {
			name: 'Data',
			field: "date",
			width: "150px"
		}, {
			name: 'Luogo',
			field: "location",
			width: "200px"
		}]],
		noscroll: true
	};
	var layout = [view1];

	/* Media */
	var grid_media_view = {
		cells: [[{
			name: 'Titolo',
			field: "title",
			width: "400px"
		}, {
			name: 'Data',
			field: "date",
			width: "350px"
		}]],
		noscroll: true
	};
	var grid_media_layout = [grid_media_view];

	/* Effetti */
	dojo.require("dojo.fx");
	dojo.require("dojox.fx.easing"); 
	var eIn_user_html_outer, eIn_user_html;

	dojo.addOnLoad(function(){
		dojo.byId('login').innerHTML = '';
		
		/* Effetti */
		eIn_user_html_outer = dojo.fadeIn({	
			node: 'user_html_outer',
			duration:2000,
			easing: dojox.fx.easing.easeIn
		})
		eIn_user_html = dojo.fadeIn({	
			node: 'user_html',
			duration:2000,
			easing: dojox.fx.easing.easeIn
		})
	});

	function display_users_thumb(){

	document.getElementById('user_html_outer').style.display = 'none';
	dojo.xhrGet({
			url: './get_users_list.php',
			handleAs: "json",
			content: {
				type: "user_list"
			},
			load: function(responseObject){
				var user_num = 0;
				var curr_row = '';
				dojo.forEach(responseObject.items,
						function(oneEntry, index, array) {
							user_num++;
							var listItem = document.createElement("td");
							//listItem.setAttribute("class", "user_thumb_div");
							listItem.innerHTML = "<img class=\"user_thumb_img\" onClick=\"javascript:display_user_html(" + oneEntry.id + ")\" src=\"./thumbs/" + oneEntry.thumb +"\"><br><span class=\"user_thumb_nick\">" + oneEntry.nick + "</span>";
							if(Math.ceil(user_num/6) == 1) dojo.byId('c_1').appendChild(listItem);
							else if(Math.ceil(user_num/6) == 2) dojo.byId('c_2').appendChild(listItem);
							else if(Math.ceil(user_num/6) == 3) dojo.byId('c_3').appendChild(listItem);
							else if(Math.ceil(user_num/6) == 4) dojo.byId('c_4').appendChild(listItem);
							else if(Math.ceil(user_num/6) == 5) dojo.byId('c_5').appendChild(listItem);							
							else if(Math.ceil(user_num/6) == 6) dojo.byId('c_6').appendChild(listItem);							
						}
				);			
			},
			error: function(data){
				console.error('Error: ', data);
			}
		});	
	}

	function display_user_html(user_id){
		dojo.xhrGet({
			url: './get_users_list.php',
			content: {
				id: user_id,
				type: "user_html"
			},
			load: function(data){
				page_div = document.getElementById('page');				
				var page_div_Y = getY(page_div);
				console.debug('page_div_Y: ' + page_div_Y);
				foot_div = document.getElementById('footer');				
				var foot_div_Y = getY(foot_div);
				document.getElementById('user_html_outer').style.display = 'block';

				dojo.query("#user_html").style("zIndex", 50);
				dojo.query("#user_html_outer").style("zIndex", 49);	
				dojo.query("#user_html_outer").style("height", foot_div_Y - page_div_Y + 'px');
				dojo.byId('user_html').innerHTML = data;

				dojo.query("#user_html_outer").style("opacity", 0);		
				dojo.query("#user_html").style("opacity", 0);		

				eIn_user_html_outer.play();
				eIn_user_html.play();
				setTimeout('dojo.query("#user_html_outer").style("opacity", 0.8)',3000);
						
			},
			error: function(data){
				console.error('Error: ', data);				
			}
		});				
	}
	
	function close_user_html(){
		dojo.byId('user_html').innerHTML = '';
		document.getElementById('user_html_outer').style.display = 'none';
	}

	/****** Media ******/

	function switch_no_media_mode(){
		document.getElementById('media_video').style.visibility = 'hidden';
		document.getElementById('media_photo').style.visibility = 'hidden';
	}
	
	function switch_photo_mode(){
		document.getElementById('media_photo').style.visibility = 'visible';
		document.getElementById('media_photo').style.display = 'block';
		document.getElementById('media_video').style.display = 'none';
	}
	
	function switch_video_mode(){
		document.getElementById('media_video').style.visibility = 'visible';
		document.getElementById('media_video').style.display = 'block';
		document.getElementById('media_photo').style.display = 'none';
	}
	function hide_media_players(){
		dojo.byId("video_html").innerHTML = '';
		dojo.byId("photo_html").innerHTML = '';
	}
	function display_photo(id_media){
		dojo.xhrGet({
			url: './get_media_list.php',
			content: {
				id: id_media,
				type: "html"
			},
			load: function(data){
				dojo.byId('photo_player').innerHTML = data;
			},
			error: function(data){
				console.error('Error: ', data);
			}
		});		
	}
	function display_video(id_media){
		dojo.xhrGet({
			url: './get_media_list.php',
			content: {
				id: id_media,
				type: "html"
			},
			load: function(data){
				dojo.byId('video_player').innerHTML = data;
			},
			error: function(data){
				console.error('Error: ', data);				
			}
		});		
	}

	/****** Gestione info eventi ******/
	function display_event_info(event_id){
		curret_event_id = event_id;
		
		dojo.xhrGet({
			url: './get_partecipation_code.php',
			load: function(data){
				dojo.byId('event_info').innerHTML = data;
				
				dojo.xhrGet({
					url: './get_event_info.php',
					content: {
						id: event_id
					},
					load: function(data){
						dojo.byId('event_html').innerHTML = data;
						dojo.byId("partecipation_list").innerHTML = '';
						dojo.byId("event_partecipation_response").innerHTML = '';
						
						/** Questa funzione non esiste se non c'è il login! **/
						display_partecipation_list2();
					},
					error: function(data){
						console.error('Error: ', data);
					}					
				});
				
			},
			error: function(data){
				console.error('Error: ', data);
			}
		});		
	}
	
	function hide_event_info(){
		dojo.byId('event_info').innerHTML = '';
	}

	function display_partecipation_list(){
		dojo.xhrPost({
			url: './display_partecipation_list.php',
			handleAs: "json",
			content: {
				event_id: curret_event_id
			},
			load: function(responseObject, ioArgs){				
				dojo.byId("partecipation_list").innerHTML = '';
				dojo.forEach(responseObject.items,
					function(oneEntry, index, array) {
						var listItem = document.createElement("li");
						listItem.innerHTML = "<b>" + oneEntry.nick + "</b> ";
						dojo.byId("partecipation_list").appendChild(listItem);
					}
				);
			},			
			error: function(data){
				console.error('Error: ', data);
			}
		});
	}
	
	function add_partecipation(user_id){
		dojo.xhrPost({
			url: './add_partecipation.php',
			content: {
				event_id: curret_event_id,
				user_id: user_id
			},
			load: function(data){
				dojo.byId('event_partecipation_response').innerHTML = data;
				display_partecipation_list2();
			},
			error: function(data){
				console.error('Error: ', data);
			}
		});		
	}
	function remove_partecipation(user_id){
		dojo.xhrPost({
			url: './remove_partecipation.php',
			content: {
				event_id: curret_event_id,
				user_id: user_id
			},
			load: function(data){
				dojo.byId('event_partecipation_response').innerHTML = data;
				display_partecipation_list2();				
			},
			error: function(data){
				console.error('Error: ', data);
			}
		});		
	}
	
	function display__newsletter(){
		dojo.query("#newsletter_inner").style("opacity", 0.5);
		dojo.query("#newsletter_inner").style("zIndex", 10);
		dojo.query("#newsletter").style("zIndex", 20);
	}
	
	/****** Login ******/
	function showLogin(){
		dojo.xhrGet({
			url: './get_login_code.php',
			load: function(data){
				dojo.byId('login').innerHTML = data;

				page_div = document.getElementById('page');				
				var page_div_Y = getY(page_div);
				console.debug('page_div_Y: ' + page_div_Y);
				foot_div = document.getElementById('footer');				
				var foot_div_Y = getY(foot_div);
				console.debug('foot_div_Y: ' + foot_div_Y);
				dojo.query("#login_VBox").style("opacity", 1);
				dojo.query("#login_VBox_outer").style("opacity", 0.8);		
				dojo.query("#login_VBox").style("zIndex", 50);
				dojo.query("#login_VBox_outer").style("zIndex", 49);	
				dojo.query("#login_VBox_outer").style("height", foot_div_Y - page_div_Y  + 'px');
			},
			error: function(data){
				console.error('Error: ', data);
			}
		});		
	}
	function closeLogin(){
		dojo.byId('login').innerHTML = '';
	}
	
	/* Gestione newsletter */
	function do_subscribe(){
		dojo.xhrPost({
			url: "./subscribe.php",
			form: "subscribeForm",
			handleAs: "text",
			load: function(data){
				dojo.byId('subscribe_response').innerHTML = data;
			},
			error: function(data){
				alert("An error occurred: " + data);
			},
			timeout: 2000
		});
	}	
	
	/* Utility */
	function getY( oElement ){
		var iReturnValue = 0;
		while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
		}
		return iReturnValue;
	}