if (typeof(Meetup.FB) === "undefined") {
	Meetup.FB = {};
}

(function () {
/**
 * @author: Takashi Mizohata <takashi@meetup.com>
 * 
 * @class This is a better replacement of what it used to be called 
 * "Meetup.Facebook.streamPublish" is now under Meetup.FB.feed.  
 * It's a simple wrapper class to FB.ui and takes care of permission 
 * setting.
 * and I think this should take care of custom domain issues, too.
 * Created on 08/22/2011
 * 
 * @example <pre>
 * Meetup.FB.feed.publish(
 *   template [..Facebook publishing template..},
 *   callback [Function]
 * );
 * </pre>
 * 
 * @see Meetup.FB
 */
Meetup.FB.feed = {};

/**
 * good old stream publishing
 * 
 * @function
 * @param template [Object] publishing template
 * @param callback [Function]
 * @see http://developers.facebook.com/docs/reference/javascript/FB.ui/
 */
Meetup.FB.feed.publish = function (template, callback) {
	callback = (typeof(callback) === 'function' ? callback : undefined);
	var re = /.+\.meetup.com$/;
	if (location.hostname.match(re)) {
		template.display = 'dialog';
		template.method = 'feed';
		var func = function () {
			FB.ui(
				template,
				callback
			);
		};
		// REMEMBER! FB.getLoginStatus won't get fired,
		// see Meetup.FB.init() for detail.
		FB.getLoginStatus(
			function (response) {
				if (!response) {
					// something's wrong
					return false;
				}
				if (response.status === 'connected') {
					func();
				}
				else {
					Meetup.FB.extendPerms(
						'publish_stream',
						func
					);
				}
			}
		);
	}
	else {
		popupPublish(template, callback);
	}
};

/**
 * popup 
 * 
 * @function
 * @param template [Object] publishing template
 * @param callback [Function]
 * @see http://developers.facebook.com/docs/reference/javascript/FB.ui/
 */
var popupPublish = function (template, callback) {
	var arr_url,
	link = template.link,
	xp = window.screenX ? window.screenX : window.screenLeft,
	yp = window.screenY ? window.screenY : window.screenTop,
	wp = window.outerWidth ? window.outerWidth : document.clientWidth,
	hp = window.outerHeight ? window.outerHeight : document.clientHeight,
	width = 600,
	height = 400,
	left = xp + Math.round((wp - width) / 2),
	top = yp + Math.round((hp - height) / 2),
	url_base = 'http://www.meetup.com/fbconnect_popup.jsp',
	name_win = 'meetup_facebook',
	str_feature = [
		'left=', left,
		',top=', top,
		',width=', width,
		',height=', height,
		',personalbar=0',
		',toolbar=0',
		',scrollbars=1',
		',resizeable=1'
	].join('');
	if (/#/.test(link)) {
		link = link.split('#').join('%23');
	}
	arr_url = [
		url_base,
		'?link=', link
	];
	if (template.from) {
		arr_url[arr_url.length] = '&from=';
		arr_url[arr_url.length] = encodeStringUrlSafe(template.from);
	}
	if (template.to) {
		arr_url[arr_url.length] = '&to=';
		arr_url[arr_url.length] = encodeStringUrlSafe(template.to);
	}
	if (template.picture) {
		arr_url[arr_url.length] = '&picture=';
		arr_url[arr_url.length] = encodeStringUrlSafe(template.picture);
	}
	if (template.name) {
		arr_url[arr_url.length] = '&name=';
		arr_url[arr_url.length] = encodeStringUrlSafe(template.name);
	}
	if (template.caption) {
		arr_url[arr_url.length] = '&caption=';
		arr_url[arr_url.length] = encodeStringUrlSafe(template.caption);
	}
	var mywin = window.open(arr_url.join(''), name_win, str_feature);
	//FIXME how to call callback?
	return mywin;
};

var encodeStringUrlSafe = function (str) {
	return encodeURIComponent(str).replace(/%20/g, '+');
};

})();

