window_width = parseInt(window.innerWidth);
window_height = parseInt(window.innerHeight);
if (navigator.userAgent.indexOf("MSIE") >= 0) {
	window_width = parseInt(document.documentElement.clientWidth);
	window_height = parseInt(document.documentElement.clientHeight);
}
window_width = Math.max(window_width, min_window_width);

var child_j; // JQuery del child - istanziato dal child on ready.

var tendeOn = true;
var tendeEnabled = true;

var tende_top_height;
var tende_left_width;
var top_fuori;
var top_dentro;
var left_fuori;
var left_dentro;
var extra_left_dentro;
var margin_tende;
var margin_poltrone;
var tende_sx_width;
var menu_width;
var menu_top_dentro;
var menu_top_fuori;
var tabs_height;
var poltrone_height_dentro;
var credits_width;
var posizione_tende;
var posizione_tende_goto;
var posizione_tende_time_refresh;
var time_to_togli_tende;

var top_width;
var	bottom_width;
var	bottom_left;
var	bottom_height_final;
var	bottom_bottom;
var	menu_left;
	
var scrollbar1 = '';
var scrollbar2 = '';

var tende_sx;
var tende_top;
var tende_dx;
var menu_top;
var poltrone;
var credits;
var wrapper;
var main_bottom;
var main_bottom_inner;
var main_bottom_table;
var main_bottom_frame;

// PARAMETRI
tende_top_height = 200; // altezza del tendone sopra
tende_left_width = 200; // larghezza della tenda laterale
top_fuori = -200; // Top, della tenda quando sta fuori
top_dentro = -105; // Top, della tenda quando sta dentro
left_fuori = -170; // Left, della tenda quando sta fuori
left_dentro = -80; // Left, della tenda quando sta dentro
extra_left_dentro = -20; // per correzioni quando lo schermo è troppo stretto
margin_tende = 7; // Margin tra main_contents e tende
margin_poltrone = -10 // Margin tra main_contents e poltrone
tende_sx_width = 624 + left_dentro; // Larghezza totale del div col tendone laterlale (la png laterale)
//menu_width = 968;
menu_width = 868;
menu_top_dentro = 5;
menu_top_fuori = -60;
tabs_height = 36;
poltrone_height_dentro = 55;
credits_width = 770;

posizione_tende = 0; // Posizione attuale delle tende; 0=tende fuori; 1=tende dentro
posizione_tende_goto = 0; // Posizione obiettivo delle tende (ogni 5 secondi verifica: se non è stato mosso il mouse diventa 0)
posizione_tende_time_refresh = 100;
time_to_togli_tende = 0; // in millisecondi

// Imposta la posizione passata
// 0 < posizione < 1
function setPosizioneTende(posizione) {


	sx_left = parseInt(Math.floor(left_fuori + posizione * (left_dentro - left_fuori)));
	sx_top = parseInt(top_fuori + posizione * (top_dentro - top_fuori));
	top_left = tende_sx_width - left_dentro + sx_left;
	top_width = parseInt(Math.ceil(Math.max(window_width - 2 * top_left, 0)));
	top_height = tende_top_height + sx_top;
	tende_sx_width_pos = parseInt(Math.ceil(Math.min(tende_sx_width + left_fuori - left_dentro - posizione * (left_fuori - left_dentro), window_width / 2)));
	dx_left = window_width - (tende_sx_width_pos);
	//dx_left = parseInt(Math.floor(Math.max(window_width - 2 * top_left, 0))) + parseInt(Math.floor(Math.min(tende_sx_width + left_fuori - left_dentro - posizione * (left_fuori - left_dentro), window_width / 2)));
	
	//bottom_width = window_width - 2 * (left_dentro + tende_left_width + margin_tende);
	//bottom_left = left_dentro + tende_left_width + margin_tende;
	//menu_left = (window_width - menu_width) / 2;
	//bottom_height = parseInt(posizione * (window_height - (top_dentro + tende_top_height + margin_tende) ));
	
	//tende_sx.style.backgroundPosition = (sx_left) + 'px ' + sx_top + 'px';
	//tende_dx.style.backgroundPosition = (left_dentro - sx_left) + 'px ' + sx_top + 'px';
	
	
	tende_top.style.width = top_width + 'px'; 
	tende_top.style.height = top_height + 'px';
	tende_top.style.left = top_left + 'px';
	
	tende_sx.style.width = tende_sx_width_pos + 'px';
	tende_dx.style.left = dx_left + 'px';
	tende_dx.style.width = tende_sx_width_pos + 'px';
	tende_sx.style.backgroundPosition = 'right ' + sx_top + 'px';
	tende_dx.style.backgroundPosition = '0px ' + sx_top + 'px';
	
	
	
	//menu_top.style.left = menu_left + 'px';
	menu_top.style.top = parseInt(menu_top_fuori + posizione * (menu_top_dentro - menu_top_fuori)) + 'px';



	

	//main_bottom.style.width = bottom_width + 'px';
	//main_bottom.style.left = bottom_left + 'px';
	
	//main_bottom.style.height = Math.max( bottom_height , 52 + tabs_height ) + 'px';
	
	if (posizione == 0) {
		//document.getElementById("main_contents").style.display = "";
		//$('#main_contents').hide('slow', function(){
			if (!tendeOn) {
				tende_sx.style.display = 'none';
				tende_top.style.display = 'none';
				tende_dx.style.display = 'none';
				menu_top.style.display = 'none';
				poltrone.style.display = 'none';
			}
		//});
		//$('#logo').fadeIn('slow');
		
	}

	//main_bottom_table.style.height = (Math.max(bottom_height - tabs_height - 10,0)) + 'px';
	//main_bottom_frame.style.height = (Math.max(bottom_height - tabs_height - 52,0)) + 'px';
	//main_bottom_inner.style.height = (Math.max(bottom_height - tabs_height - 52,0)) + 'px';

	
	if (poltrone !== null) poltrone.style.height = parseInt( posizione * poltrone_height_dentro ) + 'px';


	//document.getElementById('credits_middle').innerHTML = window_width;
}

