var natale = false;

var canvas;

var delta = [ 0, 0 ];
var stage = [ window.screenX, window.screenY, window.innerWidth, window.innerHeight ];
getBrowserDimensions();

var debug = false;
var slow = false;

var palla0_a = ['#61e025','#3b9a16','#369013'];
var palla1_a = ['#45eb60','#45eb60','#2ec940'];
var palla2_a = ['#beb312','#968d0e','#5f5a07'];
var palla3_a = ['#5e9d18','#8bd324','#497e12'];
var palla4_a = ['#49b61c','#64e526','#2b7710'];
var palla5_a = ['#51b4da','#2d657a','#27596a'];

var palla0_b = ['#2676de','#164a99','#123f85'];
var palla1_b = ['#5349ee','#3c35e4','#2e2fc8'];
var palla2_b = ['#14d1af','#0e9880','#0a7b68'];
var palla3_b = ['#1c7eaf','#2399cd','#166b9d'];
var palla4_b = ['#2c8afe','#1851a6','#103978'];
var palla5_b = ['#a4dc52','#5b7a2d','#4e6826'];

var palla0_c = ['#e28f26','#9f6917','#936013'];
var palla1_c = ['#e5ef4b','#c1cb2b','#c0be2c'];
var palla2_c = ['#d11537','#9c0f27','#7c0a1d'];
var palla3_c = ['#e75e28','#a14919','#783811'];
var palla4_c = ['#fea02c','#9f6917','#764e10'];
var palla5_c = ['#864fd7','#513082','#41276a'];

var palla_n = ['#d42f2f','#961919','#751414'];

var set_a = [palla0_a,palla1_a,palla2_a,palla3_a,palla4_a,palla5_a];
var set_b = [palla0_b,palla1_b,palla2_b,palla3_b,palla4_b,palla5_b];
var set_c = [palla0_c,palla1_c,palla2_c,palla3_c,palla4_c,palla5_c];
var set_n = [palla_n,palla_n,palla_n,palla_n,palla_n,palla_n];

var set_colori = [set_a,set_b,set_c,set_n];

var sfondo=-1;
var set_scelto = 0;
if (Modernizr.canvas) 
{
	if(natale)
	{
		document.getElementById('sfondo').style.background = "url('img/sfondo_natale.jpg')";
		set_scelto = 3;
	}
	else
	{
		while(sfondo>2 || sfondo<0)
		{
			sfondo = (Math.random() * 10 >> 0);
		}
		if(sfondo==0)
		{
			document.getElementById('sfondo').style.background = "url('img/sfondo_a.jpg')";
			set_scelto = 0;
		}
		if(sfondo==1)
		{
			document.getElementById('sfondo').style.background = "url('img/sfondo_b.jpg')";
			set_scelto = 1;
		}
		if(sfondo==2)
		{
			document.getElementById('sfondo').style.background = "url('img/sfondo_c.jpg')";
			set_scelto = 2;
		}
	}
	document.getElementById('sfondo').style.backgroundPosition="center"
}
else
{
	if(natale)
		document.getElementById('sfondo').style.background = "url('img/sfondo_natale.jpg')";
	else document.getElementById('sfondo').style.background = "url('img/sfondo_a.jpg')";
}
var combo="";

jQuery(document).ready(domo);

var startx = [0, 0, 0, 0, 0, 0, 0];
var starty = [0, 0, 0, 0, 0, 0, 0];

var movx = [0, 0, 0, 0, 0, 0, 0];
var movy = [0, 0, 0, 0, 0, 0, 0];

var endx = [0, 0, 0, 0, 0, 0, 0];
var endy = [0, 0, 0, 0, 0, 0, 0];

var themes = [ [ "#000000", "#4444AA", "#44AA44", "#FFFF33", "#AA4444",  "#222222" ] ];

var theme;

var worldAABB, world, iterations = 1, timeStep = 1 / 20;

var walls = [];
var wall_thickness = 200;
var wallsSetted = false;

