var fbcSignInButtons = [];

var FbcSignIn = new Class({

    /**
     * Creates the popover with the given inner HTML and displays it.
     */
    initialize: function(prefix, omnitureTag) {
		this.prefix = prefix;
		this.omnitureTag = omnitureTag;
        fbcSignInButtons.push(this);
	},
	
	showButton: function() {
        if (this.isDisplayed) return;

		$(this.prefix + '-fb-reg-signin').setStyle('display', '');
		this.isDisplayed = true;

		// just one backdrop
		if (!$('fb-reg-signin-backdrop')) {
			var backdrop = new Element('div', {
				'id' : 'fb-reg-signin-backdrop',
				'styles': {
		        	'display': 'none',
		        	'position': 'fixed',
		        	'_position' : 'absolute',
		        	'top': '0',
		        	'left' : '0',
		        	'width' : '100%',
		        	'height' : '100%',
		        	'background-color' : 'black',
		        	'z-index' : '3001',
		        	'-moz-opacity' : '0.7',
		        	'opacity' : '.70',
		        	'filter' : 'alpha(opacity=70)'
		    	}
			});
			MootoolsUtils.inject(backdrop, $(document.body));
		}
	}
});

FbcSignIn.signUserInWithFbc = function (returnType, returnValue, tag) {
    if(typeof OmnitureCustomLink == 'function')
    {
        OmnitureCustomLink(tag);
    }
    FB.getLoginStatus(function(response) {
        if (response.authResponse) {
            $('fb-reg-signin-backdrop').setStyle('display', '');
            // make an ajax call to attempt to authenticate the user, if account
            // is not linked, redirect to another page
            MootoolsUtils.request('/fbconnect/reg/signin.do?returnType=' + returnType + '&returnValue=' + returnValue, {
                method : 'get',
                onSuccess: function(responseText) {
                    var forwardUrl = responseText;
                    window.location = forwardUrl;
                },
                onFailure: function () {
                    alert('Could not log you in');
                    MootoolsUtils.dispose($('fb-reg-signin-backdrop'));
                }
            });

        } else {
            //user cancelled login
        }
      });

}

FbcSignIn.showAllButtons = function () {
	for (var i = 0; i < fbcSignInButtons.length; i++) {
		fbcSignInButtons[i].showButton();
    }
}


