function hideShowUsername(visible){

	//visible = true -> input username
	//visible = false -> attesa
	if (visible){
		$("attesa").toggle();
		//$("layout").morph('height: 350px;');
		$("richiestautente").toggle();
		//$("welcomeMsg").toggle();
	} else {
		//$("welcomeMsg").toggle();
		$("richiestautente").toggle();
		//$("layout").morph('height: 230px;');
		$("attesa").toggle();		
	}
	
}

function hideUsername() {

	Effect.Fade('layout', { afterFinish: hideCourtin });

}

function showCourtin() {
	
	if (cortinavisible == false) {
		$("blackCourtin").show();
		cortinavisible = true;
	}
	
}

function hideCourtin() {
	
	//Effect.Fade('blackCourtin');
	//Effect.SwitchOff('blackCourtin');
	if(cortinavisible == true) {
		//$("blackCourtin").hide();
		Effect.Fade('blackCourtin');
		cortinavisible = false;
	}
	
}

function setBusyMessage(message){

    $("lblAttesa").textContent = message;

}

function setWaitingMessage(message) {

	$('busymessage').update(message);
	
}

function toggleWaitingMessage(status) {

	if (status)
		$('loading').show();
	else
		$('loading').hide();

}

function initMappa(){

	//Init Mappa
	map = new GMap2( $("mappa") );
	map.setCenter(new GLatLng(0, 0), 2);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	
	//Per gli ad ho bisogno di sapere quando la mappa viene mossa o zoomata
	GEvent.addListener(map, "moveend", function() {
  		var center = map.getCenter();
  		var zoom = map.getZoom();
		onMapViewChanged(center.lat(), center.lng(), zoom);
	});
	
	GEvent.addListener(map, "zoomend", function() {
  		var center = map.getCenter();
  		var zoom = map.getZoom();
		onMapViewChanged(center.lat(), center.lng(), zoom);
	});

}

function showNetworkProblem(val){

	if (val==true && netproblems == false)
		//$("lblArtist").update("Problemi di rete! La connessione ad internet funziona?");
		showForm("netErrorBox");
	else if (val == false && netproblems == true)
		hideForm("netErrorBox");
		
}

function hideNewSongWindow(){

	window.setTimeout("Effect.toggle('newSongWindow', 'appear', {from: 0.8, to:0});", 6000);

}

function resizeLayout() {

	var dime = getDimensions();
	var width = dime[0];
	var height = dime[1];
	
	//Imposta la barra dell'header
	$("barraHeader").setStyle({ 
		width: (width + 'px'),
		height: '50px',
		top: "0px",
		left: '0px',
		"background-color" : '#FFFFFF',
		position: 'absolute'
	});
	
	$("logomini").setStyle( {
		left: ((width - 205) + "px")
	});

	//Imposta la barra del footer   (width + 'px'),
	$("barraFooter").setStyle({ 
		width: '100%',
		height: '99px',
		top: (height - 100) + "px",
		left: '0px',
		"background-color" : '#F0F0F0',
		position: 'absolute'
	}); 
	
	//Imposto la finestra dell'artista e delle informazioni della canzone
	$("newSongWindow").setStyle({ 
		top: ((height - $("newSongWindow").getHeight())/2) + "px",
		left: ((width - $("newSongWindow").getWidth())/2) + "px",
		position: 'absolute'
	}); 
	
	//Imposto la mappa al suo posto :P
	if (drawerVisible) {
		$("mappa").setStyle({ 
			width: ((width - $("leftPane").getWidth()) + 'px'),
			height: (height - $("barraFooter").getHeight() - $("barraHeader").getHeight() - 2) + 'px',
			top: ($("barraHeader").getHeight() + 1) + "px",
			left: $("leftPane").getWidth() + 'px',
			"background-color" : '#FFFFFF',
			position: 'absolute'
		}); 	
	} else {
		$("mappa").setStyle({ 
			width: (width + 'px'),
			height: (height - $("barraFooter").getHeight() - $("barraHeader").getHeight() - 2) + 'px',
			top: ($("barraHeader").getHeight() + 1) + "px",
			left: '0px',
			"background-color" : '#FFFFFF',
			position: 'absolute'
		}); 
	}
	
	checkArrowsEnable();

	if (drawerVisible == false) {
		$("leftSlide").setStyle({
			height: $("mappa").getHeight() + 'px',
			left: (- $("leftSlide").getWidth() + $("slideArrow").getWidth() - 1) + 'px',
			top: $("barraHeader").getHeight() + "px"
		});
	} else {
		$("leftSlide").setStyle({
			height: $("mappa").getHeight() + 'px',
			left: '0px',
			top: $("barraHeader").getHeight() + "px"
		});
	}
	
	$("leftPane").setStyle({
		height: $("leftSlide").getHeight() + 'px',
		width: ($("leftSlide").getWidth() - $("slideArrow").getWidth()) + 'px'
	});
	
	$("slideArrow").setStyle({
		height: $("leftSlide").getHeight() + 'px',
		background: "url(../img/rightArrow.png) no-repeat center"
	});
	
	$("blackCourtin").setStyle( {
		width: width + 'px',
		height: height + 'px',
		"opacity" : '0.7'
	} );
	
//	$("layout").setStyle({ 
//		top: ((height - $("layout").getHeight())/2) +"px",
//		left: ((width - $("layout").getWidth())/2) +"px",
//		position: 'absolute'
//	});

	centerForm("layout");
	centerForm("aboutBox");
	centerForm("optionsBox");
	centerForm("netErrorBox");
	
}