var bodies, elements, text;

var createMode = false;
var destroyMode = false;

var isMouseDown = false;
var mouseJoint;
var mouseX = 0;
var mouseY = 0;

var PI2 = Math.PI * 2;

var timeOfLastTouch = 0;

if (Modernizr.canvas) 
{
	//document.getElementById('infomsg').outerHTML='<span style="font-size: 13pt; z-index: 5;"><b>Prova a trascinare il contenuto della pagina agendo sul logo dell\'MCM mentre tieni premuto il pulsante del mouse e poi rilascialo!</b></span>';
	init();
	play();
} 
else
{
	document.getElementsByTagName('body')[0].style.overflow="auto";
	//alert(document.getElementsByTagName('body')[0].style.overflow);
}


function init() {

	canvas = document.getElementById( 'canvas' );

	
	// init box2d
	window.onorientationchange = detectIPadOrientation; 
	canvas.onselectstart=new Function("return false");

	worldAABB = new b2AABB();
	worldAABB.minVertex.Set( -200, -200 );
	worldAABB.maxVertex.Set( screen.width + 200, screen.height + 200 );

	world = new b2World( worldAABB, new b2Vec2( 0, 0 ), true );

	//setWalls();
	reset();
}

function play() {
 
var foo = new Date();
var l = 0;
for(l=0; l<100; l++)
{
	loop();
}
var bar = new Date();
var baz = new Date();
baz.setTime(bar.getTime() - foo.getTime());
if(baz.getMilliseconds()>150)
	slow=true;
//alert('Slow: '+slow);
//alert(baz.getMilliseconds() + " ms passati tra 100 passaggi");

if(!slow || DetectIphoneOrIpod() || DetectAndroid() )
	setInterval( "loop();", 1000 / 40 );
	
}

function reset() {

	var i;

	if ( bodies ) {

		for ( i = 0; i < bodies.length; i++ ) {

			var body = bodies[ i ]
			
			canvas.removeChild( body.GetUserData().element );
			world.DestroyBody( body );
			body = null;
		}
	}

	// color theme
	theme = themes[ Math.random() * themes.length >> 0 ];
	document.body.style[ 'backgroundColor' ] = theme[ 0 ];

	bodies = [];
	elements = [];

	createInstructions();

}

//

