//Local Variable with Flash Content id. default = flashcontent
var as_swf_name = "flashcontent";

//Initialize Facebook
function fbInit(pAsSwfName,pApi_key,pReceiver){
	as_swf_name = pAsSwfName;
	FB.init(pApi_key,pReceiver);
	FB.ensureInit(function() { FB.Connect.get_status().waitUntilReady( function( status ) { 
		switch ( status ) { 
				case FB.ConnectState.connected: 
					loggedIn = true; 
				break; 
				case FB.ConnectState.appNotAuthorized: 
				case FB.ConnectState.userNotLoggedIn: 
					FB.Connect.requireSession(); 
					loggedIn = false; 
			} 
		}); 
	}); 
}

//JavaScript Connect methods
function login(){
	FB.Connect.requireSession( onLoginHandler );
}
function logout(){
	FB.Connect.logout( onLogoutHandler );
}
//Event Handlers
function onLoginHandler(){
	flashCallBack( "onLogIn" );
}

function onLogoutHandler(){
	flashCallBack( "onLogout");
}

//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]();
	}
}

function publishComment(feedId, comment, sitename, siteurl, posttitle, postlink, postimage, synopsis){
	var template_data = {
		"comment":comment,
		"sitename":sitename,
		"siteurl":siteurl,
		"posttitle":posttitle,
		"postlink":postlink,
		"postimage":postimage,
		"synopsis":synopsis,
		"images":[{'src':postimage, 'href':postlink}]
	};
	var attachment = { media:[{ type:'image', src:postimage, href:postlink }], name:posttitle, href:postlink, caption:'{*actor*} wrote "' + comment + '" about this episode on ' + sitename + '.' };
	var actionLinks = [{ text:sitename, href:siteurl }];
	FB.Connect.streamPublish(comment, attachment, actionLinks, null, null, getComments);
}

function publishVideo(message, sitename, siteurl, posttitle, postlink, postimage, synopsis){
	var message = message;
	var attachment ={
		media:[{type:'flash', swfsrc:postlink, imgsrc:postimage, width:100, height:80, expanded_width:360, expanded_height:308}],
		name:posttitle, href:siteurl, description:synopsis
	};
	var actionLinks = [{ text:sitename, href:siteurl }];
	FB.Connect.streamPublish(message, attachment, actionLinks);
}

function getComments(post_id, exception){
	if(post_id == null){
		document[as_swf_name].getPostId(null);
	}else{
		document[as_swf_name].getPostId(post_id);
	}
}