//Local Variable with Flash Content id. default = flashcontent
var as_swf_name = "flashcontent";

var args = "TEST";

//---------------------
//Initialize Facebook
//---------------------
function fbInit(pAsSwfName, pApi_key, pReceiver){
	as_swf_name = pAsSwfName;
	FB.init(pApi_key, pReceiver);
	
}

//---------------------
//JavaScript Connect methods
//---------------------
function login(){
	FB.Connect.requireSession( onLoginHandler, null, true );
}


function logout(){
	FB.Connect.logout( "onLoggedOut" );
}

//---------------------
//Event Handlers
//---------------------
function onLoginHandler()
{
	flashCallBack( "onLogIn" );
}

function onLoggedOut()
{
	flashCallBack( "onLoggedOut" );	
}

//---------------------
// 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' );
	
}

//---------------------
//
//---------------------
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]();
	}
}