function createInstructions() {

	var size = 220;
	var smallsize = 30;
	
	var element = document.createElement( 'div' );
	element.width = size;
	element.height = size;	
	element.style.position = 'absolute';
	element.style.left = -200 + 'px';
	element.style.top = -200 + 'px';
	element.style.cursor = "default";
	element.style.zIndex = 3;
	element.id='movelement';

	canvas.appendChild(element);
	elements.push( element );

	var circle = document.createElement( 'canvas' );
	circle.width = size;
	circle.height = size;

	var graphics = circle.getContext( '2d' );

	graphics.fillStyle = set_colori[set_scelto][0][2];
	graphics.beginPath();
	graphics.arc( size * .5, size * .5, size * .5, 0, PI2, true );
	graphics.closePath();
	graphics.fill();
	
	grad = graphics.createRadialGradient( (size * .5) - 100,(size * .5) - 100,0,(size * .5) - 100,(size * .5) - 100,200 ); 
	grad.addColorStop(0, set_colori[set_scelto][0][1]);
	grad.addColorStop(1, set_colori[set_scelto][0][2]);
	graphics.fillStyle = grad;
	graphics.fill();
	
	graphics.fillStyle = set_colori[set_scelto][0][0];
	graphics.beginPath();
	graphics.arc( size * .5, size * .5, size * .47, 0, PI2, true );
	graphics.closePath();
	graphics.fill();
	
	grad = graphics.createRadialGradient( (size * .5) - 100,(size * .5) - 100,0,(size * .5) - 100,(size * .5) - 100,200 ); 
	grad.addColorStop(0, set_colori[set_scelto][0][0]);
	grad.addColorStop(1, set_colori[set_scelto][0][1]);
	graphics.fillStyle = grad;
	graphics.fill();
	
	element.appendChild( circle );
	
	//NUOVO CANVAS PER LE BALLS
	var logoimg = document.createElement( 'img' );
	logoimg.onSelectStart = null;
	if(natale)
		logoimg.src='img/logo_natale.png';
	else logoimg.src='img/logo.png';
	logoimg.border='0';
	logoimg.style.border='none';
	logoimg.style.border = '0';
	logoimg.style.position = 'absolute';
	logoimg.style.left = '0px';
	logoimg.style.top = '0px';
	logoimg.width = size;
	logoimg.height = size;
	logoimg.style.zIndex = 4;
	logoimg.id = 'img_logo';
	logoimg.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						setTimeout("invia_pagina('must_home.htm');",1500);
						};
	element.style.cursor = "hand";
	element.style.cursor = "pointer";
	element.appendChild(logoimg);
	
	//FINE DISEGNI
	
	var b2body = new b2BodyDef();

	var circle = new b2CircleDef();
	circle.radius = size / 2;
	circle.density = 1;
	circle.friction = 0.3;
	circle.restitution = 0.3;
	b2body.AddShape(circle);
	b2body.userData = {element: element};

	startx[0]=stage[2]*0.5;
	starty[0]=stage[3]*0.52;
	movx[0]=2;
	movy[0]=2;
	
	b2body.position.Set( startx[0], starty[0] );
	b2body.linearVelocity.Set( movx[0], movy[0] );
	bodies.push( world.CreateBody(b2body) );	
	
	startx[1]=stage[2]*0.7;
	startx[2]=stage[2]*0.85;
	startx[3]=stage[2]*0.3;
	startx[4]=stage[2]*0.2;
	startx[5]=stage[2]*0.8;
	
	starty[1]=stage[3]*0.15;
	starty[2]=stage[3]*0.5;
	starty[3]=stage[3]*0.4;
	starty[4]=stage[3]*0.85;
	starty[5]=stage[3]*0.9;
	
	movx[1]=3;
	movx[2]=12;
	movx[3]=4;
	movx[4]=6;
	movx[5]=4;

	
	movy[1]=7;
	movy[2]=3;
	movy[3]=4;
	movy[4]=6;
	movy[5]=3;

	createBall(startx[1],starty[1],movx[1],movy[1],set_colori[set_scelto][1][0],set_colori[set_scelto][1][1],set_colori[set_scelto][1][2],"1");
	createBall(startx[2],starty[2],movx[2],movy[2],set_colori[set_scelto][2][0],set_colori[set_scelto][2][1],set_colori[set_scelto][2][2],"2");
	createBall(startx[3],starty[3],movx[3],movy[3],set_colori[set_scelto][3][0],set_colori[set_scelto][3][1],set_colori[set_scelto][3][2],"3");
	createBall(startx[4],starty[4],movx[4],movy[4],set_colori[set_scelto][4][0],set_colori[set_scelto][4][1],set_colori[set_scelto][4][2],"4");
	createBall(startx[5],starty[5],movx[5],movy[5],set_colori[set_scelto][5][0],set_colori[set_scelto][5][1],set_colori[set_scelto][5][2],"5");
	
	createLines(startx[0],starty[0]-40,startx[1]-100,starty[1]-30,set_colori[set_scelto][1][2],"1");
	createLines(startx[0],starty[0],startx[2]-60,starty[2]-20,set_colori[set_scelto][2][2],"2",(startx[0]+startx[2]-60)/2,starty[0]+100);
	createLines(startx[0],starty[0]-30,startx[3]-80,starty[3]-20,set_colori[set_scelto][3][2],"3");
	createLines(startx[0],starty[0]+40,startx[4]-100,starty[4]-30,set_colori[set_scelto][4][2],"4");
	createLines(startx[0]-40,starty[0],startx[5]-80,starty[5]-20,set_colori[set_scelto][5][2],"5");


	endx[0]=startx[0]+(movx[0]*4);
	endx[1]=startx[1]+(movx[1]*3);
	endx[2]=startx[2]+(movx[2]*3);
	endx[3]=startx[3]+(movx[3]*4);
	endx[4]=startx[4]+(movx[4]*4);
	endx[5]=startx[5]+(movx[5]*2);

	
	endy[0]=starty[0]+(movy[0]*2);
	endy[1]=starty[1]+(movy[1]*2);
	endy[2]=starty[2]+(movy[2]*6);
	endy[3]=starty[3]+(movy[3]*5);
	endy[4]=starty[4]+(movy[4]*3);
	endy[5]=starty[5]+(movy[5]*5);

}