function inizializzaDimensioniFisse() {
	/*
	tende_sx = document.getElementById('tende_sx');
	tende_top = document.getElementById('tende_top');
	tende_dx = document.getElementById('tende_dx');
	menu_top = document.getElementById('menu_top');
	poltrone = document.getElementById('poltrone');
	main_bottom = document.getElementById('main_contents');
	main_bottom_inner = document.getElementById('inner_main_contents');
	main_bottom_table = document.getElementById('tbl_main_contents');
	main_bottom_frame = document.getElementById('ifr_main_contents');
	*/
	
	//bottom_width = window_width - 2 * (left_dentro + tende_left_width + margin_tende);
	//bottom_left = left_dentro + tende_left_width + margin_tende;
	//bottom_height_final = window_height - (top_dentro + tende_top_height + margin_tende);
	bottom_width = 1120;
	bottom_left = parseInt((window_width - bottom_width) / 2);
	bottom_height_final = Math.min( 576 , window_height - (top_dentro + tende_top_height + margin_tende + poltrone_height_dentro + margin_poltrone ));
	bottom_bottom = poltrone_height_dentro + margin_poltrone + Math.max( 0 , (window_height - (bottom_height_final + top_dentro + tende_top_height + margin_tende + poltrone_height_dentro + margin_poltrone )) / 2 );
	menu_left = parseInt( (window_width - menu_width) / 2 );
	
	main_bottom.style.width = bottom_width + 'px';
	main_bottom.style.left = bottom_left + 'px';
	main_bottom.style.height = Math.max( bottom_height_final , 52 + tabs_height ) + 'px';
	main_bottom.style.bottom = bottom_bottom + 'px';
	//main_bottom.style.bottom = '0px';
	
	menu_top.style.left = menu_left + 'px';
	if (credits !== null) {
		credits.style.right = parseInt( (window_width - credits_width) / 2 ) + 'px';
	}
	
	main_bottom_table.style.height = (Math.max(bottom_height_final - tabs_height - 10,0)) + 'px';
	if (main_bottom_frame !== null) {
		main_bottom_frame.style.height = (Math.max(bottom_height_final - tabs_height - 52,0)) + 'px';
		main_bottom_inner.style.height = (Math.max(bottom_height_final - tabs_height - 52,0)) + 'px';
	}
	if (poltrone !== null) poltrone.style.width = window_width + 'px';
	if (wrapper !== null) wrapper.style.width = window_width + 'px';
}

function setObjectsTende() {
	tende_sx = document.getElementById('tende_sx');
	tende_top = document.getElementById('tende_top');
	tende_dx = document.getElementById('tende_dx');
	menu_top = document.getElementById('menu_top');
	poltrone = document.getElementById('poltrone');
	credits = document.getElementById('credits_left');
	wrapper = document.getElementById('wrapper');
	main_bottom = document.getElementById('main_contents');
	main_bottom_inner = document.getElementById('inner_main_contents');
	main_bottom_table = document.getElementById('tbl_main_contents');
	main_bottom_frame = document.getElementById('ifr_main_contents');	
}



