function movie()
//	target the div the flash lives in
{
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window["epk-content"];
	}else{
		return document["epk-content"];
	}
}




function flashIsLoaded()
//	initialize the facebook connect functionality
//	let flash know that the user can login whenever
{
	var apiKey = 'ba4ffb29f0d435f0c2b7f8c36ec7f69a'; 
	
	FB.Bootstrap.requireFeatures(["Connect"], function() {
		FB.init(apiKey, "xd_receiver.htm");
			FB.ensureInit (  function () {
				returnToFlash('allowLogin', '');
			});
	});
}



function isLoggedIn()
//	verify the user is logged into Facebook
//	IMPORTANT you must call this function before you do anything involving the php api
//	if the returned value is false you have to log the user in otherwise you can do whatever you like.
{
	var user = FB.Facebook.apiClient.get_session() ? FB.Facebook.apiClient.get_session().uid : null;
	var isLoggedIn = false;
	
	if(user == undefined){
		return isLoggedIn;
	};
	
	return user;
}




function connectFacebook(nextCall)
//	log the user into facebook
//	if this is the initial login auto call getUserId
//	else you can pass a value into next call to ensure the user is logged in
{
	FB.ensureInit (  function () {
		FB.Connect.requireSession( function(){
			//getSecret();
			//getKey();
			//getExpires();
			
			if(nextCall.length == 0){
				requestPermission('publish_stream', 'getUserId();');
			}else{
				requestPermission('publish_stream', nextCall);
			}
		}, null, true);
	});
}




function disconnectFacebook()
//	log the user out of facebook
{
	FB.ensureInit (  function () {
		FB.Connect.logout(function() {
			returnToFlash('loggedOut', '');
		});
	});
}




function requestPermission(type, nextCall)
//	check to see if we need to show the dialogue box at all
//	if dialogue box is needed move on to showPermission dialogue
//	if its not needed then move on to nextCall
{	
	//	alert('requestStatusPermission nextCall = '+nextCall );
	
	var sequencer = new FB.BatchSequencer();
	var pendingStatusPermissionResult = FB.Facebook.apiClient.users_hasAppPermission(type, sequencer);
	
	sequencer.execute(function() {
		//alert(Boolean(pendingStatusPermissionResult.result)+' we have users permisson');
		if(Boolean(pendingStatusPermissionResult.result)){
			//alert('no dialogue needed '+nextCall);
			eval(nextCall);
		}else{
			//alert('request '+type+' permission');
			showPermissionDialogue(type, nextCall);
		}
	});
}




function showPermissionDialogue(type, nextCall)
	//	show permission dialogue of specified type
	//	if user answers yes move on to next call
	//	if user answers no return to flash false
{	
	//	alert('show '+type+' permission dialogue');
	FB.Connect.showPermissionDialog(type, function(x){
		if(x == '' || x == false){
			//alert('permission denied');
			returnToFlash('gotPermission', 'false');
		}else{
			//alert('permission granted');
			//alert(nextCall);
			if(nextCall == undefined){
				returnToFlash('gotPermission', 'true');
			}else{
				eval(nextCall);
			}
		}
	});
}




function getUserId()
//	return the uid of a facebook user to flash
{
	var user = isLoggedIn();
	//alert('hit current user id = '+user);
	//returnToFlash('FBGotUserId', user);
	getUserInfo(user);
}

function getUserInfo(user) {
	//alert("get user info" + user );
	var sequencer = new FB.BatchSequencer();
	var fields = new Array('uid', 'pic_square', 'first_name', 'last_name');
	var pendingProfileResult = FB.Facebook.apiClient.users_getInfo(user, fields, sequencer);
	
	sequencer.execute(function() {
		returnToFlash('gotUserInfo', pendingProfileResult.result[0]);
		//alert('user info = '+pendingProfileResult.result[0]);
	});
}




function publishToWall(){

	//alert('hit publish to wall');
	var user = FB.Facebook.apiClient.get_session() ? FB.Facebook.apiClient.get_session().uid : null;
	if(user == undefined){
		connectFacebook('publishToWall()');
		return;
	};
	
	requestPermission('publish_stream', 'postModule()');
}

function postModule(){
	returnToFlash('FBCanPostImage');
}

function makePost(name){
	//	alert('hit postModule');
	//var subdomain = 'dev'
	
	var paramList = name.split(",");
	var subdomain = 'www';
	var baseUrl = '.bangersbeansandmash.com/';
	var attachment = {'media': [{'type': 'flash', 'swfsrc': 'http://'+subdomain+baseUrl+'embed/shell-102.swf?id='+paramList[2]+'&v='+paramList[3]+'&vol='+paramList[4],'imgsrc': 'http://'+subdomain+baseUrl+'embed/thumb_image.jpg','width': '130','height': '110','expanded_width': '420','expanded_height': '280'}]};
	var actionLinks = [	{ "text": "Enter The Contest", "href": "http://"+subdomain+baseUrl}];
	
	var sequencer = new FB.BatchSequencer();
	//alert('contact sequencer '+ actionLinks);
	sequencer.execute(function() {
		FB.Connect.streamPublish('Watch my cover version of "Bangers, Beans and Mash" - the song is featured in Get Him to the Greek, starring Jonah Hill and Russell Brand, in theaters June 4th.', attachment, actionLinks, null, 'Post this to your wall.',
			function callback (post_id, exception) {
				/*
				if(post_id && post_id != 'null') {
					//alert(post_id+' , '+exception);
					postSuccess(post_id);
				}else{
					postFail(exception);
				}
				*/
			}
		);
	});
}









//	----------------------------------------------------------------------------
//	communicate between the js and flash add case for each interaction in flash
//	----------------------------------------------------------------------------
function returnToFlash(type, params){
	switch(type){
		case 'allowLogin':
			//alert('return to flash allowLogin');
			movie().allowLogin(params);
			break;
		
		case 'gotUserInfo':
			movie().FBGotUserInfo(params);
			break;
		
		case 'loggedOut':
			movie().FBLoggedOut(params);
			break;
			
		case 'gotPermission':
			if(params == 'true'){
				movie().FBGotStatusPermission();
			}else{
				movie().FBDenyStatusPermission();
			}
			break;
		
		case 'FBCanPostImage':
			movie().FBCanPostImage();
			break;
	}
}



