/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_FBIntegrator
 * @copyright  Copyright (c) 2003-2009 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */

var FBIntegrator = Class.create();
FBIntegrator.prototype = {
	initialize: function(apiKey, xdUrl, ajaxUrl) {
		this.ajaxUrl = ajaxUrl;
		this.onSuccess = this.success.bindAsEventListener(this);
		FB.init(apiKey, xdUrl);
	},
	
	login: function() {
		var request = new Ajax.Request(
			this.ajaxUrl+'login',
            {
                method:'post',
                onSuccess: this.onSuccess,
            }
        );
	},
	
	validateForm: function(form){
		var validator = new Validation(form);
		if (!validator.validate()) {
            return false;
        }
		return true;
	},
	
	register: function(form) {
		var request = new Ajax.Request(
			this.ajaxUrl+'register',
            {
                method:'post',
                onSuccess: this.onSuccess,
                parameters: Form.serialize(form)
            }
        );
	},
	
	savePermissions: function(permissions) {
		if (permissions == '') return false;
		var request = new Ajax.Request(
			this.ajaxUrl+'savePermissions',
            {
                method:'post',
                onSuccess: this.onSuccess,
                parameters: new Hash({'permissions' : permissions})
            }
        );
	},
	
	success: function(transport) {
        if (transport && transport.responseText){
            try{
                response = eval('(' + transport.responseText + ')');
            }
            catch (e) {
                response = {};
            }
        }
        if (response.error){
            if ((typeof response.message) == 'string') {
                alert(response.message);
            } else {
                alert(response.message.join("\n"));
            }
        }
        if ('undefined' != typeof(response.redirect)){
        	location.href = response.redirect;
        } else {
        	this.ajaxLoader.hide();
        }
    }
}