function responseMarkerClick(marker, type, tappa){

	var htmlinside;
	var obj;
	
	if (type == TOUR) {
		obj = curMilestones[tappa];
		
		htmlinside = '<div style="background-color: #FFFFFF; padding:10px; font-size:14px; color:#000000;"> ' +
						'<div style="float:left; padding-left:10px; padding-right:10px;"><a href="' + obj.flickr_url + '" target="_blank"><img id="coverImg" src="' + obj.photo_url + '" /></a></div>' +
						'<div stule="font-size:16px;"><b>' + obj.title + '</b></div>' +
						'<div><b>Luogo:</b> ' + obj.location + '</div>' +
						'<div><b>Data Inizio:</b> ' + obj.dtstart + '</div>' +
						'<div>' + obj.description + '</div>' +
						'</div>';

						//'<div><b>Data Fine:</b> ' + obj.dtend + '</div>' +

	} else if (type == RANDOM) {
		obj = curRandoms[tappa];
	
		htmlinside = '<div style="background-color: #FFFFFF; padding:10px; font-size:14px; color:#000000;"> ' +
						'<div style="padding-left:10px; padding-right:10px;"><a href="' + obj.url_flickr + '" target="_blank"><img src="' + obj.url_m + '" /></a></div>' +
						'<div stule="font-size:16px;"><b>' + obj.title + '</b></div>' +
						'<div><b>Luogo:</b> ' + obj.location + '</div>' +
						'<div><b>Scattata da: </b><a href="' + obj.url_owner + '" target="_blank">' + obj.owner + '</a></div>' +
					'</div>';


/*		htmlinside = '<div style="background-color: #FFFFFF; padding:10px; font-size:14px; color:#000000;"> ' +
						'<div style="padding-left:10px; padding-right:10px;">' +
						'<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"width="500" height="500" ><param name="movie" value="http://www.britepic.com/britepic.swf"> <param name="FlashVars" value="width=500&height=500&keywords=music,lastfm&show_ads=1&show_menu=1&href=&id=674510&src=' + obj.url_m + '"> <param name="allowscriptaccess" value="always"> <param name="wmode" value="transparent"> <embed src="http://www.britepic.com/britepic.swf" flashvars="width=500&height=500&keywords=music,lastfm&show_ads=1&show_menu=1&href=&id=674510&src=' + obj.url_m +'" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" type="application/x-shockwave-flash"width="500" height="500" ></embed></object>' +
						'<div stule="font-size:16px;"><b>' + obj.title + '</b></div>' +
						'<div><b>Luogo:</b> ' + obj.location + '</div>' +
						'<div><b>Scattata da: </b><a href="' + obj.url_owner + '" target="_blank">' + obj.owner + '</a></div>' +
					'</div>';
*/					
					
/*		htmlinside = '<script>' +
					' britepic_id="674510";' +
					' britepic_src="' + obj.flickr_url + '";' +
					'</script>'
					'<script src="http://www.britepic.com/britepic.js"></script>' +
					'<noscript></noscript>';
	*/				
	}
				
	marker.openInfoWindowHtml(htmlinside);

}


function prepLayout(){

	periodicExe = new PeriodicalExecuter(updateSong, POLLTIME); 
	updateSong();
	
	getFriends();
	getNeighbours();
	updateFriends();
	updateNeighbours();
	
	periodicOthers = new PeriodicalExecuter(updateOthers, POLLOTHERS);
	
	setBusyMessage("Fatto. Buon Divertimento!");
	$("wheel").toggle();
	window.setTimeout('hideUsername()', 500);

}

function updateOthers() {
//	getFriends();
//	getNeighbours();
//	updateFriends();
//	updateNeighbours();
}

function ajaxcheck() {

    hideShowUsername(false);
	
	var url = 'lastfm/existUser.php?username=' + encodeURIComponent($("username").value); 
 
 	var successo = function(transport) { 
						if (transport.responseText.match("false")) {
							$("wheel").toggle();
							setBusyMessage("Utente inesistente!");
							window.setTimeout('hideShowUsername(true); $("wheel").toggle();', 2500);
						} else {
							setBusyMessage("Caricamento dati...");
							username = $("username").value;
							window.setTimeout('clearAll(); prepLayout();', 100);
						}
					} 
	
	setBusyMessage("Validazione nome utente...");
	doQuery(url, successo);
	
	return false;
}

function centerForm(formname){
	
	var dime = getDimensions();
	var width = dime[0];
	var height = dime[1];

	$(formname).setStyle({ 
		top: ((height - $(formname).getHeight())/2) +"px",
		left: ((width - $(formname).getWidth())/2) +"px",
		position: 'absolute'
	});
	
}

function showForm(formname) {
	
	//Esegui l'azione beforeShow
	eval( $(formname).readAttribute('beforeShow') );

	showCourtin();
	Effect.Appear(formname);

}

function hideForm(formname) {
	
	Effect.Fade(formname, { afterFinish: hideCourtin });
	
}

function showSongs() {
	
	$("lblArtist").update(curArtist);
	$("lblTrack").update(curTrack);
	$("lblArtist2").update(curArtist);
	$("lblTrack2").update(curTrack);
	
}

function clearAll() {
	noSong();
	showSongs();
	setDrawerContent(TABARTIST,"");
	setDrawerContent(TABFRIENDS,"");
	setDrawerContent(TABNEIGH,"");
}

function logout() {
	clearAll();	
	periodicExe.stop();
	periodicOthers.stop();

	$("layout").show();
	$("wheel").show();
	showCourtin();
	hideShowUsername(true);
}

function onMapViewChanged(lat, lon, zoomlevel)
{

}