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.
	//alert('flash is loaded');
	var apiKey = '98c948de728b07de75107d2be6425546'; 
	
	
	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
	//	alert('hit connectFacebook');
	
	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]);
		//alert('user info = '+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();
	
	if(user == undefined)
	{
		//alert('user is undefined');
		return;
	};
	
	var sequencer = new FB.BatchSequencer();
	var pendingStatusPermissionResult = FB.Facebook.apiClient.users_hasAppPermission("publish_stream", sequencer);
	
	//var subdomain = 'dev'
	var subdomain = 'www'
	var baseUrl = '.soonasigethome.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": "Create your own", "href": "http://"+subdomain+baseUrl}];
	
	sequencer.execute(function() {
		if(Boolean(pendingStatusPermissionResult.result)){
			//alert('status is approved');
			FB.Connect.streamPublish('Make your own cover of The Babyface song Soon As I Get Home, featured in Our Family Wedding', 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);
					}
				}
			);
		}else{
			//	alert('status not approved');
			showStatusDialogueBox();
			//requestStatusPermission();
		};
	});
}




/*
//	--------------------------------------------------
//	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
	//	alert('allow user to login in flash');
	movie().allowLogin();
}

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

function gotUserInfo(info) {
	//	alert('hit got user info');
	movie().FBGotUserInfo(info);
}

function gotStatusPermission(val) {
	// Let flash know we have permission to post
	//	alert('gotStatusPermission '+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) / 4;
    
    	var new_window=window.open(url, windowName, 'toolbar=0,scrollbars=1,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.');
}