var loggedInUid
var loggedInName
var loggedInPhoto

var mPage = 0
var sPage = 0

var looper = 0
var msc = -1 // -1 bodge

var posi = new Array() // box start position
var dest = new Array() // box destination
var trag = new Array() // box tragectory (X or Y)
var wait = new Array() // box wait period

function pageLoaded(m,s) {
	looper = setInterval(enterPage,25)
	document.getElementById('innerPage').style.visibility = 'visible'
}

function enterPage() {
	msc ++
	for (var box in posi) {
		if (posi[box] != dest[box] && msc > wait[box]) {
			posi[box] -= (posi[box] - dest[box]) / 4
			if (posi[box] - dest[box] < 1) {
				posi[box] = dest[box]
			}
			if (trag[box] == 'X') {
				document.getElementById('box_' + box).style.left = posi[box] + 'px';
			} else {
				document.getElementById('box_' + box).style.top = posi[box] + 'px';
			}
		}
	}
	if (msc > 50) {
		clearInterval(looper)
		msc = 0
		connect()
	}
}

function exitPage() {
	msc ++
	for (var box in posi) {
		if (msc > wait[box]) {
			var a = document.getElementById('box_' + box).style.top.split('px')
			document.getElementById('box_' + box).style.top = a[0] - 75 + 'px'
		}
	}
	if (msc > 50) { 
		clearInterval(looper)
		sPage = 0
		msc = 0

		document.getElementById('innerPage').style.visibility = 'hidden'
		document.location = page
	}
}

function sendFacebookPhoto(a,b) {
	FB.Connect.streamPublish(a,b, null, null, 'Comment')
}

function bOver(a,b) {
	document.getElementById('button_' + a).src = 'gfx/button_' + a + b + '.png'
}

function bActive(ba) {
	if (document.getElementById('fb_like_wrapper') != null) document.getElementById('fb_like_wrapper').style.display = 'none';
	if (!msc) { 
		if (looper) {
			clearInterval(looper)
		}
		page = ba
		looper = setInterval(exitPage,25)
	}
}

function userNotConnected() { 
	document.getElementById("fbConnected").innerHTML = "<p class='movieBlurb'><b>Facebook</b><br>Log into your <a href='#' onclick='return login()'>Facebook</a> Account</p>"
	pageConnect();
}
function userConnected(uid) {
	FB.api('/'+uid, function(response) {
		var loggedInName = response.name;
		document.getElementById("fbConnected").style.backgroundImage = 'url(gfx/box_loggedin.png)'
		document.getElementById("fbConnected").innerHTML = "<div style='width: 49px; height: 48px; margin: 6px; overflow: hidden; float: right'><img src='https://graph.facebook.com/" + uid + "/picture'></div><p style='width: 151px' class='movieBlurb'><a href='http://www.facebook.com/profile.php?id=" + uid + "' target='_blank'>" + loggedInName + "</a><br>You are Connected with your Facebook Account</p>"
		pageConnect()
	});
}

function connect() {
	if (loggedInName) {
		pageConnect()
	} else {
//		FB.init("163367567081306","xd_receiver.htm", { "ifUserConnected": userConnected, "ifUserNotConnected": userNotConnected }) // New App ID
	}
}

function login() {
//	FB.Connect.requireSession()
//	FB.Connect.showPermissionDialog("read_stream, publish_stream, offline_access");
	window.open('https://www.facebook.com/dialog/oauth?client_id=163367567081306&redirect_uri=http://www.rsq.com.au&state=2faf03205a106d4190d1ab785c7b1d71&scope=read_stream,offline_access,publish_stream&display=popup', '', 'width=500,height=400');
	return false
}

function submenu() {
	var delay = 400;
	if(document.getElementById('photos').style.display == 'none'){
		$("#gloss").animate({top: '50'}, delay, function(){});
		$("#rsq_saturdays").animate({top: '50'}, delay, function(){});
		$("#special").animate({top: '50'}, delay, function(){});
		$("#hof").animate({top: '50'}, delay, function(){
			$("#home").animate({width: '114'}, delay, function(){});
			$("#nights").animate({left: '0'}, delay, function(){
				$("#gloss").animate({width: '0', top: '0', 'margin-right': '0'}, 0, function(){});
				$("#rsq_saturdays").animate({width: '0', top: '0', 'margin-right': '0'}, 0, function(){});
				$("#special").animate({width: '0', top: '0', 'margin-right': '0'}, 0, function(){});
				$("#hof").animate({width: '0', top: '0', 'margin-right': '0'}, 0, function(){});
				$("#photos").toggle();
				$("#crew").toggle();
				$("#birthdays").toggle();
				$("#career").toggle();
				$("#home").animate({top: '0'}, delay, function(){});
				$("#photos").animate({top: '0'}, delay, function(){});
				$("#crew").animate({top: '0'}, delay, function(){});
				$("#birthdays").animate({top: '0'}, delay, function(){});
				$("#career").animate({top: '0'}, delay, function(){});
			});
		});
	}else{
		$("#home").animate({top: '50'}, delay, function(){});
		$("#photos").animate({top: '50'}, delay, function(){});
		$("#crew").animate({top: '50'}, delay, function(){});
		$("#birthdays").animate({top: '50'}, delay, function(){});
		$("#career").animate({top: '50'}, delay, function(){
			$("#photos").toggle();
			$("#crew").toggle();
			$("#birthdays").toggle();
			$("#career").toggle();
			$("#home").animate({width: '0'}, delay, function(){});
			$("#nights").animate({left: '-25'}, delay+200, function(){
				$("#gloss").animate({width: '135', 'margin-right': '20'}, delay+200, function(){});
				$("#rsq_saturdays").animate({width: '242', 'margin-right': '20'}, delay+200, function(){});
				$("#special").animate({width: '166', 'margin-right': '20'}, delay+200, function(){});
				$("#hof").animate({width: '115', 'margin-right': '20'}, delay+200, function(){});
			});
		});
	}
}