//
function createLines(x1,y1,x2,y2,color,id,xm,ym,xm1,ym1)
{
	var element = document.createElement( 'canvas' );
	element.onSelectStart = null;
	element.style.position = 'absolute';
	element.style.left = '0px';
	element.style.top = '0px';
	element.style.zIndex = 2;

	element.width = stage[2];
	element.height = stage[3];

	var context = element.getContext( '2d' );
	
	// Add the smile					
	context.strokeStyle = color;
	context.beginPath();
	context.moveTo(x1,y1);
	if(typeof xm == 'undefined')
	{
		if(id=="4" || id=="3" || id=="1" || id=="2")
		{
			context.quadraticCurveTo(x2,y1,x2,y2);	
			context.quadraticCurveTo(x2,y1,x1,y1);	
		}
		else
		{
			context.quadraticCurveTo(x1,y2,x2,y2);	
			context.quadraticCurveTo(x1,y2,x1,y1);	
		}
	}
	else
	{
		context.quadraticCurveTo(x1,ym,xm,ym);	
		context.quadraticCurveTo(x2,ym,x2,y2);	
		context.quadraticCurveTo(x2,ym,xm,ym);	
		context.quadraticCurveTo(x1,ym,x1,y1);
	}
	context.closePath();
	context.stroke();

	canvas.appendChild(element);
	elements.push( element );
	
	var b2body = new b2BodyDef();
	b2body.userData = {element: element};
	b2body.position.Set( stage[2]/2, stage[3]/2 );
	b2body.linearVelocity.Set( 0, 0 );
	bodies.push( world.CreateBody(b2body) );
}

function invia_pagina(indirizzo)
{
	document.location=indirizzo;
}

