var resize_Delay;

window.onload = resize;		//read size room div when the webpage loads up

window.onresize = function(){	//onresize is an event that gets fired many times. this fix allows the browser to execute just once our function resize().
	clearTimeout(resize_Delay);
	resize_Delay=setTimeout("resize()", 200);
};


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//variable for light, sky and functions triggered in resize when the page loads up
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


var turned_on=true;	//for the light
var light_broken=false;
var counter_light=0;
var x=1;				//for the sky loop
var trigger=false;		//for onLoad functions triggered in resize which was trigger onload.
var last_x;
var last_y;
var actual_target;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Ys movment for the object in the room note: Xs are calculated by using the dists and the actual size of the room in resize
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var obj = new Array();

obj['table'] 		= new Array();
obj['piano'] 		= new Array();
obj['resume'] 		= new Array();
obj['typewriter'] 	= new Array();
obj['bed'] 			= new Array();
obj['window'] 		= new Array();
obj['bag'] 			= new Array();
obj['flag'] 		= new Array();
obj['light'] 		= new Array();
obj['poster']		= new Array();
obj['room_center']	= new Array();


obj['table']		['w'] = 509;
obj['piano']		['w'] = 349;
obj['resume']		['w'] = 103;
obj['flag']			['w'] = 271;
obj['bag']			['w'] = 166;
obj['window']		['w'] = 550;
obj['typewriter']	['w'] = 172;
obj['light']		['w'] = 185;
obj['bed']			['w'] = 520;
obj['poster']		['w'] = 338;
obj['room_center']	['w'] = 455;

obj['table']		['h'] = 315;
obj['piano']		['h'] = 300;
obj['resume']		['h'] = 136;
obj['flag']			['h'] = 137;
obj['bag']			['h'] = 146;
obj['window']		['h'] = 340;
obj['typewriter']	['h'] = 116;
obj['light']		['h'] = 112;
obj['bed']			['h'] = 391;
obj['poster']		['h'] = 201;
obj['room_center']	['h'] = 400;

obj['table']		['y'] = 650;
obj['piano']		['y'] = 393;
obj['resume']		['y'] = 500;
obj['flag']			['y'] = -40;
obj['bag']			['y'] = 510;
obj['window']		['y'] = 260;
obj['typewriter']	['y'] = 520;
obj['light']		['y'] = -210;
obj['bed']			['y'] = 595;
obj['poster']		['y'] = 115;
obj['room_center']	['y'] = 480;

