
//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 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_stf';
	var body = 'I just wrote a great slogan for Greenpeace to use at the UN Climate Summit.  If it is voted the best, it will end up on a giant screen outside the UN Climate Summit in Copenhagen this December.';

	if(email != null || email == '') {
	    body = body + '  Check out my package and slogan here <a href=\'' + link + '#' + email + '\'>http://action-pact.org</a>.<br/><br/>';
	} 

	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: 'Create a package!' }
		});
	});
}

function fbInit(pAsSwfName,pApi_key,pReceiver){
	as_swf_name = pAsSwfName;
	FB.init(pApi_key,pReceiver);
}
		
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" ); }
function fb_onLoginHandler(){ callFlashFunction( "jstoas_onLogin" ); }

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;
}
		
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_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 user_message_prompt = "Enter your slogan here:"; 
			
			if (str_message == null){ str_message = "" } 
		  if (str_attachment_imageurl == null) { imageUrl = 'http://www-action-pact.org/media/images/fb_default.jpg'; }

		  var actionUrl = 'http://www.action-pact.org?source=' + source;

			var attachment = {
                		'name': 'CAPTAIN CORRUGATED!',
                		'caption': '{*actor*} is… CAPTAIN CORRUGATED!  Captain Corrugated is 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 at action-pact.org',
                		'media': [{'type':'image','src':str_attachment_imageurl,'href':str_attachment_linkurl + '?source=' + source }]
      };
			
			var actionlinks = null;  
			if (str_actionlink_title!= null && str_actionlink_url != null){			
					actionlinks = [{ "text": str_actionlink_title, "href": str_actionlink_url}];			
			}
			
			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.showFeedDialog(fb_dialogId, template_data, null, "", null, FB.RequireConnect.require, fb_onWallPostCallback, user_message_prompt, user_message);
			FB.Connect.streamPublish(str_message, attachment,actionlinks,null,null, fb_onWallPostCallback);			
		}
		
			  
	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;
			}
	}			



	
function post_to_wall(email, imageUrl, str_message, source) {
	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;

	fb_dialogId = 161663693115;
	
	if (str_message == null){ str_message = "" } 
	if (source == null) { source = 'fb_feed'; }	
	if (source == 'fb_gallery') {
		if(email == null || email == '') {
			source = 'fb_gallery2';
		} else {
			source = 'fb_gallery1';
		}
	}

	if (imageUrl == '' || imageUrl == null) {
		imageUrl = 'http://www-action-pact.org/media/images/fb_default.jpg';
	}

	var body = 'Get active and create your own at <a href="http://www.action-pact.org?source=' + source + '">www.action-pact.org</a>';

	var actionUrl = 'http://www.action-pact.org?source=' + source;

	if (email == '' || email == null) {
		imageHref = 'http://www.action-pact.org?source=' + source;
	} else {
		imageHref = 'http://www.action-pact.org?source=' + source + '#' + email;
		body = 'Check out my package and slogan <a href="' + imageHref + '">here</a><br/><br/>' + body;
	}

	var attachment = {'media':[{'type':'image','src':imageUrl,'href':imageHref}]};
	var actionlinks = [{ 'text': 'Create your package!', 'href':actionUrl }];
	
	var template_data =  {'images':[{'type':'image','src':imageUrl,'href':imageHref}]};
	var user_message_prompt = "Enter your slogan here:"; 

	FB.Connect.showFeedDialog(fb_dialogId, template_data, null, body, null, FB.RequireConnect.require, fb_onWallPostCallback, user_message_prompt, user_message);
}