function createBall( x, y , velx, vely, color, sfumato, bordercolor, id) {
	var mainelement = document.createElement( 'div' );	
	mainelement.style.position = 'absolute';
	
	//var x = x || Math.random() * stage[2];
	//var y = y || Math.random() * -200;
	
	//NUOVO CANVAS PER LE BALLS
	var element = document.createElement( 'canvas' );
	element.onSelectStart = null;
	element.style.position = 'absolute';
	element.style.left = '-150px';
	element.style.top = '-100px';
	
	var size=0;
	while(size>=30 || size<=20)
	{
		size = (Math.random() * 20 >> 0) + 25;
	}
	if(id=="4")
	{
		size=25;
		element.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_home_uk.htm');",1500);
						else setTimeout("invia_pagina('must_home.htm');",1500);
						};
	}
	if(id=="3")
	{
		size=41;
		element.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_dove_siamo.htm');",1500);
						else setTimeout("invia_pagina('must_dove_siamo_uk.htm');",1500);
						};
	}
	if(id=="2")
	{
		size=46;
		element.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_che_cosa.htm');",1500);
						else setTimeout("invia_pagina('must_che_cosa_uk.htm');",1500);
						};
	}
	if(id=="5")
	{
		size=43;
		element.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_contatti.htm');",1500);
						else setTimeout("invia_pagina('must_contatti_uk.htm');",1500);
						};
	}
	if(id=="1")
	{
		element.onclick = function(event){
						vis_pannelli('aNews');
						};
	}
	element.width = size*3;
	element.height = size*3;
	element.style.zIndex = 3;
	element.id = 'ball_'+color;
	element.style.cursor = "hand";
	element.style.cursor = "pointer";					
	var ctx3 = element.getContext( '2d' );
	
	ctx3.scale(3,3);
	ctx3.fillStyle = bordercolor;
	ctx3.beginPath();
	ctx3.arc( size * .5, size * .5, size * .5, 0, PI2, true );
	ctx3.closePath();
	ctx3.fill();
	ctx3.fillStyle = color;
	ctx3.beginPath();
	ctx3.arc( size * .5, size * .5, size * .45, 0, PI2, true );
	ctx3.closePath();
	ctx3.fill();
	grad = ctx3.createRadialGradient( 0 ,0 ,size*0.5,(size * .5) ,(size * .5) ,size ); 
	grad.addColorStop(0, color);
	grad.addColorStop(1, sfumato);
	ctx3.fillStyle = grad;
	ctx3.fill();

	mainelement.appendChild(element);
	canvas.appendChild(mainelement);
	elements.push( mainelement );
	var b2body = new b2BodyDef();

	var circle = new b2CircleDef();
	circle.radius = 40;
	circle.density = 1;
	circle.friction = 0.3;
	circle.restitution = 0.3;
	b2body.AddShape(circle);
	b2body.userData = {mainelement: mainelement};

	b2body.position.Set( x, y );
	b2body.linearVelocity.Set( velx, vely );
	bodies.push( world.CreateBody(b2body) );

	//testo
	text = document.createElement( 'div' );
	text.onSelectStart = null;
	if(id=="3")
	{
		if(lang=="it")
			text.innerHTML = '<b>dove<BR>siamo</b>';
		else text.innerHTML = '<b>where<BR>we are</b>';
		text.style.width = '150px';
		text.style.height = '40px';
		text.style.left = '-163px';
		text.style.top = '-57px';
		text.setAttribute("class", "shadow");
		text.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_dove_siamo.htm');",1500);
						else setTimeout("invia_pagina('must_dove_siamo_uk.htm');",1500);
						};
		text.style.cursor = "hand";
		text.style.cursor = "pointer";
	}
	if(id=="2")
	{
		if(lang=="it")
			text.innerHTML = '<b>che cosa<BR>facciamo</b>';
		else text.innerHTML = '<b>what<BR>we do</b>';
		text.style.width = '150px';
		text.style.height = '40px';
		text.style.left = '-155px';
		text.style.top = '-50px';
		text.setAttribute("class", "shadow");
		text.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_che_cosa.htm');",1500);
						else setTimeout("invia_pagina('must_che_cosa_uk.htm');",1500);
						};
		text.style.cursor = "hand";
		text.style.cursor = "pointer";
	}
	if(id=="5")
	{
		if(lang=="it")
			text.innerHTML = '<b>contatti</b>';
		else text.innerHTML = '<b>contacts</b>';
		text.style.width = '150px';
		text.style.height = '40px';
		text.style.left = '-160px';
		text.style.top = '-45px';
		text.setAttribute("class", "shadow");
		text.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_contatti.htm');",1500);
						else setTimeout("invia_pagina('must_contatti_uk.htm');",1500);
						};
		text.style.cursor = "hand";
		text.style.cursor = "pointer";
	}
	if(id=="4")
	{
		if(lang=="it")
			text.innerHTML = '<img src="img/flag_eng.png" border="0" title="English version" alt="English version" />';
		else text.innerHTML = '<img src="img/flag_ita.png" border="0" title="Versione italiana" alt="Versione italiana" />';
		text.style.left = '-148px';
		text.style.top = '-98px';
		text.onclick = function(event){
						$('#sfondo').fadeOut(1500);
						$('#canvas').fadeOut(1500);
						if(lang=="it")
							setTimeout("invia_pagina('must_home_uk.htm');",1500);
						else setTimeout("invia_pagina('must_home.htm');",1500);
						};
		text.style.cursor = "hand";
		text.style.cursor = "pointer";
	}
	if(id=="1")
	{
		text.innerHTML = '<img src="img/news.png" border="0" title"NEWS" alt="NEWS" />';
		text.style.left = '-140px';
		text.style.top = '-84px';
		text.onclick = function(event){
						vis_pannelli('aNews');
						};
		text.style.cursor = "hand";
		text.style.cursor = "pointer";
	}
	text.style.color = "#FFFFFF";
	text.style.position = 'absolute';

	text.style.fontFamily = 'Verdana';
	text.style.textAlign = 'center';
	text.style.zIndex = '5';
	mainelement.appendChild(text);
	
	var maxsat=6;
	
	if(!DetectIphoneOrIpod() && !DetectAndroid() )
	{
		//NUOVO CANVAS PER I SATELLITI
		var celement = document.createElement( 'canvas' );
		celement.onSelectStart = null;
		celement.style.position = 'absolute';
		celement.style.left = '-280px';
		celement.style.top = '-200px';
		celement.width = 400;
		celement.height = 350;
		celement.style.zIndex = 2;
			
		var ctx3 = celement.getContext( '2d' );
		ctx3.scale(2,2);
		var xpos=[20,180,120,40,60,160,40,140,180,160]; 
		var ypos=[40,105,40,60,120,20,120,110,100,40];

		

		for(var h=0; h<maxsat; h++)
		{	
			var SUBsize=0; 
			var xindice=-1; 
			var yindice=-1;
			while(SUBsize>=70 || SUBsize<=30)
			{
				SUBsize = (Math.random() * 40 >> 0) + 20;
			}
			while( xindice>5 || xindice<0 || xpos[xindice]==0 )
			{
				xindice = (Math.random()*10 >>0);
			}
			while( yindice>5 || yindice<0 || ypos[yindice]==0)
			{
				yindice = (Math.random()*10 >>0);
			}
			
			if(natale)
			{
				if(h<=1)
					ctx3.fillStyle = '#b8bcb3';
				if(h>=2 && h<=3)
					ctx3.fillStyle = '#cdd4d6';
				if(h>=4 && h<=5)
					ctx3.fillStyle = '#e3e6e7';
			}
			else ctx3.fillStyle = set_colori[set_scelto][h][0];

			ctx3.beginPath();
			//EDIT!!!!
			ctx3.arc( xpos[xindice] , ypos[yindice] , SUBsize * .2, 0, PI2, true );
			xpos[xindice]=0;
			ypos[yindice]=0;
			ctx3.closePath();
			ctx3.fill();	
		}
		mainelement.appendChild(celement);
	}
	
	if(debug)
	{
	//Numero
	text1 = document.createElement( 'div' );
	text1.onSelectStart = null;
	text1.innerHTML = id;
	text1.style.color = "#FFFFFF";
	text1.style.position = 'absolute';
	text1.style.left = '0px';
	text1.style.top = '0px';
	text1.style.width = '10px';
	text1.style.height = '10px';
	text1.style.fontFamily = 'Georgia';
	text1.style.textAlign = 'center';
	text1.style.zIndex = '10';
	mainelement.appendChild(text1);
	text1.style.left = -text1.clientWidth + 'px';
	text1.style.top = - text1.clientHeight +'px';	
	}
}
//