obj['table']		['dist'] = 450;
obj['piano']		['dist'] = 200;
obj['resume']		['dist'] = 365;
obj['flag']			['dist'] = 1300;
obj['bag']			['dist'] = 1144;
obj['window']		['dist'] = 674;
obj['typewriter']	['dist'] = 580;
obj['light']		['dist'] = 826;
obj['bed']			['dist'] = 1530;
obj['poster']		['dist'] = 200;
obj['room_center']	['dist'] = 490;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// set of senteces for every object and or situation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj ['room_center']	['desc'] 		= "This is Francesco's Room. Feel free to take a look around by clicking on the voices at the top of the webpage. Some objects are clickable some aren't. In any case enjoy!";
obj	['table']		['desc'] 		= "I'm a useless table and I do nothing.";
obj	['piano']		['desc'] 		= "I'm Francesco's piano. If you want hear Francesco's compositions on soundcloud click on me.";
obj	['resume']		['desc'] 		= "I'm a resume. Click on me if you want to check me out.";
obj	['flag']		['desc'] 		= "I'm the American flag. Aren't I beautiful?";
obj	['bag']			['desc'] 		= "I'm a briefcase and I will be containing all Francesco's work. Right now, this section is 'Work in Progress'.";
obj	['window']		['desc'] 		= "I'm a window and this is Chicago. Isn't it a great view?";
obj	['typewriter']	['desc'] 		= "I'm a typewriter and I help Francesco writing his blog; Click on me to visit it!";
obj	['light']		['desc']		= "I'm a light. Turn me on and off by clicking on me.";
obj	['light']		['desc2'] 		= "Damn, you **** ! The light is broken now! You better refresh the page...";
obj	['bed']			['desc'] 		= "I'm the bed of Francesco's dreams: a huge Hamburger bed.";
obj	['poster']		['desc'] 		= "I DO hate Internet Explorer and that is why I'm begging you, to change browser by clicking <a href='https://www.google.com/chrome/'>here</a>. I appreciated your attention.";

 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function resize(){	

	var width_room = document.getElementById('room_container').offsetWidth; // get size room div
	
	if (document.getElementById('room_container').offsetWidth >= 970)
	{
		document.getElementById('room_container').style.left='50%';
		document.getElementById('room_container').style.marginLeft=(-width_room/2)+'px';
	}
	else
	{
		document.getElementById('room_container').style.left='0%';
		document.getElementById('room_container').style.marginLeft='0px';
	}
	
	obj['table']		['x'] 		= obj['table']['dist']-(width_room/2);
	obj['resume']		['x'] 		= obj['resume']['dist']-(width_room/2);
	obj['piano']		['x'] 		= obj['piano']['dist']-(width_room/2);
	obj['flag']			['x'] 		= obj['flag']['dist']-(width_room/2);
	obj['bag']			['x'] 		= obj['bag']['dist']-(width_room/2);
	obj['window']		['x']		= obj['window']['dist']-(width_room/2);
	obj['typewriter']	['x'] 		= obj['typewriter']['dist']-(width_room/2);
	obj['light']		['x']		= obj['light']['dist']-(width_room/2);
	obj['bed']			['x']		= obj['bed']['dist']-(width_room/2);
	obj['poster']		['x']		= obj['poster']['dist']-(width_room/2);
	obj['room_center']	['x']		= obj['room_center']['dist']-(width_room/2);
	
	if (!trigger) {
		
		goToByScroll('room_center'); 										//focus on the light to let the user turn them on	
		change_desc(obj['room_center']['desc']); 						//set description for lights
		moveSky(); 												//start moving the recursive function for the sky
		trigger=true;												//don't do the above anymore
	}
	
	goToByScroll(actual_target);
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * jQuery.support.cssProperty
 * To verify that a CSS property is supported (or any of its browser-specific implementations)
 *
 * @param string p - css property name
 * [@param] bool rp - optional, if set to true, the css property name will be returned, instead of a boolean support indicator
 *
 * @Author: Axel Jack Fuchs (Cologne, Germany)
 * @Date: 08-29-2010 18:43
 *
 * Example: $.support.cssProperty('boxShadow');
 * Returns: true
 *
 * Example: $.support.cssProperty('boxShadow', true);
 * Returns: 'MozBoxShadow' (On Firefox4 beta4)
 * Returns: 'WebkitBoxShadow' (On Safari 5)
 */
$.support.cssProperty = (function() {
  function cssProperty(p, rp) {
    var b = document.body || document.documentElement,
    s = b.style;

    // No css support detected
    if(typeof s == 'undefined') { return false; }

    // Tests for standard prop
    if(typeof s[p] == 'string') { return rp ? p : true; }

    // Tests for vendor specific prop
    v = ['Moz', 'Webkit', 'Khtml', 'O', 'ms', 'Icab'],
    p = p.charAt(0).toUpperCase() + p.substr(1);
    for(var i=0; i<v.length; i++) {
      if(typeof s[v[i] + p] == 'string') { return rp ? (v[i] + p) : true; }
    }
  }

  return cssProperty;
})();

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function goToByScroll(target){
	
	actual_target = target;	//salva l'ultima posizione
		
		
	//supported = false;
	
	if (supported) {
		
		//alert("CSS supported!");
	
		if (obj[target]["x"]>=0) {							//if margin-left is positive then make it equal to 0 to avoid graphic bugs
			$("#room").css("margin-left",obj[target]["x"]*(-1));	
			$("#info").css("margin-left",(obj[target]["dist"]+obj[target]["w"]/2));
		}
		else {
			$("#room").css("margin-left","0px");
			$("#info").css("margin-left",(obj[target]["dist"]+obj[target]["w"]/2));
		}
		
		if (obj[target]["y"]<=565) {
			$("#room").css("margin-top",obj[target]["y"]*(-1));
			$("#info").css("margin-top",(obj[target]["y"]-obj[target]["h"]/2)-10);
		}
		else {
			$("#room").css("margin-top","-565px");
			$("#info").css("margin-top",(obj[target]["y"]-obj[target]["h"]/2)-10);
		}
	}
		
	else
	
	{
		//alert("CSS not supported!");
		
		$('#info').animate({marginLeft:(obj[target]["dist"]+obj[target]["w"]/2)} ,500);
		$('#info').animate({marginTop:(obj[target]["y"]-obj[target]["h"]/2)-10} ,500);
		$('#room_container').scrollTo({top : obj[target]['y'],left : obj[target]['x']}, 1000);
													
	}

};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function moveSky(){
	//if (!supported) {
			$('#sky').animate({
			marginLeft:x*(-5000)
			} ,100000, function(){if (x==1) {x=-1;} else {x=1;} moveSky(); }
		)
	//}
	//else {
		
	//	$("#sky").css("margin-left",x*(-5000));
	//	if (x==1) {x=-1;} else {x=1;}
	//}
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function change_desc(new_sentence) {
	document.getElementById('info').innerHTML = new_sentence;
};

function light() {
	if (turned_on) {
		document.getElementById('darkness').style.display='block'; 
		turned_on=false; 		 
	}
	else {
		counter_light++;
		if (counter_light>=20) {
			change_desc(obj['light']['desc2']);
		}
		else
		{
		document.getElementById('darkness').style.display='none'; 
		turned_on=true;
		}
	}
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function () {
    $("body").queryLoader2();
	supported=$.support.cssProperty('transition');
});

//window.addEventListener('DOMContentLoaded', function() {
   //$("body").queryLoader2();
//});


