var javascript_loaded = true; var is_initialized = false; 
function refresh_page() { location.reload(true); }
function ensure_init(callback) {
    if(window.is_initialized) {
        callback();
    } else {
        FB_RequireFeatures(["XFBML", "CanvasUtil"], function() {
            	FB.Facebook.init(api_key, "/xd_receiver.htm");
            	window.is_initialized = true;
				FB.XFBML.Host.autoParseDomTree = true;
            	FB.Facebook.get_sessionState().waitUntilReady(function() {
            });
            callback();
        });
    }
}
function facebook_onload(already_logged_into_facebook, cookies_still_set) {
	ensure_init(function() {
		FB.Facebook.get_sessionState().waitUntilReady(function(session) {
			if (!already_logged_into_facebook && cookies_still_set) {
				FB.Connect.logout(function() { refresh_page(); });
				window.is_initialized = false;
				return;
            }
			var is_now_logged_into_facebook = session ? true : false;
			if (is_now_logged_into_facebook == already_logged_into_facebook) {
				return;
				refresh_page();
			}
        });
    });
}
function popupFacebookLogin(){
	if (!FB.Connect || !javascript_loaded) {
		return;
	}
	FB.Connect.requireSession();
	FB.Facebook.get_sessionState().waitUntilReady(function(){
		location.reload(true);
	});
	return false;
}
function popup_tell_Form(domain){
	FB.IFrameUtil.CanvasUtilServer.run(true);
	var _frame=document.createElement("div");
	_frame.setAttribute("iframeHeight","560px");
	_frame.setAttribute("iframeWidth","630px");
	var _content=new FB.UI.PopupDialog("Invite your friends to join the"+ domain + "411",_frame,false,false);
	_content.setContentWidth(630);
	_content.setContentHeight(560);
	_content.set_placement(FB.UI.PopupPlacement.center);
	_frame.setAttribute("fbml","<fb:fbml>"+"<fb:request-form style=\"width:630px; height:560px;\" action=\""+document.location.href+"\"\tmethod=\"POST\" invite=\"false\" type=\"Invite your friends to join the"+ domain + "411\" "+"content=\"I would like you to join this site: <fb:req-choice url='[% c.req.base %]' label='Go to site' />\">"+"<fb:multi-friend-selector\tshowborder=\"false\" actiontext=\"Invite your friends to join the" + domain + "411\" rows=\"3\" cols=\"4\" email_invite=\"true\" bypass=\"cancel\"\tshowborder=\"false\" />"+"</fb:request-form>"+"</fb:fbml>");
	_content.show();
	FB_RequireFeatures(["XFBML"],function(){
	var _confirm=new FB.XFBML.ServerFbml(_frame);
		FB.XFBML.Host.addElement(_confirm);
	});
}
function popupFacebookInvite(domain){
	ensure_init( function() {
			popup_tell_Form(domain);
		}
	);
	return false;
}
function popup_tell_Form2(domain,danceschool){
    FB.IFrameUtil.CanvasUtilServer.run(true);
    var _frame=document.createElement("div");
    _frame.setAttribute("iframeHeight","600px");
    _frame.setAttribute("iframeWidth","630px");
    var _content=new FB.UI.PopupDialog("Invite your friends to join " + danceschool + " @ the"+ domain + "411",_frame,false,false);
    _content.setContentWidth(630);
    _content.setContentHeight(560);
    _content.set_placement(FB.UI.PopupPlacement.center);
    _frame.setAttribute("fbml","<fb:fbml>"+"<fb:request-form style=\"width:630px; height:560px;\" action=\""+document.location.href+"\"\tmethod=\"POST\" invite=\"false\" type=\"Invite your friends to join " + danceschool + " @ the" + domain + "411\" "+"content=\"I would like you to join " + danceschool + " @ the" + domain + "411.com: <fb:req-choice url='[% c.req.base %]dance-school/" + danceschool + "' label='Go to site' />\">"+"<fb:multi-friend-selector\tshowborder=\"false\" rows=\"3\" cols=\"4\" email_invite=\"true\" actiontext=\"Invite your friends to join " + danceschool + " @ The" +domain+ "411\" rows=\"5\" bypass=\"cancel\"\tshowborder=\"false\" />"+"</fb:request-form>"+"</fb:fbml>");
    _content.show();
    FB_RequireFeatures(["XFBML"],function(){
    var _confirm=new FB.XFBML.ServerFbml(_frame);
        FB.XFBML.Host.addElement(_confirm);
    });
}

function popupFacebookSchoolInvite(domain,danceschool){
    ensure_init( function() {
			if (danceschool) {
	            popup_tell_Form2(domain,danceschool);
			} else {
				popup_tell_Form(domain);
			}
        }
    );
    return false;
}