function loop() {

	if(!DetectIphoneOrIpod())
	{
		if (getBrowserDimensions()) {
			document.location.href='must_home.htm';
		}
	}

	delta[0] += (0 - delta[0]) * .5;
	delta[1] += (0 - delta[1]) * .5;

	world.m_gravity.x = 0;
	world.m_gravity.y = 0;

	world.Step(timeStep, iterations);

	for (i = 0; i < bodies.length; i++) {

		var body = bodies[i];
		var element = elements[i];
		
		element.style.left = (body.m_position0.x - (element.width >> 1)) + 'px';
		element.style.top = (body.m_position0.y - (element.height >> 1)) + 'px';
		
		for (j = 0; j < 7; j++) {
			if(body.m_position0.x>=endx[j] && j==(i) && body.m_linearVelocity.x>0)	{
				var v = new b2Vec2(-body.m_linearVelocity.x,body.m_linearVelocity.y);
				body.SetLinearVelocity(v);
			}
			if(body.m_position0.y>=endy[j] && j==(i) && body.m_linearVelocity.y>0)	{
				var v = new b2Vec2(body.m_linearVelocity.x,-body.m_linearVelocity.y);
				body.SetLinearVelocity(v);
			}
			if(body.m_position0.x<=startx[j] && j==(i) && body.m_linearVelocity.x<0)	{
				var v = new b2Vec2(-body.m_linearVelocity.x,body.m_linearVelocity.y);
				body.SetLinearVelocity(v);
			}
			if(body.m_position0.y<=starty[j] && j==(i) && body.m_linearVelocity.y<0)	{
				var v = new b2Vec2(body.m_linearVelocity.x,-body.m_linearVelocity.y);
				body.SetLinearVelocity(v);
			}
		}
	}
	
}

