//Local Variable with Flash Content id. default = flashcontent
var as_swf_name = "flashcontent";

var args = "TEST";
var loggedIn = false;

//---------------------
//Initialize Facebook
//---------------------
function fbInit(pAsSwfName, pApi_key, pReceiver){
	as_swf_name = pAsSwfName;
	FB.init(pApi_key, pReceiver, {"ifUserConnected":onConnected, "ifUserNotConnected":onNotConnected});
	
	//FB.Connect.logout();
	//var status =  FB.Connect.get_status();
	//alert( "TEST: " );
}

function onConnected(user_id) 
{
  //alert( "USER Connected: " + user_id );
  loggedIn = true;
  //FB.Connect.logout();
} 

function onNotConnected() {
  loggedIn = false;
  //alert( "USER not Connect: " );
} 

//---------------------
//Log user in, if they are already logged in then 
//log them out then back in
//---------------------
function login()
{
	if ( loggedIn == true )
	{
		FB.Connect.logout( onLoggedOut );
	}
	else
	{
		FB.Connect.requireSession( onLoginHandler, null, true );	
	}
}

//---------------------
//User has been logged out so we can now log them back in
//---------------------
function onLoggedOut()
{
	FB.Connect.requireSession( onLoginHandler, null, true );	
}
//---------------------
//Event Handlers
//---------------------
function onLoginHandler(){
	flashCallBack( "onLogIn" );
}

//---------------------
// http://developers.facebook.com/docs/?u=facebook.jslib.FB.Connect.streamPublish
//
// post a message on a users wall
// @param args:Array
// args[0] : String - headingMessage, heading prompt 
// args[1] : String - userMessage, appears in input box
// args[2] : String - url to icon/logo
// args[3] : String - url to challenge
// args[4] : String - message to place on wall
// args[5] : String - id
//
//Example:
// postChallengeOnWall(['headingMessage', 'userMessage', 
//                      'http://www.mobil1globalchallenge.com/images/M1challenge.jpg', 
//                      'http://www.4t2.co.uk',
//					    'wallMessage', '100000739770213'
//					     ]);
//
//---------------------
function postChallengeOnWall( arg )
{
	var attachment = {'media': [{'type':'image',
                             'src':arg[2],
                             'href':arg[3]}]};
                             
    var actionLinks = [{ 'text':arg[4], 'href':arg[3]}];
		 
	var test = FB.Connect.streamPublish(arg[1], attachment, actionLinks, arg[5], arg[0], onChallengePublished, false );
	
}

//---------------------
//
//---------------------
function setChallengeOnWall(arg) {
	
	
	document.getElementById("headingMessage").value = arg[0];
	document.getElementById("userMessage").value = arg[1];
	document.getElementById("url_icon").value = arg[2];
	document.getElementById("url_challenge").value = arg[3];
	document.getElementById("wall_message").value = arg[4];
	document.getElementById("wall_id").value = arg[5];
	window.open('popup.html', 'win', 'height=500, width=650, toolbar=0, menubar=0, top=0' );

	
	/*
	var postvars = 'message=' + arg[1] +
				   '&attachment=' + '{"media":[{"type":"image","src":'+ arg[2] + ',"href":'+ arg[3] + '}]}' + 
				   '&action_links=' + '[{"text":' + arg[1] +'","href":"' + arg[3] + '"}]' +
				   '&target_id=' + arg[5] + 
				   '&user_message_prompt=' + arg[0] + 
				   '&preview=true' + 
				   '&api_key=e799b8ca0f86c1ad3934ecd6f56b4854' +
				   '&channel_url=http://www.thebestofgames.co.uk/xd_receiver.htm' + 
				   '&extern=2' + 
				   '&cancel=http://www.thebestofgames.co.uk/closeme.htm' + 
				   '&callback=http://www.thebestofgames.co.uk/closeme.htm';
				   
	
	var fbUrl = 'http://www.facebook.com/connect/prompt_feed.php?' + postvars;
	var linkUrl = 'http://www.thebestofgames.co.uk/popup.php?url=fbUrl';
	
	window.open( linkUrl, 'win', 'height=400, width=600, toolbar=0, menubar=0, top=0, resizable=0' );

	*/
}

//---------------------
//
//---------------------
function onChallengePublished()
{
	flashCallBack( "onChallengePosted" );
}

//---------------------
//Method to dispatch an Event to Flash
//---------------------
function flashCallBack ( func ) {
	if( arguments.length > 1 ){
		document[as_swf_name][func]( Array.prototype.slice.call(arguments).slice(1)[0]);
	}else{
		document[as_swf_name][func]();
	}
}
