function movie() {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window["epk-content"]
	}else{
		return document["epk-content"]
	}
}

function flashIsLoaded(){
	//	called from flash once the flash is loaded and ready to go.
	var apiKey = '5b16a12f45cab8369070c5c4ce5c57dd'; 
	
	FB.Bootstrap.requireFeatures(["Connect"], function() {
		FB.init(apiKey, "xd_receiver.htm");
			FB.ensureInit (  function () {
				allowFlashLogin();
			});
	});
}

function connectFacebook() {
	//	called from flash when the user wants to connect
	FB.ensureInit (  function () {
		FB.Connect.requireSession( function(){
			getUserId();
		});
	});
}

function disconnectFacebook() {
	//	called from flash when the user wants to disconnect
	FB.ensureInit (  function () {
		FB.Connect.logout(function() {
			loggedOut();
		});
	});
}

function getUserId() {
	//alert('hit get user id');
	var user = FB.Facebook.apiClient.get_session() ? FB.Facebook.apiClient.get_session().uid : null;
	getUserInfo(user);
}

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

function requestStatusPermission(){
	//alert('requestStatusPermission');
	var sequencer = new FB.BatchSequencer();
	var pendingStatusPermissionResult = FB.Facebook.apiClient.users_hasAppPermission("publish_stream", sequencer);
	
	sequencer.execute(function() {
		//alert('result = '+Boolean(pendingStatusPermissionResult.result));
		if(Boolean(pendingStatusPermissionResult.result)){
			//alert('no dialogue needed');
			gotStatusPermission(true);
		}else{
			//alert('show dialogue box');
			showStatusDialogueBox();
		}
	});
}

function showStatusDialogueBox(){
	//alert('show status dialogue');
	FB.Connect.showPermissionDialog("publish_stream", function(x){
		if(x == '' || x == false){
			gotStatusPermission(false);
		}else{
			gotStatusPermission(true);
		}
	});
}

function publishToWall(name){
	//alert('hit publish to wall');
	var paramList = name.split(",");
	var user = FB.Facebook.apiClient.get_session() ? FB.Facebook.apiClient.get_session().uid : connectFacebook();
	
	var sequencer = new FB.BatchSequencer();
	var pendingStatusPermissionResult = FB.Facebook.apiClient.users_hasAppPermission("publish_stream", sequencer);
	var content = "sci-fi name is "+paramList[2]+". Make yours magical at http://www.gentlemenbroncos.com/?dl=feature";
	sequencer.execute(function() {
		if(Boolean(pendingStatusPermissionResult.result)){
		//alert('no dialogue needed');
		FB.Connect.streamPublish(content, null, null, null, "Ronald Chevaliers' Magical Renamerator",
			function callback (post_id, exception) {
				if(post_id && post_id != 'null') {
					//alert(post_id+' , '+exception);
					postSuccess(post_id);
				}else{
					postFail(exception);
				}
			}
		);
		}else{
			//alert('show dialogue box');
			showStatusDialogueBox();
		}
	});
}




/*
//	--------------------------------------------------
//	start functions that communicate between the js and flash
//	each one of these functions must be setup in flash using ExternalInterface in your flash
//	example:
//	actionscript - ExternalInterface.addCallback(	"allowLogin", showLoginButton	);
//	allows the flash to call an actionscript function showLoginButton when you call movie.allowLogin(); in the javascript.
//	--------------------------------------------------
*/
function allowFlashLogin(){
	//	Let flash know the facebook has init is complete
	movie().allowLogin();
}

function loggedOut() {
	// Let flash know we're logged out
	movie().FBConnectLoggedOut();
}

function gotUserInfo(info) {
	// Let flash know we're logged out
	//alert('hit got user info');
	movie().FBGotUserInfo(info);
}

function gotStatusPermission(val) {
	// Let flash know we're returned friends
	//alert(val);
	if(!val){
		movie().FBDenyStatusPermission();
	}else{
		movie().FBGotStatusPermission();
	}
}

function postSuccess(id){
	//alert('pass '+id+' to flash');
	movie().FBPostSuccess(id);
}

function postFail(exception){
	//alert(exception);
	movie().FBPostFail();
}
//	--------------------------------------------------
//	end functions that communicate between the js and flash
//	--------------------------------------------------






function sizedPopup(val) {
	//alert(val);
	
	var p = val.split(",");
	var url = String(p[0]);
	var win = String(p[1]);
	var w = Number(p[2]);
	var h = Number(p[3]);
	//alert(url+','+w+','+h+','+win);
	var openTest = openWindow(url, w, h, win);
	if(!openTest){
		notifyPopUpFail();
	}
}




function openWindow( url, width, height, windowName )
{
	//alert('hit open window');
	try{
		//get center coords
   		var left = (screen.width - width) / 2;
   		var top = (screen.height - height) / 2;
    
    	var new_window=window.open(url, windowName, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);

		if ( new_window == null )
				return false;
			if ( window.opera )
				if (!new_window.opera)
					return false;
	}catch(err){
		//alert('error occurred');
		return false;
	}
	
	new_window.focus();
	//alert('error did not occurred');
	return true;

}

function notifyPopUpFail()
{
	alert('Turn off your pop up blocker to view this content.');
}