function getBodyAtMouse() {

	// Make a small box.
	var mousePVec = new b2Vec2();
	mousePVec.Set(mouseX, mouseY);

	var aabb = new b2AABB();
	aabb.minVertex.Set(mouseX - 1, mouseY - 1);
	aabb.maxVertex.Set(mouseX + 1, mouseY + 1);

	// Query the world for overlapping shapes.
	var k_maxCount = 10;
	var shapes = new Array();
	var count = world.Query(aabb, shapes, k_maxCount);
	var body = null;

	for (var i = 0; i < count; ++i) {

		if (shapes[i].m_body.IsStatic() == false) {

			if ( shapes[i].TestPoint(mousePVec) ) {

				body = shapes[i].m_body;
				break;

			}

		}

	}

	return body;

}

// BROWSER DIMENSIONS
 
 function detectIPadOrientation () {  
  
	$('#sfondo').fadeOut(100);
	$('#canvas').fadeOut(100);
	document.location.href='must_home.htm';
	/*
    if ( orientation == 0 ) {  
     alert ('Portrait Mode, Home Button bottom');  
    }  
    else if ( orientation == 90 ) {  
     alert ('Landscape Mode, Home Button right');  
    }  
    else if ( orientation == -90 ) {  
     alert ('Landscape Mode, Home Button left');  
    }  
    else if ( orientation == 180 ) {  
     alert ('Portrait Mode, Home Button top');  
    } */ 
 }  

function getBrowserDimensions() {
	
	var changed = false;

	if (stage[0] != window.screenX) {

		delta[0] = (window.screenX - stage[0]) * 50;
		stage[0] = window.screenX;
		//changed = true;

	}

	if (stage[1] != window.screenY) {

		delta[1] = (window.screenY - stage[1]) * 50;
		stage[1] = window.screenY;
		//changed = true;

	}

	if ( (stage[2] != window.innerWidth && window.innerWidth>700 && window.innerHeight>500)) 
	{
		stage[2] = window.innerWidth;
		document.getElementsByTagName('body')[0].style.overflow="hidden";
		changed = true;
	}
	if(window.innerWidth<700 && stage[2]!=700 )
	{
		stage[2]=700;
		stage[3]=500;
		document.getElementsByTagName('body')[0].style.overflow="scroll";
		document.getElementsByTagName('body')[0].style.width="740px";
		document.getElementsByTagName('body')[0].style.height="620px";
		changed = true;
	}

	
	if ( (stage[3] != window.innerHeight && window.innerHeight>500 && window.innerWidth>700)) 
	{
		stage[3] = window.innerHeight;
		document.getElementsByTagName('body')[0].style.overflow="hidden";
		changed = true;
	}
	if(window.innerHeight<500 && stage[3]!=500)
	{
		stage[3]=500;
		stage[2]=700;
		document.getElementsByTagName('body')[0].style.overflow="scroll";
		document.getElementsByTagName('body')[0].style.height="620px";
		changed = true;
	}

	return changed;

}


