
//Local Variable with Flash Content id. 
var fb_str_swfname = "critpack"
var fb_str_postowall_fn_cb_success = "";
var fb_str_postowall_fn_cb_cancel = "";
var fb_str_postowall_fn_cb_fail = "";		
var fb_dialogId = 157866378115;


function fbInit(pAsSwfName,pApi_key,pReceiver){
	as_swf_name = pAsSwfName;
	FB.init(pApi_key,pReceiver);
}

function c8FbInviteFriends(opt) {
	url = '/media/js/friend_dialog.php?c8callback=' + opt.callBack + '&c8title=' + escape(opt.title) + '&c8text=' + escape(opt.text) + '&c8label=' + escape(opt.button.label) + '&c8url=' + escape(opt.button.url) + '&fb_force_mode=fbml';
	window.open(url,'invitewindow','width=785,height=540,status=no,toolbar=no,location=no,resizable=no,menubar=no,dependent=yes,dialog=yes,minimize=no,scrollbars=yes');
}

function fb_astojs_invite(email, url) {
	var link = 'http://www.action-pact.org?source=fb/stf2';

	var body = 'I just told the world\'s polluters to PACK IT IN, Greenpeace style. I sent a package containing a petition and a piece of my mind to Copenhagen. Now it\'s your turn!';
	
	FB.Connect.requireSession(function() {
		c8FbInviteFriends({ callBack: 'http://action-pact.org/media/js/friend_dialog.php?close=1',
					       title: 'Action-Pact!',
					       text: body,
					       button: { url: link, label: 'Get Packing!' }
		});
	});
}

		
//JavaScript Connect methods
function fb_astojs_login(){		
	FB.Connect.requireSession(fb_onLoginHandler,fn_onLoginCancel);							
}
		
function fb_astojs_login_passive(){		
	FB.Connect.ifUserConnected(fb_astojs_login,fn_onLoginCancel)
}		
		
function fn_onLoginCancel(){
	callFlashFunction( "jstoas_onLoginCancelled" );	
}
		
		//Event Handlers
function fb_onLoginHandler(){		
	callFlashFunction( "jstoas_onLogin" );		  	
}
		
//Method to dispatch an Event to Flash
function callFlashFunction ( str_functionname ) {
	if (getFlashObjectById(fb_str_swfname)){
		if( arguments.length > 1 ){
			getFlashObjectById(fb_str_swfname)[str_functionname]( Array.prototype.slice.call(arguments).slice(1)[0]);
		}else{
			getFlashObjectById(fb_str_swfname)[str_functionname]();
		}
	} 
}

function getFlashObjectById(str_id){
	if(navigator.appName.indexOf("Microsoft") != -1) {
		obj_flash = window[str_id];
	}else {
		obj_flash = window.document[str_id];
	}
	return obj_flash;
}

/* not sure where this function gets called, but we don't want two copies */
function fb_astojs_postToWall(str_message,str_attchment_imageurl,str_attachment_linkurl,str_actionlink_title,str_actionlink_url,str_cb_function_success,str_cb_function_cancel,str_cb_function_fail){
	return fb_astojs_critpackPostToWallWithTemplate(str_message,str_attchment_imageurl,str_attachment_linkurl,str_actionlink_title,str_actionlink_url,str_cb_function_success,str_cb_function_cancel,str_cb_function_fail);
}	
		

function fb_onWallPostCallback(post_id, exception){
	if (exception){
		if (fb_str_postowall_fn_cb_fail!=null){
			callFlashFunction(fb_str_postowall_fn_cb_fail);
			return;
		}
	} else if(post_id) { 
		if (post_id=="null"){
			if (fb_str_postowall_fn_cb_cancel != null){
				callFlashFunction(fb_str_postowall_fn_cb_cancel)
				return;
			}
		} else {
			if (fb_str_postowall_fn_cb_success!=null){
				callFlashFunction(fb_str_postowall_fn_cb_success,String(post_id))
				return;
			}		
		}
	} 
	if (fb_str_postowall_fn_cb_cancel != null){
		callFlashFunction(fb_str_postowall_fn_cb_cancel)
		return;
	}
}			

/* Post to Facebook Stream */
function fb_astojs_critpackPostToWallWithTemplate(str_message,str_attchment_imageurl,str_attachment_linkurl,str_actionlink_title,str_actionlink_url,str_cb_function_success,str_cb_function_cancel,str_cb_function_fail){
	fb_str_postowall_fn_cb_success = str_cb_function_success;
	fb_str_postowall_fn_cb_cancel = str_cb_function_cancel;
	fb_str_postowall_fn_cb_fail = str_cb_function_fail;
	
	var source = 'fb/feed2';
	
	if (str_message == null){ str_message = "" } 

	lang = {
		en: '{*actor*} is CAPTAIN CORRUGATED, one of the newest recruits in Greenpeace\'s fight for action at the UN Climate Summit in Copenhagen this December. Join the fight by creating your own super-package!',		
		cz: '{*actor*} právě pomohl Greenpeace poslat balík delegátům OSN na klimatickou konferenci v Kodani. Balík má delegáty podpořit, aby uzavřeli účinnou dohodu směřující k zastavení klimatických změn.'
	}

	var attachment = null;
	if (str_attchment_imageurl != null){ 
		attachment = {
			'name': 'Action-Pact: CAPTAIN CORRUGATED!',
			'caption': '{*actor*} is CAPTAIN CORRUGATED, one of the newest recruits in Greenpeace\'s fight for action at the UN Climate Summit in Copenhagen this December. Join the fight by creating your own super-package!',		
			'media': [{'type':'image','src':str_attchment_imageurl,'href':str_attachment_linkurl + '?source=' + source} ]
		}; 		
	}
	
	var actionlinks = null;  
	if (str_actionlink_title!= null && str_actionlink_url != null){			
		actionlinks = [{ "text": 'Create your own' , "href": str_actionlink_url + '?source=' + source}];			
	}
	
	var template_data =  {'images':[{'type':'image','src':str_attchment_imageurl,'href':str_attachment_linkurl + '?source=' + source}]}; 	
	var user_message_prompt = "Enter your slogan here:"; 

	if (str_message!= null){
		var user_message = {value: str_message };
	} else {
		var user_message = {value: ""};
	}
	
	FB.Connect.streamPublish(str_message, attachment,actionlinks,null,null,fb_onWallPostCallback);			
}