// Cicla ogni "posizione_tende_time_refresh" millisecondi. 
// Porta le tende verso la posizione "posizione_tende_goto", fino al raggiungimento posizione_tende_goto.
function setMainContentMoveToSize() {
	if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
		// Eccezione: se iPad, tende dentro;
		posizione_tende = 1;
		setPosizioneTende(posizione_tende);
	} else if (posizione_tende > posizione_tende_goto) {
		posizione_tende -=  posizione_tende_time_refresh/1000;
		if (posizione_tende < 0) posizione_tende = 0;
		setPosizioneTende(posizione_tende);
		setTimeout("setMainContentMoveToSize()", posizione_tende_time_refresh);
	} else if (posizione_tende < posizione_tende_goto)  {
		posizione_tende +=  posizione_tende_time_refresh/1000;
		if (posizione_tende > 1) posizione_tende = 1;
		if (posizione_tende == posizione_tende_time_refresh/1000) {
			//$('#logo').fadeOut('slow');
			
			
			/*
			$('#main_contents').show('slow', function() {
				setPosizioneTende(posizione_tende);
				
				setTimeout("setMainContentMoveToSize()", posizione_tende_time_refresh);
			});
			
			*/
			
			//mainContentEnter();
			setPosizioneTende(posizione_tende);				
			setTimeout("setMainContentMoveToSize()", posizione_tende_time_refresh);
			
		} else {
			setPosizioneTende(posizione_tende);
			setTimeout("setMainContentMoveToSize()", posizione_tende_time_refresh);
		}
		
	}
}

function enterExitButton() {
	//alert('enter');
	//document.getElementById('exit_button').style.display = '';
	$('#exit_button').show('slow');
}

function richiamaTende() {
	if (!tendeEnabled) return;
	time_to_togli_tende = 5000;
	//time_to_togli_tende = 50000;
	if (posizione_tende_goto < 1) {
		posizione_tende_goto = 1;
		if (tendeOn) {
			//inizializzaDimensioniFisse();
			setMainContentMoveToSize();
		} else {
			enterExitButton();
		}
	}	
}
function setMainContentSize() {
	//alert('goto='+posizione_tende_goto+'; now='+posizione_tende+'; timeto='+time_to_togli_tende);
	if (time_to_togli_tende > 0) {
		time_to_togli_tende -= 1000;
	} else {
		posizione_tende_goto = 0;
		//inizializzaDimensioniFisse();
		setMainContentMoveToSize();
		if (!tendeOn) {
			//alert('exit');
			$('#exit_button').hide('slow');
		}
	}
	setTimeout("setMainContentSize()", 1000);
}

function disableRichiamoTende(time) {
	tendeEnabled = false;
	setTimeout("enableRichiamoTende()", time);
}

function enableRichiamoTende(time) {
	tendeEnabled = true;
}

function mainContentEnter() {
	$('#main_contents').slideDown('slow', function() {
		//bottom_height = parseInt(window_height - (top_dentro + tende_top_height + margin_tende) );
		
		/*
		$("#main_contents").animate({
			height: bottom_height + "px",
		}, 1500 );
		$("#tbl_main_contents").animate({
			height: (bottom_height - tabs_height - 10) + "px",
		}, 1500 );
		$("#ifr_main_contents").animate({
			height: (bottom_height - tabs_height - 52) + "px",
		}, 1500 );
		$("#inner_main_contents").animate({
			height: (bottom_height - tabs_height - 52) + "px",
		}, 1500 );
		*/
	});
}

function mainContentExit() {
	//bottom_height = parseInt(window_height - (top_dentro + tende_top_height + margin_tende) );
	
	/*
	$("#inner_main_contents").animate({
		height: "0px",
		}, 1500, function () {
			$("#ifr_main_contents").animate({
				height: "0px",
			}, 1500, function () {
				$("#tbl_main_contents").animate({
					height: "0px",
				}, 1500, function () {
					$("#main_contents").animate({
						height: (52 + tabs_height) + "px",
					}, 1500, function () {
						$('#main_contents').hide('slow');
					});
				} );
			
		});
	});
	*/
	$('#main_contents').slideUp('slow');
}
