/**
 * This Software is the property of OXID eSales and is protected
 * by copyright law - it is NOT Freeware.
 *
 * Any unauthorized use of this software without a valid license key
 * is a violation of the license agreement and will be prosecuted by
 * civil and criminal law.
 *
 * @link      http://www.oxid-esales.com
 * @package   out
 * @copyright (C) OXID eSales AG 2003-2011
 * @version OXID eShop PE
 * @version   SVN: $Id: oxfacebook.js 35529 2011-05-23 07:31:20Z vilma $
 */
    /*
     * Facebook related scripts
     */
    oxFacebook = {

        /*
         * FB widgets/buttons array
         */
        buttons: {
        },

        /*
         * Enables FB widgets
         */
        showFbWidgets: function ( sFbAppId, sLocale, sLoginUrl, sLogoutUrl ) {

            var self = this;
            self.key = null;

            for ( key in this.buttons ) {
                if ( this.buttons[key].script ) {
                    self.key = key;
                    $.getScript( this.buttons[key].script, function () {
                        $( self.key ).html( unescape( self.buttons[self.key].html ) );
                    } );
                } else {
                    $( key ).html( unescape( this.buttons[key].html ) );
                }
            }

            $.cookie( "fbwidgetson", "1" );
            this.fbInit( sFbAppId, sLocale, sLoginUrl, sLogoutUrl );
        },

        /*
         * Initing Facebook API
         *
         */
        fbInit: function ( sFbAppId, sLocale, sLoginUrl, sLogoutUrl ) {

            window.fbAsyncInit = function() {

                FB.init({appId: sFbAppId, status: true, cookie: true, xfbml: true});
                FB.Event.subscribe('auth.login', function(response) {
                    // redirecting after successfull login
                    oxFacebook.redirectPage( sLoginUrl );

                    if ( FB.XFBML.Host.parseDomTree )
                          setTimeout( FB.XFBML.Host.parseDomTree, 0 );
                });

                FB.Event.subscribe('auth.logout', function(response) {
                    // redirecting after logout
                    oxFacebook.redirectPage( sLogoutUrl );
                });
            };

            // loading FB script file
            var e   = document.createElement('script');
            e.type  = 'text/javascript';
            e.src   = document.location.protocol + '//connect.facebook.net/' + sLocale + '/all.js';
            e.async = true;
            $('#fb-root').append(e);
        },

        /*
         * Redicrecting page to given url
         */
        redirectPage: function ( sUrl ) {

           sUrl = sUrl.toString().replace(/&amp;/g,"&");
           document.location.href = sUrl;
        },

        /*
         * Add scripts from tpl
         */
        initDetailsPagePartial : function () {
            if (window.fbAsyncInit) {
                window.fbAsyncInit();
            }
        }

    };


